Converting old and new local times to UTC under Windows XP/Server 2003
-
12-09-2019 - |
Question
My application converts past and present dates from local time to UTC.
I need to ensure I will honor any future DST updates to Windows while still correctly handling past dates.
The application is written in C++ and is running on Server 2003.
Options I've researched:
gmtime()
andlocaltime()
are not always correct for past dates because they will only ever observe current DST rules. (related SO question)A tz database is out because it requires a separate manual update.
GetTimeZoneInformationForYear()
is out because it requires Vista/Server 2008.Past DST information is stored in the registry, but I'm looking for something higher-level.
Boost
date_time
:- class
us_dst_rules
is deprecated and does not update if the OS updates. - class
dst_calc_engine<>
is its successor, but it does not respect OS updates either.
- class
So...
... is anyone else using the raw registry solution to do this?
... any other suggestions?
(edit: found out dst_calc_engine
doesn't support DST updates)
Solution
I think I'd prefer to re-implement GetTimeZoneInformationForYear
and possibly GetDynamicTimeZoneInformation
based on the information in the registry at HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Time Zones
.
That way, your code will follow Windows updates and you can swap the dirty code out for the actual implementation on up-level platforms.
Since you don't want to use an external database, I think no other options are viable.
OTHER TIPS
You could use gmtime() and localtime() for dates in 2007 and later (and take advantage of Windows DST updates), and use Boost or one of the other solutions you mentioned to use the correct DST rules for 2006 and earlier.