82376

How to reliably convert local time to UTC on a Windows machine using C++ WinAPI

I need to be able to convert local time to UTC on a Windows machine. I found the SystemTimeToTzSpecificLocalTime API that claims to do this, but then I read its description and found this:

The SystemTimeToTzSpecificLocalTime function may calculate the local time incorrectly under the following conditions:

    <li>The time zone uses a different UTC offset for the old and new years.</li> <li>The UTC time to be converted and the calculated local time are in different years.</li> </ul>

    My first rhetorical question to MS, "How difficult is it to write a reliable API?"

    And then, the actual question — how do you do it to work in 100% of the times? (And not like that API suggests.)

    Answer1:

    The answer to both your questions is that it is essentially impossible. This is one of the reasons computer science folks tend to hate dealing with local time.

    Here are some examples of the confounding issues: When we jump back an hour from 2:00 to 1:00, there are two 1:30's. We also have no way to know today what the daylight savings time rules, if any, will be in ten years. Time zone boundaries are sometimes moved, so just knowing that you're in Pacific time today may not be enough to tell us what your time zone offset was 85 years ago. There is no way to know today what leap seconds may or may not be inserted in the future.

    If you find yourself in a situation when it is possible, you will have to code in the specific circumstances that make those operations possible in your particular case. The operations provided are generalized and provide "usually correct" results without constraining the cases in which they can be used.

    Moral: Don't use local time except for display. And even then, converting times in the past or future into local times is problematic.

    Answer2:

    Since the DST periods sometimes change in certain areas, it's hard to write something that is correct in all cases, past and future.

    Nevertheless, I you need this functionality, I would suggest using the C functions (localtime, mktime, ...). I recently had a problem where the Windows function SystemTimeToTzSpecificLocalTime did not work correctly on a Citrix server (Citrix does quite some strange (but advanced) things regarding timezones), but the C functions worked correctly.

Recommend

  • Rounding returned float values from a DB to their 'correct' values
  • How to check whether a command can be executed?
  • Web API Basic Auth inside an MVC app with Identity Auth
  • SiteMesh: Changing the content-type of the response
  • Disadvantages to high make job values
  • Assignment of Allocatables of Different Shapes in Fortran [duplicate]
  • const char **a = {“string1”,“string2”} and pointer arithametic
  • Read stdin in chunks in Bash pipe
  • Quickly or concisely determine the longest string per column in a row-based data collection
  • Submission of new app with iAds
  • Typecasting `this` of a base class template to its derived class
  • MonadTransControl instance for ProxyFast/ProxyCorrect
  • Should I be afraid to use UDP to make a client/server broadcast talk?
  • Quick Question About Get and Set
  • Find VMID for running instance
  • Bound DataGridViewCheckBoxColumn not updating binding source
  • Can my PDF ping my server when it is opened?
  • How do I mock an exported typescript function in a jasmine test?
  • Access user's phone number on iOS 7
  • Detecting null parameter in preprocessor macro
  • SyntaxError: (irb):26: both block arg and actual block given
  • Salesforce Different WSDL files and when to use
  • Clarification on min distance on LocationManager.requestLocationUpdates method, min Distance paramet
  • GAE: Way to get reference to an HttpSession from its ID?
  • Sequential (transactional) API calls in angular 4 with state management
  • presentShareDialogWithParams posts to FB wall, but callback handler results say error
  • Time complexity of a program which involves multiple variables
  • Checking free space on FTP server
  • Email format validation in mvc3 view
  • C# - Is there a limit to the size of an httpWebRequest stream?
  • PHP - How to update data to MySQL when click a radio button
  • How to add date and time under each post in guestbook in google app engine
  • Javascript Callbacks with Object constructor
  • How can I estimate amount of memory left with calling System.gc()?
  • Jquery - Jquery Wysiwyg return html as a string
  • Arrays break string types in Julia
  • Matrix multiplication with MKL
  • WPF Applying a trigger on binding failure
  • coudnt use logback because of log4j
  • Java static initializers and reflection