Time Zone application after strptime?

attn.steven.kuo at gmail.com attn.steven.kuo at gmail.com
Tue Mar 11 22:30:21 EDT 2008


On Mar 11, 11:00 am, Jim Carroll <j... at maplesong.com> wrote:

(snipped)
>
> p.parse("10:29:52 Feb 29, 2008 PST", tzinfos=zones)
> datetime.datetime(2008, 2, 29, 10, 29, 52, tzinfo=tzoffset('PST', -28800))
>
> But I cannot figure out how to get dateutil to use the
> zoneinfo file that it includes in its own package.  It has a
> zoneinfo-2007k.tar.gz right in the package, and a class
> to parse the binary zoneinfo, but no clear way to get it to
> parse its own file and use those during the parsing.


Try the pytz module.

import datetime
import pytz
import time

utc = pytz.utc
zones = { 'PST': pytz.timezone('US/Pacific'), }

aware_dt = datetime.datetime(tzinfo=zones['PST'],
    *time.strptime("10:29:52 Feb 29, 2008 PST",
    "%H:%M:%S %b %d, %Y %Z")[0:6])
print aware_dt.strftime("%H:%M:%S %b %d, %Y %Z")
print utc.normalize(aware_dt.astimezone(utc)).strftime("%H:%M:%S %b
%d, %Y %Z")

# 10:29:52 Feb 29, 2008 PST
# 18:29:52 Feb 29, 2008 UTC

--
Hope this helps,
Steven



More information about the Python-list mailing list