roohan
roohan

Reputation: 751

Returning a formatted string from a time structure

I wrote a little structure for getting certain time information. This structure is a helper, for my logging class im planning to write. Here is the code:

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;

Now Id like to add a function wich returns a formatted version of the time information. For example:

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

This function should return something like: "2011 Nov", or another example:

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

This function should return something like: "Mon 20:43:24". Can anyone please point me in the most effecient way to do this? Im just not sure about effeciency because in a logger this function might get called alot. Thank you very much.

Upvotes: 1

Views: 1605

Answers (2)

Mooing Duck
Mooing Duck

Reputation: 66932

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;
}

This should be fairly fast since it reserves a fair amount of space, and simply appends chacters to the end of the already allocated buffer most of the time. I highly recommend matching a standard formatting scheme like strftime as parapura rajkumar suggested.

Upvotes: 0

parapura rajkumar
parapura rajkumar

Reputation: 24403

You can use strftime. It supports lots of format

Upvotes: 2

Related Questions