Pytz将时间转换为UTC而无需DST [英] Pytz convert time to UTC without DST

查看:139
本文介绍了Pytz将时间转换为UTC而无需DST的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在发布此信息之前,我已经做了很多研究,但似乎无法正确完成转换。我有一些带有时间戳的数据,有些应用了DST,而另一些则没有。我认为指定不带DST的正确方法是将 is_dst 参数用于pytz。所有3个选项的UTC偏移都相同,这是不正确的。偏移量应为+1000。进行此转换的最佳方法是什么,为什么is_dst参数没有任何区别?

I've done a quite a bit of research before posting this, but I can't seem to get the conversion right. I have some data which has timestamps, and some have DST applied, and others don't. I thought the correct way to specify that it's without DST is using the is_dst parameter for pytz. All 3 options give the same offset from UTC, which is incorrect. The offset should be +1000. What's the best way to do this conversion, and why does the is_dst parameter not make any difference?

pytz_eastern.localize(datetime(2018, 1, 18, 18, 50), is_dst=None).strftime('%Y-%m-%d %H:%M %z')
'2018-01-18 18:50 +1100'
pytz_eastern.localize(datetime(2018, 1, 18, 18, 50), is_dst=False).strftime('%Y-%m-%d %H:%M %z')
'2018-01-18 18:50 +1100'
pytz_eastern.localize(datetime(2018, 1, 18, 18, 50), is_dst=True).strftime('%Y-%m-%d %H:%M %z')
'2018-01-18 18:50 +1100'


推荐答案


is_dst 参数在大多数时间戳中都会被忽略。它仅在DST过渡模糊期间使用,以解决这种歧义。

The is_dst parameter is ignored for most timestamps. It is only used during DST transition ambiguous periods to resolve that ambiguity.

您正试图通过忽略过渡规则来转换日期时间。我认为pytz不会支持这一点。取而代之的是,您可以选择标准时间的日期并要求其偏移量,然后使用它。

You're trying to convert a datetime by ignoring the transition rules. I don't think pytz will support that. Instead you can pick a date in standard time and ask for its offset, then use that.

>>> from datetime import *
>>> import pytz
>>> pytz_eastern = pytz.timezone('Australia/Sydney')

utcoffset 方法提供特定日期时间的偏移量( dst 方法也将仅提供DST偏移量。

The utcoffset method gives the offset for a particular datetime (and the dst method will also give just the DST offset).

>>> pytz_eastern.dst(datetime(2018, 6, 1))
datetime.timedelta(0)
>>> pytz_eastern.utcoffset(datetime(2018, 6, 1))
datetime.timedelta(0, 36000)

>>> pytz_eastern.dst(datetime(2018, 1, 1))
datetime.timedelta(0, 3600)
>>> pytz_eastern.utcoffset(datetime(2018, 1, 1))
datetime.timedelta(0, 39600)

从标准时间的日期中获取utcoffset,并直接使用日期时间的 tzinfo kwarg进行设置,然后再将其提供给pytz进行转换。

Take the utcoffset from a date in standard time and set it directly with the tzinfo kwarg of datetime, and only then give it to pytz for conversion.

因此,这是一个未针对DST进行调整的时钟上显示的日期时间:

So here's a datetime that was shown on a clock which was not adjusted for DST:

>>> standard_offset = timezone(pytz_eastern.utcoffset(datetime(2018, 6, 1)))
>>> datetime(2018, 1, 18, 18, 50, tzinfo=standard_offset).strftime('%Y-%m-%d %H:%M %z')
'2018-01-18 18:50 +1000'

这是同一日期时间重新变为现实:

And here's that same datetime brought back into reality:

>>> datetime(2018, 1, 18, 18, 50, tzinfo=standard_offset).astimezone(pytz_eastern).strftime('%Y-%m-%d %H:%M %z')
'2018-01-18 19:50 +1100'

(标准偏移量似乎也可以作为 ._ utcoffset ,但是没有记录,因此这是要求特定日期的utcoffset的原因,因为过去的偏移量不太可能更改。)

(The standard offset also seems to be available as ._utcoffset, but that's not documented, so that's a reason to ask for the utcoffset of a specific date, as it's less likely for offsets in the past to ever change.)

实际上,由于pytz为您提供了计算的偏移量和当前DST值,因此您可以将两者相减以获得忽略DST的标准偏移量。

In fact, since pytz gives you the computed offset and the current DST value, you could subtract the two to get the standard offset ignoring DST.

def add_forgotten_dst(dt, zoneinfo):
    '''Like pytz.localize, but ignores dst.'''
    utcoffset = zoneinfo.utcoffset(dt)
    dst = zoneinfo.dst(dt)
    standard_offset = utcoffset - dst
    dt = dt.replace(tzinfo=timezone(standard_offset))
    return dt.astimezone(zoneinfo)

>>> naive_time = datetime(2018, 1, 18, 18, 50)
>>> print(pytz_eastern.localize(naive_time))
2018-01-18 18:50:00+11:00
>>> print(add_forgotten_dst(naive_time, pytz_eastern))
2018-01-18 19:50:00+11:00

这篇关于Pytz将时间转换为UTC而无需DST的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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