这是使用dateutil设置时区的正确方法吗? [英] Is this the right way to set a timezone with dateutil?

查看:638
本文介绍了这是使用dateutil设置时区的正确方法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

>>> import dateutil.parser, dateutil.tz as tz
>>> dateutil.parser.parse('2017-08-09 10:45 am').replace(tzinfo=tz.gettz('America/New_York'))
datetime.datetime(2017, 8, 9, 10, 45, tzinfo=tzfile('/usr/share/zoneinfo/America/New_York'))

是真的是我们应该设置默认时区进行解析的方式吗?我已经阅读了 parser 示例,但我似乎找不到任何内容,这是设置默认时区的方法for dateutil.parser.parse,甚至其他类似的东西。

Is that really the way that we're supposed to set a default timezone for parsing? I've read the documentation for the parser and examples but I cannot seem to find anything that says, "This is how to set the default timezone for dateutil.parser.parse", or even anything like it.

由于此起作用,在某些情况下它会做错事情东西,如果提供了区域。

Because while this works, there are cases where it would do the wrong thing, if the zone were provided. Does that mean we should do this?

>>> d = dateutil.parser.parse('2017-08-09 10:45 am +06:00')
>>> d = d.replace(tzinfo=d.tzinfo or tz.gettz('America/Chicago'))

因为它也很笨。

在解析时设置默认时区的建议方法是什么?

What's the recommended way to set a default timezone when parsing?

推荐答案

基本上有两种正确的方法可以做到这一点。您可以看到这是在上作为第94期出现的。 dateutil 的问题跟踪程序和设置默认时区被确定不在范围内,因为使用解析器返回的信息可以轻松完成此操作(因此,需要将其内置到解析器本身中)。两种方式是:

There are basically two "correct" ways to do this. You can see that this was brought up as Issue #94 on dateutil's issue tracker, and "set a default time zone" is determined to be out of scope, since this is something that can be easily done with the information returned by the parser anyway (and thus no need to build it in to the parser itself). The two ways are:


  1. 提供一个默认值的日期,时区。如果您不介意默认日期是什么,则只需指定一些日期文字即可完成操作。如果您希望行为与 dateutil 的默认行为基本相同(替换今天午夜的日期中缺少的元素),则必须有一些样板:

  1. Provide a default date that has a time zone. If you don't care what the default date is, you can just specify some date literal and be done with it. If you want the behavior to be basically the same as dateutil's default behavior (replacing missing elements from "today's date at midnight"), you have to have a bit of boilerplate:

from datetime import datetime, time
from dateutil import tz, parser
default_date = datetime.combine(datetime.now(),
                                time(0, tzinfo=tz.gettz("America/New_York")))
dt = parser.parse(some_dt_str, default=default_date)


  • .replace 使用第二种方法:

    from dateutil import parser
    def my_parser(*args, default_tzinfo=tz.gettz("America/New_York"), **kwargs):
        dt = parser.parse(*args, **kwargs)
        return dt.replace(tzinfo=dt.tzinfo or default_tzinfo)
    


  • 最后一个可能比第一个稍微干净一些,但是如果在自从e第一个只需要创建一次的默认日期),但是 dateutil 的解析器实际上很慢,因此如果您使用重新运行它。

    This last one is probably slightly cleaner than the first, but has a slight performance decrease if run in a tight loop (since the first one only needs the default date created once), but dateutil's parser is actually quite slow, so an extra date construction is likely the least of your problems if you're running it in a tight loop.

    这篇关于这是使用dateutil设置时区的正确方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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