简体   繁体   中英

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.

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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