Tags: aroundin, circles, convert, documentation, guessing, local, programming, python, stamp, string, time, utc
convert time string in UTC to time in local time
I'm guessing there is an easy way to do this but I keep going around
in circles in the documentation.
I have a time stamp that looks like this (corresponding to UTC time):
start_time = '2007-03-13T15:00:00Z'
I want to convert it to my local time.
start_time = time.mktime(time.strptime(start_time, '%Y-%m-%dT%H:%M:
start_time -= time.timezone
This was working fine now, but if I do it for a date next week (such
as March 13th in the above example), it breaks because my local time
moves to daylight savings time this weekend. So my time is now off by
an hour. I'm guessing if I try this next week it will work okay
because time.timezone will be give a different value next week - is
Is there a direct way to convert that timestamp in UTC to a local
time stamp that will always work?
Leave a comment...
- 1 Comments
- davelist.python.todaysummary.com.mac.com wrote:Quote:
I think you have to test tm_isdst and then apply time.timezone (if
tm_isdst is 0) or time.altzone (if tm_isdst is 1) in the way you've
attempted. But as I said, you have to be careful with mktime, since
tests both with Python and directly against glibc suggest that it
considers the time to be local, and a subsequent call to localtime
won't actually give you a different hour, minute and second than that
provided in the supposedly GMT/UTC time.
P.S. There's more to come on this topic, since I'm trying to improve
time zone support on time tuples/structures. It's an arcane business,
however, and I'm still trying to make sense out of the behaviour of
various library functions.#1; Fri, 04 Jan 2008 12:49:00 GMT