Skip to content

gh-81708: Support negative datetime.datetime.timestamp values from naive datetimes on Windows #134517

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions Lib/_pydatetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import time as _time
import math as _math
import sys
import os
from operator import index as _index

def _cmp(x, y):
Expand Down Expand Up @@ -1877,7 +1877,7 @@ def _fromtimestamp(cls, t, utc, tz):
# thus we can't perform fold detection for values of time less
# than the max time fold. See comments in _datetimemodule's
# version of this method for more details.
if t < max_fold_seconds and sys.platform.startswith("win"):
if t < max_fold_seconds and os.name == 'nt':
return result

y, m, d, hh, mm, ss = converter(t - max_fold_seconds)[:6]
Expand Down Expand Up @@ -2050,6 +2050,9 @@ def local(u):
def timestamp(self):
"Return POSIX timestamp as float"
if self._tzinfo is None:
if self < _NAIVE_EPOCH and os.name == 'nt':
# Windows converters throw an OSError for negative values.
return (self - _NAIVE_EPOCH).total_seconds()
s = self._mktime()
return s + self.microsecond / 1e6
else:
Expand Down Expand Up @@ -2561,6 +2564,7 @@ def _name_from_offset(delta):
timezone.min = timezone._create(-timedelta(hours=23, minutes=59))
timezone.max = timezone._create(timedelta(hours=23, minutes=59))
_EPOCH = datetime(1970, 1, 1, tzinfo=timezone.utc)
_NAIVE_EPOCH = datetime(1970, 1, 1)

# Some time zone algebra. For a datetime x, let
# x.n = x stripped of its timezone -- its naive time.
Expand Down
4 changes: 4 additions & 0 deletions Lib/test/datetimetester.py
Original file line number Diff line number Diff line change
Expand Up @@ -2671,6 +2671,10 @@ def test_timestamp_aware(self):
self.assertEqual(t.timestamp(),
18000 + 3600 + 2*60 + 3 + 4*1e-6)

def test_naive_timestamp_before_epoch(self):
# Before #81708 this raised OSError on Windows.
self.assertLess(self.theclass(1969,1,1).timestamp(), 0)

@support.run_with_tz('MSK-03') # Something east of Greenwich
def test_microsecond_rounding(self):
def utcfromtimestamp(*args, **kwargs):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Support negative :meth:`datetime.datetime.timestamp` values from naive
datetimes (before the UNIX epoch of 1970-01-01) on Windows by
subtracting a naive epoch. Patch by John Keith Hohm.
14 changes: 14 additions & 0 deletions Modules/_datetimemodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -6869,6 +6869,20 @@ datetime_timestamp(PyObject *op, PyObject *Py_UNUSED(dummy))
result = delta_total_seconds(delta, NULL);
Py_DECREF(delta);
}
#ifdef MS_WINDOWS
else if (GET_YEAR(self) < 1970) {
PyObject *naive_epoch = new_datetime(1970, 1, 1, 0, 0, 0, 0, Py_None, 0);
if (naive_epoch == NULL) {
return NULL;
}
PyObject *delta = datetime_subtract(op, naive_epoch);
Py_DECREF(naive_epoch);
if (delta == NULL)
return NULL;
result = delta_total_seconds(delta, NULL);
Py_DECREF(delta);
}
#endif
else {
long long seconds;
seconds = local_to_seconds(GET_YEAR(self),
Expand Down
Loading