This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: datetime.max conversion
Type: behavior Stage:
Components: Library (Lib) Versions: Python 3.9
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: eric.smith, joris.geysens
Priority: normal Keywords:

Created on 2022-02-25 16:10 by joris.geysens, last changed 2022-04-11 14:59 by admin.

Messages (6)
msg414013 - (view) Author: Joris Geysens (joris.geysens) Date: 2022-02-25 16:10
Reading the documentation, I don't understand how this is not possible : 

# get the max utc timestamp
ts = datetime.max.replace(tzinfo=timezone.utc).timestamp()

# similarly 
ts2 = datetime(9999, 12, 31, 23, 59, 59, 999999, tzinfo=timezone.utc).timestamp()

# timestamp value 253402300800 seems correct
# converting back to timestamp is impossible, these all fail : 

dt = datetime.fromtimestamp(ts, tz=timezone.utc)
dt = datetime.utcfromtimestamp(ts)

It should be possible to get a datetime back from the initially converted timestamp, no?
msg414023 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2022-02-25 17:17
Please show us how they fail.
msg414032 - (view) Author: Joris Geysens (joris.geysens) Date: 2022-02-25 18:41
a ValueError is raised : 

ValueError: year 10000 is out of range 

on 

dt = datetime.fromtimestamp(ts, tz=timezone.utc)

or 

dt = datetime.utcfromtimestamp(ts)
msg414043 - (view) Author: Joris Geysens (joris.geysens) Date: 2022-02-25 21:38
I see this in the python source code being tested (datetimetester.py), so I guess it is a rounding problem : 

# maximum timestamp: set seconds to zero to avoid rounding issues
        max_dt = self.theclass.max.replace(tzinfo=timezone.utc,
                                           second=0, microsecond=0)
        max_ts = max_dt.timestamp()
        # date 9999-12-31 23:59:00+00:00: timestamp 253402300740
        self.assertEqual(self.theclass.fromtimestamp(max_ts, tz=timezone.utc),
                         max_dt)
msg414044 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2022-02-25 21:58
Probably so. You could step through the code to make sure that's what's going on.
msg414275 - (view) Author: Joris Geysens (joris.geysens) Date: 2022-03-01 15:47
I looked at this a bit more in detail.
What happens it the following, starting in the datetime fromtimestamp fragment :

converter = _time.gmtime if utc else _time.localtime
y, m, d, hh, mm, ss, weekday, jday, dst = converter(t)

That will call the system gmtime_r(), which indeed returns Sat Jan  1 00:00:00 10000

I have not looked at the cpp implementation for that method and I am not sure if this is something that can be improved.
History
Date User Action Args
2022-04-11 14:59:56adminsetgithub: 91012
2022-03-01 15:47:18joris.geysenssetmessages: + msg414275
2022-02-25 21:58:48eric.smithsetmessages: + msg414044
2022-02-25 21:38:57joris.geysenssetmessages: + msg414043
components: + Library (Lib)
2022-02-25 18:41:13joris.geysenssetmessages: + msg414032
2022-02-25 17:17:22eric.smithsetnosy: + eric.smith
messages: + msg414023
2022-02-25 16:10:01joris.geysenscreate