Use the Neri–Schneider algorithm for ordinal-to-date conversion in datetime
メンテナーはふだん 1 日以内に返信
まだ誰も着手していません。
評価
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 初心者へのやさしさ
- 25/100
調査の方向性
Read ord_to_ymd() in Modules/_datetimemodule.c and _ord2ymd() in Lib/_pydatetime.py, then compare their current logic with the proposed Figure 12 algorithm. Check the datetime and calendar test suites and the month-boundary tests described in the issue. Done means both implementations preserve ordinal conversion results and error behavior while passing the relevant tests; the linked PR indicates work is already underway.
索引モデルが issue の本文から書いたものです。
説明
Proposal
ord_to_ymd() in Modules/_datetimemodule.c and _ord2ymd() in Lib/_pydatetime.py
convert a proleptic Gregorian ordinal to (year, month, day). They still use the
algorithm from 2002: four divisions by the 400/100/4/1-year cycle lengths, a
special case for December 31 at the end of a 4- or 400-year cycle, and a month
estimate that is "either exact or one too large" and is corrected afterwards
(for about 24% of dates).
Neri and Schneider, "Euclidean affine functions and their application to
calendar algorithms" (Softw Pract Exper. 2023;53(4):937–970,
https://arxiv.org/abs/2102.06959) give an algorithm with no lookup tables and
no estimate-and-correct step. GCC's libstdc++, the Linux kernel, .NET and Go
already use it. I propose replacing both functions with their Figure 12.
The conversion is used by date.fromordinal(), date.fromisocalendar(),
fromisoformat() with ISO week dates, strptime() with %j or week
directives, and date/datetime ± timedelta when the result leaves the
current month by more than one day.
Verification
- Same results as
mainfor all 3,652,059 ordinals (0001-01-01 … 9999-12-31):
SHA-256 over everydate.fromordinal()result, plusfromisocalendar()and
date + timedelta, for both the C and the pure-Python implementations. - C: same results for every positive
intordinal (1 … 2**31-1), including
the error messages past 9999-12-31; checked with gcc 13 and clang. - Pure Python: same results for 2.3 million other integers, including negative
and huge ones. - test_datetime, test_calendar, test_strptime, test_time, test_zoneinfo,
test_plistlib and test_xmlrpc pass. The PR adds a test for the first and
last days of every month in years at the ends of the 4-, 100- and 400-year
cycles.
Benchmarks
pyperf, x86-64 VM, gcc 13.3, -O3, no PGO; µs per 1000 operations:
| Benchmark | main | patched | Speedup |
|---|---|---|---|
date.fromordinal, random dates |
35.6 | 25.3 | 1.40x |
date + timedelta(days=40) |
42.9 | 35.7 | 1.20x |
date.fromisocalendar |
83.5 | 71.0 | 1.18x |
datetime + timedelta(days=40) |
37.9 | 34.1 | 1.11x |
date.fromordinal, consecutive dates |
26.3 | 23.8 | 1.10x |
date + timedelta(days=1), 1000 steps |
32.8 | 31.8 | 1.03x |
The C function itself takes 3-4 ns per call instead of 15-16 ns on random
dates (gcc 13 and clang, -O3). The pure-Python date.fromordinal() is about
1.3x faster.
With a PGO build (--enable-optimizations, gcc 13.3) of main (e3262d9) and of
the patch, 1,000,000 operations, best of 7, builds alternated:
| Operation | main | patched | Speedup |
|---|---|---|---|
date.fromordinal, random dates |
62.6 ms | 45.9 ms | 1.36x |
date + timedelta(days=90) |
69.0 ms | 60.8 ms | 1.13x |
date.fromisoformat('2024-W07-1') |
88.6 ms | 77.8 ms | 1.14x |
date + timedelta(days=10) |
53.0 ms | 52.5 ms | 1.01x |
pyperformance 1.14.0 (--fast, same two PGO builds, 119 benchmarks in common):
geometric mean 1.00x. No pyperformance benchmark exercises this code path, so
no change is expected there; the differences per benchmark are noise.
Other platforms and checks
- 32-bit x86 (
gcc -m32): full CPython build; the seven test suites above
pass, the all-dates digest is identical, and the C function matches the old
one for every positiveint. - AArch64 (clang cross build, run under QEMU): the C function matches the old
one for every positiveint. - UBSan + ASan (gcc): no reports over every positive
int. - No warnings with
-Wall -Wextra -Wpedantic -Wconversion -Wsign-conversion
(gcc, 64- and 32-bit) or clang-Weverythingon the new function. - Full test suite (
./python -m test -j2): 52,706 tests; the only failures
are test_socket and test_urllib, which fail the same way on unpatched main
in my sandbox (no network). - Not tested locally: Windows/MSVC and macOS (left to CI).
I used an AI assistant
(Claude) to research the algorithm and prepare the patch; I reviewed the change
and its verification myself and can explain every line.
Linked PRs
- gh-161747
- 主要言語
- Python
- スター
- 78.2k
- フォーク
- 36.6k
- 平均マージ
- 19時間 57分
- マージ済み PR(30日)
- 630
環境構築
このプロジェクトの開発コンテナを、あなたの GitHub アカウントでブラウザ上に起動します。
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートあり
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
python/cpython のほかの issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
メンテナーはふだん 1 日以内に返信
-
stdlib type-bug
難易度 2/5 1〜3時間 初心者へのやさしさ 75/100
メンテナーはふだん 1 日以内に返信
-
extension-modules stdlib topic-XML type-bug
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
python/cpython#161807 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
stdlib type-feature
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
python/cpython#161788 · コメント 7 件 ·
メンテナーはふだん 1 日以内に返信
-
extension-modules topic-IO type-bug
難易度 2/5 1〜3時間 初心者へのやさしさ 75/100
メンテナーはふだん 1 日以内に返信
似ている issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 62/100
cpp-best-practices/project_validator#267 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
Project-N-E-K-O/N.E.K.O#3725 ·
メンテナーはふだん 1 日以内に返信
-
[Bug] Sending a Telegram message chain twice duplicates mentions対応中かも @123XieC が今日担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
AstrBotDevs/AstrBot#11125 ·
メンテナーはふだん 1 日以内に返信
-
backend bug priority: high
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
nooriegarg/ContribIQ#73 ·
メンテナーはふだん 1 日以内に返信
-
bug p3 security
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
メンテナーはふだん 1 日以内に返信