I need somehow to determine whether some TDateTime value is within the Daylight Saving Time range for my timezone or not (in C# the same thing does the DateTime.IsDaylightSavingTime() method).
I know in Delphi there’s no similar function, because Delphi TDateTime contains no information about timezone, but I suppose there’s some way how to do this using Win32 API.
I’ve looked at Win32 API GetTimeZoneInformation and GetTimeZoneInformationForYear functions, but I don’t quite understand how to use them, so I’d like to ask you for help. Thanks in advance for any tips.
Edit:
Example:
In my timezone (Central European) Daylight Saving Time started this year on
28th March at 2 am and ends on 31st October 2010 at 3 am.
I need a function with header:
function IsDaylightSavingTime(input: TDateTime): boolean;
that will return true if the input date is between 28th March 2010 2:00 and 31st October 2010 3:00 and false if not.
(The example is just for year 2010, but I’d need it to work for all years.)
Once again, I know that information saved in TDateTime alone is not enough, but I think that with some Win32 API function I should be able to get e.g. information about current timezone from Windows settings.
It is not as easy as it sounds, because:
1) The switch-over date between DST and standard time is not the same for all countries
2) The switch-over date between DST and standard time is not the same algorithm for the same country for all years (for example in Central Europe it was previously first Sunday in April, IIRC, now it is last Sunday in March). The USA changed from first Sunday in April to the second Sunday in March from 2007 and on.
So – a simple date is not enough, you’ll also need a geographical location.
But, if you can live with the fact, that you limit yourself to the switch-over dates that can be calculated from the CURRENT algorithm for the CURRENT year for the CURRENT locale (country) and that this may be wrong for dates both in the future and in the past, then you can use the information in TIME_ZONE_INFORMATION to calculate the switch-over dates:
Looping through the years 2000 to 2020 on my PC (Central Europe Time Zone), I get the following dates:
but at least some of these years are incorrect due to the algorithm having changed from my locale in the years listed.
Your function would then be: