SetWaitableTimer and daylight saving adjustments
For the purpose of a mini-scheduler inside my app I need to be able to schedule an event at some absolute local time.
For instance, I may want to schedule it for 7 AM
on November 5, 2017
. As anyone living in US probably knows, that is the night when the daylight saving ends at 2 AM
.
So I use the following method:
//ERROR CHECKS ARE OMITTED FOR BREVITY
HANDLE hTimer = ::CreateWaitableTimer(NULL, FALSE, NULL);
//Nov 5, 2017 at 7:00 AM
SYSTEMTIME stLocal = {2017, 11, 0, 5, 7, 0, 0, 0};
SYSTEMTIME stUTC = {0};
::TzSpecificLocalTimeToSystemTime(NULL, &stLocal, &stUTC);
FILETIME ftUTC = {0};
::SystemTimeToFileTime(&stUTC, &ftUTC);
//Set timer
::SetWaitableTimer(hTimer, (LARGE_INTEGER*)&ftUTC, 0, NULL, NULL, FALSE);
//Wait for it
::WaitForSingleObject(hTimer, INFINITE);
::CloseHandle(hTimer);
Since the SetWaitableTimer
API takes time in UTC
form, the method above will be off by 1 hour because of the local-to-UTC conversion done before DST commences on Nov. 5th.
So assuming any arbitrary absolute time for the timer, how do you factor in the daylight saving adjustment?
You do not have to change your code to factor in the daylight saving adjustment, the TzSpecificLocalTimeToSystemTime API will already do that for you.
Please note that for future timestamps, the data Windows has for the DST->STD (and vice versa) transitions may be inaccurate. The government may choose to use a different moment for that in the future. Also, sometimes a government may choose to use a whole different time zone, that has happened! For timestamps in the past you probably will get good results, but you will never know how complete Windows' database is.
So, you don't have to change your code, but be aware of the fact that you still may get inaccurate results.
链接地址: http://www.djcxy.com/p/29398.html上一篇: Windows中的历史夏令时