Is this the correct way to convert a UTC datetime string into localtime?
Posted
by Steve
on Stack Overflow
See other posts from Stack Overflow
or by Steve
Published on 2010-03-25T03:15:01Z
Indexed on
2010/03/25
3:23 UTC
Read the original article
Hit count: 386
Is this the correct way to convert a UTC string into local time allowing for daylight savings? It looks ok to me but you never know :)
import time
UTC_STRING = "2010-03-25 02:00:00"
stamp = time.mktime(time.strptime(UTC_STRING,"%Y-%m-%d %H:%M:%S"))
stamp -= time.timezone
now = time.localtime()
if now[8] == 1:
stamp += 60*60
elif now[8] == -1:
stamp -= 60*60
print 'UTC: ', time.gmtime(stamp)
print 'Local: ', time.localtime(stamp)
--- Results from New Zealand (GMT+12 dst=1) ---
UTC: (2010, 3, 25, 2, 0, 0, 3, 84, 0)
Local: (2010, 3, 25, 15, 0, 0, 3, 84, 1)
© Stack Overflow or respective owner