“根据夏令时变化调整时钟”选项如何影响 Delphi NOW 函数的结果?

发布于 2024-10-11 16:22:12 字数 588 浏览 8 评论 0原文

我意识到这个问题可以通过编写一些测试代码来回答。我并不懒惰,我只是认为答案可能普遍有用。

我有一个应用程序生成了大量数据,其中的记录带有当地时间(由 NOW 例程返回)。我们在夏令时的进出转换方面遇到了障碍 - 即,当我们更改为夏令时时,会缺少一个小时,而当我们退出夏令时时,会重复一个小时。这会导致假设日期排序记录的操作出现问题。

因此,该应用程序已更改为可以使用 UTC 中的所有日期时间,但我将能够显示 UTC 或本地时间中的日期时间。我还必须处理以本地时间存储的日期时间,并确保它们正确地转换为 UTC。这很棘手,因为日期时间可能是在 DST 生效时存储的,因此在一般情况下,我需要能够确定任何随机日期是否在 DST 期间内或之外。当然,有一小时的日期时间是不明确的,可能是夏令时结束前的最后一小时,也可能是夏令时结束后的第一个小时。没有办法解决这个问题。

在对更改进行编码时,我想知道 NOW 调用的结果。它在内部调用 GetLocalTime。当您处于 DST 期间,但“根据夏令时变化调整时钟”选项已关闭时,GetLocalTime(和 NOW)会返回什么?

如何编写一个例程,无论“调整夏令时变化的时钟”是关闭还是打开,都返回 DST 周期内的当前日期时间(应用了 DST 偏差)?

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

I have an app that has generated a large amount of data with records that were stamped with the local time (as returned by the NOW routine). We have run into a snag with transitions in and out of daylight savings time - namely that there is an hour missing when we change to DST, and an hour repeated when we exit from DST. This causes problems with manipulations that assume date ordered records.

The app has been altered therefore to work with all datetimes in UTC, but I will have the ability to display datetimes in UTC or in local time. I also have to deal with datetimes that were stored in local time, and make sure they are correctly shifted to UTC. This is tricky, as the datetime might have been stored while DST was in effect, so in the general case I need to be able to determine if any random date is within or outside a DST period. There is of course a period of one hour where a datetime is ambiguous and could be in the last hour before daylight savings ended, or in the first hour after it ended. There is no way of resolving this.

In coding the changes, I wondered about the result of NOW calls. Internally it calls GetLocalTime. What does GetLocalTime (and NOW) return when you are inside a DST period, but the option to "Adjust clock for daylight saving changes" is turned off?

How do I write a routine that returns the current datetime inside a DST period (with the DST bias applied) regardless of whether "Adjust clock for daylight saving changes" is off or on?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

拥醉 2024-10-18 16:22:12

我不认为你能轻易解决你的问题。
有太多变量:

  • 存储的时间戳
  • 您所在的时区
  • 不断变化的时区规则
  • 确认这些时区规则在您使用的所有设备上都是准确的(即每个人总是应用补丁)
  • 您的时钟不

准确可以帮助您处理时区规则的 Delphi TZDB 项目

我认为不依赖上述所有变量,而是存储三个字段更实用:

  • 本地格式的时间戳
  • 当前时区
  • UTC 格式的时间戳

您对第三个字段和前两个字段执行排序显示。

——杰罗恩

I don't think you can solve your problem easily.
There are too many variables:

  • the stored timestamp
  • the time zone you are in
  • the ever changing time zone rules
  • confirmation that these time zone rules are accurate on all the equipment you use (i.e. everyone always applied their patches)
  • the inaccuracy of your clock

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

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

  • the timestamp in your local format
  • the current timezone
  • the timestamp in UTC format

You perform the sorting on the third field, and the first two fields for displaying.

--jeroen

鲜血染红嫁衣 2024-10-18 16:22:12

使用 TzSpecificLocalTimeToSystemTime (及其明显的逆)。这些允许您根据本地日期/时间有效的夏令时设置在 UTC 和本地日期/时间之间进行转换。如果您希望您的应用程序在 XP 之前的任何操作系统上运行,请使用“延迟”函数属性加载此应用程序(从 kernel32):

function TzSpecificLocalTimeToSystemTime(lpTimeZoneInformation: PTimeZoneInformation;
  var lpLocalTime, lpUniversalTime: TSystemTime): BOOL; stdcall;

function TzSpecificLocalTimeToSystemTime; external kernel32 name 'TzSpecificLocalTimeToSystemTime' delayed;

Use TzSpecificLocalTimeToSystemTime (and its obvious inverse). These allow you to convert between UTC and local date/time based on the daylight savings settings in effect at the local date/time. If you want your app to run on anything earlier than XP, load this (from kernel32) with the 'delayed' function attribute:

function TzSpecificLocalTimeToSystemTime(lpTimeZoneInformation: PTimeZoneInformation;
  var lpLocalTime, lpUniversalTime: TSystemTime): BOOL; stdcall;

function TzSpecificLocalTimeToSystemTime; external kernel32 name 'TzSpecificLocalTimeToSystemTime' delayed;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文