python-3.x - python中人类可读的时区?

标签 python-3.x datetime timezone localtime

如何从python中的这些时间格式获得人类可读的时区?
以及如何将同一时间转换为该时区?

'scheduled_datetime': '2017-07-30T10:00:00+05:30'

session.scheduled_datetime
datetime.datetime(2017, 7, 30, 10, 0, tzinfo=<DstTzInfo 'Asia/Kolkata' IST+5:30:00 STD>)

最佳答案

您可以使用 iso8601dateutil.parser 来完成:

import iso8601
import dateutil.parser
from pytz import timezone

fmt = '%Y-%m-%d %H:%M:%S %Z'
ist =  timezone('Asia/Kolkata')

str = '2017-07-30T10:00:00+05:30'
d = iso8601.parse_date(str).astimezone(ist)
print(d.strftime(fmt))

d = dateutil.parser.parse(str).astimezone(ist)
print(d.strftime(fmt))

这两种情况的输出是:

2017-07-30 10:00:00 IST



请注意,我使用了 Asia/Kolkata 而不是 IST 。那是因为这些 3 个字母的名称是 ambiguous and not standard : IST 可以是 India Standard TimeIsrael Standard TimeIrish Standard Time (最后一个也称为 45 爱尔兰夏令时 在爱尔兰的夏令时期间)。

总是喜欢使用 IANA timezones names (总是采用 Continent/City 格式,如 America/Sao_PauloEurope/Berlin )。如果 Asia/Kolkata 不是您所需要的,请检查 this list 以找到最适合您情况的那个。

如果你不知道使用哪个时区,想根据偏移量来猜测,恐怕没那么简单。

以您的输入为例:偏移量为 +05:30(比 UTC 早 5 小时 30 分钟)。有 more than one timezone 使用此偏移量(或在其历史记录中使用过一次)。维基百科链接包含 3 个,但我可以找到以下内容:

Asia/Dhaka, Asia/Calcutta, Asia/Karachi, Asia/Dacca, Asia/Thimbu, Asia/Katmandu, Asia/Thimphu, Asia/Kolkata, Asia/Colombo, Asia/Kathmandu



所有这些时区都使用(或过去曾使用过)+05:30 偏移量。由于时区在历史上可能会发生变化,因此在尝试根据偏移量猜测时区时会遇到一些问题:
  • 您需要知道指定日期和时间的当前偏移量是多少。在您的情况下,您使用的是 2017-07-30 ,但我假设您想处理过去的任何日期。
  • 在指定的日期和时间可以有多个具有有效偏移量的时区(然后您必须根据任意条件选择一个)。
  • 某些时区(取决于日期)可能处于夏令时,这意味着偏移量不会是 +05:30(或者更糟糕的是,有些时区可能会在 DST 期间使用 +05:30)。

  • 因此,根据偏移量,您不能只获得一个时区。您能做的最好的事情是获取 候选列表 :包含偏移在相应日期/时间有效的所有时区的列表。然后你必须选择其中之一(也许如果你有“首选”列表):
    str = '2017-07-30T10:00:00+05:30'
    # parse date and get the offset (in this case, +05:30)
    d = iso8601.parse_date(str)
    offset = d.tzinfo.utcoffset(d)
    
    # a set with all candidate timezones
    zones = set()
    # find a zone where the offset is valid for the date
    for tz in pytz.all_timezones:
        zone = timezone(tz)
        # get the offset for the zone at the specified date/time
        zoneoffset = zone.utcoffset(d.replace(tzinfo=None))
        if (zoneoffset == offset):
            zones.add(zone)
    
    # just checking all the candidates timezones
    for z in zones:
        print(z, d.astimezone(z).strftime(fmt))
    

    输出将是:

    Asia/Kolkata 2017-07-30 10:00:00 IST
    Asia/Colombo 2017-07-30 10:00:00 +0530
    Asia/Calcutta 2017-07-30 10:00:00 IST



    请注意,发现了 3 个时区(加尔各答、科伦坡和加尔各答)。这是因为所有这 3 个时区在 +05:30 上的有效偏移量为 2017-07-30 。出于某种原因,科伦坡没有被格式化为 IST :我认为这取决于这个时区在 Python 中的配置方式,或者我的版本没有更新(我使用的是 Python 3.5.2 ) - 我已经在 J​​ava 中对此进行了测试,它的格式为 IST ,所以这可能是我的 Python 安装中的配置问题。

    一旦你拥有了所有候选时区,你就必须选择一个(我不确定什么是最好的标准)。

    PS:实际上只有2个选项,因为Asia/KolkataAsia/Calcutta是同义词。但是您仍然有不止一种选择,问题仍然存在:选择哪一种?

    关于python-3.x - python中人类可读的时区?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44798558/

    相关文章:

    java - 在 Elastic Search 中设置动态日期格式

    java - 卡萨布兰卡时区的偏移量错误(java)

    python - 使用请求的响应时间极长

    python-3.x - 如何根据列表中的单词创建新的 Pandas 列

    javascript - Angular2+ 如何显示日期的毫秒数?

    html - 接收带有时区偏移量的属性日期时间的错误值

    flutter - 如何在 Flutter 中获取当前时区(tz 数据库名称)

    javascript - 如何设置javascript全局时区

    python - 通过引用改变 python 传入对象

    python - 用测试集中的中位数填充 Nan 值