oracle / graalpython

A high-performance embeddable Python 3 runtime for Java
https://www.graalvm.org/python/
Other
1.22k stars 104 forks source link

Don't ignore days in timedelta when converting to TimeZone #360

Closed JaroslavTulach closed 11 months ago

JaroslavTulach commented 12 months ago

Trying to create a date in US/New_York timezone and convert it into Java fails. This was reported to Enso by our US based developer: https://github.com/enso-org/enso/issues/7655 - the final exception is:

Execution finished with an error: java.time.DateTimeException: Zone offset not in valid range: -18:00 to +18:00
        at <java> java.base/java.time.ZoneOffset.ofTotalSeconds(ZoneOffset.java:417)
        at <java> com.oracle.graal.python.builtins.objects.PythonAbstractObject.createZoneId(PythonAbstractObject.java:1001)
        at <java> com.oracle.graal.python.builtins.objects.PythonAbstractObject.asTimeZone(PythonAbstractObject.java:949)

I have investigated the issue and I am providing a test to simulate the problem as well as a possible fix. the issue is that running:

from datetime import timedelta, datetime, timezone

zone = timezone(timedelta(seconds=-18000), "US/Eastern")
dt = datetime(1970, 1, 1, 0, 0, 1, tzinfo=zone)

print(dt)
print(dt.tzinfo)
print(dt.tzinfo.utcoffset(dt).seconds)

dt.tzinfo

yields

1970-01-01 00:00:01-05:00
US/Eastern
68400

e.g. the value of seconds field is bigger than allowed -18:00 and +18:00. My fix also reads value of days which is (in this case) -1 and compensates the value to fit into the desired range.