0

See how Python3 shows the timezone as -07:53

~/dev> python3
Python 3.7.3 (default, Jun  2 2020, 19:48:59) 
[Clang 11.0.3 (clang-1103.0.32.62)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import datetime, pytz
>>> '%s'%datetime.datetime(2021, 9, 3, 12, 0, 0, 0, pytz.timezone('US/Pacific'))
'2021-09-03 12:00:00-07:53'
>>>

While in Python2 the timezone is -08:00

~/dev> python2
WARNING: Python 2.7 is not recommended. 
This version is included in macOS for compatibility with legacy software. 
Future versions of macOS will not include Python 2.7. 
Instead, it is recommended that you transition to using 'python3' from within Terminal.

Python 2.7.16 (default, Jun 18 2021, 03:23:53) 
[GCC Apple LLVM 12.0.5 (clang-1205.0.19.59.6) [+internal-os, ptrauth-isa=deploy on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import datetime, pytz
>>> '%s'%datetime.datetime(2021, 9, 3, 12, 0, 0, 0, pytz.timezone('US/Pacific'))
'2021-09-03 12:00:00-08:00'
>>> 

What's going on? Which is right? I know Pacific time can be -8 or -7 but I never heard of these 53 minutes.

ubershmekel
  • 10,560
  • 7
  • 65
  • 81
  • I think this is related to: https://stackoverflow.com/questions/18541051/datetime-and-timezone-conversion-with-pytz-mind-blowing-behaviour – Mark Sep 02 '21 at 22:23
  • I found the linked duplicate by putting `pytz timezone 7:53` into a search engine. As weird of a thing as that is to search for, that suggests to me that just about anything should have worked. Please keep in mind that [you are expected to do some research here](https://meta.stackoverflow.com/questions/261592/how-much-research-effort-is-expected-of-stack-overflow-users). With 6 gold badges and an account that's almost 12(!) years old now, you should know this. – Karl Knechtel Sep 02 '21 at 22:26
  • Thank you. I did search, but did not find these answers. I did not know the problem was a bug with pytz. I thought it was literally a real timezone. – ubershmekel Sep 02 '21 at 22:31
  • @ubershmekel: it is not a but of pytz. It is just that timezones are complex, and python structure is not enough to handle them well. So..use pytz functions or datetime python functions, but try not to mix them. – Giacomo Catenazzi Sep 03 '21 at 13:43
  • @GiacomoCatenazzi - Python had kind of a hard start with handling date/time and time zones correctly in the beginning. That brought up 3rd party libs like `pytz` (which has this "localize-trap"), and later `dateutil`, `arrow`, `pendulum` etc. With Python 3.9, time zone handling is part of the [standard lib](https://docs.python.org/3/library/zoneinfo.html). So basically, I'd say *don't use pytz anymore*. But you still have to mix *datetime* library and *zoneinfo* library if you don't want to stick to plain UTC offsets. – FObersteiner Sep 08 '21 at 19:44
  • I totally agree that it is a mess, but timezone is very difficult (in fact the official source of timezone added few years ago a new set of data, to correct some fundamental problems, but also without disrupting existing installations). So I'm not sure there is a time library with handle timezone correctly (in any language). But no. I recommend you to use pytz as much as possible to handle timezones. The good: you make explicit what it is naive (datetime) and localized (pytz). – Giacomo Catenazzi Sep 09 '21 at 07:17
  • PS: python (as far I know) have yet decided on how to handle timezones in stdlib: the problem: it is dynamic (various changes per year), but stdlib is mostly linked about python language version and few releases. Should we have a new python release at every timezone change? – Giacomo Catenazzi Sep 09 '21 at 07:19
  • @GiacomoCatenazzi did you have a look at [zoneinfo](https://docs.python.org/3/library/zoneinfo.html)? To me, it seems pytz is outdated. Regarding localization with pytz, I've seen too many posts here on SO where people end up with LMT because they get pytz's localization method wrong. Not a good concept in my opinion. – FObersteiner Sep 11 '21 at 13:29
  • It is very new (from Python 3.9). I still didn't have an idea: it takes some time to evaluate such things (too many exceptions). OTOH the authors knew all the problem of datetime and pytz,so possibly it could be the solution. – Giacomo Catenazzi Sep 12 '21 at 14:56

0 Answers0