Re: convert time string in UTC to time in local time



davelist@xxxxxxx wrote:
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')
)

Here, strptime in this case will probably produce a time whose
daylight saving time (DST) status will be undecided, although I've
done some tests with glibc which suggests that the native (unused by
Python) strptime produces non-DST-affected times for both the given
date and for a date one month later in my time zone (contrary to what
one might expect). Then, mktime is called, but this may apply various
time zone magic to treat the supplied time as a local time, not a GMT/
UTC time - this seems to be an artifact of the way mktime is defined
and implemented in various standards.

Do this to see what I'm talking about:

time.mktime(time.gmtime(0))

Unless you have GMT/UTC as your time zone, the above may well not
produce zero as its result. Similar tests in C behave identically.

start_time -= time.timezone

So, here you've applied a time shift in order to hopefully go from GMT/
UTC to local time. Given my observations of mktime, I'm inclined to
believe that you've time shifted beyond local time, but the principle
seems reasonable. See below for more discussion.

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?

The value of time.timezone should remain the same. In your time tuple/
structure (produced by strptime) there's a field called tm_isdst (it's
the value in the last position) which governs when DST applies. If DST
applies you need to use time.altzone instead of time.timezone to guide
conversions between local time and GMT/UTC.

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.

.



Relevant Pages

  • Re: Network Time Protocol (NTP) client support question
    ... compensating adjustment to the time zone offset. ... The whole point of using UTC time stamps is to avoid issues ... fudging the local time offset fails to satisfy it. ...
    (bit.listserv.ibm-main)
  • Re: Convert UTC to Local Time?
    ... adjusted for DST and time zone? ... time_t has no timezone concept. ... 01/01/1970 00:00 UTC ... mktime() always uses local timezone. ...
    (comp.unix.programmer)
  • Re: Rotor Bug: TimeZone ToLocalTime
    ... Or are you just basing this on a code examination? ... all of the information from the time zone about how to offset the times. ... into a local time fails, but not converting a utc date into a local date. ...
    (microsoft.public.dotnet.framework.clr)
  • Re: How to compare DateTime Objects?
    ... If the server records in GMT ... It does not matter what time zone the ... server is actually in as long as it records in UTC. ... display based on their local time. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Problems with the Timezone
    ... it UTC - 4 hours) then you must set the TZ environment variable to GMT4 ... the abbreviation of the time zone name since it implies that the displayed ... associated to what you call your local time zone. ...
    (comp.protocols.time.ntp)