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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Daylight saving problems when using winapi's timezone functions.

Daylight saving problems when using winapi's timezone functions.

Scheduled Pinned Locked Moved C / C++ / MFC
sysadminjsonhelpquestionworkspace
2 Posts 1 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.
  • P Offline
    P Offline
    Patric_J
    wrote on last edited by
    #1

    Hi, I get times from a server given in UTC (Coordinated Universal Time or Universal Time Coordinated), that is similar to GMT (Greenwich Mean Time) but not adjusted to daylight saving time. That is, during summer, UTC differs one hour from London time, since UK uses daylight saving during summer. The application I develop is used worldwide so I can not assume or hardcode anything. This should not be a problem since winapi have functions to handle this. I do the following. The function I use to convert UTC time to local time (New York time for me) looks like this: COleDateTime ConvertToLocalTime(const COleDateTime& timeUTC) { // Set time environment variables. _tzset(); // Output environment variables CString str; str.Format("offset = %d, daylight = %d, %s, %s\n", _timezone, _daylight, _tzname[0], _tzname[1]); cout << (LPCTSTR)str<< endl; // Adjust given time to local time. COleDateTimeSpan timeSpanOffset(0, 0, 0, _timezone); COleDateTime timeLocal = timeUTC - timeSpanOffset; // Output local time str.Format("time UTC = %s, time local = %s", timeUTC.Format("%H:%M:%S"), timeLocal.Format("%H:%M:%S")); cout << (LPCTSTR)str<< endl; return timeLocal; } and produces the following output: offset = 18000, daylight = 1, Eastern Standard Time, Eastern Daylight Time time UTC = 11:23:45, time local = 06:23:45 Five hour difference which is fine during winter time, but when daylight saving is in effect, it only differs four hours. Windows apperently handles this fine, the clock in the notification area adjusts properly, but is there some way from the API to find out if daylight saving is in effect or not? Are there other ways than using the functions I been using? _daylight only tells you if Windows should adjust for daylight saving or not. Disabling automatic adjust of daylight saving in the Windows clock only affects the output as follows: offset = 18000, daylight = 0, Eastern Standard Time, Eastern Standard Time time UTC = 11:23:45, time local = 06:23:45 Thanks, Mellowman

    P 1 Reply Last reply
    0
    • P Patric_J

      Hi, I get times from a server given in UTC (Coordinated Universal Time or Universal Time Coordinated), that is similar to GMT (Greenwich Mean Time) but not adjusted to daylight saving time. That is, during summer, UTC differs one hour from London time, since UK uses daylight saving during summer. The application I develop is used worldwide so I can not assume or hardcode anything. This should not be a problem since winapi have functions to handle this. I do the following. The function I use to convert UTC time to local time (New York time for me) looks like this: COleDateTime ConvertToLocalTime(const COleDateTime& timeUTC) { // Set time environment variables. _tzset(); // Output environment variables CString str; str.Format("offset = %d, daylight = %d, %s, %s\n", _timezone, _daylight, _tzname[0], _tzname[1]); cout << (LPCTSTR)str<< endl; // Adjust given time to local time. COleDateTimeSpan timeSpanOffset(0, 0, 0, _timezone); COleDateTime timeLocal = timeUTC - timeSpanOffset; // Output local time str.Format("time UTC = %s, time local = %s", timeUTC.Format("%H:%M:%S"), timeLocal.Format("%H:%M:%S")); cout << (LPCTSTR)str<< endl; return timeLocal; } and produces the following output: offset = 18000, daylight = 1, Eastern Standard Time, Eastern Daylight Time time UTC = 11:23:45, time local = 06:23:45 Five hour difference which is fine during winter time, but when daylight saving is in effect, it only differs four hours. Windows apperently handles this fine, the clock in the notification area adjusts properly, but is there some way from the API to find out if daylight saving is in effect or not? Are there other ways than using the functions I been using? _daylight only tells you if Windows should adjust for daylight saving or not. Disabling automatic adjust of daylight saving in the Windows clock only affects the output as follows: offset = 18000, daylight = 0, Eastern Standard Time, Eastern Standard Time time UTC = 11:23:45, time local = 06:23:45 Thanks, Mellowman

      P Offline
      P Offline
      Patric_J
      wrote on last edited by
      #2

      Got the answer in another forum, here's the information if anyone else have similar problem: Subject: Re: Daylight saving problems when using winapi's timezone functions. From: "David A. Mair" Sent: 10/17/2003 1:02:16 PM I have a simple SNTP client app where I need to do the same thing. My case correctly handles daylight savings, here's the relevant section of code: DWORD tzResult; TIME_ZONE_INFORMATION tzInfo; // Get time zone offset tzResult = GetTimeZoneInformation(&tzInfo); m_Bias = (int)tzInfo.Bias; switch(tzResult) { case TIME_ZONE_ID_STANDARD: m_Bias += (int)tzInfo.StandardBias; m_TZ = tzInfo.StandardName; break; case TIME_ZONE_ID_DAYLIGHT: m_Bias += (int)tzInfo.DaylightBias; m_TZ = tzInfo.DaylightName; break; case TIME_ZONE_ID_UNKNOWN: break; default: m_Bias = 0; } m_Bias now holds the correct timezone offset from UTC in seconds for the local time regardless of the time of year (assuming the computer has a correctly configured clock and operating system). GetTimeZoneInformation() is a Win32 function IIRC.

      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