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-130718: Normalize edge cases indatetime.timestamp anddatetime.astimezone#130752

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
donBarbos wants to merge5 commits intopython:main
base:main
Choose a base branch
Loading
fromdonBarbos:issue-130718
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
6 changes: 5 additions & 1 deletionLib/_pydatetime.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -1989,7 +1989,11 @@ def _mktime(self):
t = (self - epoch) // timedelta(0, 1)
def local(u):
y, m, d, hh, mm, ss = _time.localtime(u)[:6]
return (datetime(y, m, d, hh, mm, ss) - epoch) // timedelta(0, 1)
try:
return ((datetime(y, m, d, hh, mm, ss) - epoch) //
timedelta(0, 1))
except (ValueError, OverflowError):
return u

# Our goal is to solve t = local(u) for u.
a = local(t) - t
Expand Down
12 changes: 12 additions & 0 deletionsLib/test/datetimetester.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -3122,6 +3122,18 @@ def dst(self, dt): return 1
with self.assertRaises(TypeError):
dt_broken.astimezone()

dt_big = self.theclass(9999, 12, 31, 23, 59, 59)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

I don't love that the original style violates the one-assert-per-test style, and we should probably not continue that.

Let's move this into its own method, liketest_astimezone_max andtest_astimezone_min. We can test the values neardatetime.min anddatetime.max separately, and useself.subTest to try a few different values for time zone.

I'm not sure if@support.run_with_tz can be parameterized, but if it can we should try it on a few different time zones.

dt_big_utc = dt_big.replace(hour=19,
tzinfo=timezone(timedelta(hours=-4), 'EDT'))
self.assertEqual(dt_big_utc,
dt_big.replace(tzinfo=timezone.utc).astimezone())

other_tz = timezone(timedelta(hours=+4), '+04')
dt_other_tz = dt_big.replace(tzinfo=other_tz)
dt_utc_tz = dt_big.replace(tzinfo=timezone.utc)
self.assertEqual(dt_other_tz, dt_other_tz.astimezone(tz=other_tz))
self.assertEqual(dt_utc_tz, dt_utc_tz.astimezone(tz=timezone.utc))

def test_subclass_datetime(self):

class C(self.theclass):
Expand Down
10 changes: 9 additions & 1 deletionModules/_datetimemodule.c
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -5415,6 +5415,7 @@
{
struct tm local_time;
time_t t;
long long old_u = u;
u -= epoch;
t = u;
if (t != u) {
Expand All@@ -5424,7 +5425,14 @@
}
if (_PyTime_localtime(t, &local_time) != 0)
return -1;
return utc_to_seconds(local_time.tm_year + 1900,

// Check edge cases
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

This should be more specific — what this code does is very opaque, so we should have a "why" comment here. Something like "When the year is outside the allowed year boundaries, return the original utc timestamp because ".

int year = local_time.tm_year + 1900;
if (year < MINYEAR || year > MAXYEAR) {
Py_DECREF(year);

Check warning on line 5432 in Modules/_datetimemodule.c

View workflow job for this annotation

GitHub Actions/ Windows / build (arm64)

'type cast': conversion from 'int' to 'PyObject *' of greater size [D:\a\cpython\cpython\PCbuild\pythoncore.vcxproj]

Check warning on line 5432 in Modules/_datetimemodule.c

View workflow job for this annotation

GitHub Actions/ Windows (free-threading) / build (arm64)

'type cast': conversion from 'int' to 'PyObject *' of greater size [D:\a\cpython\cpython\PCbuild\pythoncore.vcxproj]

Check warning on line 5432 in Modules/_datetimemodule.c

View workflow job for this annotation

GitHub Actions/ Windows / build and test (x64)

'type cast': conversion from 'int' to 'PyObject *' of greater size [D:\a\cpython\cpython\PCbuild\pythoncore.vcxproj]

Check warning on line 5432 in Modules/_datetimemodule.c

View workflow job for this annotation

GitHub Actions/ Windows (free-threading) / build and test (x64)

'type cast': conversion from 'int' to 'PyObject *' of greater size [D:\a\cpython\cpython\PCbuild\pythoncore.vcxproj]
return old_u;
}
return utc_to_seconds(year,
local_time.tm_mon + 1,
local_time.tm_mday,
local_time.tm_hour,
Expand Down
Loading

[8]ページ先頭

©2009-2025 Movatter.jp