从时间结构返回格式化字符串

时间:2011-11-07 19:09:44

标签: c++

我写了一个小结构来获取某些时间信息。这个结构是一个帮手, 我计划编写的日志类。这是代码:

struct UTime
{
  char Month      [4];
  char DayOfMonth [3];
  char DayOfWeek  [4];
  char Year       [5];
  char Time       [9];
  char Full       [25];

  UTime()
  {
    this->refresh();
  }

  void refresh()
  {
    char      TimeBuffer[26] = {};
    time_t    RawTime        = 0;

    time(&RawTime);
    ctime_s(TimeBuffer, 26*sizeof(char), &RawTime);

    this->DayOfWeek[0]  = TimeBuffer[0];
    this->DayOfWeek[1]  = TimeBuffer[1];
    this->DayOfWeek[2]  = TimeBuffer[2];
    this->DayOfWeek[3]  = 0;

    this->Month[0]      = TimeBuffer[4];
    this->Month[1]      = TimeBuffer[5];
    this->Month[2]      = TimeBuffer[6];
    this->Month[3]      = 0;

    this->DayOfMonth[0] = TimeBuffer[8];
    this->DayOfMonth[1] = TimeBuffer[9];
    this->DayOfMonth[2] = 0;

    this->Time[0]       = TimeBuffer[11];
    this->Time[1]       = TimeBuffer[12];
    this->Time[2]       = TimeBuffer[13];
    this->Time[3]       = TimeBuffer[14];
    this->Time[4]       = TimeBuffer[15];
    this->Time[5]       = TimeBuffer[16];
    this->Time[6]       = TimeBuffer[17];
    this->Time[7]       = TimeBuffer[18];
    this->Time[8]       = 0;

    this->Year[0]       = TimeBuffer[20];
    this->Year[1]       = TimeBuffer[21];
    this->Year[2]       = TimeBuffer[22];
    this->Year[3]       = TimeBuffer[23];
    this->Year[4]       = 0;

    memcpy(this->Full, TimeBuffer, 25);
    this->Full[24] = 0;
  }
}; // struct UTime;

现在我想添加一个函数,它返回时间信息的格式化版本。 例如:

std::string formatted = utime.get(Year, Month)

此功能应返回类似:“2011年11月”或其他示例:

std::string formated = utime.get(DayOfWeek, Time);

此功能应返回类似:“Mon 20:43:24”。任何人都可以用最有效的方式指出我这样做吗?我只是不确定效率,因为在记录器中这个函数可能被称为很多。非常感谢你。

2 个答案:

答案 0 :(得分:2)

您可以使用strftime。它支持多种格式

答案 1 :(得分:0)

std::string utime::get(char* format) {
    std::string formatted;
    formatted.reserve(30);
    for( ; *format!='\0'; ++format) {
        if (*format != '%')
            formatted.append(*format);
        else {
            ++format;
            switch (*format) {
                case 'a': formatted.append(DayOfWeek); break;
                case 'b': formatted.append(Month); break;
                case 'd': formatted.append(DayOfMonth); break;
                case 'H': formatted.append(Time, 2); break;
                case 'M': formatted.append(Time+3, 2); break;
                case 'S': formatted.append(Time+6, 2); break;
                case 'x': formatted.append(Month); 
                          formatted.append(' '); 
                          formatted.append(DayOfMonth); 
                          formatted.append(' '); 
                          formatted.append(Year); 
                          break;
                case 'X': formatted.append(Time); break;
                case 'y': formatted.append(Year+2); break;
                case 'Y': formatted.append(Year); break;
                case '%': formatted.append('%'); break;
                default: throw std::logic_error("Unsupported string format");
            };
        }
    }
    return formatted;
}

这应该相当快,因为​​它保留了相当大的空间,并且在大多数情况下简单地将字符附加到已分配的缓冲区的末尾。我强烈建议像parapura rajkumar建议的那样匹配像strftime这样的标准格式化方案。