Tags: aroundin, circles, convert, documentation, guessing, local, programming, python, stamp, string, time, utc

convert time string in UTC to time in local time

On Programmer » Python

5,134 words with 1 Comments; publish: Fri, 04 Jan 2008 12:48:00 GMT; (20093.75, « »)

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:

00Z'))

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

that correct?

Is there a direct way to convert that timestamp in UTC to a local

time stamp that will always work?

TIA,

Dave

All Comments

Leave a comment...

  • 1 Comments
    • davelist.python.questionfor.info.mac.com wrote:
      Quote:
      === Original Words ===

      Is there a direct way to convert that timestamp in UTC to a local

      time stamp that will always work?

      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.

      Paul

      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