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?
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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With