New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
datetime.max conversion #91012
Comments
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 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? |
Please show us how they fail. |
a ValueError is raised : ValueError: year 10000 is out of range on dt = datetime.fromtimestamp(ts, tz=timezone.utc) or dt = datetime.utcfromtimestamp(ts) |
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) |
Probably so. You could step through the code to make sure that's what's going on. |
I looked at this a bit more in detail. 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. |
The min range of the datetime is also affected:
|
I have noticed that the behavior of In reality, this does not matter, because timestamps that far away from the current date are essentially meaningless, but we should probably have well-defined limits so that people can reason well enough about their code. I propose that on all platforms that support it (e.g. not Windows):
|
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: