python偏移量口诀_关于python:GMT时间戳的固定偏移量规范

我的数据以格林威治标准时间(GMT)的时间戳存储:

import pandas as pd

import pytz

# data is stored in UTC

timestamp_utc = pd.Timestamp('2018-1-18 23:00', tz='Etc/GMT')

print(timestamp_utc)

2018-01-18 23:00:00+00:00

现在,我想在中欧时间(CET)中查看我的数据,该时间与格林尼治标准时间+1小时固定偏移。

# Central European Time (CET)

cet_tz = pytz.timezone('Etc/GMT+1')

timestamp_cet = timestamp_utc.astimezone(cet_tz)

print(timestamp_cet)

2018-01-18 22:00:00-01:00

这对我来说非常令人困惑,我本来希望2018-01-19 00:00:00+01:00。

在文档中,以下是固定偏移量的说明:

Fixed offsets

The 'Etc/GMT*' time zones mentioned above provide fixed

offset specifications, but watch out for the counter-intuitive sign

convention.

谁能解释这是什么意思? 这是否真的意味着,如果我要Etc/GMT+1,我必须做Etc/GMT-1吗?

以来:

print(timestamp_utc.astimezone(pytz.timezone('Etc/GMT-1')))

预期结果:

2018-01-19 00:00:00+01:00

谁能解释这种违反直觉的行为的逻辑?

编辑

我以为我可以在中欧时间使用pytz.timezone('CET')。 但这映射到夏季DST期间(UTC+2:00)的CEST,因此不适合用作真正的CET时区。 此外,时区CET也被弃用。

因此,Etc/GMT-1是代表真实中欧时间(UTC+01:00)的规范方法。

Etc/GMT*区域旨在与POSIX兼容,以便在未安装完整时区数据库的系统上与TZ环境变量向后兼容。 POSIX时区规则的偏移符号与我们通常期望的ISO 8601标准相反。

tz数据库中的注释对此进行了解释:

Be consistent with POSIX TZ settings in the Zone names, even though this is the opposite of what many people expect. POSIX has positive signs west of Greenwich, but many people expect positive signs east of Greenwich. For example, TZ='Etc/GMT+4' uses the abbreviation"-04" and corresponds to 4 hours behind UT (i.e. west of Greenwich) even though many people would expect it to mean 4 hours ahead of UT (i.e. east of Greenwich).

维基百科上也对此进行了描述:

The special area of"Etc" is used for some administrative zones, particularly for"Etc/UTC" which represents Coordinated Universal Time. In order to conform with the POSIX style, those zone names beginning with"Etc/GMT" have their sign reversed from the standard ISO 8601 convention. In the"Etc" area, zones west of GMT have a positive sign and those east have a negative sign in their name (e.g"Etc/GMT-14" is 14 hours ahead of GMT.)

通常,对于陆地上的有人居住的位置,您应该更喜欢使用基于位置的区域标识符。 例如,在荷兰使用Europe/Amsterdam作为时间。 这具有在正确的时间点以及任何以前的历史转换之间在CET和CEST之间正确切换的优势。

使用Etc/GMT*区域进行储备,以应对边缘情况,例如海上船只的跟踪时间。

您可以在此处查看tzdb区域名称的完整列表。

感谢您的明确解释! 此链接:data.iana.org/time-zones/theory.html为我提供了更多时区理论以及POSIX合规性的信息

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值