Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

gh-81708: Support negativedatetime.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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
jhohm wants to merge2 commits intopython:main
base:main
Choose a base branch
Loading
fromjhohm:gh-81708
Open
Show file tree
Hide file tree
Changes fromall 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 deletionsLib/_pydatetime.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -8,7 +8,7 @@

import time as _time
import math as _math
importsys
importos
from operator import index as _index

def _cmp(x, y):
Expand DownExpand 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 andsys.platform.startswith("win"):
if t < max_fold_seconds andos.name == 'nt':
return result

y, m, d, hh, mm, ss = converter(t - max_fold_seconds)[:6]
Expand DownExpand 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 DownExpand 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 deletionsLib/test/datetimetester.py
View file
Open in desktop
Original file line numberDiff line numberDiff 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
View file
Open in desktop
Original file line numberDiff line numberDiff 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 deletionsModules/_datetimemodule.c
View file
Open in desktop
Original file line numberDiff line numberDiff 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

[8]ページ先頭

©2009-2025 Movatter.jp