python icon indicating copy to clipboard operation
python copied to clipboard

bullseye images has issue with mktime OverflowError

Open AhmadMS1988 opened this issue 2 years ago • 8 comments

Hi We are trying to run the following code snippet on bullseye after installing pytz

from datetime import datetime
import time
import pytz
tz=pytz.timezone('America/New_York')
nytime= datetime.utcnow().astimezone(tz)
time.mktime(nytime.timetuple())

But we get the following exception: OverflowError: mktime argument out of range

Knowing that it works normally in buster images. Is there any OS dependency that I need to install for bullseye? Thank you

AhmadMS1988 avatar Mar 16 '23 14:03 AhmadMS1988

Sorry for the delay! I can't seem to reproduce: :sweat_smile: (maybe it was fixed in the meantime?)

$ docker run -it --rm python:bullseye bash
Unable to find image 'python:bullseye' locally
bullseye: Pulling from library/python
d1da99c2f148: Already exists 
577ff23cfe55: Already exists 
c7b1e60e9d5a: Already exists 
beefab36cbfe: Already exists 
fa938ef3feea: Pull complete 
b42187d88e49: Pull complete 
c7bef4d98a8c: Pull complete 
f521a6ee79f8: Pull complete 
Digest: sha256:ea707c4da86081316413f76c2c8726886742ff6aedca71a9f81bfb4d55ecd396
Status: Downloaded newer image for python:bullseye
root@fef26b257d05:/# pip install pytz
Collecting pytz
  Obtaining dependency information for pytz from https://files.pythonhosted.org/packages/32/4d/aaf7eff5deb402fd9a24a1449a8119f00d74ae9c2efa79f8ef9994261fc2/pytz-2023.3.post1-py2.py3-none-any.whl.metadata
  Downloading pytz-2023.3.post1-py2.py3-none-any.whl.metadata (22 kB)
Downloading pytz-2023.3.post1-py2.py3-none-any.whl (502 kB)
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 502.5/502.5 kB 13.7 MB/s eta 0:00:00
Installing collected packages: pytz
Successfully installed pytz-2023.3.post1
WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv

[notice] A new release of pip is available: 23.2.1 -> 23.3.2
[notice] To update, run: pip install --upgrade pip
root@fef26b257d05:/# python
Python 3.12.1 (main, Dec  9 2023, 00:30:59) [GCC 10.2.1 20210110] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from datetime import datetime
>>> import time
>>> import pytz
>>> tz=pytz.timezone('America/New_York')
>>> nytime= datetime.utcnow().astimezone(tz)
<stdin>:1: DeprecationWarning: datetime.datetime.utcnow() is deprecated and scheduled for removal in a future version. Use timezone-aware objects to represent datetimes in UTC: datetime.datetime.now(datetime.UTC).
>>> time.mktime(nytime.timetuple())
1702924216.0

tianon avatar Dec 18 '23 23:12 tianon

Tried different arches and couldn't reproduce either.

LaurentGoderre avatar Jan 18 '24 21:01 LaurentGoderre

I was able to repro with Python 3.7.13 on bullseye (python:3.7.13-bullseye and python:3.7.13-slim-bullseye) when the datetime is in daylight savings.

Firstly, staying with the example of Eastern Time, when we are in EST, things work fine:

from datetime import datetime
# pytz==2023.3
import pytz
import time

tz = pytz.timezone("America/New_York")
nytime = datetime(2024, 12, 1).astimezone(tz).timetuple()
print(f"time.mktime(nytime) = {time.mktime(nytime)}")

time.mktime(nytime) = 1732993200.0

However, if using a value when daylight saving time is in effect (EDT), it blows up:

nytime = datetime(2024, 6, 1).astimezone(tz).timetuple()
print(f"time.mktime(nytime) = {time.mktime(nytime)}")
...
OverflowError: mktime argument out of range

vly-ginger avatar Jun 04 '24 00:06 vly-ginger

That snippet actually ends up being reducible to the following

from datetime import datetime
import time
time.mktime(time.struct_time(sequence=(2000, 11, 30, 0, 0, 0, 3, 335, 1)))

LaurentGoderre avatar Jun 04 '24 17:06 LaurentGoderre

~~This seems to be a bug with astimezone where it returns 1 for tm_isdst when it can only be 0 or -1~~

The docs says that value 1 for tm_isdst is valid but that is what is causing the issue in mktime

LaurentGoderre avatar Jun 04 '24 17:06 LaurentGoderre

Hacky way would be to do:

nytime = datetime(2024, 6, 1).astimezone(tz).timetuple()
fix = list(nytime)
fix[-1] =- 1
nytimefixed = tuple(fix)
print(f"time.mktime(nytimefixed) = {time.mktime(nytimefixed)}")

LaurentGoderre avatar Jun 04 '24 17:06 LaurentGoderre

It also seems to fail only on bullseye -- the newer bookworm images are also fine :thinking:

tianon avatar Jun 04 '24 21:06 tianon

Aha, even better, it also fails on Debian's Python in bullseye, so it's something about bullseye specifically that's causing this (not something we did):

$ docker run -it --rm --pull=always debian:bullseye-slim
bullseye-slim: Pulling from library/debian
728328ac3bde: Already exists 
Digest: sha256:0e75382930ceb533e2f438071307708e79dc86d9b8e433cc6dd1a96872f2651d
Status: Downloaded newer image for debian:bullseye-slim
root@15b362c1a62e:/# apt-get update -qq
root@15b362c1a62e:/# apt-get install -yqq python3
...
root@15b362c1a62e:/# python3
Python 3.9.2 (default, Feb 28 2021, 17:03:44) 
[GCC 10.2.1 20210110] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from datetime import datetime
>>> import time
>>> time.mktime(time.struct_time(sequence=(2000, 11, 30, 0, 0, 0, 3, 335, 1)))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
OverflowError: mktime argument out of range

tianon avatar Jun 04 '24 21:06 tianon