ctime - localtime -> GMT question
-
Hi, Just a quick question on how this conversion from local time to UTC / GMT works in the "gmtime" function. I created a tm object called m_timeinfo as you can see below. I adjusted the month from January to June but it keeps on printing the time as 14:00:00 no matter what month. I would expect that it would print 15:00:00 for January and the same for June. Have I missed a flag or something? Thanks for any input.
tm m_timeinfo; m_timeinfo.tm_mday = 1; m_timeinfo.tm_mon = 2 - 1; // tm struct takes a range from 0 - 11 m_timeinfo.tm_year = 2010 - 1900; // tm struct takes the number of years after 1900 m_timeinfo.tm_hour = 15; m_timeinfo.tm_min = 00; m_timeinfo.tm_sec = 00; tm * ptm = &m_timeinfo; //time_t mktime ( struct tm * timeptr ); time_t useme = mktime ( ptm ); time_t * usemeptr = &useme; //struct tm * gmtime ( const time_t * timer ); tm * result = gmtime ( usemeptr ); std::cout << "year is " << result->tm_year + 1900 << std::endl; std::cout << "month is " << result->tm_mon + 1 << std::endl; std::cout << "day is " << result->tm_mday << std::endl; std::cout << "hour is " << result->tm_hour << std::endl; std::cout << "min is " << result->tm_min << std::endl; std::cout << "sec is " << result->tm_sec << std::endl;
-
Hi, Just a quick question on how this conversion from local time to UTC / GMT works in the "gmtime" function. I created a tm object called m_timeinfo as you can see below. I adjusted the month from January to June but it keeps on printing the time as 14:00:00 no matter what month. I would expect that it would print 15:00:00 for January and the same for June. Have I missed a flag or something? Thanks for any input.
tm m_timeinfo; m_timeinfo.tm_mday = 1; m_timeinfo.tm_mon = 2 - 1; // tm struct takes a range from 0 - 11 m_timeinfo.tm_year = 2010 - 1900; // tm struct takes the number of years after 1900 m_timeinfo.tm_hour = 15; m_timeinfo.tm_min = 00; m_timeinfo.tm_sec = 00; tm * ptm = &m_timeinfo; //time_t mktime ( struct tm * timeptr ); time_t useme = mktime ( ptm ); time_t * usemeptr = &useme; //struct tm * gmtime ( const time_t * timer ); tm * result = gmtime ( usemeptr ); std::cout << "year is " << result->tm_year + 1900 << std::endl; std::cout << "month is " << result->tm_mon + 1 << std::endl; std::cout << "day is " << result->tm_mday << std::endl; std::cout << "hour is " << result->tm_hour << std::endl; std::cout << "min is " << result->tm_min << std::endl; std::cout << "sec is " << result->tm_sec << std::endl;
You need to correctly set the
tm_isdst
field - the daylight savings time option. mktime, _mktime64[^] -
You need to correctly set the
tm_isdst
field - the daylight savings time option. mktime, _mktime64[^]