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.
You can use strftime. It supports lots of format