@@ -471,88 +471,35 @@ days_before_month(int year, int month)
471471#define DI100Y 36524 /* _PyTime_DaysBeforeYear(101); days in 100 years */
472472#define DI400Y 146097 /* _PyTime_DaysBeforeYear(401); days in 400 years */
473473
474- /* ordinal -> year, month, day, considering 01-Jan-0001 as day 1. */
474+ /* ordinal -> year, month, day, considering 01-Jan-0001 as day 1.
475+ *
476+ * Neri and Schneider's algorithm, https://doi.org/10.1002/spe.3172; see
477+ * _ord2ymd() in Lib/_pydatetime.py for an explanation of the steps.
478+ *
479+ * The intermediate values only fit in 32 bits for small ordinals;
480+ * iso_to_ymd() passes the largest, MAXORDINAL + 2 (10000-01-02).
481+ */
475482static void
476483ord_to_ymd (int ordinal , int * year , int * month , int * day )
477484{
478- int n , n1 , n4 , n100 , n400 , leapyear , preceding ;
479-
480- /* ordinal is a 1-based index, starting at 1-Jan-1. The pattern of
481- * leap years repeats exactly every 400 years. The basic strategy is
482- * to find the closest 400-year boundary at or before ordinal, then
483- * work with the offset from that boundary to ordinal. Life is much
484- * clearer if we subtract 1 from ordinal first -- then the values
485- * of ordinal at 400-year boundaries are exactly those divisible
486- * by DI400Y:
487- *
488- * D M Y n n-1
489- * -- --- ---- ---------- ----------------
490- * 31 Dec -400 -DI400Y -DI400Y -1
491- * 1 Jan -399 -DI400Y +1 -DI400Y 400-year boundary
492- * ...
493- * 30 Dec 000 -1 -2
494- * 31 Dec 000 0 -1
495- * 1 Jan 001 1 0 400-year boundary
496- * 2 Jan 001 2 1
497- * 3 Jan 001 3 2
498- * ...
499- * 31 Dec 400 DI400Y DI400Y -1
500- * 1 Jan 401 DI400Y +1 DI400Y 400-year boundary
501- */
502- assert (ordinal >= 1 );
503- -- ordinal ;
504- n400 = ordinal / DI400Y ;
505- n = ordinal % DI400Y ;
506- * year = n400 * 400 + 1 ;
507-
508- /* Now n is the (non-negative) offset, in days, from January 1 of
509- * year, to the desired date. Now compute how many 100-year cycles
510- * precede n.
511- * Note that it's possible for n100 to equal 4! In that case 4 full
512- * 100-year cycles precede the desired day, which implies the
513- * desired day is December 31 at the end of a 400-year cycle.
514- */
515- n100 = n / DI100Y ;
516- n = n % DI100Y ;
517-
518- /* Now compute how many 4-year cycles precede it. */
519- n4 = n / DI4Y ;
520- n = n % DI4Y ;
521-
522- /* And now how many single years. Again n1 can be 4, and again
523- * meaning that the desired day is December 31 at the end of the
524- * 4-year cycle.
525- */
526- n1 = n / 365 ;
527- n = n % 365 ;
528-
529- * year += n100 * 100 + n4 * 4 + n1 ;
530- if (n1 == 4 || n100 == 4 ) {
531- assert (n == 0 );
532- * year -= 1 ;
533- * month = 12 ;
534- * day = 31 ;
535- return ;
485+ assert (1 <= ordinal && ordinal <= MAXORDINAL + 2 );
486+ const uint32_t days = (uint32_t )ordinal + 305 ;
487+ const uint32_t century = (4 * days + 3 ) / DI400Y ;
488+ const uint32_t day_of_century = (4 * days + 3 ) % DI400Y / 4 ;
489+ const uint32_t year_of_century = (4 * day_of_century + 3 ) / DI4Y ;
490+ const uint32_t day_of_year = (4 * day_of_century + 3 ) % DI4Y / 4 ;
491+ const uint32_t m = (5 * day_of_year + 461 ) / 153 ;
492+ const uint32_t d = (5 * day_of_year + 461 ) % 153 / 5 + 1 ;
493+ const uint32_t y = 100 * century + year_of_century ;
494+ if (m > 12 ) { /* January or February */
495+ * year = (int )(y + 1 );
496+ * month = (int )(m - 12 );
536497 }
537-
538- /* Now the year is correct, and n is the offset from January 1. We
539- * find the month via an estimate that's either exact or one too
540- * large.
541- */
542- leapyear = n1 == 3 && (n4 != 24 || n100 == 3 );
543- assert (leapyear == is_leap (* year ));
544- * month = (n + 50 ) >> 5 ;
545- preceding = (_days_before_month [* month ] + (* month > 2 && leapyear ));
546- if (preceding > n ) {
547- /* estimate is too large */
548- * month -= 1 ;
549- preceding -= days_in_month (* year , * month );
498+ else {
499+ * year = (int )y ;
500+ * month = (int )m ;
550501 }
551- n -= preceding ;
552- assert (0 <= n );
553- assert (n < days_in_month (* year , * month ));
554-
555- * day = n + 1 ;
502+ * day = (int )d ;
556503}
557504
558505/* year, month, day -> ordinal, considering 01-Jan-0001 as day 1. */
@@ -3444,7 +3391,16 @@ datetime_date_fromordinal_impl(PyTypeObject *type, int ordinal)
34443391 PyErr_SetString (PyExc_ValueError , "ordinal must be >= 1" );
34453392 return NULL ;
34463393 }
3447- ord_to_ymd (ordinal , & year , & month , & day );
3394+ if (ordinal <= MAXORDINAL ) {
3395+ ord_to_ymd (ordinal , & year , & month , & day );
3396+ }
3397+ else {
3398+ /* Out of range, but compute the year for the error message.
3399+ * The calendar repeats every 400 years (DI400Y days). */
3400+ int cycles = (ordinal - 1 ) / DI400Y ;
3401+ ord_to_ymd (ordinal - cycles * DI400Y , & year , & month , & day );
3402+ year += 400 * cycles ;
3403+ }
34483404 return new_date_subclass_ex (year , month , day , type );
34493405}
34503406
0 commit comments