Python datetime strptime () incompatible between machines

I'm at a dead end. I wrote date clearing functions in Python 2.7.5 on my Mac, but not 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 version 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 lowercase% z, but still get an error (albeit 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' 
+5
source share
2 answers

Abbreviations for time zones are mixed. For example, EST may mean Eastern Standard Time in the USA, or it may mean Eastern Daylight Saving in Australia.

Consequently, datetime strings containing time reductions cannot be reliably parsed in datetime objects oriented to the time zone.

strptime '%Z' format will correspond only to UTC, GMT or the abbreviated time zone listed in time.tzname , which depend on the machine language.

If you can change the date and time strings to units containing UTC offsets, then you can use dateutil to parse strings in datetime objects that take into account the time zone:

 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)) 
+4
source

%Z will only accept GMT, UTC and everything that is specified in time.tzname , since the time zone functionality is platform-specific, as indicated here :

Support for the% Z directive is based on the values โ€‹โ€‹contained in tzname and whether daylight is true. Because of this, for the platform, with the exception of the recognition of UTC and GMT, which are always (and are considered time zones that do not include daylight saving time).

So, try to find out what time zones are supported by your platform by doing the following:

 import time time.tzname 

I get the following:

 ('PST', 'PDT') 

Thus, the best option would be to convert your time in advance to one of the default allowed time zones.

+3
source

Source: https://habr.com/ru/post/1202275/


All Articles