473,385 Members | 1,356 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,385 software developers and data experts.

strptime and timezones

Hello!

Possibly i'm missing something really obvious here. But ...

If i have a date-time string of the kind specified in RFC 1123, like this:

Tue, 12 Aug 2008 20:48:59 -0700

Can i turn that into a seconds-since-the-epoch time using the standard
time module without jumping through substantial hoops?

Apart from the timezone, this can be parsed using time.strptime with the
format:

%a, %d %b %Y %H:%M:%S

You can stick a %Z on the end for the timezone, but that parses timezone
names ('BST', 'EDT'), not numeric specifiers. Also, it doesn't actually
parse anything, it just requires that the timezone that's in the string
matches your local timezone.

Okay, no problem, so you use a regexp to split off the timezone specifier,
parse that yourself, then parse the raw time with strptime.

Now you just need to adjust the parsed time for the timezone. Now, from
strptime, you get a struct_time, and that doesn't have room for a timezone
(although it does have room for a daylight saving time flag), so you can't
add the timezone in before you convert to seconds-since-the-epoch.

Okay, so convert the struct_time to seconds-since-the-epoch as if it were
UTC, then apply the timezone correction. Converting a struct_time to
seconds-since-the-epoch is done with mktime, right? Wrong! That does the
conversion *in your local timezone*. There's no way to tell it to use any
specific timezone, not even just UTC.

So how do you do this?

Can we convert from struct_time to seconds-since-the-epoch by hand? Well,
the hours, minutes and seconds are pretty easy, but dealing with the date
means doing some hairy calculations with leap years, which are doable but
way more effort than i thought i'd be expending on parsing the date format
found in every single email in the world.

Can we pretend the struct_time is a local time, convert it to
seconds-since-the-epoch, then adjust it by whatever our current timezone
is to get true seconds-since-the-epoch, *then* apply the parsed timezone?
I think so:

def mktime_utc(tm):
"Return what mktime would return if we were in the UTC timezone"
return time.mktime(tm) - time.timezone

Then:

def mktime_zoned(tm, tz):
"Return what mktime would return if we were in the timezone given by tz"
return mktime_utc(tm) - tz

The only problem there is that mktime_utc doesn't deal with DST: if tm is
a date for which DST would be in effect for the local timezone, then we
need to subtract time.altzone, not time.timezone. strptime doesn't fill in
the dst flag, as far as i can see, so we have to round-trip via
mktime/localtim:

def isDST(tm):
tm2 = time.localtime(time.mktime(tm))
assert (tm2.isdst != -1)
return bool(tm2.isdst)

def timezone(tm):
if (isDST(tm)):
return time.altzone
else:
return time.timezone

mktime_utc then becomes:

def mktime_utc(tm):
return time.mktime(tm) - timezone(tm)

And you can of course inline that and eliminate a redundant call to
mktime:

def mktime_utc(tm):
t = time.mktime(tm)
isdst = time.localtime(t).isdst
assert (isdst != -1)
if (isdst):
tz = time.altzone
else:
tz = time.timezone
return t - tz

So, firstly, does that work? Answer: i've tested it a it, and yes.

Secondly, do you really have to do this just to parse a date with a
timezone? If so, that's ridiculous.

tom

--
102 FX 6 (goblins)
Aug 13 '08 #1
3 2830
Tom Anderson wrote:
Secondly, do you really have to do this just to parse a date with a
timezone? If so, that's ridiculous.
No, you don't. :) Download the pytz package from the Python package
index. It's *the* tool for timezone handling in Python. The time zone
definition are not part of the Python standard library because they
change every few of months. Stupid politicians ...

Christian
Aug 13 '08 #2
Tom Anderson wrote:
Secondly, do you really have to do this just to parse a date with a
timezone? If so, that's ridiculous.
No, you don't. :) Download the pytz package from the Python package
index. It's *the* tool for timezone handling in Python. The time zone
definition are not part of the Python standard library because they
change every few of months. Stupid politicians ...

Christian

Aug 13 '08 #3
On Wed, 13 Aug 2008, Christian Heimes wrote:
Tom Anderson wrote:
>Secondly, do you really have to do this just to parse a date with a
timezone? If so, that's ridiculous.

No, you don't. :) Download the pytz package from the Python package
index. It's *the* tool for timezone handling in Python. The time zone
definition are not part of the Python standard library because they
change every few of months. Stupid politicians ...
My problem has absolutely nothing to do with timezone definitions. In
fact, it involves less timezone knowledge than the time package supplies!
The wonderful thing about RFC 1123 timestamps is that they give the
numeric value of their timezone, so you don't have to decode a symbolic
one or anything like that. Knowing about timezones thus isn't necessary.

The problem is simply that the standard time package doesn't think that
way, and always assumes that a time is in your local timezone.

That said, it does look like pytz might be able to parse RFC 1123 dates.
Ill check it out.

tom

--
Come on thunder; come on thunder.
Aug 14 '08 #4

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

Similar topics

4
by: Henk-Jan de Jong | last post by:
Hello all, I'm completely new in Python and have the next problem. I have a huge file with dates a want to convert from one format to another. e.g 31-12-2003 should become 31122003. I'm using...
4
by: Afanasiy | last post by:
Can I easily convert from ISO to Gregorian calendars in Python 2.3? For example, what Gregorian date is year 2004, week 1, day 1? If possible, I'd greatly prefer to do this with the standard...
1
by: Michele Simionato | last post by:
I have strings representing UTC dates and I want to convert them in time tuples using time.strptime. I have an issue with daylight savings. For instance Tue Sep 14 06:06:15 2004 is converted...
2
by: Robert | last post by:
A certain DAV server reports a time stamp as "Tues, 30 Aug 2005 20:48:31" ( but not "Tue, ..." as usual) This produces the error below. >>> time.strptime("Tue, 30 Aug 2005 20:48:31","%a, %d...
13
by: Michael | last post by:
I would like to set the timezone of a thread to allow me to calculate the UTC time for data sourced from a number of time zones. Although this can be done in C and C++, I annot find how to do...
4
by: kongkolvyu | last post by:
Hi The strptime function works just fine on Solaris. Here is an example on how I use it: struct tm tmpTm; if(strptime("20010101010101","%Y%m%d%H%M%S",&tmpTm)==NULL) printf("Error,String convert...
4
by: Maximilian Michel | last post by:
Hi all, I have an interesting problem: I have written code, that reads a logfile and parses it for date string (Thu Jun 29 14:01:23 2006). Standalone everthing works fine, all is recognized. ...
27
by: Sanjay | last post by:
Hi All, I am using pytz.common_timezones to populate the timezone combo box of some user registration form. But as it has so many timezones (around 400), it is a bit confusing to the users. Is...
3
by: W. eWatson | last post by:
Apparently, use of strptime of datetime needs a workaround in Python 2.4 to work properly. The workaround is d = datetime.datetime(*(time.strptime(date_string, format))). However, when I try to...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...

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.