As a result of the Delphi NOW function, it affects the "Setting the clock for daylight saving time"

I understand that this question could be answered by writing some test code. I'm not lazy, I just thought that the answer might be generally useful.

I have an application that generated a large amount of data with records that were marked with local time (as was returned using the NOW procedure). We encountered obstacles with transitions to and from daylight saving time - namely, that the hour when we switch to daylight saving time and the hour we repeat when we exit DST. This causes problems with manipulations that involve records sorted by date.

The application has been changed, therefore, to work with all dates in UTC, but I will have the opportunity to display dates in UTC or in local time. I also have to deal with datetimes that were saved in local time, and make sure they are correctly moved to UTC. This is tricky since datetime could be saved when the DST was in effect, so in general I need to determine if there is any random date inside or outside the daylight saving time period. Of course, there is a period of one hour, when the date-time is ambiguous and may be at the last hour before daylight ends, or at the first hour after it ends. Unable to resolve this.

When coding the changes, I wondered about the result of the NOW calls. Inside, it calls GetLocalTime. What does GetLocalTime (and NOW) return when you are in the DST period, but the "Set clock for daylight saving time" option is disabled?

How to write a procedure that returns the current datetime in the DST period (using the DST offset), regardless of whether the "Setting the clock for daylight saving time" is turned off or on?

+4
source share
2 answers

I do not think you can easily solve your problem.
Too many variables:

  • saved timestamp
  • the time zone in which you are located
  • time zone change rules
  • confirmation that these time zone rules are accurate on all equipment you use (i.e. everyone always applied their corrections)
  • inaccuracy of your watch

There is a Delphi TZDB project that can help you with time zone rules.

I think it’s much more practical not to rely on all of the above variables, but to store three fields:

  • local timestamp
  • current time zone
  • UTC timestamp

You sort by the third field and the first two fields to display.

- Jeroen

+4
source

Use TzSpecificLocalTimeToSystemTime (and its obvious inverse). This allows you to convert between UTC and local date / time based on the day time settings that apply to the local date / time. If you want your application to start on something earlier than XP, download this (from kernel32) using the "delayed" function attribute:

function TzSpecificLocalTimeToSystemTime(lpTimeZoneInformation: PTimeZoneInformation; var lpLocalTime, lpUniversalTime: TSystemTime): BOOL; stdcall; function TzSpecificLocalTimeToSystemTime; external kernel32 name 'TzSpecificLocalTimeToSystemTime' delayed; 
+2
source

Source: https://habr.com/ru/post/1335172/


All Articles