Python tzinfo和夏令时

编程入门 行业动态 更新时间:2024-10-25 16:28:31
本文介绍了Python tzinfo和夏令时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

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

以下是我的限制:

  • 如果用户在输入上输入时间,它将是当地时间(如果适用,包括DST)。
  • 如果用户没有输入时间,系统必须输入他们在当地时间(包括DST,适当时)。
  • 当日期和时间显示给用户时,它必须在当地时间(包括DST,适当时)
  • 我知道时间将在UTC内部存储为tzinfo对象,并且App Engine将以UTC时间存储模型。 / p>

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

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

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

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

    http: //labix/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:

  • If a user enters the time on an input, it will be local time (including DST, when appropriate).
  • If a user does not enter the time, the system must enter it for them in their local time (including DST, when appropriate).
  • 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.

    labix/python-dateutil

    更多推荐

    Python tzinfo和夏令时

    本文发布于:2023-10-28 08:23:51,感谢您对本站的认可!
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:夏令时   Python   tzinfo

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!