Search code examples
c++timezoneborland-c++

Looking for the next "Daylight Saving Time" using the Windows registery


Looking for the next "Daylight Saving Time" using the Windows registry.

I have to use an old version of Borland C++.

Idea from Daylight Saving Time change with an absolute date

The "W. Europe Standard Time" in the registry says that the "Daylight Saving Time" occurs in the 5th occurrence of the Sunday of March which is false because it is the 4th occurrence. MSDN doc.

So when I am doing :

void findDate(int year, SYSTEMTIME systemTime)
{
    struct tm timestr;
    int occ = 0;
    bool found = false;

    timestr.tm_year = year - 1900;
    timestr.tm_mon = systemTime.wMonth - 1;
    timestr.tm_mday = 0;
    timestr.tm_hour = systemTime.wHour;
    timestr.tm_min = systemTime.wMinute;
    timestr.tm_sec = systemTime.wSecond;
    timestr.tm_isdst = 0;

    while((timestr.tm_mday <= 31 && !found)) {
        timestr.tm_mday += 1;
        time_t standard = mktime(&timestr);
        struct tm * temp = localtime(&standard);
        if(temp->tm_wday == systemTime.wDayOfWeek) {
           occ++;
        }

        if(occ >= sysemTime.wDay) {
             found = true;
             printf("\n\nFound: %s", asctime( &timestr ));
        }
    }
}

int main(int argc, char* argv[])
{
 // Get the timezone info.
 TIME_ZONE_INFORMATION TimeZoneInfo;
 GetTimeZoneInformation( &TimeZoneInfo );
 DYNAMIC_TIME_ZONE_INFORMATION TimeZoneInfoDyn;

 printf("\n\nStandardDate %d wDay%d wDayOfWeek%d %d %d %d", TimeZoneInfo.StandardDate.wMonth, TimeZoneInfo.StandardDate.wDay, TimeZoneInfo.StandardDate.wDayOfWeek, TimeZoneInfo.StandardDate.wHour, TimeZoneInfo.StandardDate.wMinute, TimeZoneInfo.StandardDate.wSecond);
 printf("\nDaylightDate %d wDay%d wDayOfWeek%d %d %d %d", TimeZoneInfo.DaylightDate.wMonth, TimeZoneInfo.DaylightDate.wDay, TimeZoneInfo.DaylightDate.wDayOfWeek, TimeZoneInfo.DaylightDate.wHour, TimeZoneInfo.DaylightDate.wMinute, TimeZoneInfo.DaylightDate.wSecond);

 findDate(2017, TimeZoneInfo.StandardDate);
 findDate(2018, TimeZoneInfo.DaylightDate);
 getchar();
 return 0;

}

It displays :

Found: Sun Oct 29 03:00:00 2017
Found: Sun Apr 01 03:00:00 2018

Instead of :

Found: Sun Oct 29 03:00:00 2017
Found: Sun Mar 25 03:00:00 2018

What am I doing wrong?


Solution

  • The "W. Europe Standard Time" in the registery says that the "Daylight Saving Time" occurs in the 5th occurance of the sunday of March which is false because it is the 4th occurance.

    From the documentation quoted in the linked answer:

    (1 to 5, where 5 indicates the final occurrence during the month if that day of the week does not occur 5 times).

    So it is not false.

    Furthermore, in your loop you increment tm_mday and occ before checking whether you've already reached the desired day. If you were on it to start with, you'll overshoot.

    By the way, sysemTime is misspelt.