Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python's datetime strptime() inconsistent between machines

I'm stumped. The date-cleaning functions I wrote work in Python 2.7.5 on my Mac but not in 2.7.6 on my 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" for more information.
>>> from 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 does this 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" for more information.
>>> from 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' does not match format '%Y-%m-%d %H:%M:%S %Z'

Edit: I tried using the timezone offset with the lowercase %z, but still get an error (although a 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' is a bad directive in format '%Y-%m-%d %H:%M:%S %z'
like image 291
Nicole White Avatar asked Sep 10 '14 20:09

Nicole White


People also ask

What does datetime Strptime do in Python?

The strptime() function in Python is used to format and return a string representation of date and time. It takes in the date, time, or both as an input, and parses it according to the directives given to it.

Which two parameters does the datetime Strptime () function requires?

The strptime() class method takes two arguments: string (that be converted to datetime) format code.

What is Strptime ()?

The strptime() function is the converse of strftime(3); it converts the character string pointed to by s to values which are stored in the "broken-down time" structure pointed to by tm, using the format specified by format. The broken-down time structure tm is defined in <time.

What is the difference between Strftime and Strptime in Python?

strptime is short for "parse time" where strftime is for "formatting time". That is, strptime is the opposite of strftime though they use, conveniently, the same formatting specification.


2 Answers

Timezone abbreviations are ambiguous. For example, EST can mean Eastern Standard Time in the US, or it could mean Eastern Summer Time in Australia.

Therefore, datetime strings which contain timezone abbreviations can not be reliably parsed into timezone-aware datetime objects.

strptime's '%Z' format will only match UTC, GMT or the timezone abbreviation listed in time.tzname, which is machine-locale dependent.

If you can change the datetime strings to ones containing UTC offsets, then you could use dateutil to parse the strings into timezone-aware datetime objects:

import dateutil
import dateutil.parser as 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))
like image 200
unutbu Avatar answered Oct 06 '22 17:10

unutbu


%Z will only accept GMT, UTC, and whatever is listed in time.tzname, since the time zone functionality is platform specific, as is given here:

Support for the %Z directive is based on the values contained in tzname and whether daylight is true. Because of this, it is platform-specific except for recognizing UTC and GMT which are always known (and are considered to be non-daylight savings timezones).

So try to figure out what time zones are supported by your platform by running the following:

import time
time.tzname

I get the following:

('PST', 'PDT')

Thus, your best bet might be to convert your time beforehand to one of the default allowed time zones.

like image 3
Pacific Stickler Avatar answered Oct 06 '22 17:10

Pacific Stickler