Python tzinfo和夏令时 [英] Python tzinfo and daylight time

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

问题描述

(我是Python和Google App Engine的新手,请原谅我,如果我的问题看起来很基本)。

我有一段时间试图管理多个我的Google App Engine应用程序中的用户时区。



以下是我的限制:


  1. 如果用户在输入上输入时间,它将是当地时间(如果适用,包括DST)。

  2. 如果用户没有输入时间,系统必须输入他们在当地时间(包括DST,适当时)。

  3. 当日期和时间显示给用户时,它必须在当地时间(包括DST,适当时)

我知道时间将在UTC内部存储为tzinfo对象,并且App Engine将以UTC时间存储模型。 / p>

我以前认为我通过要求用户在他们的偏好中指定他们的时区完成了这一切。然后,我只需加载它们的首选项,并将tzinfo添加到引用该用户的任何日期时间对象中。



然而,我们最近的夏令时打破了它。事实证明,我没有在我的tzinfo对象中正确实现dst()。据我了解,我必须确定DST是否正在运行,如果是,请返回正确的tzinfo偏移量。



问题是,我不知道如何以确定时区的夏时制是否为当前时间。我错过了一些明显的东西吗?

如果可以的话,我建议看一看 python-dateutil 包,其中包含为所有时区预先构建的tz对象,包括DST偏移量信息。我不认为如果没有支持数据,DST是否开启就没有办法知道......它是由大多数时区(或至少支持DST的时区)驱动的日期,并且日期根据本地习惯,政治和各种事情。



http: //labix.org/python-dateutil


(I am new to Python and Google App Engine, please forgive me if my questions seem basic).

I'm having a helluva time trying to manage multiple user timezones in my Google App Engine application.

Here are my constraints:

  1. If a user enters the time on an input, it will be local time (including DST, when appropriate).
  2. If a user does not enter the time, the system must enter it for them in their local time (including DST, when appropriate).
  3. When the date and time are displayed to the user, it must be in their local time (including DST, when appropriate)

I understand that time will be internally stored as UTC with a tzinfo object and that the App Engine will store the Models with UTC time.

I previously thought I had this all worked out by asking the user to specify their time zone in their preferences. Then, I would simply load their preferences and add that tzinfo to any datetime objects in reference to that user.

However, our recent daylight saving time broke it. It turns out that I had not correctly implemented the dst() in my tzinfo objects. As I understand it, I must determine if DST is currently on, and if so, return the correct offset for the tzinfo.

The problem is, I have no idea how to determine if the timezone's daylight time is current or not. Am I missing something obvious?

解决方案

If you can, I'd recommend having a look at the python-dateutil package, which has tz objects pre-built for all timezones, including DST offset information. I don't think there's a way to "just know" if DST is on or not without supporting data... it's date driven for most timezones (or, at least, timezones that support DST), and the dates vary according to local custom, politics, and all manner of things.

http://labix.org/python-dateutil

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

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