date 3.4.1 → 3.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/ext/date/date_core.c CHANGED
@@ -27,7 +27,7 @@ static VALUE eDateError;
27
27
  static VALUE half_days_in_day, day_in_nanoseconds;
28
28
  static double positive_inf, negative_inf;
29
29
 
30
- // used by deconstruct_keys
30
+ /* used by deconstruct_keys */
31
31
  static VALUE sym_year, sym_month, sym_day, sym_yday, sym_wday;
32
32
  static VALUE sym_hour, sym_min, sym_sec, sym_sec_fraction, sym_zone;
33
33
 
@@ -452,11 +452,43 @@ do {\
452
452
  static int c_valid_civil_p(int, int, int, double,
453
453
  int *, int *, int *, int *);
454
454
 
455
+ /* Check if using pure Gregorian calendar (sg == -Infinity) */
456
+ #define c_gregorian_only_p(sg) (isinf(sg) && (sg) < 0)
457
+
458
+ /*
459
+ * Fast path macros for pure Gregorian calendar.
460
+ * Sets *rjd to the JD value, *ns to 1 (new style), and returns.
461
+ */
462
+ #define GREGORIAN_JD_FAST_PATH_RET(sg, jd_expr, rjd, ns) \
463
+ if (c_gregorian_only_p(sg)) { \
464
+ *(rjd) = (jd_expr); \
465
+ *(ns) = 1; \
466
+ return 1; \
467
+ }
468
+
469
+ #define GREGORIAN_JD_FAST_PATH(sg, jd_expr, rjd, ns) \
470
+ if (c_gregorian_only_p(sg)) { \
471
+ *(rjd) = (jd_expr); \
472
+ *(ns) = 1; \
473
+ return; \
474
+ }
475
+
476
+ /* Forward declarations for Neri-Schneider optimized functions */
477
+ static int c_gregorian_civil_to_jd(int y, int m, int d);
478
+ static void c_gregorian_jd_to_civil(int jd, int *ry, int *rm, int *rd);
479
+ static int c_gregorian_fdoy(int y);
480
+ static int c_gregorian_ldoy(int y);
481
+ static int c_gregorian_ldom_jd(int y, int m);
482
+ static int ns_jd_in_range(int jd);
483
+
455
484
  static int
456
485
  c_find_fdoy(int y, double sg, int *rjd, int *ns)
457
486
  {
458
487
  int d, rm, rd;
459
488
 
489
+ GREGORIAN_JD_FAST_PATH_RET(sg, c_gregorian_fdoy(y), rjd, ns);
490
+
491
+ /* Keep existing loop for Julian/reform period */
460
492
  for (d = 1; d < 31; d++)
461
493
  if (c_valid_civil_p(y, 1, d, sg, &rm, &rd, rjd, ns))
462
494
  return 1;
@@ -468,6 +500,9 @@ c_find_ldoy(int y, double sg, int *rjd, int *ns)
468
500
  {
469
501
  int i, rm, rd;
470
502
 
503
+ GREGORIAN_JD_FAST_PATH_RET(sg, c_gregorian_ldoy(y), rjd, ns);
504
+
505
+ /* Keep existing loop for Julian/reform period */
471
506
  for (i = 0; i < 30; i++)
472
507
  if (c_valid_civil_p(y, 12, 31 - i, sg, &rm, &rd, rjd, ns))
473
508
  return 1;
@@ -493,6 +528,9 @@ c_find_ldom(int y, int m, double sg, int *rjd, int *ns)
493
528
  {
494
529
  int i, rm, rd;
495
530
 
531
+ GREGORIAN_JD_FAST_PATH_RET(sg, c_gregorian_ldom_jd(y, m), rjd, ns);
532
+
533
+ /* Keep existing loop for Julian/reform period */
496
534
  for (i = 0; i < 30; i++)
497
535
  if (c_valid_civil_p(y, m, 31 - i, sg, &rm, &rd, rjd, ns))
498
536
  return 1;
@@ -502,55 +540,69 @@ c_find_ldom(int y, int m, double sg, int *rjd, int *ns)
502
540
  static void
503
541
  c_civil_to_jd(int y, int m, int d, double sg, int *rjd, int *ns)
504
542
  {
505
- double a, b, jd;
543
+ int jd;
544
+
545
+ GREGORIAN_JD_FAST_PATH(sg, c_gregorian_civil_to_jd(y, m, d), rjd, ns);
546
+
547
+ /* Calculate Gregorian JD using optimized algorithm */
548
+ jd = c_gregorian_civil_to_jd(y, m, d);
506
549
 
507
- if (m <= 2) {
508
- y -= 1;
509
- m += 12;
510
- }
511
- a = floor(y / 100.0);
512
- b = 2 - a + floor(a / 4.0);
513
- jd = floor(365.25 * (y + 4716)) +
514
- floor(30.6001 * (m + 1)) +
515
- d + b - 1524;
516
550
  if (jd < sg) {
517
- jd -= b;
551
+ /* Before Gregorian switchover - use Julian calendar */
552
+ int y2 = y, m2 = m;
553
+ if (m2 <= 2) {
554
+ y2 -= 1;
555
+ m2 += 12;
556
+ }
557
+ jd = (int)(floor(365.25 * (y2 + 4716)) +
558
+ floor(30.6001 * (m2 + 1)) +
559
+ d - 1524);
518
560
  *ns = 0;
519
561
  }
520
- else
562
+ else {
521
563
  *ns = 1;
564
+ }
522
565
 
523
- *rjd = (int)jd;
566
+ *rjd = jd;
524
567
  }
525
568
 
526
569
  static void
527
570
  c_jd_to_civil(int jd, double sg, int *ry, int *rm, int *rdom)
528
571
  {
529
- double x, a, b, c, d, e, y, m, dom;
530
-
531
- if (jd < sg)
532
- a = jd;
533
- else {
534
- x = floor((jd - 1867216.25) / 36524.25);
535
- a = jd + 1 + x - floor(x / 4.0);
536
- }
537
- b = a + 1524;
538
- c = floor((b - 122.1) / 365.25);
539
- d = floor(365.25 * c);
540
- e = floor((b - d) / 30.6001);
541
- dom = b - d - floor(30.6001 * e);
542
- if (e <= 13) {
543
- m = e - 1;
544
- y = c - 4716;
545
- }
546
- else {
547
- m = e - 13;
548
- y = c - 4715;
572
+ /* Fast path: pure Gregorian or date after switchover, within safe range */
573
+ if ((c_gregorian_only_p(sg) || jd >= sg) && ns_jd_in_range(jd)) {
574
+ c_gregorian_jd_to_civil(jd, ry, rm, rdom);
575
+ return;
549
576
  }
550
577
 
551
- *ry = (int)y;
552
- *rm = (int)m;
553
- *rdom = (int)dom;
578
+ /* Original algorithm for Julian calendar or extreme dates */
579
+ {
580
+ double x, a, b, c, d, e, y, m, dom;
581
+
582
+ if (jd < sg)
583
+ a = jd;
584
+ else {
585
+ x = floor((jd - 1867216.25) / 36524.25);
586
+ a = jd + 1 + x - floor(x / 4.0);
587
+ }
588
+ b = a + 1524;
589
+ c = floor((b - 122.1) / 365.25);
590
+ d = floor(365.25 * c);
591
+ e = floor((b - d) / 30.6001);
592
+ dom = b - d - floor(30.6001 * e);
593
+ if (e <= 13) {
594
+ m = e - 1;
595
+ y = c - 4716;
596
+ }
597
+ else {
598
+ m = e - 13;
599
+ y = c - 4715;
600
+ }
601
+
602
+ *ry = (int)y;
603
+ *rm = (int)m;
604
+ *rdom = (int)dom;
605
+ }
554
606
  }
555
607
 
556
608
  static void
@@ -725,6 +777,147 @@ c_gregorian_last_day_of_month(int y, int m)
725
777
  return monthtab[c_gregorian_leap_p(y) ? 1 : 0][m];
726
778
  }
727
779
 
780
+ /*
781
+ * Neri-Schneider algorithm for optimized Gregorian date conversion.
782
+ * Reference: Neri & Schneider, "Euclidean Affine Functions and Applications
783
+ * to Calendar Algorithms", Software: Practice and Experience, 2023.
784
+ * https://arxiv.org/abs/2102.06959
785
+ *
786
+ * This algorithm provides ~2-3x speedup over traditional floating-point
787
+ * implementations by using pure integer arithmetic with multiplication
788
+ * and bit-shifts instead of expensive division operations.
789
+ */
790
+
791
+ /* JDN of March 1, Year 0 in proleptic Gregorian calendar */
792
+ #define NS_EPOCH 1721120
793
+
794
+ /* Days in a 4-year cycle (3 normal years + 1 leap year) */
795
+ #define NS_DAYS_IN_4_YEARS 1461
796
+
797
+ /* Days in a 400-year Gregorian cycle (97 leap years in 400 years) */
798
+ #define NS_DAYS_IN_400_YEARS 146097
799
+
800
+ /* Years per century */
801
+ #define NS_YEARS_PER_CENTURY 100
802
+
803
+ /*
804
+ * Multiplier for extracting year within century using fixed-point arithmetic.
805
+ * This is ceil(2^32 / NS_DAYS_IN_4_YEARS) for the Euclidean affine function.
806
+ */
807
+ #define NS_YEAR_MULTIPLIER 2939745
808
+
809
+ /*
810
+ * Coefficients for month calculation from day-of-year.
811
+ * Maps day-of-year to month using: month = (NS_MONTH_COEFF * doy + NS_MONTH_OFFSET) >> 16
812
+ */
813
+ #define NS_MONTH_COEFF 2141
814
+ #define NS_MONTH_OFFSET 197913
815
+
816
+ /*
817
+ * Coefficients for civil date to JDN month contribution.
818
+ * Maps month to accumulated days: days = (NS_CIVIL_MONTH_COEFF * m - NS_CIVIL_MONTH_OFFSET) / 32
819
+ */
820
+ #define NS_CIVIL_MONTH_COEFF 979
821
+ #define NS_CIVIL_MONTH_OFFSET 2919
822
+ #define NS_CIVIL_MONTH_DIVISOR 32
823
+
824
+ /* Days from March 1 to December 31 (for Jan/Feb year adjustment) */
825
+ #define NS_DAYS_BEFORE_NEW_YEAR 306
826
+
827
+ /*
828
+ * Safe bounds for Neri-Schneider algorithm to avoid integer overflow.
829
+ * These correspond to approximately years -1,000,000 to +1,000,000.
830
+ */
831
+ #define NS_JD_MIN -364000000
832
+ #define NS_JD_MAX 538000000
833
+
834
+ inline static int
835
+ ns_jd_in_range(int jd)
836
+ {
837
+ return jd >= NS_JD_MIN && jd <= NS_JD_MAX;
838
+ }
839
+
840
+ /* Optimized: Gregorian date -> Julian Day Number */
841
+ static int
842
+ c_gregorian_civil_to_jd(int y, int m, int d)
843
+ {
844
+ /* Shift epoch to March 1 of year 0 (Jan/Feb belong to previous year) */
845
+ int j = (m < 3) ? 1 : 0;
846
+ int y0 = y - j;
847
+ int m0 = j ? m + 12 : m;
848
+ int d0 = d - 1;
849
+
850
+ /* Calculate year contribution with leap year correction */
851
+ int q1 = DIV(y0, NS_YEARS_PER_CENTURY);
852
+ int yc = DIV(NS_DAYS_IN_4_YEARS * y0, 4) - q1 + DIV(q1, 4);
853
+
854
+ /* Calculate month contribution using integer arithmetic */
855
+ int mc = (NS_CIVIL_MONTH_COEFF * m0 - NS_CIVIL_MONTH_OFFSET) / NS_CIVIL_MONTH_DIVISOR;
856
+
857
+ /* Combine and add epoch offset to get JDN */
858
+ return yc + mc + d0 + NS_EPOCH;
859
+ }
860
+
861
+ /* Optimized: Julian Day Number -> Gregorian date */
862
+ static void
863
+ c_gregorian_jd_to_civil(int jd, int *ry, int *rm, int *rd)
864
+ {
865
+ int r0, n1, q1, r1, n2, q2, r2, n3, q3, r3, y0, j;
866
+ uint64_t u2;
867
+
868
+ /* Convert JDN to rata die (March 1, Year 0 epoch) */
869
+ r0 = jd - NS_EPOCH;
870
+
871
+ /* Extract century and day within 400-year cycle */
872
+ /* Use Euclidean (floor) division for negative values */
873
+ n1 = 4 * r0 + 3;
874
+ q1 = DIV(n1, NS_DAYS_IN_400_YEARS);
875
+ r1 = MOD(n1, NS_DAYS_IN_400_YEARS) / 4;
876
+
877
+ /* Calculate year within century and day of year */
878
+ n2 = 4 * r1 + 3;
879
+ /* Use 64-bit arithmetic to avoid overflow */
880
+ u2 = (uint64_t)NS_YEAR_MULTIPLIER * (uint64_t)n2;
881
+ q2 = (int)(u2 >> 32);
882
+ r2 = (int)((uint32_t)u2 / NS_YEAR_MULTIPLIER / 4);
883
+
884
+ /* Calculate month and day using integer arithmetic */
885
+ n3 = NS_MONTH_COEFF * r2 + NS_MONTH_OFFSET;
886
+ q3 = n3 >> 16;
887
+ r3 = (n3 & 0xFFFF) / NS_MONTH_COEFF;
888
+
889
+ /* Combine century and year */
890
+ y0 = NS_YEARS_PER_CENTURY * q1 + q2;
891
+
892
+ /* Adjust for January/February (shift from fiscal year) */
893
+ j = (r2 >= NS_DAYS_BEFORE_NEW_YEAR) ? 1 : 0;
894
+
895
+ *ry = y0 + j;
896
+ *rm = j ? q3 - 12 : q3;
897
+ *rd = r3 + 1;
898
+ }
899
+
900
+ /* O(1) first day of year for Gregorian calendar */
901
+ inline static int
902
+ c_gregorian_fdoy(int y)
903
+ {
904
+ return c_gregorian_civil_to_jd(y, 1, 1);
905
+ }
906
+
907
+ /* O(1) last day of year for Gregorian calendar */
908
+ inline static int
909
+ c_gregorian_ldoy(int y)
910
+ {
911
+ return c_gregorian_civil_to_jd(y, 12, 31);
912
+ }
913
+
914
+ /* O(1) last day of month (JDN) for Gregorian calendar */
915
+ inline static int
916
+ c_gregorian_ldom_jd(int y, int m)
917
+ {
918
+ return c_gregorian_civil_to_jd(y, m, c_gregorian_last_day_of_month(y, m));
919
+ }
920
+
728
921
  static int
729
922
  c_valid_julian_p(int y, int m, int d, int *rm, int *rd)
730
923
  {
@@ -799,12 +992,16 @@ c_valid_commercial_p(int y, int w, int d, double sg,
799
992
  if (w < 0) {
800
993
  int rjd2;
801
994
 
995
+ if (w < -53) return 0;
802
996
  c_commercial_to_jd(y + 1, 1, 1, sg, &rjd2, &ns2);
803
997
  c_jd_to_commercial(rjd2 + w * 7, sg, &ry2, &rw2, &rd2);
804
998
  if (ry2 != y)
805
999
  return 0;
806
1000
  w = rw2;
807
1001
  }
1002
+ else {
1003
+ if (w < 1 || 53 < w) return 0;
1004
+ }
808
1005
  c_commercial_to_jd(y, w, d, sg, rjd, ns);
809
1006
  c_jd_to_commercial(*rjd, sg, &ry2, rw, rd);
810
1007
  if (y != ry2 || w != *rw || d != *rd)
@@ -1599,7 +1796,7 @@ m_ajd(union DateData *x)
1599
1796
 
1600
1797
  if (simple_dat_p(x)) {
1601
1798
  r = m_real_jd(x);
1602
- if (FIXNUM_P(r) && FIX2LONG(r) <= (FIXNUM_MAX / 2)) {
1799
+ if (FIXNUM_P(r) && FIX2LONG(r) <= (FIXNUM_MAX / 2) && FIX2LONG(r) >= (FIXNUM_MIN + 1) / 2) {
1603
1800
  long ir = FIX2LONG(r);
1604
1801
  ir = ir * 2 - 1;
1605
1802
  return rb_rational_new2(LONG2FIX(ir), INT2FIX(2));
@@ -2428,9 +2625,9 @@ offset_to_sec(VALUE vof, int *rof)
2428
2625
  if (!FIXNUM_P(vn))
2429
2626
  return 0;
2430
2627
  n = FIX2LONG(vn);
2431
- if (n < -DAY_IN_SECONDS || n > DAY_IN_SECONDS)
2432
- return 0;
2433
2628
  }
2629
+ if (n < -DAY_IN_SECONDS || n > DAY_IN_SECONDS)
2630
+ return 0;
2434
2631
  *rof = (int)n;
2435
2632
  return 1;
2436
2633
  }
@@ -2498,7 +2695,7 @@ date_s__valid_jd_p(int argc, VALUE *argv, VALUE klass)
2498
2695
  *
2499
2696
  * Date.valid_jd?(2451944) # => true
2500
2697
  *
2501
- * See argument {start}[rdoc-ref:calendars.rdoc@Argument+start].
2698
+ * See argument {start}[rdoc-ref:language/calendars.rdoc@Argument+start].
2502
2699
  *
2503
2700
  * Related: Date.jd.
2504
2701
  */
@@ -2592,7 +2789,7 @@ date_s__valid_civil_p(int argc, VALUE *argv, VALUE klass)
2592
2789
  * Date.valid_date?(2001, 2, 29) # => false
2593
2790
  * Date.valid_date?(2001, 2, -1) # => true
2594
2791
  *
2595
- * See argument {start}[rdoc-ref:calendars.rdoc@Argument+start].
2792
+ * See argument {start}[rdoc-ref:language/calendars.rdoc@Argument+start].
2596
2793
  *
2597
2794
  * Related: Date.jd, Date.new.
2598
2795
  */
@@ -2680,7 +2877,7 @@ date_s__valid_ordinal_p(int argc, VALUE *argv, VALUE klass)
2680
2877
  * Date.valid_ordinal?(2001, 34) # => true
2681
2878
  * Date.valid_ordinal?(2001, 366) # => false
2682
2879
  *
2683
- * See argument {start}[rdoc-ref:calendars.rdoc@Argument+start].
2880
+ * See argument {start}[rdoc-ref:language/calendars.rdoc@Argument+start].
2684
2881
  *
2685
2882
  * Related: Date.jd, Date.ordinal.
2686
2883
  */
@@ -2770,7 +2967,7 @@ date_s__valid_commercial_p(int argc, VALUE *argv, VALUE klass)
2770
2967
  *
2771
2968
  * See Date.commercial.
2772
2969
  *
2773
- * See argument {start}[rdoc-ref:calendars.rdoc@Argument+start].
2970
+ * See argument {start}[rdoc-ref:language/calendars.rdoc@Argument+start].
2774
2971
  *
2775
2972
  * Related: Date.jd, Date.commercial.
2776
2973
  */
@@ -3002,34 +3199,66 @@ date_s_gregorian_leap_p(VALUE klass, VALUE y)
3002
3199
  return f_boolcast(c_gregorian_leap_p(ry));
3003
3200
  }
3004
3201
 
3202
+ #ifndef HAVE_RB_GC_MARK_MOVABLE
3203
+ #define rb_gc_mark_movable rb_gc_mark
3204
+ #else
3005
3205
  static void
3006
- d_lite_gc_mark(void *ptr)
3206
+ d_lite_gc_compact(void *ptr)
3007
3207
  {
3008
3208
  union DateData *dat = ptr;
3009
3209
  if (simple_dat_p(dat))
3010
- rb_gc_mark(dat->s.nth);
3210
+ dat->s.nth = rb_gc_location(dat->s.nth);
3011
3211
  else {
3012
- rb_gc_mark(dat->c.nth);
3013
- rb_gc_mark(dat->c.sf);
3212
+ dat->c.nth = rb_gc_location(dat->c.nth);
3213
+ dat->c.sf = rb_gc_location(dat->c.sf);
3014
3214
  }
3015
3215
  }
3216
+ #endif
3016
3217
 
3017
- static size_t
3018
- d_lite_memsize(const void *ptr)
3218
+ static void
3219
+ d_lite_gc_mark(void *ptr)
3019
3220
  {
3020
- const union DateData *dat = ptr;
3021
- return complex_dat_p(dat) ? sizeof(struct ComplexDateData) : sizeof(struct SimpleDateData);
3221
+ union DateData *dat = ptr;
3222
+ if (simple_dat_p(dat))
3223
+ rb_gc_mark_movable(dat->s.nth);
3224
+ else {
3225
+ rb_gc_mark_movable(dat->c.nth);
3226
+ rb_gc_mark_movable(dat->c.sf);
3227
+ }
3022
3228
  }
3023
3229
 
3024
3230
  #ifndef HAVE_RB_EXT_RACTOR_SAFE
3025
3231
  # define RUBY_TYPED_FROZEN_SHAREABLE 0
3026
3232
  #endif
3027
3233
 
3234
+ #if defined(RUBY_TYPED_EMBEDDABLE) || defined(HAVE_CONST_RUBY_TYPED_EMBEDDABLE)
3235
+ # define HAVE_RUBY_TYPED_EMBEDDABLE 1
3236
+ #else
3237
+ # define RUBY_TYPED_EMBEDDABLE 0
3238
+ #endif
3239
+
3240
+ static size_t
3241
+ d_lite_memsize(const void *ptr)
3242
+ {
3243
+ #ifdef HAVE_RUBY_TYPED_EMBEDDABLE
3244
+ return 0;
3245
+ #else
3246
+ return sizeof(union DateData);
3247
+ #endif
3248
+ }
3249
+
3028
3250
  static const rb_data_type_t d_lite_type = {
3029
3251
  "Date",
3030
- {d_lite_gc_mark, RUBY_TYPED_DEFAULT_FREE, d_lite_memsize,},
3252
+ {
3253
+ d_lite_gc_mark,
3254
+ RUBY_TYPED_DEFAULT_FREE,
3255
+ d_lite_memsize,
3256
+ #ifdef HAVE_RB_GC_MARK_MOVABLE
3257
+ d_lite_gc_compact,
3258
+ #endif
3259
+ },
3031
3260
  0, 0,
3032
- RUBY_TYPED_FREE_IMMEDIATELY|RUBY_TYPED_WB_PROTECTED|RUBY_TYPED_FROZEN_SHAREABLE,
3261
+ RUBY_TYPED_FREE_IMMEDIATELY|RUBY_TYPED_WB_PROTECTED|RUBY_TYPED_EMBEDDABLE|RUBY_TYPED_FROZEN_SHAREABLE,
3033
3262
  };
3034
3263
 
3035
3264
  inline static VALUE
@@ -3040,10 +3269,11 @@ d_simple_new_internal(VALUE klass,
3040
3269
  unsigned flags)
3041
3270
  {
3042
3271
  struct SimpleDateData *dat;
3272
+ union DateData *u_dat;
3043
3273
  VALUE obj;
3044
3274
 
3045
- obj = TypedData_Make_Struct(klass, struct SimpleDateData,
3046
- &d_lite_type, dat);
3275
+ obj = TypedData_Make_Struct(klass, union DateData, &d_lite_type, u_dat);
3276
+ dat = &u_dat->s;
3047
3277
  set_to_simple(obj, dat, nth, jd, sg, y, m, d, flags);
3048
3278
 
3049
3279
  assert(have_jd_p(dat) || have_civil_p(dat));
@@ -3061,10 +3291,11 @@ d_complex_new_internal(VALUE klass,
3061
3291
  unsigned flags)
3062
3292
  {
3063
3293
  struct ComplexDateData *dat;
3294
+ union DateData *u_dat;
3064
3295
  VALUE obj;
3065
3296
 
3066
- obj = TypedData_Make_Struct(klass, struct ComplexDateData,
3067
- &d_lite_type, dat);
3297
+ obj = TypedData_Make_Struct(klass, union DateData, &d_lite_type, u_dat);
3298
+ dat = &u_dat->c;
3068
3299
  set_to_complex(obj, dat, nth, jd, df, sf, of, sg,
3069
3300
  y, m, d, h, min, s, flags);
3070
3301
 
@@ -3350,7 +3581,7 @@ static VALUE d_lite_plus(VALUE, VALUE);
3350
3581
  *
3351
3582
  * Date.jd(Date::ITALY - 1).julian? # => true
3352
3583
  *
3353
- * See argument {start}[rdoc-ref:calendars.rdoc@Argument+start].
3584
+ * See argument {start}[rdoc-ref:language/calendars.rdoc@Argument+start].
3354
3585
  *
3355
3586
  * Related: Date.new.
3356
3587
  */
@@ -3415,7 +3646,7 @@ date_s_jd(int argc, VALUE *argv, VALUE klass)
3415
3646
  *
3416
3647
  * Raises an exception if +yday+ is zero or out of range.
3417
3648
  *
3418
- * See argument {start}[rdoc-ref:calendars.rdoc@Argument+start].
3649
+ * See argument {start}[rdoc-ref:language/calendars.rdoc@Argument+start].
3419
3650
  *
3420
3651
  * Related: Date.jd, Date.new.
3421
3652
  */
@@ -3492,7 +3723,7 @@ date_s_civil(int argc, VALUE *argv, VALUE klass)
3492
3723
  * where +n+ is the number of days in the month;
3493
3724
  * when the argument is negative, counts backward from the end of the month.
3494
3725
  *
3495
- * See argument {start}[rdoc-ref:calendars.rdoc@Argument+start].
3726
+ * See argument {start}[rdoc-ref:language/calendars.rdoc@Argument+start].
3496
3727
  *
3497
3728
  * Related: Date.jd.
3498
3729
  */
@@ -3598,7 +3829,7 @@ date_initialize(int argc, VALUE *argv, VALUE self)
3598
3829
  * Date.commercial(2020, 1, 1).to_s # => "2019-12-30"
3599
3830
  Date.commercial(2020, 1, 7).to_s # => "2020-01-05"
3600
3831
  *
3601
- * See argument {start}[rdoc-ref:calendars.rdoc@Argument+start].
3832
+ * See argument {start}[rdoc-ref:language/calendars.rdoc@Argument+start].
3602
3833
  *
3603
3834
  * Related: Date.jd, Date.new, Date.ordinal.
3604
3835
  */
@@ -3783,7 +4014,7 @@ static void set_sg(union DateData *, double);
3783
4014
  *
3784
4015
  * Date.today.to_s # => "2022-07-06"
3785
4016
  *
3786
- * See argument {start}[rdoc-ref:calendars.rdoc@Argument+start].
4017
+ * See argument {start}[rdoc-ref:language/calendars.rdoc@Argument+start].
3787
4018
  *
3788
4019
  */
3789
4020
  static VALUE
@@ -3878,7 +4109,6 @@ static VALUE
3878
4109
  rt_complete_frags(VALUE klass, VALUE hash)
3879
4110
  {
3880
4111
  static VALUE tab = Qnil;
3881
- int g;
3882
4112
  long e;
3883
4113
  VALUE k, a, d;
3884
4114
 
@@ -3975,9 +4205,13 @@ rt_complete_frags(VALUE klass, VALUE hash)
3975
4205
  rb_gc_register_mark_object(tab);
3976
4206
  }
3977
4207
 
4208
+ k = a = Qnil;
4209
+
3978
4210
  {
3979
- long i, eno = 0, idx = 0;
4211
+ long i, eno = 0;
4212
+ VALUE t = Qnil;
3980
4213
 
4214
+ e = 0;
3981
4215
  for (i = 0; i < RARRAY_LEN(tab); i++) {
3982
4216
  VALUE x, a;
3983
4217
 
@@ -3992,23 +4226,20 @@ rt_complete_frags(VALUE klass, VALUE hash)
3992
4226
  n++;
3993
4227
  if (n > eno) {
3994
4228
  eno = n;
3995
- idx = i;
4229
+ t = x;
3996
4230
  }
3997
4231
  }
3998
4232
  }
3999
- if (eno == 0)
4000
- g = 0;
4001
- else {
4002
- g = 1;
4003
- k = RARRAY_AREF(RARRAY_AREF(tab, idx), 0);
4004
- a = RARRAY_AREF(RARRAY_AREF(tab, idx), 1);
4005
- e = eno;
4233
+ if (eno > 0) {
4234
+ k = RARRAY_AREF(t, 0);
4235
+ a = RARRAY_AREF(t, 1);
4006
4236
  }
4237
+ e = eno;
4007
4238
  }
4008
4239
 
4009
4240
  d = Qnil;
4010
4241
 
4011
- if (g && !NIL_P(k) && (RARRAY_LEN(a) - e)) {
4242
+ if (!NIL_P(k) && (RARRAY_LEN(a) > e)) {
4012
4243
  if (k == sym("ordinal")) {
4013
4244
  if (NIL_P(ref_hash("year"))) {
4014
4245
  if (NIL_P(d))
@@ -4095,7 +4326,7 @@ rt_complete_frags(VALUE klass, VALUE hash)
4095
4326
  }
4096
4327
  }
4097
4328
 
4098
- if (g && k == sym("time")) {
4329
+ if (k == sym("time")) {
4099
4330
  if (f_le_p(klass, cDateTime)) {
4100
4331
  if (NIL_P(d))
4101
4332
  d = date_s_today(0, (VALUE *)0, cDate);
@@ -4322,7 +4553,8 @@ d_new_by_frags(VALUE klass, VALUE hash, VALUE sg)
4322
4553
  }
4323
4554
 
4324
4555
  VALUE date__strptime(const char *str, size_t slen,
4325
- const char *fmt, size_t flen, VALUE hash);
4556
+ const char *fmt, size_t flen,
4557
+ VALUE hash, rb_encoding *enc);
4326
4558
 
4327
4559
  static VALUE
4328
4560
  date_s__strptime_internal(int argc, VALUE *argv, VALUE klass,
@@ -4331,10 +4563,12 @@ date_s__strptime_internal(int argc, VALUE *argv, VALUE klass,
4331
4563
  VALUE vstr, vfmt, hash;
4332
4564
  const char *str, *fmt;
4333
4565
  size_t slen, flen;
4566
+ rb_encoding *enc;
4334
4567
 
4335
4568
  rb_scan_args(argc, argv, "11", &vstr, &vfmt);
4336
4569
 
4337
4570
  StringValue(vstr);
4571
+ if (argc > 1) StringValue(vfmt);
4338
4572
  if (!rb_enc_str_asciicompat_p(vstr))
4339
4573
  rb_raise(rb_eArgError,
4340
4574
  "string should have ASCII compatible encoding");
@@ -4343,34 +4577,18 @@ date_s__strptime_internal(int argc, VALUE *argv, VALUE klass,
4343
4577
  if (argc < 2) {
4344
4578
  fmt = default_fmt;
4345
4579
  flen = strlen(default_fmt);
4580
+ enc = rb_enc_get(vstr);
4346
4581
  }
4347
4582
  else {
4348
- StringValue(vfmt);
4349
4583
  if (!rb_enc_str_asciicompat_p(vfmt))
4350
4584
  rb_raise(rb_eArgError,
4351
4585
  "format should have ASCII compatible encoding");
4586
+ enc = rb_enc_check(vstr, vfmt);
4352
4587
  fmt = RSTRING_PTR(vfmt);
4353
4588
  flen = RSTRING_LEN(vfmt);
4354
4589
  }
4355
4590
  hash = rb_hash_new();
4356
- if (NIL_P(date__strptime(str, slen, fmt, flen, hash)))
4357
- return Qnil;
4358
-
4359
- {
4360
- VALUE zone = ref_hash("zone");
4361
- VALUE left = ref_hash("leftover");
4362
-
4363
- if (!NIL_P(zone)) {
4364
- rb_enc_copy(zone, vstr);
4365
- set_hash("zone", zone);
4366
- }
4367
- if (!NIL_P(left)) {
4368
- rb_enc_copy(left, vstr);
4369
- set_hash("leftover", left);
4370
- }
4371
- }
4372
-
4373
- return hash;
4591
+ return date__strptime(str, slen, fmt, flen, hash, enc);
4374
4592
  }
4375
4593
 
4376
4594
  /*
@@ -4383,7 +4601,7 @@ date_s__strptime_internal(int argc, VALUE *argv, VALUE klass,
4383
4601
  * Date._strptime('2001-02-03', '%Y-%m-%d') # => {:year=>2001, :mon=>2, :mday=>3}
4384
4602
  *
4385
4603
  * For other formats, see
4386
- * {Formats for Dates and Times}[https://docs.ruby-lang.org/en/master/strftime_formatting_rdoc.html].
4604
+ * {Formats for Dates and Times}[rdoc-ref:language/strftime_formatting.rdoc].
4387
4605
  * (Unlike Date.strftime, does not support flags and width.)
4388
4606
  *
4389
4607
  * See also {strptime(3)}[https://man7.org/linux/man-pages/man3/strptime.3.html].
@@ -4412,10 +4630,10 @@ date_s__strptime(int argc, VALUE *argv, VALUE klass)
4412
4630
  * Date.strptime('sat3feb01', '%a%d%b%y') # => #<Date: 2001-02-03>
4413
4631
  *
4414
4632
  * For other formats, see
4415
- * {Formats for Dates and Times}[https://docs.ruby-lang.org/en/master/strftime_formatting_rdoc.html].
4633
+ * {Formats for Dates and Times}[rdoc-ref:language/strftime_formatting.rdoc].
4416
4634
  * (Unlike Date.strftime, does not support flags and width.)
4417
4635
  *
4418
- * See argument {start}[rdoc-ref:calendars.rdoc@Argument+start].
4636
+ * See argument {start}[rdoc-ref:language/calendars.rdoc@Argument+start].
4419
4637
  *
4420
4638
  * See also {strptime(3)}[https://man7.org/linux/man-pages/man3/strptime.3.html].
4421
4639
  *
@@ -4464,11 +4682,10 @@ get_limit(VALUE opt)
4464
4682
  #define rb_category_warn(category, fmt) rb_warn(fmt)
4465
4683
  #endif
4466
4684
 
4467
- static void
4685
+ static VALUE
4468
4686
  check_limit(VALUE str, VALUE opt)
4469
4687
  {
4470
4688
  size_t slen, limit;
4471
- if (NIL_P(str)) return;
4472
4689
  StringValue(str);
4473
4690
  slen = RSTRING_LEN(str);
4474
4691
  limit = get_limit(opt);
@@ -4476,6 +4693,7 @@ check_limit(VALUE str, VALUE opt)
4476
4693
  rb_raise(rb_eArgError,
4477
4694
  "string length (%"PRI_SIZE_PREFIX"u) exceeds the limit %"PRI_SIZE_PREFIX"u", slen, limit);
4478
4695
  }
4696
+ return str;
4479
4697
  }
4480
4698
 
4481
4699
  static VALUE
@@ -4484,8 +4702,7 @@ date_s__parse_internal(int argc, VALUE *argv, VALUE klass)
4484
4702
  VALUE vstr, vcomp, hash, opt;
4485
4703
 
4486
4704
  argc = rb_scan_args(argc, argv, "11:", &vstr, &vcomp, &opt);
4487
- check_limit(vstr, opt);
4488
- StringValue(vstr);
4705
+ vstr = check_limit(vstr, opt);
4489
4706
  if (!rb_enc_str_asciicompat_p(vstr))
4490
4707
  rb_raise(rb_eArgError,
4491
4708
  "string should have ASCII compatible encoding");
@@ -4505,7 +4722,7 @@ date_s__parse_internal(int argc, VALUE *argv, VALUE klass)
4505
4722
  * This method recognizes many forms in +string+,
4506
4723
  * but it is not a validator.
4507
4724
  * For formats, see
4508
- * {"Specialized Format Strings" in Formats for Dates and Times}[https://docs.ruby-lang.org/en/master/strftime_formatting_rdoc.html#label-Specialized+Format+Strings]
4725
+ * {"Specialized Format Strings" in Formats for Dates and Times}[rdoc-ref:language/strftime_formatting.rdoc@Specialized+Format+Strings]
4509
4726
  *
4510
4727
  * If +string+ does not specify a valid date,
4511
4728
  * the result is unpredictable;
@@ -4540,7 +4757,7 @@ date_s__parse(int argc, VALUE *argv, VALUE klass)
4540
4757
  * This method recognizes many forms in +string+,
4541
4758
  * but it is not a validator.
4542
4759
  * For formats, see
4543
- * {"Specialized Format Strings" in Formats for Dates and Times}[https://docs.ruby-lang.org/en/master/strftime_formatting_rdoc.html#label-Specialized+Format+Strings]
4760
+ * {"Specialized Format Strings" in Formats for Dates and Times}[rdoc-ref:language/strftime_formatting.rdoc@Specialized+Format+Strings]
4544
4761
  * If +string+ does not specify a valid date,
4545
4762
  * the result is unpredictable;
4546
4763
  * consider using Date._strptime instead.
@@ -4560,7 +4777,7 @@ date_s__parse(int argc, VALUE *argv, VALUE klass)
4560
4777
  *
4561
4778
  * See:
4562
4779
  *
4563
- * - Argument {start}[rdoc-ref:calendars.rdoc@Argument+start].
4780
+ * - Argument {start}[rdoc-ref:language/calendars.rdoc@Argument+start].
4564
4781
  * - Argument {limit}[rdoc-ref:Date@Argument+limit].
4565
4782
  *
4566
4783
  * Related: Date._parse (returns a hash).
@@ -4604,7 +4821,7 @@ VALUE date__jisx0301(VALUE);
4604
4821
  * Date._iso8601(string, limit: 128) -> hash
4605
4822
  *
4606
4823
  * Returns a hash of values parsed from +string+, which should contain
4607
- * an {ISO 8601 formatted date}[https://docs.ruby-lang.org/en/master/strftime_formatting_rdoc.html#label-ISO+8601+Format+Specifications]:
4824
+ * an {ISO 8601 formatted date}[rdoc-ref:language/strftime_formatting.rdoc@ISO+8601+Format+Specifications]:
4608
4825
  *
4609
4826
  * d = Date.new(2001, 2, 3)
4610
4827
  * s = d.iso8601 # => "2001-02-03"
@@ -4620,7 +4837,7 @@ date_s__iso8601(int argc, VALUE *argv, VALUE klass)
4620
4837
  VALUE str, opt;
4621
4838
 
4622
4839
  rb_scan_args(argc, argv, "1:", &str, &opt);
4623
- check_limit(str, opt);
4840
+ if (!NIL_P(str)) str = check_limit(str, opt);
4624
4841
 
4625
4842
  return date__iso8601(str);
4626
4843
  }
@@ -4631,7 +4848,7 @@ date_s__iso8601(int argc, VALUE *argv, VALUE klass)
4631
4848
  *
4632
4849
  * Returns a new \Date object with values parsed from +string+,
4633
4850
  * which should contain
4634
- * an {ISO 8601 formatted date}[https://docs.ruby-lang.org/en/master/strftime_formatting_rdoc.html#label-ISO+8601+Format+Specifications]:
4851
+ * an {ISO 8601 formatted date}[rdoc-ref:language/strftime_formatting.rdoc@ISO+8601+Format+Specifications]:
4635
4852
  *
4636
4853
  * d = Date.new(2001, 2, 3)
4637
4854
  * s = d.iso8601 # => "2001-02-03"
@@ -4639,7 +4856,7 @@ date_s__iso8601(int argc, VALUE *argv, VALUE klass)
4639
4856
  *
4640
4857
  * See:
4641
4858
  *
4642
- * - Argument {start}[rdoc-ref:calendars.rdoc@Argument+start].
4859
+ * - Argument {start}[rdoc-ref:language/calendars.rdoc@Argument+start].
4643
4860
  * - Argument {limit}[rdoc-ref:Date@Argument+limit].
4644
4861
  *
4645
4862
  * Related: Date._iso8601 (returns a hash).
@@ -4673,7 +4890,7 @@ date_s_iso8601(int argc, VALUE *argv, VALUE klass)
4673
4890
  * Date._rfc3339(string, limit: 128) -> hash
4674
4891
  *
4675
4892
  * Returns a hash of values parsed from +string+, which should be a valid
4676
- * {RFC 3339 format}[https://docs.ruby-lang.org/en/master/strftime_formatting_rdoc.html#label-RFC+3339+Format]:
4893
+ * {RFC 3339 format}[rdoc-ref:language/strftime_formatting.rdoc@RFC+3339+Format]:
4677
4894
  *
4678
4895
  * d = Date.new(2001, 2, 3)
4679
4896
  * s = d.rfc3339 # => "2001-02-03T00:00:00+00:00"
@@ -4690,7 +4907,7 @@ date_s__rfc3339(int argc, VALUE *argv, VALUE klass)
4690
4907
  VALUE str, opt;
4691
4908
 
4692
4909
  rb_scan_args(argc, argv, "1:", &str, &opt);
4693
- check_limit(str, opt);
4910
+ if (!NIL_P(str)) str = check_limit(str, opt);
4694
4911
 
4695
4912
  return date__rfc3339(str);
4696
4913
  }
@@ -4701,7 +4918,7 @@ date_s__rfc3339(int argc, VALUE *argv, VALUE klass)
4701
4918
  *
4702
4919
  * Returns a new \Date object with values parsed from +string+,
4703
4920
  * which should be a valid
4704
- * {RFC 3339 format}[https://docs.ruby-lang.org/en/master/strftime_formatting_rdoc.html#label-RFC+3339+Format]:
4921
+ * {RFC 3339 format}[rdoc-ref:language/strftime_formatting.rdoc@RFC+3339+Format]:
4705
4922
  *
4706
4923
  * d = Date.new(2001, 2, 3)
4707
4924
  * s = d.rfc3339 # => "2001-02-03T00:00:00+00:00"
@@ -4709,7 +4926,7 @@ date_s__rfc3339(int argc, VALUE *argv, VALUE klass)
4709
4926
  *
4710
4927
  * See:
4711
4928
  *
4712
- * - Argument {start}[rdoc-ref:calendars.rdoc@Argument+start].
4929
+ * - Argument {start}[rdoc-ref:language/calendars.rdoc@Argument+start].
4713
4930
  * - Argument {limit}[rdoc-ref:Date@Argument+limit].
4714
4931
  *
4715
4932
  * Related: Date._rfc3339 (returns a hash).
@@ -4759,7 +4976,7 @@ date_s__xmlschema(int argc, VALUE *argv, VALUE klass)
4759
4976
  VALUE str, opt;
4760
4977
 
4761
4978
  rb_scan_args(argc, argv, "1:", &str, &opt);
4762
- check_limit(str, opt);
4979
+ if (!NIL_P(str)) str = check_limit(str, opt);
4763
4980
 
4764
4981
  return date__xmlschema(str);
4765
4982
  }
@@ -4777,7 +4994,7 @@ date_s__xmlschema(int argc, VALUE *argv, VALUE klass)
4777
4994
  *
4778
4995
  * See:
4779
4996
  *
4780
- * - Argument {start}[rdoc-ref:calendars.rdoc@Argument+start].
4997
+ * - Argument {start}[rdoc-ref:language/calendars.rdoc@Argument+start].
4781
4998
  * - Argument {limit}[rdoc-ref:Date@Argument+limit].
4782
4999
  *
4783
5000
  * Related: Date._xmlschema (returns a hash).
@@ -4811,7 +5028,7 @@ date_s_xmlschema(int argc, VALUE *argv, VALUE klass)
4811
5028
  * Date._rfc2822(string, limit: 128) -> hash
4812
5029
  *
4813
5030
  * Returns a hash of values parsed from +string+, which should be a valid
4814
- * {RFC 2822 date format}[https://docs.ruby-lang.org/en/master/strftime_formatting_rdoc.html#label-RFC+2822+Format]:
5031
+ * {RFC 2822 date format}[rdoc-ref:language/strftime_formatting.rdoc@RFC+2822+Format]:
4815
5032
  *
4816
5033
  * d = Date.new(2001, 2, 3)
4817
5034
  * s = d.rfc2822 # => "Sat, 3 Feb 2001 00:00:00 +0000"
@@ -4828,7 +5045,7 @@ date_s__rfc2822(int argc, VALUE *argv, VALUE klass)
4828
5045
  VALUE str, opt;
4829
5046
 
4830
5047
  rb_scan_args(argc, argv, "1:", &str, &opt);
4831
- check_limit(str, opt);
5048
+ if (!NIL_P(str)) str = check_limit(str, opt);
4832
5049
 
4833
5050
  return date__rfc2822(str);
4834
5051
  }
@@ -4839,7 +5056,7 @@ date_s__rfc2822(int argc, VALUE *argv, VALUE klass)
4839
5056
  *
4840
5057
  * Returns a new \Date object with values parsed from +string+,
4841
5058
  * which should be a valid
4842
- * {RFC 2822 date format}[https://docs.ruby-lang.org/en/master/strftime_formatting_rdoc.html#label-RFC+2822+Format]:
5059
+ * {RFC 2822 date format}[rdoc-ref:language/strftime_formatting.rdoc@RFC+2822+Format]:
4843
5060
  *
4844
5061
  * d = Date.new(2001, 2, 3)
4845
5062
  * s = d.rfc2822 # => "Sat, 3 Feb 2001 00:00:00 +0000"
@@ -4847,7 +5064,7 @@ date_s__rfc2822(int argc, VALUE *argv, VALUE klass)
4847
5064
  *
4848
5065
  * See:
4849
5066
  *
4850
- * - Argument {start}[rdoc-ref:calendars.rdoc@Argument+start].
5067
+ * - Argument {start}[rdoc-ref:language/calendars.rdoc@Argument+start].
4851
5068
  * - Argument {limit}[rdoc-ref:Date@Argument+limit].
4852
5069
  *
4853
5070
  * Related: Date._rfc2822 (returns a hash).
@@ -4881,7 +5098,7 @@ date_s_rfc2822(int argc, VALUE *argv, VALUE klass)
4881
5098
  * Date._httpdate(string, limit: 128) -> hash
4882
5099
  *
4883
5100
  * Returns a hash of values parsed from +string+, which should be a valid
4884
- * {HTTP date format}[https://docs.ruby-lang.org/en/master/strftime_formatting_rdoc.html#label-HTTP+Format]:
5101
+ * {HTTP date format}[rdoc-ref:language/strftime_formatting.rdoc@HTTP+Format]:
4885
5102
  *
4886
5103
  * d = Date.new(2001, 2, 3)
4887
5104
  * s = d.httpdate # => "Sat, 03 Feb 2001 00:00:00 GMT"
@@ -4896,7 +5113,7 @@ date_s__httpdate(int argc, VALUE *argv, VALUE klass)
4896
5113
  VALUE str, opt;
4897
5114
 
4898
5115
  rb_scan_args(argc, argv, "1:", &str, &opt);
4899
- check_limit(str, opt);
5116
+ if (!NIL_P(str)) str = check_limit(str, opt);
4900
5117
 
4901
5118
  return date__httpdate(str);
4902
5119
  }
@@ -4907,7 +5124,7 @@ date_s__httpdate(int argc, VALUE *argv, VALUE klass)
4907
5124
  *
4908
5125
  * Returns a new \Date object with values parsed from +string+,
4909
5126
  * which should be a valid
4910
- * {HTTP date format}[https://docs.ruby-lang.org/en/master/strftime_formatting_rdoc.html#label-HTTP+Format]:
5127
+ * {HTTP date format}[rdoc-ref:language/strftime_formatting.rdoc@HTTP+Format]:
4911
5128
  *
4912
5129
  * d = Date.new(2001, 2, 3)
4913
5130
  s = d.httpdate # => "Sat, 03 Feb 2001 00:00:00 GMT"
@@ -4915,7 +5132,7 @@ date_s__httpdate(int argc, VALUE *argv, VALUE klass)
4915
5132
  *
4916
5133
  * See:
4917
5134
  *
4918
- * - Argument {start}[rdoc-ref:calendars.rdoc@Argument+start].
5135
+ * - Argument {start}[rdoc-ref:language/calendars.rdoc@Argument+start].
4919
5136
  * - Argument {limit}[rdoc-ref:Date@Argument+limit].
4920
5137
  *
4921
5138
  * Related: Date._httpdate (returns a hash).
@@ -4949,7 +5166,7 @@ date_s_httpdate(int argc, VALUE *argv, VALUE klass)
4949
5166
  * Date._jisx0301(string, limit: 128) -> hash
4950
5167
  *
4951
5168
  * Returns a hash of values parsed from +string+, which should be a valid
4952
- * {JIS X 0301 date format}[https://docs.ruby-lang.org/en/master/strftime_formatting_rdoc.html#label-JIS+X+0301+Format]:
5169
+ * {JIS X 0301 date format}[rdoc-ref:language/strftime_formatting.rdoc@JIS+X+0301+Format]:
4953
5170
  *
4954
5171
  * d = Date.new(2001, 2, 3)
4955
5172
  * s = d.jisx0301 # => "H13.02.03"
@@ -4965,7 +5182,7 @@ date_s__jisx0301(int argc, VALUE *argv, VALUE klass)
4965
5182
  VALUE str, opt;
4966
5183
 
4967
5184
  rb_scan_args(argc, argv, "1:", &str, &opt);
4968
- check_limit(str, opt);
5185
+ if (!NIL_P(str)) str = check_limit(str, opt);
4969
5186
 
4970
5187
  return date__jisx0301(str);
4971
5188
  }
@@ -4975,7 +5192,7 @@ date_s__jisx0301(int argc, VALUE *argv, VALUE klass)
4975
5192
  * Date.jisx0301(string = '-4712-01-01', start = Date::ITALY, limit: 128) -> date
4976
5193
  *
4977
5194
  * Returns a new \Date object with values parsed from +string+,
4978
- * which should be a valid {JIS X 0301 format}[https://docs.ruby-lang.org/en/master/strftime_formatting_rdoc.html#label-JIS+X+0301+Format]:
5195
+ * which should be a valid {JIS X 0301 format}[rdoc-ref:language/strftime_formatting.rdoc@JIS+X+0301+Format]:
4979
5196
  *
4980
5197
  * d = Date.new(2001, 2, 3)
4981
5198
  * s = d.jisx0301 # => "H13.02.03"
@@ -4987,7 +5204,7 @@ date_s__jisx0301(int argc, VALUE *argv, VALUE klass)
4987
5204
  *
4988
5205
  * See:
4989
5206
  *
4990
- * - Argument {start}[rdoc-ref:calendars.rdoc@Argument+start].
5207
+ * - Argument {start}[rdoc-ref:language/calendars.rdoc@Argument+start].
4991
5208
  * - Argument {limit}[rdoc-ref:Date@Argument+limit].
4992
5209
  *
4993
5210
  * Related: Date._jisx0301 (returns a hash).
@@ -5745,7 +5962,7 @@ d_lite_leap_p(VALUE self)
5745
5962
  * Date.new(2001, 2, 3, Date::GREGORIAN).start # => -Infinity
5746
5963
  * Date.new(2001, 2, 3, Date::JULIAN).start # => Infinity
5747
5964
  *
5748
- * See argument {start}[rdoc-ref:calendars.rdoc@Argument+start].
5965
+ * See argument {start}[rdoc-ref:language/calendars.rdoc@Argument+start].
5749
5966
  *
5750
5967
  */
5751
5968
  static VALUE
@@ -5811,7 +6028,7 @@ dup_obj_with_new_start(VALUE obj, double sg)
5811
6028
 
5812
6029
  /*
5813
6030
  * call-seq:
5814
- * new_start(start = Date::ITALY]) -> new_date
6031
+ * new_start(start = Date::ITALY) -> new_date
5815
6032
  *
5816
6033
  * Returns a copy of +self+ with the given +start+ value:
5817
6034
  *
@@ -5820,7 +6037,7 @@ dup_obj_with_new_start(VALUE obj, double sg)
5820
6037
  * d1 = d0.new_start(Date::JULIAN)
5821
6038
  * d1.julian? # => true
5822
6039
  *
5823
- * See argument {start}[rdoc-ref:calendars.rdoc@Argument+start].
6040
+ * See argument {start}[rdoc-ref:language/calendars.rdoc@Argument+start].
5824
6041
  *
5825
6042
  */
5826
6043
  static VALUE
@@ -6936,13 +7153,24 @@ d_lite_eql_p(VALUE self, VALUE other)
6936
7153
  static VALUE
6937
7154
  d_lite_hash(VALUE self)
6938
7155
  {
6939
- st_index_t v, h[4];
7156
+ st_index_t v, h[5];
7157
+ VALUE nth;
6940
7158
 
6941
7159
  get_d1(self);
6942
- h[0] = m_nth(dat);
6943
- h[1] = m_jd(dat);
6944
- h[2] = m_df(dat);
6945
- h[3] = m_sf(dat);
7160
+ nth = m_nth(dat);
7161
+
7162
+ if (FIXNUM_P(nth)) {
7163
+ h[0] = 0;
7164
+ h[1] = (st_index_t)nth;
7165
+ } else {
7166
+ h[0] = 1;
7167
+ h[1] = (st_index_t)FIX2LONG(rb_hash(nth));
7168
+ }
7169
+
7170
+ h[2] = m_jd(dat);
7171
+ h[3] = m_df(dat);
7172
+ h[4] = m_sf(dat);
7173
+
6946
7174
  v = rb_memhash(h, sizeof(h));
6947
7175
  return ST2FIX(v);
6948
7176
  }
@@ -6957,7 +7185,7 @@ static VALUE strftimev(const char *, VALUE,
6957
7185
  * to_s -> string
6958
7186
  *
6959
7187
  * Returns a string representation of the date in +self+
6960
- * in {ISO 8601 extended date format}[https://docs.ruby-lang.org/en/master/strftime_formatting_rdoc.html#label-ISO+8601+Format+Specifications]
7188
+ * in {ISO 8601 extended date format}[rdoc-ref:language/strftime_formatting.rdoc@ISO+8601+Format+Specifications]
6961
7189
  * (<tt>'%Y-%m-%d'</tt>):
6962
7190
  *
6963
7191
  * Date.new(2001, 2, 3).to_s # => "2001-02-03"
@@ -7238,7 +7466,7 @@ date_strftime_internal(int argc, VALUE *argv, VALUE self,
7238
7466
  * Date.new(2001, 2, 3).strftime # => "2001-02-03"
7239
7467
  *
7240
7468
  * For other formats, see
7241
- * {Formats for Dates and Times}[https://docs.ruby-lang.org/en/master/strftime_formatting_rdoc.html].
7469
+ * {Formats for Dates and Times}[rdoc-ref:language/strftime_formatting.rdoc].
7242
7470
  *
7243
7471
  */
7244
7472
  static VALUE
@@ -7270,12 +7498,12 @@ strftimev(const char *fmt, VALUE self,
7270
7498
  * asctime -> string
7271
7499
  *
7272
7500
  * Equivalent to #strftime with argument <tt>'%a %b %e %T %Y'</tt>
7273
- * (or its {shorthand form}[https://docs.ruby-lang.org/en/master/strftime_formatting_rdoc.html#label-Shorthand+Conversion+Specifiers]
7501
+ * (or its {shorthand form}[rdoc-ref:language/strftime_formatting.rdoc@Shorthand+Conversion+Specifiers]
7274
7502
  * <tt>'%c'</tt>):
7275
7503
  *
7276
7504
  * Date.new(2001, 2, 3).asctime # => "Sat Feb 3 00:00:00 2001"
7277
7505
  *
7278
- * See {asctime}[https://linux.die.net/man/3/asctime].
7506
+ * See {asctime}[https://man7.org/linux/man-pages/man3/asctime.3p.html].
7279
7507
  *
7280
7508
  */
7281
7509
  static VALUE
@@ -7289,7 +7517,7 @@ d_lite_asctime(VALUE self)
7289
7517
  * iso8601 -> string
7290
7518
  *
7291
7519
  * Equivalent to #strftime with argument <tt>'%Y-%m-%d'</tt>
7292
- * (or its {shorthand form}[https://docs.ruby-lang.org/en/master/strftime_formatting_rdoc.html#label-Shorthand+Conversion+Specifiers]
7520
+ * (or its {shorthand form}[rdoc-ref:language/strftime_formatting.rdoc@Shorthand+Conversion+Specifiers]
7293
7521
  * <tt>'%F'</tt>);
7294
7522
  *
7295
7523
  * Date.new(2001, 2, 3).iso8601 # => "2001-02-03"
@@ -7306,7 +7534,7 @@ d_lite_iso8601(VALUE self)
7306
7534
  * rfc3339 -> string
7307
7535
  *
7308
7536
  * Equivalent to #strftime with argument <tt>'%FT%T%:z'</tt>;
7309
- * see {Formats for Dates and Times}[https://docs.ruby-lang.org/en/master/strftime_formatting_rdoc.html]:
7537
+ * see {Formats for Dates and Times}[rdoc-ref:language/strftime_formatting.rdoc]:
7310
7538
  *
7311
7539
  * Date.new(2001, 2, 3).rfc3339 # => "2001-02-03T00:00:00+00:00"
7312
7540
  *
@@ -7322,7 +7550,7 @@ d_lite_rfc3339(VALUE self)
7322
7550
  * rfc2822 -> string
7323
7551
  *
7324
7552
  * Equivalent to #strftime with argument <tt>'%a, %-d %b %Y %T %z'</tt>;
7325
- * see {Formats for Dates and Times}[https://docs.ruby-lang.org/en/master/strftime_formatting_rdoc.html]:
7553
+ * see {Formats for Dates and Times}[rdoc-ref:language/strftime_formatting.rdoc]:
7326
7554
  *
7327
7555
  * Date.new(2001, 2, 3).rfc2822 # => "Sat, 3 Feb 2001 00:00:00 +0000"
7328
7556
  *
@@ -7338,7 +7566,7 @@ d_lite_rfc2822(VALUE self)
7338
7566
  * httpdate -> string
7339
7567
  *
7340
7568
  * Equivalent to #strftime with argument <tt>'%a, %d %b %Y %T GMT'</tt>;
7341
- * see {Formats for Dates and Times}[https://docs.ruby-lang.org/en/master/strftime_formatting_rdoc.html]:
7569
+ * see {Formats for Dates and Times}[rdoc-ref:language/strftime_formatting.rdoc]:
7342
7570
  *
7343
7571
  * Date.new(2001, 2, 3).httpdate # => "Sat, 03 Feb 2001 00:00:00 GMT"
7344
7572
  *
@@ -7517,10 +7745,7 @@ d_lite_marshal_dump_old(VALUE self)
7517
7745
  m_of_in_day(dat),
7518
7746
  DBL2NUM(m_sg(dat)));
7519
7747
 
7520
- if (FL_TEST(self, FL_EXIVAR)) {
7521
- rb_copy_generic_ivar(a, self);
7522
- FL_SET(a, FL_EXIVAR);
7523
- }
7748
+ rb_copy_generic_ivar(a, self);
7524
7749
 
7525
7750
  return a;
7526
7751
  }
@@ -7542,10 +7767,8 @@ d_lite_marshal_dump(VALUE self)
7542
7767
  INT2FIX(m_of(dat)),
7543
7768
  DBL2NUM(m_sg(dat)));
7544
7769
 
7545
- if (FL_TEST(self, FL_EXIVAR)) {
7546
- rb_copy_generic_ivar(a, self);
7547
- FL_SET(a, FL_EXIVAR);
7548
- }
7770
+
7771
+ rb_copy_generic_ivar(a, self);
7549
7772
 
7550
7773
  return a;
7551
7774
  }
@@ -7606,8 +7829,6 @@ d_lite_marshal_load(VALUE self, VALUE a)
7606
7829
  if (simple_dat_p(dat)) {
7607
7830
  if (df || !f_zero_p(sf) || of) {
7608
7831
  /* loading a fractional date; promote to complex */
7609
- dat = ruby_xrealloc(dat, sizeof(struct ComplexDateData));
7610
- RTYPEDDATA(self)->data = dat;
7611
7832
  goto complex_data;
7612
7833
  }
7613
7834
  set_to_simple(self, &dat->s, nth, jd, sg, 0, 0, 0, HAVE_JD);
@@ -7618,10 +7839,7 @@ d_lite_marshal_load(VALUE self, VALUE a)
7618
7839
  HAVE_JD | HAVE_DF);
7619
7840
  }
7620
7841
 
7621
- if (FL_TEST(a, FL_EXIVAR)) {
7622
- rb_copy_generic_ivar(self, a);
7623
- FL_SET(self, FL_EXIVAR);
7624
- }
7842
+ rb_copy_generic_ivar(self, a);
7625
7843
 
7626
7844
  return self;
7627
7845
  }
@@ -8715,7 +8933,7 @@ dt_lite_to_s(VALUE self)
8715
8933
  * DateTime.now.strftime # => "2022-07-01T11:03:19-05:00"
8716
8934
  *
8717
8935
  * For other formats,
8718
- * see {Formats for Dates and Times}[https://docs.ruby-lang.org/en/master/strftime_formatting_rdoc.html]:
8936
+ * see {Formats for Dates and Times}[rdoc-ref:language/strftime_formatting.rdoc]:
8719
8937
  *
8720
8938
  */
8721
8939
  static VALUE
@@ -9494,6 +9712,7 @@ Init_date_core(void)
9494
9712
  sym_zone = ID2SYM(rb_intern_const("zone"));
9495
9713
 
9496
9714
  half_days_in_day = rb_rational_new2(INT2FIX(1), INT2FIX(2));
9715
+ rb_gc_register_mark_object(half_days_in_day);
9497
9716
 
9498
9717
  #if (LONG_MAX / DAY_IN_SECONDS) > SECOND_IN_NANOSECONDS
9499
9718
  day_in_nanoseconds = LONG2NUM((long)DAY_IN_SECONDS *
@@ -9505,8 +9724,6 @@ Init_date_core(void)
9505
9724
  day_in_nanoseconds = f_mul(INT2FIX(DAY_IN_SECONDS),
9506
9725
  INT2FIX(SECOND_IN_NANOSECONDS));
9507
9726
  #endif
9508
-
9509
- rb_gc_register_mark_object(half_days_in_day);
9510
9727
  rb_gc_register_mark_object(day_in_nanoseconds);
9511
9728
 
9512
9729
  positive_inf = +INFINITY;
@@ -9517,12 +9734,12 @@ Init_date_core(void)
9517
9734
  * calendar dates.
9518
9735
  *
9519
9736
  * Consider using
9520
- * {class Time}[https://docs.ruby-lang.org/en/master/Time.html]
9737
+ * {class Time}[rdoc-ref:Time]
9521
9738
  * instead of class \Date if:
9522
9739
  *
9523
9740
  * - You need both dates and times; \Date handles only dates.
9524
9741
  * - You need only Gregorian dates (and not Julian dates);
9525
- * see {Julian and Gregorian Calendars}[rdoc-ref:calendars.rdoc].
9742
+ * see {Julian and Gregorian Calendars}[rdoc-ref:language/calendars.rdoc].
9526
9743
  *
9527
9744
  * A \Date object, once created, is immutable, and cannot be modified.
9528
9745
  *
@@ -9569,7 +9786,7 @@ Init_date_core(void)
9569
9786
  * Date.strptime('fri31dec99', '%a%d%b%y') # => #<Date: 1999-12-31>
9570
9787
  *
9571
9788
  * See also the specialized methods in
9572
- * {"Specialized Format Strings" in Formats for Dates and Times}[https://docs.ruby-lang.org/en/master/strftime_formatting_rdoc.html#label-Specialized+Format+Strings]
9789
+ * {"Specialized Format Strings" in Formats for Dates and Times}[rdoc-ref:language/strftime_formatting.rdoc@Specialized+Format+Strings]
9573
9790
  *
9574
9791
  * == Argument +limit+
9575
9792
  *