Time Zone application after strptime?

M.-A. Lemburg mal at egenix.com
Fri Mar 7 16:43:31 EST 2008


On 2008-03-07 22:24, Jim Carroll wrote:
> It's taken me a couple of hours to give up on strptime with %Z for recognizing
> time zones... but that still leaves me in the wrong zone:
> 
> def paypal_to_mysql_date(ppDate):
>     # a typical paypal date is 10:29:52 Feb 29, 2008 PST
>     date_parts = ppDate.split()
>     withouttz = " ".join(date_parts[:-1])
> 
>     # eventually we need to apply the timezone
>     timezone = date_parts[-1]
> 
>     dt = datetime.strptime(withouttz, "%H:%M:%S %b %d, %Y")
>     return dt.strftime("%Y-%m-%d %H:%M")
> 
> 
> How can I use the "PST" (or any other time zone name) to adjust dt by the
> correct number of hours to get it into UTC before storing in MySQL?

You could try mxDateTime's parser. It will convert most timezones
into UTC for you:

 >>> from mx.DateTime import *
 >>> DateTimeFrom('10:29:52 PST, Feb 29, 2008')
<mx.DateTime.DateTime object for '2008-02-29 18:29:52.00' at 2afdc7078f50>

However, note that I changed the position of the timezone in your
example.

Having the timezone at the end of the string and after the date
is a rather unusual format and not supported out-of-the-box by
mxDateTime (even though it does support a very wide range
of formats).

http://www.egenix.com/products/python/mxBase/mxDateTime/

-- 
Marc-Andre Lemburg
eGenix.com

Professional Python Services directly from the Source  (#1, Mar 07 2008)
 >>> Python/Zope Consulting and Support ...        http://www.egenix.com/
 >>> mxODBC.Zope.Database.Adapter ...             http://zope.egenix.com/
 >>> mxODBC, mxDateTime, mxTextTools ...        http://python.egenix.com/
________________________________________________________________________

:::: Try mxODBC.Zope.DA for Windows,Linux,Solaris,MacOSX for free ! ::::


    eGenix.com Software, Skills and Services GmbH  Pastor-Loeh-Str.48
     D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg
            Registered at Amtsgericht Duesseldorf: HRB 46611



More information about the Python-list mailing list