473,396 Members | 1,756 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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

Mar 8 '07 #1
1 14569
da******@mac.com 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.

Mar 11 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: OZ | last post by:
the serproxy claim itself a multi-thread proxy thing. I have sent email to write the original writer and there is no replay after 3 weeks. my configuration and setting are good. ...
6
by: Tim Cartwright | last post by:
I have a page that has the login control on it, nothing else. This page inherits from a master page, neither page has any code in it. This page works perfectly when running on the WebDev debug web...
5
by: ECVerify.com | last post by:
I am trying to convert a computers local time to Pacific time, I can get it back to UTC pretty easy but I actually want to convert whatever the local time is on the computer I want to calculate...
1
by: Mark Hollander | last post by:
Hi All, Could you please help me convert this code so that it will run in VB.NET Thank You Mark Hollander Private Type USER_INFO Name As String
2
by: yxq | last post by:
There are 8 bytes binary value stored date and time in Registry. 84 8B D7 DF 8B 28 C5 01 I want to convert the binary value to date using VB.NET. Dim a As FILETIME a.dwHighDateTime = 29698187...
29
by: Jan | last post by:
Hi: I have an Access database that's been running (in one form or another) for a couple of different clients for a few years. Now a new client has requested that it be implemented with a SQL...
6
by: vunet.us | last post by:
How can I convert UTC time such as 1173451235415 to a local time I can read? Thank you
2
by: ZR | last post by:
Hello, I need to convert a GMT time string to local time. I can fill out a "tm" structure with the GMT time string. Are there any standard C (or OS) time functions that will allow me to do this? ...
8
by: deepak_kamath_n | last post by:
Hello, I have the following scenario: 1. My application receives the date from another application as a string 2. The other application is running in a different time zone as compared to my...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.