简体   繁体   English

Python:UTC到本地夏令时

[英]python: UTC to local daylight savings time

Now that it's daylight savings time in my timezone, I'm having a problem translating UTC to local time. 现在已经是我所在时区的夏令时了,将UTC转换为当地时间时遇到了问题。

>>> from dateutil.parser import parse
>>> from dateutil import tz
>>> dt = parse('1:30').replace(tzinfo=tz.tzutc())
>>> dt
datetime.datetime(2015, 3, 8, 1, 30, tzinfo=tzutc())
>>> dt = dt.astimezone(tz.tzlocal())
>>> dt
datetime.datetime(2015, 3, 7, 20, 30, tzinfo=tzlocal())

When it's 1:30 UTC it's 21:30 local time, but I'm getting 20:30, which would be correct if we hadn't changed to DST this morning. 如果是世界标准时间(UTC)1:30,则是当地时间21:30,但我现在是20:30,如果我们今天早上不更改为DST,那将是正确的。

I have the feeling I'm missing something obvious. 我觉得我缺少明显的东西。

I think I see the problem. 我想我看到了问题。 You began with the UTC time 1:30 on March 8. This corresponds to a time in Eastern Time before daylight savings took effect, since daylight savings takes effect at 2am local time on March 8. Try using a later UTC time for your test and it should work. 您在8这对应于美国东部时间时间夏令的情况,因为夏令使用后UTC时间生效,在凌晨2点当地时间3月8日尝试测试3月开始与UTC时间1:30它应该工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM