日程安排DST [英] Scheduling & DST

查看:154
本文介绍了日程安排DST的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在PHP中编写日历/日程安排应用程序。现在我拿起​​你想要的事件发生的一周的时间和时间。我也要求时区,并相应地调整,以获取事件时间在GMT。

I am writing a calendar/scheduling app in PHP. Right now I take the day of the week you want the event to occur on and the time. I also ask for the timezone and adjust accordingly to get the event time in GMT.

然后我将该时间存储为节目当天午夜的偏移量。这是好的,工作伟大,但当我打开夏令时会发生什么?我不知道当发生了什么。另一个问题是,并不是所有的国家都有DST,所以我有一种约束。

Then I store that time as an offset from midnight of the day of the show. This is fine and works great, but what happens when I hit daylight savings time? I'm not sure what to do when that happens. The other problem is that not all countries have DST so I'm kind of in a bind there.

我在日历上显示这些事件,因此时间很重要。

I am displaying these events on a calendar, so timing is important.

推荐答案

我想你在GMT(UTC)的正确轨道上。每当有特定时间点发生(或已发生)的某个事件时,使用UTC作为规范时间戳表示。 UTC不受DST规则的影响,因此它作为一个伟大的,明确的时间点表示。

I think you are on the right track with GMT (UTC). Use UTC as your canonical timestamp representation whenever you have some event that occurs (or has occurred) at a specific point-in-time. UTC is unaffected by DST rules, so it serves as a great, unambiguous point-in-time representation.

一旦您有明确表示事件日期/时间的策略,就可以很容易地解决日期/时间的本地化问题基于什么时区最接近您的用户(或显示给他们)。您可能需要知道并存储事件的时区,但是,因为您以UTC的形式存储实际的事件日期/时间,您可以很容易地将其定位到用户的时区,如果这更相关给他们在任何给定的用例。

Once you have a strategy for unambiguous representation of the event date/time, then you can pretty easily solve the problem of localizing date/times based on what time zone makes the most sense to accept from your users (or display to them). You probably need to know and store the time zone of the event as well, but, because you're storing the actual event date/time in UTC, you can pretty easily localize it to the time zone of the user if that is more relevant to them in any given use case.

此本地化通常使用SDK提供的时区库(例如Java有java.util.Calendar)或第三方扩展(例如Python pytz)。我确定PHP有一个等价的,但我不熟悉它的库。

This localization is usually performed with a time zone library either provided by your SDK (e.g. Java has java.util.Calendar) or as a third party extension (e.g. Python has pytz). I'm sure PHP has an equivalent, but I'm not as familiar with its libraries.

这些库通常建立在规则数据库的顶部,如< a href =http://en.wikipedia.org/wiki/Zoneinfo =nofollow> Olson Zoneinfo DB 。这些规则可以频繁更改,因此您必须始终了解底层数据库的更新,特别是如果您正在开发一个真正的全局应用程序。然而,他们做了很好的外部化深奥的,模糊的时区规则,以便你可以(理论上)更新规则数据库,而不必执行运行时环境的主要升级或在DST规则在一个特殊的区域变化。

These libraries are usually built on top of a rules database such as the Olson Zoneinfo DB. These rules can change quite frequently, so you have to stay on top of updates to the underlying database, especially if you are developing a truly global application. However, they do a good job of externalizing the esoteric, nebulous time zone rules so that you can (in theory) update the rules database without have to perform a major upgrade of your runtime environment or make significant code changes when the DST rules in a particular area change.

这不是世界上最简单的问题,臭味,我们必须这样做,但一旦你做了几次,在存储精确的时间点表示和本地化的关注之间的关系,然后它成为第二个性质。

It's not the easiest problem in the world and stinks that we have to do it, but once you've done it a couple times and grok the separation of concerns between storing exact point-in-time representation vs. the concern of localization, then it becomes second nature.

这篇关于日程安排DST的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆