Python's datetime strptime() inconsistent between machines -


i'm stumped. date-cleaning functions wrote work in python 2.7.5 on mac not in 2.7.6 on ubuntu server.

python 2.7.5 (default, mar  9 2014, 22:15:05)  [gcc 4.2.1 compatible apple llvm 5.0 (clang-500.0.68)] on darwin type "help", "copyright", "credits" or "license" more information. >>> datetime import datetime >>> date = datetime.strptime('2013-08-15 10:23:05 pdt', '%y-%m-%d %h:%m:%s %z') >>> print(date) 2013-08-15 10:23:05 

why not work in 2.7.6 on ubuntu?

python 2.7.6 (default, mar 22 2014, 22:59:56)  [gcc 4.8.2] on linux2 type "help", "copyright", "credits" or "license" more information. >>> datetime import datetime >>> date = datetime.strptime('2013-08-15 10:23:05 pdt', '%y-%m-%d %h:%m:%s %z') traceback (most recent call last):   file "<stdin>", line 1, in <module>   file "/usr/lib/python2.7/_strptime.py", line 325, in _strptime     (data_string, format)) valueerror: time data '2013-08-15 10:23:05 pdt' not match format '%y-%m-%d %h:%m:%s %z' 

edit: tried using timezone offset lowercase %z, still error (although different one):

>>> date = datetime.strptime('2013-08-15 10:23:05 -0700', '%y-%m-%d %h:%m:%s %z') traceback (most recent call last):   file "<stdin>", line 1, in <module>   file "/system/library/frameworks/python.framework/versions/2.7/lib/python2.7/_strptime.py", line 317, in _strptime     (bad_directive, format)) valueerror: 'z' bad directive in format '%y-%m-%d %h:%m:%s %z' 

timezone abbreviations ambiguous. for example, est can mean eastern standard time in us, or mean eastern summer time in australia.

therefore, datetime strings contain timezone abbreviations can not reliably parsed timezone-aware datetime objects.

strptime's '%z' format match utc, gmt or timezone abbreviation listed in time.tzname, machine-locale dependent.

if can change datetime strings ones containing utc offsets, use dateutil parse strings timezone-aware datetime objects:

import dateutil import dateutil.parser dp date = dp.parse('2013-08-15 10:23:05 -0700') print(repr(date)) # datetime.datetime(2013, 8, 15, 10, 23, 5, tzinfo=tzoffset(none, -25200)) 

Comments

Popular posts from this blog

javascript - how to protect a flash video from refresh? -

android - Associate same looper with different threads -

visual studio 2010 - Connect to informix database windows form application -