Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to determine if a date/time does not exist?

Fun fact that I'm sure most of us who get to play in the time realms know - there are date/times that can appear valid but actually do not exist, e.g. 2:30 AM on a daylight savings switching time.

Is there a way in C++ (standard or Windows) to figure out if a given date/time is valid in a given time zone specification?

like image 960
dlanod Avatar asked Apr 08 '16 07:04

dlanod


Video Answer


1 Answers

Using Windows-specific functions, you can make a call to TzSpecificLocalTimeToSystemTime() followed by a call to SystemTimeToTzSpecificLocalTime() with the relevant time zone.

Upon completion, if the two differ you have an invalid time as TzSpecificLocalTimeToSystemTime converts the invalid time to a "real" time.

bool IsValidTime(TIME_ZONE_INFORMATION tz, SYSTEMTIME st)
{
    SYSTEMTIME utcSystemTime, st2;
    TzSpecificLocalTimeToSystemTime(&tz, &st, &utcSystemTime);
    SystemTimeToTzSpecificLocalTime(&tz, &utcSystemTime, &st2);

    return (st != st2);
}
like image 180
dlanod Avatar answered Oct 19 '22 02:10

dlanod