Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. ctime - localtime -> GMT question

ctime - localtime -> GMT question

Scheduled Pinned Locked Moved C / C++ / MFC
question
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    minkowski
    wrote on last edited by
    #1

    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;

    G 1 Reply Last reply
    0
    • M minkowski

      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;

      G Offline
      G Offline
      Graham Breach
      wrote on last edited by
      #2

      You need to correctly set the tm_isdst field - the daylight savings time option. mktime, _mktime64[^]

      M 1 Reply Last reply
      0
      • G Graham Breach

        You need to correctly set the tm_isdst field - the daylight savings time option. mktime, _mktime64[^]

        M Offline
        M Offline
        minkowski
        wrote on last edited by
        #3

        Thanks!

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups