tags:

views:

207

answers:

2

So I'm trying to parse strings similar to this:

"Sat, 11/01/09 8:00PM EST" in python, but I'm having trouble finding a solution that will handle the time zone abbreviation (I don't necessarily know what the timezone will be beforehand)

I'm trying to use the parse() function dateutil but it's not picking up on the abbreviated timezone. Is there an easy way to do this?

+3  A: 

That probably won't work because those abbreviations aren't unique. See this page for details. You might wind up just having to manually handle it yourself if you're working with a known set of inputs.

Hank Gay
Does it get easier if we limit it to timezones in the US? Are there a "standard" set of abbreviations in that event?
gct
Don't forget that "timezones in the US" includes AKST, AKDT, HAST, and HADT. If you just mean the continental 48 states, then you only have the 8 3-letter timezones to deal with (4 timezones, standard and daylight savings times).
Paul McGuire
Apparently for good measure some places uses HST and HDT as equivalents for HAST and HADT too =\
gct
By far the easiest route (though not often the most practical) is to adjust whatever program is providing the data so it sends it all in UTC, or failing that, using offsets from UTC, or failing that a full, valid timezone from the zoneinfo database.
Hank Gay
A: 

The parse() function in dateutil can't handle time zones. The thing I've been using is the %Z formatter and the time.strptime() function. I have no idea how it deals with the ambiguity in time zones, but it seems to tell the difference between CDT and CST, which is all I needed.

Background: I store backup images in directories whose names are timestamps using local time, since I don't have GMT clocks handy at home. So I use time.strptime(d, r"%Y-%m-%dT%H:%M:%S_%Z") to parse the directory names back into an actual time for age analysis.

Mike DeSimone