Python中人类可读的时区? [英] Human readable timezone in python?

查看:216
本文介绍了Python中人类可读的时区?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从python中的这些时间格式中获取人类可读的时区?

How can I get human readable timezone from these timeformat in python?

如何将同一时间转换为该时区?

And how can I convert the same time to be in that timezone?

'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>)


推荐答案

您可以使用 iso8601 dateutil.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 1 0:00:00 IST

2017-07-30 10:00:00 IST






请注意,我使用的是亚洲/加尔各答,而不是 IST 。这是因为这些3个字母的名称模棱两可而不是标准 IST 可以是 印度标准时间 > 以色列标准时间 爱尔兰标准时间 (最后一个也称为爱尔兰夏令时,因为它是爱尔兰的夏令时使用的时区。


Note that I used Asia/Kolkata instead of IST. That's because these 3-letter names are ambiguous and not standard: IST can be India Standard Time, Israel Standard Time or Irish Standard Time (this last one is also called Irish Summer Time, as it's the timezone used during Ireland's Daylight Saving Time).

总是喜欢使用 IANA时区名称(始终以 Continent / City ,例如 America / Sao_Paulo Europe / Berlin )。如果您不需要亚洲/加尔各答,请检查此列表以找到最适合您的情况的列表。

Always prefer to use IANA timezones names (always in the format Continent/City, like America/Sao_Paulo or Europe/Berlin). If Asia/Kolkata is not what you need, check this list to find the one that suits best for your case.

如果您不知道哪个恐怕不是那么简单。

If you don't know which timezone to use and want to guess based on the offset, I'm afraid is not that simple.

以您的输入为例:偏移量为 +05:30 (比UTC提前5小时30分钟)。有多个时区使用此偏移量(或在其历史记录中使用过一次)。 Wikipedia链接包含3,但是我可以找到以下内容:

Using your input as example: the offset is +05:30 (5 hours and 30 minutes ahead of UTC). There's more than one timezone that uses this offset (or used once in its history). The wikipedia link contains 3, but I could find the following:


亚洲/达卡,亚洲/ Calcutta,亚洲/卡拉奇,亚洲/达卡,亚洲/廷布,亚洲/加德满都,亚洲/廷布,亚洲/加尔各答,亚洲/科伦坡,亚洲/加德满都

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

所有这些时区使用(或曾经使用过) +05:30 偏移量。由于时区在历史记录中可能会发生变化,因此在尝试根据偏移量猜测时区时会有一些问题:

All of these timezones uses (or had used in the past) the +05:30 offset. As timezones can change during history, you have some problems when trying to guess the timezone based on the offset:


  • 您需要知道什么指定日期和时间的当前偏移量。就您而言,您正在使用 2017-07-30 ,但我假设您想处理过去的任何日期。

  • 在指定的日期和时间可以有多个时区以及有效的偏移量(然后,您必须根据任意条件选择一个时区)。

  • 某些时区,具体取决于日期,可以采用夏令时,这意味着偏移量不会是 +05:30 (甚至更糟的是,有些可能会使用 +05:30 在DST中)。

  • you need to know what was the current offset at the specified date and time. In your case, you're using 2017-07-30, but I'm assuming you want to handle any dates in the past.
  • there can be more than one timezone with the valid offset at the specified date and time (then you'll have to choose one, based on any arbitrary criteria).
  • some timezones, depending on the date, can be in Daylight Saving Time, which means that the offset won't be +05:30 (or even worse, some might use +05:30 during DST).

因此,您不能只得到一个单个时区,基于偏移量。最好的办法是获取候选人的列表:包含所有时区的列表,其中时区在相应的日期/时间有效。然后,您必须选择其中一个(也许如果您有首选列表):

So, you can't get just one single timezone, based on the offset. The best you can do is to get a list of candidates: a list with all timezones in which the offset was valid at the respective date/time. Then you'll have to choose one of them (maybe if you have a list of "prefered ones"):

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))

输出将为:


亚洲/加尔各答2017-07-30 10:00:00 IST

亚洲/科伦坡2017-07-30 10:00:00 +0530

亚洲/加尔各答2017-07-30 10 :00:00 IST

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个时区(加尔各答,科伦坡和加尔各答)。这是因为这三个时区在 2017-07-30 上的有效偏移量为 +05:30 。由于某些原因,科伦坡未格式化为 IST :我认为这取决于在Python中配置此时区的方式,或者我的版本未更新(我使用的是 Python 3.5.2 )-我已经在Java中对其进行了测试,其格式为 IST ,因此在我的Python安装中可能是配置问题。

Note that 3 timezones were found (Kolkata, Colombo and Calcutta). That's because all these 3 timezones have a valid offset of +05:30 on 2017-07-30. For some reason, Colombo were not formatted as IST: I think it depends on how this timezone is configured in Python, or my version is not updated (I'm using Python 3.5.2) - I've tested this in Java and it's formatted as IST, so probably it's a configuration issue in my Python instalation.

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

Once you have all the candidates timezones, you'll have to choose one (and I'm not sure what would be the best criteria for that).

PS:实际上只有两个选择,因为亚洲/加尔各答亚洲/加尔各答是同义词。但是您仍然有多个选择,问题仍然存在:选择哪个?

PS: actually there are only 2 options, because Asia/Kolkata and Asia/Calcutta are synonyms. But you still have more than one option and the question remains: which one to choose?

这篇关于Python中人类可读的时区?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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