Skip to content

Instantly share code, notes, and snippets.

@ynkdir
Created June 25, 2026 15:22
Show Gist options
  • Select an option

  • Save ynkdir/13d9477c9b9e8a0dbbcb05ab124b3a6b to your computer and use it in GitHub Desktop.

Select an option

Save ynkdir/13d9477c9b9e8a0dbbcb05ab124b3a6b to your computer and use it in GitHub Desktop.
Test SystemTimeToTzSpecificLocalTime() behavior.
// Test SystemTimeToTzSpecificLocalTime() behavior.
// Does SystemTimeToTzSpecificLocalTime() calculate Daylight Saving Time (DST) with dynamic time zone information?
//
// Test with "Eastern Standard Time"
// 2006: DST start at first Sunday in April
// 2007: DST start at second Sunday in March
//
// tzutil.exe /s "Eastern Standard Time"
// .\a.exe
//
// test1: call with NULL (calculate dst with current dynamic time zone)
// utc: 2006-03-30 00:00
// local: 2006-03-29 19:00 < STD (UTC-5)
// utc: 2007-03-30 00:00
// local: 2007-03-29 20:00 < DST (UTC-4)
//
// test2: call with current TIME_ZONE_INFORMATION (calculate dst with current dynamic time zone)
// utc: 2006-03-30 00:00
// local: 2006-03-29 19:00 < STD (UTC-5)
// utc: 2007-03-30 00:00
// local: 2007-03-29 20:00 < DST (UTC-4)
//
// test3: call with modified TIME_ZONE_INFORMATION (specified time_zone_information is used)
// utc: 2006-03-30 00:00
// local: 2006-03-29 19:59 < DST (UTC-4-1m)
// utc: 2007-03-30 00:00
// local: 2007-03-29 19:59 < DST (UTC-4-1m)
#include <windows.h>
#include <print>
void test1(WORD year, WORD month, WORD day, WORD hour, WORD minute)
{
SYSTEMTIME st_utc { year, month, 0, day, hour, minute, 0, 0 };
SYSTEMTIME st_local;
if (!SystemTimeToTzSpecificLocalTime(nullptr, &st_utc, &st_local))
{
std::println("test1 fails");
return;
}
std::println(" utc: {:04}-{:02}-{:02} {:02}:{:02}", st_utc.wYear, st_utc.wMonth, st_utc.wDay, st_utc.wHour, st_utc.wMinute);
std::println("local: {:04}-{:02}-{:02} {:02}:{:02}", st_local.wYear, st_local.wMonth, st_local.wDay, st_local.wHour, st_local.wMinute);
}
void test2(WORD year, WORD month, WORD day, WORD hour, WORD minute)
{
TIME_ZONE_INFORMATION tzi;
SYSTEMTIME st_utc { year, month, 0, day, hour, minute, 0, 0 };
SYSTEMTIME st_local;
if (GetTimeZoneInformation(&tzi) == TIME_ZONE_ID_INVALID)
{
std::println("test2 fails");
return;
}
if (!SystemTimeToTzSpecificLocalTime(&tzi, &st_utc, &st_local))
{
std::println("test2 fails");
return;
}
std::println(" utc: {:04}-{:02}-{:02} {:02}:{:02}", st_utc.wYear, st_utc.wMonth, st_utc.wDay, st_utc.wHour, st_utc.wMinute);
std::println("local: {:04}-{:02}-{:02} {:02}:{:02}", st_local.wYear, st_local.wMonth, st_local.wDay, st_local.wHour, st_local.wMinute);
}
void test3(WORD year, WORD month, WORD day, WORD hour, WORD minute)
{
TIME_ZONE_INFORMATION tzi;
SYSTEMTIME st_utc { year, month, 0, day, hour, minute, 0, 0 };
SYSTEMTIME st_local;
if (GetTimeZoneInformation(&tzi) == TIME_ZONE_ID_INVALID)
{
std::println("test3 fails");
return;
}
tzi.Bias += 1;
if (!SystemTimeToTzSpecificLocalTime(&tzi, &st_utc, &st_local))
{
std::println("test3 fails");
return;
}
std::println(" utc: {:04}-{:02}-{:02} {:02}:{:02}", st_utc.wYear, st_utc.wMonth, st_utc.wDay, st_utc.wHour, st_utc.wMinute);
std::println("local: {:04}-{:02}-{:02} {:02}:{:02}", st_local.wYear, st_local.wMonth, st_local.wDay, st_local.wHour, st_local.wMinute);
}
int main()
{
test1(2006, 3, 30, 0, 0);
test1(2007, 3, 30, 0, 0);
std::println("");
test2(2006, 3, 30, 0, 0);
test2(2007, 3, 30, 0, 0);
std::println("");
test3(2006, 3, 30, 0, 0);
test3(2007, 3, 30, 0, 0);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment