UTC时间,时区,夏令时和夏令时切换日期 [英] UTC Time, Timezones, Daylight Savings, and Daylight Savings switchover dates

查看:604
本文介绍了UTC时间,时区,夏令时和夏令时切换日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个应用程序,它将能够在任何特定的本地时间将电子邮件发送到世界各地。



例如,我的日程安排(本地时间):




  • 8:00上午-向加拿大多伦多的约翰发送电子邮件

  • 9:15 AM西部标准时间(澳大利亚)-向澳大利亚珀斯的鲍勃发送电子邮件




我希望能够在Amazon EC2上执行此代码单个位置的服务器(例如,巴西的圣保罗)。



我也知道多伦多是东部标准时间(UTC-5h),但是从2012年3月11日到2012年11月4日,它是东部夏令时(UTC-4h)。



我也知道珀斯处于西部标准时间(UTC + 8h),没有夏令时。



我也知道拉巴特位于西欧时间(UTC),但是从2012年4月29日至2012年7月20日以及2012年8月19日至9月30日, 2012年是西欧夏令时(UTC + 1h)



为了跟踪时区,夏令时等的组合,我当然会坚持将所有内部服务器时间都设置为UTC。但是,我需要某种方式来跟踪由于夏时制或斋月(对于拉巴特而言)每个时区管辖区何时以及如何切换时区,然后调整我的crontab来适应这些更改。



在某处是否有权威的Web服务或一组表,可以帮助我使这些时区更改与我希望每天在同一本地时间发送电子邮件的需求保持同步?


解决方案

大多数编程语言都使您可以访问时区转换功能。最基础的服务器只能在UTC和服务器的本地时区之间工作,因此您将需要功能齐全的服务器,例如 pytz (适用于Python),可让您使用时区名称(例如 America / Toronto)指定本地时间,并为您将其转换为UTC。鉴于此,您无需担心不同时区的UTC偏移(包括已更改的历史偏移),也不必担心DST开始结束结束时间:库会为您处理。只需确保您拥有最新的数据库,该数据库位于 tzdata 软件包中。



对于您的crontab,如果运行cron的服务器上的本地时区是UTC,则可能是最好的选择,这样您就可以将UTC时间直接放在crontab中。另一方面,根据事件的数量,我建议您以固定的时间间隔(例如每5分钟)运行cron运行您的代码,然后您的代码根据需要确定哪些事件需要触发当前UTC时间和数据库内容。那么服务器的时区是什么都没关系。


I'm building an application which will be able to send emails at any specific local time to any place in the world.

For example, my daily schedule (localtime):

  • 8:00 AM - Send email to John in Toronto, Canada
  • 9:15 AM Western Standard Time (Australia) - Send email to Bob in Perth, Australia
  • 10:12 PM - Send email to Anas in Rabat, Morocco

I want to be able to execute this code on and Amazon EC2 server in a single location (e.g. São Paulo, Brasil).

I also know that Toronto is in Eastern Standard Time, (UTC - 5h) , but from March 11, 2012 to November 4, 2012, it is in Eastern Daylight Time (UTC - 4h).

I also know that Perth is in Western Standard Time (UTC + 8h), with no daylight savings.

I also know that Rabat is in Western European Time (UTC), but from April 29,2012 to July 20,2012, and August 19,2012 to Sept 30, 2012 it is in in West European Summer Time (UTC + 1h)

To keep track of these combinations of time zone, daylight savings, et cetera, I will, of course insist that all internal server times be in UTC. However, I need some way to keep track of when and how each time-zone jurisdiction switches time zones because of Daylight Savings or (in the case of Rabat) Ramadan, and then adjust my crontabs to accommodate these changes.

Is there an authoritative web service or set of tables somewhere which would help me keep these timezone changes in sync with my desire to deliver emails at the same local time every day to users in different time zones with different switchover dates for daylight savings?

解决方案

Most programming languages give you access to timezone conversion functions. The most rudimentary ones only work between UTC and the "local" timezone of the server, so you will need a full-featured one, such as pytz for Python that will let you specify a local time with a timezone name (e.g. "America/Toronto") and convert it to UTC for you. Given that, you don't need to worry about the UTC offsets of different timezones (including historical offsets if they've changed) nor DST start end end times: the library will take care of it for you. Just make sure you have the latest database, which comes in the tzdata package.

As for your crontab, you're probably best off if the local timezone on the server that runs cron is UTC, that way you can put UTC times directly in the crontab. On the other hand, depending on the volume of events that you have, I would advice just having cron run your code at regular schedules intervals (such as every 5 minutes) and then your code figures out what events need to be triggered based on the current UTC time and the contents of your database. Then it doesn't matter what the timezone of the server is.

这篇关于UTC时间,时区,夏令时和夏令时切换日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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