I can’t figure out what mistake I did i the following code but using visual c++ 2010 sp1 the result of the code as I pasted below shows one month mistake.
struct tm timeStamp;
timeStamp.tm_year = 2012 - 1900;
timeStamp.tm_mon = 7;
timeStamp.tm_mday = 19;
timeStamp.tm_hour = 20;
timeStamp.tm_min = 55;
timeStamp.tm_sec = 22;
timeStamp.tm_isdst = -1;
time_t time_val = mktime(&timeStamp);
const int buff_size = 20;
char buff[buff_size] = {0};
strftime(buff, buff_size, "%Y-%m-%d %H:%M:%S", localtime( &time_val ));
cout << "Original time:\t"
<< timeStamp.tm_year + 1900 << "-" << timeStamp.tm_mon << "-" << timeStamp.tm_mday << " "
<< timeStamp.tm_hour << ":" << timeStamp.tm_min << ":" << timeStamp.tm_sec << endl;
cout << "Converted time:\t" << buff << endl;
and the output result for this code as I compile is:
Original time: 2012-7-19 20:55:22
Converted time: 2012-08-19 20:55:22
The
tm_monfield ofstruct tmis from0-11, where 0 == January and 11 == December.strftimeknows this and compensates internally. yourcoutdoesn’t compensate for this.The C++ Reference page for
struct tmcontains the definition of the ranges of all the values.