10 #if defined(HAVE_SYS_TIME_H) 18 #include RUBY_EXTCONF_H 23 static ID id_cmp, id_le_p, id_ge_p, id_eqeq_p;
24 static VALUE cDate, cDateTime;
25 static VALUE half_days_in_day, day_in_nanoseconds;
26 static double positive_inf, negative_inf;
28 #define f_boolcast(x) ((x) ? Qtrue : Qfalse) 30 #define f_abs(x) rb_funcall(x, rb_intern("abs"), 0) 31 #define f_negate(x) rb_funcall(x, rb_intern("-@"), 0) 32 #define f_add(x,y) rb_funcall(x, '+', 1, y) 33 #define f_sub(x,y) rb_funcall(x, '-', 1, y) 34 #define f_mul(x,y) rb_funcall(x, '*', 1, y) 35 #define f_div(x,y) rb_funcall(x, '/', 1, y) 36 #define f_quo(x,y) rb_funcall(x, rb_intern("quo"), 1, y) 37 #define f_idiv(x,y) rb_funcall(x, rb_intern("div"), 1, y) 38 #define f_mod(x,y) rb_funcall(x, '%', 1, y) 39 #define f_remainder(x,y) rb_funcall(x, rb_intern("remainder"), 1, y) 40 #define f_expt(x,y) rb_funcall(x, rb_intern("**"), 1, y) 41 #define f_floor(x) rb_funcall(x, rb_intern("floor"), 0) 42 #define f_ceil(x) rb_funcall(x, rb_intern("ceil"), 0) 43 #define f_truncate(x) rb_funcall(x, rb_intern("truncate"), 0) 44 #define f_round(x) rb_funcall(x, rb_intern("round"), 0) 46 #define f_to_i(x) rb_funcall(x, rb_intern("to_i"), 0) 47 #define f_to_r(x) rb_funcall(x, rb_intern("to_r"), 0) 48 #define f_to_s(x) rb_funcall(x, rb_intern("to_s"), 0) 49 #define f_inspect(x) rb_funcall(x, rb_intern("inspect"), 0) 51 #define f_add3(x,y,z) f_add(f_add(x, y), z) 52 #define f_sub3(x,y,z) f_sub(f_sub(x, y), z) 125 #define f_nonzero_p(x) (!f_zero_p(x)) 128 f_negative_p(
VALUE x)
135 #define f_positive_p(x) (!f_negative_p(x)) 137 #define f_ajd(x) rb_funcall(x, rb_intern("ajd"), 0) 138 #define f_jd(x) rb_funcall(x, rb_intern("jd"), 0) 139 #define f_year(x) rb_funcall(x, rb_intern("year"), 0) 140 #define f_mon(x) rb_funcall(x, rb_intern("mon"), 0) 141 #define f_mday(x) rb_funcall(x, rb_intern("mday"), 0) 142 #define f_wday(x) rb_funcall(x, rb_intern("wday"), 0) 143 #define f_hour(x) rb_funcall(x, rb_intern("hour"), 0) 144 #define f_min(x) rb_funcall(x, rb_intern("min"), 0) 145 #define f_sec(x) rb_funcall(x, rb_intern("sec"), 0) 148 #define NDIV(x,y) (-(-((x)+1)/(y))-1) 149 #define NMOD(x,y) ((y)-(-((x)+1)%(y))-1) 150 #define DIV(n,d) ((n)<0 ? NDIV((n),(d)) : (n)/(d)) 151 #define MOD(n,d) ((n)<0 ? NMOD((n),(d)) : (n)%(d)) 153 #define HAVE_JD (1 << 0) 154 #define HAVE_DF (1 << 1) 155 #define HAVE_CIVIL (1 << 2) 156 #define HAVE_TIME (1 << 3) 157 #define COMPLEX_DAT (1 << 7) 159 #define have_jd_p(x) ((x)->flags & HAVE_JD) 160 #define have_df_p(x) ((x)->flags & HAVE_DF) 161 #define have_civil_p(x) ((x)->flags & HAVE_CIVIL) 162 #define have_time_p(x) ((x)->flags & HAVE_TIME) 163 #define complex_dat_p(x) ((x)->flags & COMPLEX_DAT) 164 #define simple_dat_p(x) (!complex_dat_p(x)) 166 #define ITALY 2299161 167 #define ENGLAND 2361222 168 #define JULIAN positive_inf 169 #define GREGORIAN negative_inf 170 #define DEFAULT_SG ITALY 172 #define UNIX_EPOCH_IN_CJD INT2FIX(2440588) 174 #define MINUTE_IN_SECONDS 60 175 #define HOUR_IN_SECONDS 3600 176 #define DAY_IN_SECONDS 86400 177 #define SECOND_IN_MILLISECONDS 1000 178 #define SECOND_IN_NANOSECONDS 1000000000 180 #define JC_PERIOD0 1461 181 #define GC_PERIOD0 146097 182 #define CM_PERIOD0 71149239 183 #define CM_PERIOD (0xfffffff / CM_PERIOD0 * CM_PERIOD0) 184 #define CM_PERIOD_JCY (CM_PERIOD / JC_PERIOD0 * 4) 185 #define CM_PERIOD_GCY (CM_PERIOD / GC_PERIOD0 * 400) 187 #define REFORM_BEGIN_YEAR 1582 188 #define REFORM_END_YEAR 1930 189 #define REFORM_BEGIN_JD 2298874 190 #define REFORM_END_JD 2426355 200 #define MIN_SHIFT SEC_WIDTH 201 #define HOUR_SHIFT (MIN_WIDTH + SEC_WIDTH) 202 #define MDAY_SHIFT (HOUR_WIDTH + MIN_WIDTH + SEC_WIDTH) 203 #define MON_SHIFT (MDAY_WIDTH + HOUR_WIDTH + MIN_WIDTH + SEC_WIDTH) 205 #define PK_MASK(x) ((1 << (x)) - 1) 207 #define EX_SEC(x) (((x) >> SEC_SHIFT) & PK_MASK(SEC_WIDTH)) 208 #define EX_MIN(x) (((x) >> MIN_SHIFT) & PK_MASK(MIN_WIDTH)) 209 #define EX_HOUR(x) (((x) >> HOUR_SHIFT) & PK_MASK(HOUR_WIDTH)) 210 #define EX_MDAY(x) (((x) >> MDAY_SHIFT) & PK_MASK(MDAY_WIDTH)) 211 #define EX_MON(x) (((x) >> MON_SHIFT) & PK_MASK(MON_WIDTH)) 213 #define PACK5(m,d,h,min,s) \ 214 (((m) << MON_SHIFT) | ((d) << MDAY_SHIFT) |\ 215 ((h) << HOUR_SHIFT) | ((min) << MIN_SHIFT) | ((s) << SEC_SHIFT)) 218 (((m) << MON_SHIFT) | ((d) << MDAY_SHIFT)) 225 #if defined(FLT_RADIX) && defined(FLT_MANT_DIG) && FLT_RADIX == 2 && FLT_MANT_DIG > 22 226 #define date_sg_t float 228 #define date_sg_t double 289 union DateData *dat;\ 290 TypedData_Get_Struct(x, union DateData, &d_lite_type, dat); 293 union DateData *adat;\ 294 TypedData_Get_Struct(x, union DateData, &d_lite_type, adat); 297 union DateData *bdat;\ 298 TypedData_Get_Struct(x, union DateData, &d_lite_type, bdat); 301 union DateData *adat, *bdat;\ 302 TypedData_Get_Struct(x, union DateData, &d_lite_type, adat);\ 303 TypedData_Get_Struct(y, union DateData, &d_lite_type, bdat); 317 #define set_to_simple(obj, x, _nth, _jd ,_sg, _year, _mon, _mday, _flags) \ 319 RB_OBJ_WRITE((obj), &(x)->nth, canon(_nth)); \ 321 (x)->sg = (date_sg_t)(_sg);\ 325 (x)->flags = _flags;\ 328 #define set_to_simple(obj, x, _nth, _jd ,_sg, _year, _mon, _mday, _flags) \ 330 RB_OBJ_WRITE((obj), &(x)->nth, canon(_nth)); \ 332 (x)->sg = (date_sg_t)(_sg);\ 334 (x)->pc = PACK2(_mon, _mday);\ 335 (x)->flags = _flags;\ 340 #define set_to_complex(obj, x, _nth, _jd ,_df, _sf, _of, _sg,\ 341 _year, _mon, _mday, _hour, _min, _sec, _flags) \ 343 RB_OBJ_WRITE((obj), &(x)->nth, canon(_nth));\ 346 RB_OBJ_WRITE((obj), &(x)->sf, canon(_sf));\ 348 (x)->sg = (date_sg_t)(_sg);\ 355 (x)->flags = _flags;\ 358 #define set_to_complex(obj, x, _nth, _jd ,_df, _sf, _of, _sg,\ 359 _year, _mon, _mday, _hour, _min, _sec, _flags) \ 361 RB_OBJ_WRITE((obj), &(x)->nth, canon(_nth));\ 364 RB_OBJ_WRITE((obj), &(x)->sf, canon(_sf));\ 366 (x)->sg = (date_sg_t)(_sg);\ 368 (x)->pc = PACK5(_mon, _mday, _hour, _min, _sec);\ 369 (x)->flags = _flags;\ 374 #define copy_simple_to_complex(obj, x, y) \ 376 RB_OBJ_WRITE((obj), &(x)->nth, (y)->nth);\ 379 (x)->sf = INT2FIX(0);\ 381 (x)->sg = (date_sg_t)((y)->sg);\ 382 (x)->year = (y)->year;\ 383 (x)->mon = (y)->mon;\ 384 (x)->mday = (y)->mday;\ 388 (x)->flags = (y)->flags;\ 391 #define copy_simple_to_complex(obj, x, y) \ 393 RB_OBJ_WRITE((obj), &(x)->nth, (y)->nth);\ 396 RB_OBJ_WRITE((obj), &(x)->sf, INT2FIX(0));\ 398 (x)->sg = (date_sg_t)((y)->sg);\ 399 (x)->year = (y)->year;\ 400 (x)->pc = PACK5(EX_MON((y)->pc), EX_MDAY((y)->pc), 0, 0, 0);\ 401 (x)->flags = (y)->flags;\ 406 #define copy_complex_to_simple(obj, x, y) \ 408 RB_OBJ_WRITE((obj), &(x)->nth, (y)->nth);\ 410 (x)->sg = (date_sg_t)((y)->sg);\ 411 (x)->year = (y)->year;\ 412 (x)->mon = (y)->mon;\ 413 (x)->mday = (y)->mday;\ 414 (x)->flags = (y)->flags;\ 417 #define copy_complex_to_simple(obj, x, y) \ 419 RB_OBJ_WRITE((obj), &(x)->nth, (y)->nth);\ 421 (x)->sg = (date_sg_t)((y)->sg);\ 422 (x)->year = (y)->year;\ 423 (x)->pc = PACK2(EX_MON((y)->pc), EX_MDAY((y)->pc));\ 424 (x)->flags = (y)->flags;\ 430 static int c_valid_civil_p(
int,
int,
int,
double,
431 int *,
int *,
int *,
int *);
434 c_find_fdoy(
int y,
double sg,
int *rjd,
int *ns)
438 for (d = 1; d < 31; d++)
439 if (c_valid_civil_p(y, 1, d, sg, &rm, &rd, rjd, ns))
445 c_find_ldoy(
int y,
double sg,
int *rjd,
int *ns)
449 for (i = 0; i < 30; i++)
450 if (c_valid_civil_p(y, 12, 31 - i, sg, &rm, &rd, rjd, ns))
457 c_find_fdom(
int y,
int m,
double sg,
int *rjd,
int *ns)
461 for (d = 1; d < 31; d++)
462 if (c_valid_civil_p(y, m, d, sg, &rm, &rd, rjd, ns))
469 c_find_ldom(
int y,
int m,
double sg,
int *rjd,
int *ns)
473 for (i = 0; i < 30; i++)
474 if (c_valid_civil_p(y, m, 31 - i, sg, &rm, &rd, rjd, ns))
480 c_civil_to_jd(
int y,
int m,
int d,
double sg,
int *rjd,
int *ns)
488 a = floor(y / 100.0);
489 b = 2 - a + floor(a / 4.0);
490 jd = floor(365.25 * (y + 4716)) +
491 floor(30.6001 * (m + 1)) +
504 c_jd_to_civil(
int jd,
double sg,
int *ry,
int *rm,
int *rdom)
506 double x, a, b, c, d, e, y, m, dom;
511 x = floor((jd - 1867216.25) / 36524.25);
512 a = jd + 1 + x - floor(x / 4.0);
515 c = floor((b - 122.1) / 365.25);
516 d = floor(365.25 * c);
517 e = floor((b - d) / 30.6001);
518 dom = b - d - floor(30.6001 * e);
534 c_ordinal_to_jd(
int y,
int d,
double sg,
int *rjd,
int *ns)
538 c_find_fdoy(y, sg, rjd, &ns2);
540 *ns = (*rjd <
sg) ? 0 : 1;
544 c_jd_to_ordinal(
int jd,
double sg,
int *ry,
int *rd)
546 int rm2, rd2, rjd, ns;
548 c_jd_to_civil(jd, sg, ry, &rm2, &rd2);
549 c_find_fdoy(*ry, sg, &rjd, &ns);
550 *rd = (jd - rjd) + 1;
554 c_commercial_to_jd(
int y,
int w,
int d,
double sg,
int *rjd,
int *ns)
558 c_find_fdoy(y, sg, &rjd2, &ns2);
561 (rjd2 -
MOD((rjd2 - 1) + 1, 7)) +
564 *ns = (*rjd <
sg) ? 0 : 1;
568 c_jd_to_commercial(
int jd,
double sg,
int *ry,
int *rw,
int *rd)
570 int ry2, rm2, rd2, a, rjd2, ns2;
572 c_jd_to_civil(jd - 3, sg, &ry2, &rm2, &rd2);
574 c_commercial_to_jd(a + 1, 1, 1, sg, &rjd2, &ns2);
578 c_commercial_to_jd(a, 1, 1, sg, &rjd2, &ns2);
581 *rw = 1 +
DIV(jd - rjd2, 7);
582 *rd =
MOD(jd + 1, 7);
588 c_weeknum_to_jd(
int y,
int w,
int d,
int f,
double sg,
int *rjd,
int *ns)
592 c_find_fdoy(y, sg, &rjd2, &ns2);
594 *rjd = (rjd2 -
MOD(((rjd2 - f) + 1), 7) - 7) + 7 * w + d;
595 *ns = (*rjd <
sg) ? 0 : 1;
599 c_jd_to_weeknum(
int jd,
int f,
double sg,
int *ry,
int *rw,
int *rd)
601 int rm, rd2, rjd, ns, j;
603 c_jd_to_civil(jd, sg, ry, &rm, &rd2);
604 c_find_fdoy(*ry, sg, &rjd, &ns);
606 j = jd - (rjd -
MOD((rjd - f) + 1, 7)) + 7;
607 *rw = (int)
DIV(j, 7);
608 *rd = (int)
MOD(j, 7);
613 c_nth_kday_to_jd(
int y,
int m,
int n,
int k,
double sg,
int *rjd,
int *ns)
618 c_find_fdom(y, m, sg, &rjd2, &ns2);
622 c_find_ldom(y, m, sg, &rjd2, &ns2);
625 *rjd = (rjd2 -
MOD((rjd2 - k) + 1, 7)) + 7 * n;
626 *ns = (*rjd <
sg) ? 0 : 1;
633 return MOD(jd + 1, 7);
638 c_jd_to_nth_kday(
int jd,
double sg,
int *ry,
int *rm,
int *rn,
int *rk)
642 c_jd_to_civil(jd, sg, ry, rm, &rd);
643 c_find_fdom(*ry, *rm, sg, &rjd, &ns2);
644 *rn =
DIV(jd - rjd, 7) + 1;
645 *rk = c_jd_to_wday(jd);
650 c_valid_ordinal_p(
int y,
int d,
double sg,
651 int *rd,
int *rjd,
int *ns)
658 if (!c_find_ldoy(y, sg, &rjd2, &ns2))
660 c_jd_to_ordinal(rjd2 + d + 1, sg, &ry2, &rd2);
665 c_ordinal_to_jd(y, d, sg, rjd, ns);
666 c_jd_to_ordinal(*rjd, sg, &ry2, &rd2);
667 if (ry2 != y || rd2 != d)
672 static const int monthtab[2][13] = {
673 { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
674 { 0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
678 c_julian_leap_p(
int y)
680 return MOD(y, 4) == 0;
684 c_gregorian_leap_p(
int y)
686 return (
MOD(y, 4) == 0 && y % 100 != 0) ||
MOD(y, 400) == 0;
690 c_julian_last_day_of_month(
int y,
int m)
692 assert(m >= 1 && m <= 12);
693 return monthtab[c_julian_leap_p(y) ? 1 : 0][m];
697 c_gregorian_last_day_of_month(
int y,
int m)
699 assert(m >= 1 && m <= 12);
700 return monthtab[c_gregorian_leap_p(y) ? 1 : 0][m];
704 c_valid_julian_p(
int y,
int m,
int d,
int *rm,
int *rd)
712 last = c_julian_last_day_of_month(y, m);
715 if (d < 1 || d > last)
723 c_valid_gregorian_p(
int y,
int m,
int d,
int *rm,
int *rd)
731 last = c_gregorian_last_day_of_month(y, m);
734 if (d < 1 || d > last)
742 c_valid_civil_p(
int y,
int m,
int d,
double sg,
743 int *rm,
int *rd,
int *rjd,
int *ns)
750 if (!c_find_ldom(y, m, sg, rjd, ns))
752 c_jd_to_civil(*rjd + d + 1, sg, &ry, rm, rd);
753 if (ry != y || *rm != m)
757 c_civil_to_jd(y, m, d, sg, rjd, ns);
758 c_jd_to_civil(*rjd, sg, &ry, rm, rd);
759 if (ry != y || *rm != m || *rd != d)
765 c_valid_commercial_p(
int y,
int w,
int d,
double sg,
766 int *rw,
int *rd,
int *rjd,
int *ns)
768 int ns2, ry2, rw2, rd2;
775 c_commercial_to_jd(y + 1, 1, 1, sg, &rjd2, &ns2);
776 c_jd_to_commercial(rjd2 + w * 7, sg, &ry2, &rw2, &rd2);
781 c_commercial_to_jd(y, w, d, sg, rjd, ns);
782 c_jd_to_commercial(*rjd, sg, &ry2, rw, rd);
783 if (y != ry2 || w != *rw || d != *rd)
789 c_valid_weeknum_p(
int y,
int w,
int d,
int f,
double sg,
790 int *rw,
int *rd,
int *rjd,
int *ns)
792 int ns2, ry2, rw2, rd2;
799 c_weeknum_to_jd(y + 1, 1, f, f, sg, &rjd2, &ns2);
800 c_jd_to_weeknum(rjd2 + w * 7, f, sg, &ry2, &rw2, &rd2);
805 c_weeknum_to_jd(y, w, d, f, sg, rjd, ns);
806 c_jd_to_weeknum(*rjd, f, sg, &ry2, rw, rd);
807 if (y != ry2 || w != *rw || d != *rd)
814 c_valid_nth_kday_p(
int y,
int m,
int n,
int k,
double sg,
815 int *rm,
int *rn,
int *rk,
int *rjd,
int *ns)
817 int ns2, ry2, rm2, rn2, rk2;
828 c_nth_kday_to_jd(ny, nm, 1, k, sg, &rjd2, &ns2);
829 c_jd_to_nth_kday(rjd2 + n * 7, sg, &ry2, &rm2, &rn2, &rk2);
830 if (ry2 != y || rm2 != m)
834 c_nth_kday_to_jd(y, m, n, k, sg, rjd, ns);
835 c_jd_to_nth_kday(*rjd, sg, &ry2, rm, rn, rk);
836 if (y != ry2 || m != *rm || n != *rn || k != *rk)
843 c_valid_time_p(
int h,
int min,
int s,
int *rh,
int *rmin,
int *rs)
854 return !(h < 0 || h > 24 ||
855 min < 0 || min > 59 ||
857 (h == 24 && (min > 0 || s > 0)));
861 c_valid_start_p(
double sg)
873 df_local_to_utc(
int df,
int of)
884 df_utc_to_local(
int df,
int of)
895 jd_local_to_utc(
int jd,
int df,
int of)
906 jd_utc_to_local(
int jd,
int df,
int of)
917 time_to_df(
int h,
int min,
int s)
923 df_to_time(
int df,
int *h,
int *min,
int *s)
950 return f_quo(n, day_in_nanoseconds);
980 safe_mul_p(
VALUE x,
long m)
1010 return f_mul(d, day_in_nanoseconds);
1049 VALUE s = day_to_sec(d);
1060 VALUE n = sec_to_ns(s);
1073 *jd = div_day(d, &f);
1074 *df = div_df(f, &f);
1078 inline static double 1083 if (f_zero_p(x->
s.
nth))
1085 else if (f_negative_p(x->
s.
nth))
1086 return positive_inf;
1087 return negative_inf;
1090 inline static double 1095 if (f_zero_p(x->
c.
nth))
1097 else if (f_negative_p(x->
c.
nth))
1098 return positive_inf;
1099 return negative_inf;
1102 inline static double 1106 return s_virtual_sg(x);
1108 return c_virtual_sg(x);
1111 #define canonicalize_jd(_nth, _jd) \ 1114 _nth = f_sub(_nth, INT2FIX(1));\ 1117 if (_jd >= CM_PERIOD) {\ 1118 _nth = f_add(_nth, INT2FIX(1));\ 1144 c_civil_to_jd(x->
s.
year, x->
s.mon, x->
s.mday,
1145 s_virtual_sg(x), &jd, &ns);
1148 s_virtual_sg(x), &jd, &ns);
1163 c_jd_to_civil(x->
s.
jd, s_virtual_sg(x), &y, &m, &d);
1182 x->
c.
df = df_local_to_utc(time_to_df(x->
c.hour, x->
c.min, x->
c.sec),
1202 r = df_utc_to_local(x->
c.
df, x->
c.
of);
1203 df_to_time(r, &x->
c.hour, &x->
c.min, &x->
c.sec);
1206 int r, m, d, h, min, s;
1211 r = df_utc_to_local(x->
c.
df, x->
c.
of);
1212 df_to_time(r, &h, &min, &s);
1240 c_civil_to_jd(x->
c.
year, x->
c.mon, x->
c.mday,
1241 c_virtual_sg(x), &jd, &ns);
1244 c_virtual_sg(x), &jd, &ns);
1249 x->
c.
jd = jd_local_to_utc(jd,
1250 time_to_df(x->
c.hour, x->
c.min, x->
c.sec),
1253 x->
c.
jd = jd_local_to_utc(jd,
1271 int jd, y, m, d, h, min, s;
1276 jd = jd_utc_to_local(x->
c.
jd, x->
c.
df, x->
c.
of);
1277 c_jd_to_civil(jd, c_virtual_sg(x), &y, &m, &d);
1298 return jd_utc_to_local(x->
c.
jd, x->
c.
df, x->
c.
of);
1306 return df_utc_to_local(x->
c.
df, x->
c.
of);
1310 decode_year(
VALUE y,
double style,
1316 period = (style < 0) ?
1326 inth =
DIV(it, ((
long)period));
1329 it =
MOD(it, ((
long)period));
1330 *ry = (int)it - 4712;
1342 encode_year(
VALUE nth,
int y,
double style,
1348 period = (style < 0) ?
1364 if (f_zero_p(*nth)) {
1374 if (f_zero_p(nth)) {
1381 inline static double 1382 guess_style(
VALUE y,
double sg)
1395 style = positive_inf;
1397 style = negative_inf;
1407 canonicalize_s_jd(obj, x);
1411 canonicalize_c_jd(obj, x);
1448 encode_jd(nth, jd, &rjd);
1475 encode_jd(nth, jd, &rjd);
1494 return isec_to_day(m_df(x));
1511 m_local_df_in_day(
union DateData *x)
1513 return isec_to_day(m_local_df(x));
1530 return ns_to_day(m_sf(x));
1537 return ns_to_sec(m_sf(x));
1551 fr = isec_to_day(df);
1553 fr =
f_add(fr, ns_to_day(sf));
1558 #define HALF_DAYS_IN_SECONDS (DAY_IN_SECONDS / 2) 1584 r =
f_add(r, isec_to_day(df));
1587 r =
f_add(r, ns_to_day(sf));
1613 r =
f_add(r, isec_to_day(df));
1616 r =
f_add(r, ns_to_day(sf));
1635 return isec_to_day(m_of(x));
1638 inline static double 1658 sg = s_virtual_sg(x);
1663 sg = c_virtual_sg(x);
1666 return sg == positive_inf;
1673 return !m_julian_p(x);
1677 m_proleptic_julian_p(
union DateData *x)
1682 if (
isinf(sg) && sg > 0)
1688 m_proleptic_gregorian_p(
union DateData *x)
1693 if (
isinf(sg) && sg < 0)
1723 encode_year(nth, year,
1724 m_gregorian_p(x) ? -1 : +1,
1771 static const int yeartab[2][13] = {
1772 { 0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 },
1773 { 0, 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335 }
1777 c_julian_to_yday(
int y,
int m,
int d)
1779 assert(m >= 1 && m <= 12);
1780 return yeartab[c_julian_leap_p(y) ? 1 : 0][m] + d;
1784 c_gregorian_to_yday(
int y,
int m,
int d)
1786 assert(m >= 1 && m <= 12);
1787 return yeartab[c_gregorian_leap_p(y) ? 1 : 0][m] + d;
1797 sg = m_virtual_sg(x);
1799 if (m_proleptic_gregorian_p(x) ||
1801 return c_gregorian_to_yday(m_year(x), m_mon(x), m_mday(x));
1802 if (m_proleptic_julian_p(x))
1803 return c_julian_to_yday(m_year(x), m_mon(x), m_mday(x));
1804 c_jd_to_ordinal(jd, sg, &ry, &rd);
1811 return c_jd_to_wday(m_local_jd(x));
1819 c_jd_to_commercial(m_local_jd(x), m_virtual_sg(x),
1836 encode_year(nth, year,
1837 m_gregorian_p(x) ? -1 : +1,
1847 c_jd_to_commercial(m_local_jd(x), m_virtual_sg(x),
1868 c_jd_to_weeknum(m_local_jd(x), f, m_virtual_sg(x),
1876 return m_wnumx(x, 0);
1882 return m_wnumx(x, 1);
1930 #define decode_offset(of,s,h,m)\ 1933 s = (of < 0) ? '-' : '+';\ 1934 a = (of < 0) ? -of : of;\ 1935 h = a / HOUR_IN_SECONDS;\ 1936 m = a % HOUR_IN_SECONDS / MINUTE_IN_SECONDS;\ 1953 return of2str(m_of(x));
1965 return f_kind_of_p(x, cDate);
1969 k_numeric_p(
VALUE x)
1975 k_rational_p(
VALUE x)
1981 expect_numeric(
VALUE x)
1983 if (!k_numeric_p(x))
1989 civil_to_jd(
VALUE y,
int m,
int d,
double sg,
1990 VALUE *nth,
int *ry,
1994 double style = guess_style(y, sg);
1999 c_civil_to_jd(
FIX2INT(y), m, d, sg, &jd, ns);
2000 decode_jd(
INT2FIX(jd), nth, rjd);
2005 decode_year(y, *ns ? -1 : +1, &nth2, ry);
2009 decode_year(y, style, nth, ry);
2010 c_civil_to_jd(*ry, m, d, style, rjd, ns);
2015 jd_to_civil(
VALUE jd,
double sg,
2016 VALUE *nth,
int *rjd,
2017 int *ry,
int *rm,
int *rd)
2019 decode_jd(jd, nth, rjd);
2020 c_jd_to_civil(*rjd, sg, ry, rm, rd);
2024 ordinal_to_jd(
VALUE y,
int d,
double sg,
2025 VALUE *nth,
int *ry,
2029 double style = guess_style(y, sg);
2034 c_ordinal_to_jd(
FIX2INT(y), d, sg, &jd, ns);
2035 decode_jd(
INT2FIX(jd), nth, rjd);
2040 decode_year(y, *ns ? -1 : +1, &nth2, ry);
2044 decode_year(y, style, nth, ry);
2045 c_ordinal_to_jd(*ry, d, style, rjd, ns);
2050 jd_to_ordinal(
VALUE jd,
double sg,
2051 VALUE *nth,
int *rjd,
2054 decode_jd(jd, nth, rjd);
2055 c_jd_to_ordinal(*rjd, sg, ry, rd);
2059 commercial_to_jd(
VALUE y,
int w,
int d,
double sg,
2060 VALUE *nth,
int *ry,
2064 double style = guess_style(y, sg);
2069 c_commercial_to_jd(
FIX2INT(y), w, d, sg, &jd, ns);
2070 decode_jd(
INT2FIX(jd), nth, rjd);
2075 decode_year(y, *ns ? -1 : +1, &nth2, ry);
2079 decode_year(y, style, nth, ry);
2080 c_commercial_to_jd(*ry, w, d, style, rjd, ns);
2085 jd_to_commercial(
VALUE jd,
double sg,
2086 VALUE *nth,
int *rjd,
2087 int *ry,
int *rw,
int *rd)
2089 decode_jd(jd, nth, rjd);
2090 c_jd_to_commercial(*rjd, sg, ry, rw, rd);
2094 weeknum_to_jd(
VALUE y,
int w,
int d,
int f,
double sg,
2095 VALUE *nth,
int *ry,
2099 double style = guess_style(y, sg);
2104 c_weeknum_to_jd(
FIX2INT(y), w, d, f, sg, &jd, ns);
2105 decode_jd(
INT2FIX(jd), nth, rjd);
2110 decode_year(y, *ns ? -1 : +1, &nth2, ry);
2114 decode_year(y, style, nth, ry);
2115 c_weeknum_to_jd(*ry, w, d, f, style, rjd, ns);
2120 jd_to_weeknum(
VALUE jd,
int f,
double sg,
2121 VALUE *nth,
int *rjd,
2122 int *ry,
int *rw,
int *rd)
2124 decode_jd(jd, nth, rjd);
2125 c_jd_to_weeknum(*rjd, f, sg, ry, rw, rd);
2129 nth_kday_to_jd(
VALUE y,
int m,
int n,
int k,
double sg,
2130 VALUE *nth,
int *ry,
2134 double style = guess_style(y, sg);
2139 c_nth_kday_to_jd(
FIX2INT(y), m, n, k, sg, &jd, ns);
2140 decode_jd(
INT2FIX(jd), nth, rjd);
2145 decode_year(y, *ns ? -1 : +1, &nth2, ry);
2149 decode_year(y, style, nth, ry);
2150 c_nth_kday_to_jd(*ry, m, n, k, style, rjd, ns);
2155 jd_to_nth_kday(
VALUE jd,
double sg,
2156 VALUE *nth,
int *rjd,
2157 int *ry,
int *rm,
int *rn,
int *rk)
2159 decode_jd(jd, nth, rjd);
2160 c_jd_to_nth_kday(*rjd, sg, ry, rm, rn, rk);
2165 valid_ordinal_p(
VALUE y,
int d,
double sg,
2166 VALUE *nth,
int *ry,
2170 double style = guess_style(y, sg);
2176 r = c_valid_ordinal_p(
FIX2INT(y), d, sg, rd, &jd, ns);
2179 decode_jd(
INT2FIX(jd), nth, rjd);
2184 decode_year(y, *ns ? -1 : +1, &nth2, ry);
2188 decode_year(y, style, nth, ry);
2189 r = c_valid_ordinal_p(*ry, d, style, rd, rjd, ns);
2195 valid_gregorian_p(
VALUE y,
int m,
int d,
2196 VALUE *nth,
int *ry,
2199 decode_year(y, -1, nth, ry);
2200 return c_valid_gregorian_p(*ry, m, d, rm, rd);
2204 valid_civil_p(
VALUE y,
int m,
int d,
double sg,
2205 VALUE *nth,
int *ry,
2206 int *rm,
int *rd,
int *rjd,
2209 double style = guess_style(y, sg);
2215 r = c_valid_civil_p(
FIX2INT(y), m, d, sg, rm, rd, &jd, ns);
2218 decode_jd(
INT2FIX(jd), nth, rjd);
2223 decode_year(y, *ns ? -1 : +1, &nth2, ry);
2227 decode_year(y, style, nth, ry);
2229 r = c_valid_gregorian_p(*ry, m, d, rm, rd);
2231 r = c_valid_julian_p(*ry, m, d, rm, rd);
2234 c_civil_to_jd(*ry, *rm, *rd, style, rjd, ns);
2240 valid_commercial_p(
VALUE y,
int w,
int d,
double sg,
2241 VALUE *nth,
int *ry,
2242 int *rw,
int *rd,
int *rjd,
2245 double style = guess_style(y, sg);
2251 r = c_valid_commercial_p(
FIX2INT(y), w, d, sg, rw, rd, &jd, ns);
2254 decode_jd(
INT2FIX(jd), nth, rjd);
2259 decode_year(y, *ns ? -1 : +1, &nth2, ry);
2263 decode_year(y, style, nth, ry);
2264 r = c_valid_commercial_p(*ry, w, d, style, rw, rd, rjd, ns);
2270 valid_weeknum_p(
VALUE y,
int w,
int d,
int f,
double sg,
2271 VALUE *nth,
int *ry,
2272 int *rw,
int *rd,
int *rjd,
2275 double style = guess_style(y, sg);
2281 r = c_valid_weeknum_p(
FIX2INT(y), w, d, f, sg, rw, rd, &jd, ns);
2284 decode_jd(
INT2FIX(jd), nth, rjd);
2289 decode_year(y, *ns ? -1 : +1, &nth2, ry);
2293 decode_year(y, style, nth, ry);
2294 r = c_valid_weeknum_p(*ry, w, d, f, style, rw, rd, rjd, ns);
2301 valid_nth_kday_p(
VALUE y,
int m,
int n,
int k,
double sg,
2302 VALUE *nth,
int *ry,
2303 int *rm,
int *rn,
int *rk,
int *rjd,
2306 double style = guess_style(y, sg);
2312 r = c_valid_nth_kday_p(
FIX2INT(y), m, n, k, sg, rm, rn, rk, &jd, ns);
2315 decode_jd(
INT2FIX(jd), nth, rjd);
2320 decode_year(y, *ns ? -1 : +1, &nth2, ry);
2324 decode_year(y, style, nth, ry);
2325 r = c_valid_nth_kday_p(*ry, m, n, k, style, rm, rn, rk, rjd, ns);
2334 offset_to_sec(
VALUE vof,
int *rof)
2336 switch (
TYPE(vof)) {
2342 if (n != -1 && n != 0 && n != 1)
2354 *rof = (int)
round(n);
2360 expect_numeric(vof);
2362 #ifdef CANONICALIZATION_FOR_MATHN 2363 if (!k_rational_p(vof))
2364 return offset_to_sec(vof, rof);
2372 vs = day_to_sec(vof);
2374 #ifdef CANONICALIZATION_FOR_MATHN 2375 if (!k_rational_p(vs)) {
2392 if (!f_eqeq_p(vn, vs))
2422 #define valid_sg(sg) \ 2424 if (!c_valid_start_p(sg)) {\ 2426 rb_warning("invalid start is ignored");\ 2453 return valid_jd_sub(2, argv2, klass, 1);
2468 date_s_valid_jd_p(
int argc,
VALUE *argv,
VALUE klass)
2481 if (
NIL_P(valid_jd_sub(2, argv2, klass, 0)))
2487 valid_civil_sub(
int argc,
VALUE *argv,
VALUE klass,
int need_jd)
2490 int m, d, ry, rm, rd;
2500 if (!need_jd && (guess_style(y, sg) < 0)) {
2501 if (!valid_gregorian_p(y, m, d,
2511 if (!valid_civil_p(y, m, d, sg,
2518 encode_jd(nth, rjd, &rjd2);
2525 date_s__valid_civil_p(
int argc,
VALUE *argv,
VALUE klass)
2527 VALUE vy, vm, vd, vsg;
2540 return valid_civil_sub(4, argv2, klass, 1);
2557 date_s_valid_civil_p(
int argc,
VALUE *argv,
VALUE klass)
2559 VALUE vy, vm, vd, vsg;
2572 if (
NIL_P(valid_civil_sub(4, argv2, klass, 0)))
2578 valid_ordinal_sub(
int argc,
VALUE *argv,
VALUE klass,
int need_jd)
2594 if (!valid_ordinal_p(y, d, sg,
2601 encode_jd(nth, rjd, &rjd2);
2608 date_s__valid_ordinal_p(
int argc,
VALUE *argv,
VALUE klass)
2622 return valid_ordinal_sub(3, argv2, klass, 1);
2638 date_s_valid_ordinal_p(
int argc,
VALUE *argv,
VALUE klass)
2652 if (
NIL_P(valid_ordinal_sub(3, argv2, klass, 0)))
2658 valid_commercial_sub(
int argc,
VALUE *argv,
VALUE klass,
int need_jd)
2661 int w, d, ry, rw, rd;
2675 if (!valid_commercial_p(y, w, d, sg,
2682 encode_jd(nth, rjd, &rjd2);
2689 date_s__valid_commercial_p(
int argc,
VALUE *argv,
VALUE klass)
2691 VALUE vy, vw, vd, vsg;
2704 return valid_commercial_sub(4, argv2, klass, 1);
2720 date_s_valid_commercial_p(
int argc,
VALUE *argv,
VALUE klass)
2722 VALUE vy, vw, vd, vsg;
2735 if (
NIL_P(valid_commercial_sub(4, argv2, klass, 0)))
2742 valid_weeknum_sub(
int argc,
VALUE *argv,
VALUE klass,
int need_jd)
2745 int w, d,
f, ry, rw, rd;
2760 if (!valid_weeknum_p(y, w, d, f, sg,
2767 encode_jd(nth, rjd, &rjd2);
2773 date_s__valid_weeknum_p(
int argc,
VALUE *argv,
VALUE klass)
2775 VALUE vy, vw, vd, vf, vsg;
2778 rb_scan_args(argc, argv,
"41", &vy, &vw, &vd, &vf, &vsg);
2789 return valid_weeknum_sub(5, argv2, klass, 1);
2793 date_s_valid_weeknum_p(
int argc,
VALUE *argv,
VALUE klass)
2795 VALUE vy, vw, vd, vf, vsg;
2798 rb_scan_args(argc, argv,
"41", &vy, &vw, &vd, &vf, &vsg);
2809 if (
NIL_P(valid_weeknum_sub(5, argv2, klass, 0)))
2815 valid_nth_kday_sub(
int argc,
VALUE *argv,
VALUE klass,
int need_jd)
2818 int m, n, k, ry, rm, rn, rk;
2831 if (!valid_nth_kday_p(y, m, n, k, sg,
2833 &rm, &rn, &rk, &rjd,
2838 encode_jd(nth, rjd, &rjd2);
2844 date_s__valid_nth_kday_p(
int argc,
VALUE *argv,
VALUE klass)
2846 VALUE vy, vm, vn, vk, vsg;
2849 rb_scan_args(argc, argv,
"41", &vy, &vm, &vn, &vk, &vsg);
2860 return valid_nth_kday_sub(5, argv2, klass, 1);
2864 date_s_valid_nth_kday_p(
int argc,
VALUE *argv,
VALUE klass)
2866 VALUE vy, vm, vn, vk, vsg;
2869 rb_scan_args(argc, argv,
"41", &vy, &vm, &vn, &vk, &vsg);
2880 if (
NIL_P(valid_nth_kday_sub(5, argv2, klass, 0)))
2908 decode_year(y, +1, &nth, &ry);
2924 date_s_gregorian_leap_p(
VALUE klass,
VALUE y)
2929 decode_year(y, -1, &nth, &ry);
2934 d_lite_gc_mark(
void *ptr)
2946 d_lite_memsize(
const void *ptr)
2960 d_simple_new_internal(
VALUE klass,
2963 int y,
int m,
int d,
2979 d_complex_new_internal(
VALUE klass,
2983 int y,
int m,
int d,
2984 int h,
int min,
int s,
3002 d_lite_s_alloc_simple(
VALUE klass)
3004 return d_simple_new_internal(klass,
3012 d_lite_s_alloc_complex(
VALUE klass)
3014 return d_complex_new_internal(klass,
3024 d_lite_s_alloc(
VALUE klass)
3026 return d_lite_s_alloc_complex(klass);
3032 int *rof,
double *rsg)
3036 decode_day(
f_add(ajd, half_days_in_day),
3041 if (!f_eqeq_p(of2, t))
3044 decode_jd(jd, rnth, rjd);
3062 if (!c_valid_start_p(*rsg)) {
3070 date_s_new_bang(
int argc,
VALUE *argv,
VALUE klass)
3087 old_to_new(ajd, of, sg,
3088 &nth, &jd, &df, &sf, &rof, &rsg);
3090 if (!df && f_zero_p(sf) && !rof)
3091 return d_simple_new_internal(klass,
3097 return d_complex_new_internal(klass,
3118 return round(d) == d;
3144 if (wholenum_p(d)) {
3155 #define jd_trunc d_trunc 3156 #define k_trunc d_trunc 3163 if (wholenum_p(h)) {
3180 if (wholenum_p(min)) {
3181 rmin = to_integer(min);
3197 if (wholenum_p(s)) {
3209 #define num2num_with_frac(s,n) \ 3211 s = s##_trunc(v##s, &fr);\ 3212 if (f_nonzero_p(fr)) {\ 3214 rb_raise(rb_eArgError, "invalid fraction");\ 3219 #define num2int_with_frac(s,n) \ 3221 s = NUM2INT(s##_trunc(v##s, &fr));\ 3222 if (f_nonzero_p(fr)) {\ 3224 rb_raise(rb_eArgError, "invalid fraction");\ 3229 #define canon24oc() \ 3233 fr2 = f_add(fr2, INT2FIX(1));\ 3237 #define add_frac() \ 3239 if (f_nonzero_p(fr2))\ 3240 ret = d_lite_plus(ret, fr2);\ 3243 #define val2sg(vsg,dsg) \ 3245 dsg = NUM2DBL(vsg);\ 3246 if (!c_valid_start_p(dsg)) {\ 3248 rb_warning("invalid start is ignored");\ 3268 date_s_jd(
int argc,
VALUE *argv,
VALUE klass)
3270 VALUE vjd, vsg,
jd, fr, fr2, ret;
3290 decode_jd(jd, &nth, &rjd);
3291 ret = d_simple_new_internal(klass,
3318 date_s_ordinal(
int argc,
VALUE *argv,
VALUE klass)
3320 VALUE vy, vd, vsg, y, fr, fr2, ret;
3342 int ry, rd, rjd, ns;
3344 if (!valid_ordinal_p(y, d, sg,
3350 ret = d_simple_new_internal(klass,
3386 date_s_civil(
int argc,
VALUE *argv,
VALUE klass)
3388 VALUE vy, vm, vd, vsg, y, fr, fr2, ret;
3411 if (guess_style(y, sg) < 0) {
3415 if (!valid_gregorian_p(y, m, d,
3420 ret = d_simple_new_internal(klass,
3428 int ry, rm, rd, rjd, ns;
3430 if (!valid_civil_p(y, m, d, sg,
3436 ret = d_simple_new_internal(klass,
3463 date_s_commercial(
int argc,
VALUE *argv,
VALUE klass)
3465 VALUE vy, vw, vd, vsg, y, fr, fr2, ret;
3490 int ry, rw, rd, rjd, ns;
3492 if (!valid_commercial_p(y, w, d, sg,
3498 ret = d_simple_new_internal(klass,
3510 date_s_weeknum(
int argc,
VALUE *argv,
VALUE klass)
3512 VALUE vy, vw, vd, vf, vsg, y, fr, fr2, ret;
3516 rb_scan_args(argc, argv,
"05", &vy, &vw, &vd, &vf, &vsg);
3540 int ry, rw, rd, rjd, ns;
3542 if (!valid_weeknum_p(y, w, d, f, sg,
3548 ret = d_simple_new_internal(klass,
3559 date_s_nth_kday(
int argc,
VALUE *argv,
VALUE klass)
3561 VALUE vy, vm, vn, vk, vsg, y, fr, fr2, ret;
3565 rb_scan_args(argc, argv,
"05", &vy, &vm, &vn, &vk, &vsg);
3589 int ry, rm, rn, rk, rjd, ns;
3591 if (!valid_nth_kday_p(y, m, n, k, sg,
3593 &rm, &rn, &rk, &rjd,
3597 ret = d_simple_new_internal(klass,
3608 #if !defined(HAVE_GMTIME_R) 3610 gmtime_r(
const time_t *t,
struct tm *tm)
3612 auto struct tm *tmp = gmtime(t);
3619 localtime_r(
const time_t *t,
struct tm *tm)
3621 auto struct tm *tmp = localtime(t);
3628 static void set_sg(
union DateData *,
double);
3639 date_s_today(
int argc,
VALUE *argv,
VALUE klass)
3657 if (!localtime_r(&t, &tm))
3660 y = tm.tm_year + 1900;
3664 decode_year(
INT2FIX(y), -1, &nth, &ry);
3666 ret = d_simple_new_internal(klass,
3678 #define set_hash0(k,v) rb_hash_aset(hash, k, v) 3679 #define ref_hash0(k) rb_hash_aref(hash, k) 3680 #define del_hash0(k) rb_hash_delete(hash, k) 3682 #define set_hash(k,v) rb_hash_aset(hash, ID2SYM(rb_intern(k)), v) 3683 #define ref_hash(k) rb_hash_aref(hash, ID2SYM(rb_intern(k))) 3684 #define del_hash(k) rb_hash_delete(hash, ID2SYM(rb_intern(k))) 3687 rt_rewrite_frags(
VALUE hash)
3692 if (!
NIL_P(seconds)) {
3693 VALUE offset, d, h, min, s, fr;
3697 seconds =
f_add(seconds, offset);
3721 #define sym(x) ID2SYM(rb_intern(x)) 3829 long i, eno = 0, idx = 0;
3862 if (k ==
sym(
"ordinal")) {
3865 d = date_s_today(0, (
VALUE *)0, cDate);
3871 else if (k ==
sym(
"civil")) {
3880 d = date_s_today(0, (
VALUE *)0, cDate);
3888 else if (k ==
sym(
"commercial")) {
3897 d = date_s_today(0, (
VALUE *)0, cDate);
3905 else if (k ==
sym(
"wday")) {
3907 d = date_s_today(0, (
VALUE *)0, cDate);
3912 else if (k ==
sym(
"wnum0")) {
3921 d = date_s_today(0, (
VALUE *)0, cDate);
3929 else if (k ==
sym(
"wnum1")) {
3938 d = date_s_today(0, (
VALUE *)0, cDate);
3948 if (g && k ==
sym(
"time")) {
3949 if (
f_le_p(klass, cDateTime)) {
3951 d = date_s_today(0, (
VALUE *)0, cDate);
3979 int ry, rd, rjd, ns;
3986 encode_jd(nth, rjd, &rjd2);
3994 int ry, rm, rd, rjd, ns;
4001 encode_jd(nth, rjd, &rjd2);
4009 int ry, rw, rd, rjd, ns;
4016 encode_jd(nth, rjd, &rjd2);
4024 int ry, rw, rd, rjd, ns;
4031 encode_jd(nth, rjd, &rjd2);
4042 VALUE jd = rt__valid_jd_p(vjd, sg);
4053 VALUE jd = rt__valid_ordinal_p(year, yday, sg);
4065 VALUE jd = rt__valid_civil_p(year, mon, mday, sg);
4085 VALUE jd = rt__valid_commercial_p(year, week, wday, sg);
4098 if (f_eqeq_p(wday,
INT2FIX(7)))
4105 VALUE jd = rt__valid_weeknum_p(year, week, wday,
INT2FIX(0), sg);
4124 VALUE jd = rt__valid_weeknum_p(year, week, wday,
INT2FIX(1), sg);
4137 if (!c_valid_start_p(
NUM2DBL(sg))) {
4150 jd = rt__valid_civil_p(
ref_hash(
"year"),
4154 hash = rt_rewrite_frags(hash);
4155 hash = rt_complete_frags(klass, hash);
4156 jd = rt__valid_date_frags_p(hash, sg);
4165 decode_jd(jd, &nth, &rjd);
4166 return d_simple_new_internal(klass,
4175 const char *fmt,
size_t flen,
VALUE hash);
4178 date_s__strptime_internal(
int argc,
VALUE *argv,
VALUE klass,
4179 const char *default_fmt)
4181 VALUE vstr, vfmt, hash;
4182 const char *str, *fmt;
4190 "string should have ASCII compatible encoding");
4195 flen =
strlen(default_fmt);
4201 "format should have ASCII compatible encoding");
4242 date_s__strptime(
int argc,
VALUE *argv,
VALUE klass)
4244 return date_s__strptime_internal(argc, argv, klass,
"%F");
4266 date_s_strptime(
int argc,
VALUE *argv,
VALUE klass)
4282 VALUE argv2[2], hash;
4286 hash = date_s__strptime(2, argv2, klass);
4287 return d_new_by_frags(klass, hash, sg);
4294 date_s__parse_internal(
int argc,
VALUE *argv,
VALUE klass)
4296 VALUE vstr, vcomp, hash;
4302 "string should have ASCII compatible encoding");
4336 date_s__parse(
int argc,
VALUE *argv,
VALUE klass)
4338 return date_s__parse_internal(argc, argv, klass);
4357 date_s_parse(
int argc,
VALUE *argv,
VALUE klass)
4373 VALUE argv2[2], hash;
4377 hash = date_s__parse(2, argv2, klass);
4378 return d_new_by_frags(klass, hash, sg);
4413 date_s_iso8601(
int argc,
VALUE *argv,
VALUE klass)
4427 VALUE hash = date_s__iso8601(klass, str);
4428 return d_new_by_frags(klass, hash, sg);
4454 date_s_rfc3339(
int argc,
VALUE *argv,
VALUE klass)
4468 VALUE hash = date_s__rfc3339(klass, str);
4469 return d_new_by_frags(klass, hash, sg);
4495 date_s_xmlschema(
int argc,
VALUE *argv,
VALUE klass)
4509 VALUE hash = date_s__xmlschema(klass, str);
4510 return d_new_by_frags(klass, hash, sg);
4539 date_s_rfc2822(
int argc,
VALUE *argv,
VALUE klass)
4547 str =
rb_str_new2(
"Mon, 1 Jan -4712 00:00:00 +0000");
4553 VALUE hash = date_s__rfc2822(klass, str);
4554 return d_new_by_frags(klass, hash, sg);
4581 date_s_httpdate(
int argc,
VALUE *argv,
VALUE klass)
4589 str =
rb_str_new2(
"Mon, 01 Jan -4712 00:00:00 GMT");
4595 VALUE hash = date_s__httpdate(klass, str);
4596 return d_new_by_frags(klass, hash, sg);
4622 date_s_jisx0301(
int argc,
VALUE *argv,
VALUE klass)
4636 VALUE hash = date_s__jisx0301(klass, str);
4637 return d_new_by_frags(klass, hash, sg);
4668 dup_obj_as_complex(
VALUE self)
4693 #define val2off(vof,iof) \ 4695 if (!offset_to_sec(vof, &iof)) {\ 4697 rb_warning("invalid offset is ignored");\ 4703 d_lite_initialize(
int argc,
VALUE *argv,
VALUE self)
4705 VALUE jd, vjd, vdf, sf, vsf, vof, vsg;
4712 rb_scan_args(argc, argv,
"05", &vjd, &vdf, &vsf, &vof, &vsg);
4744 decode_jd(jd, &nth, &rjd);
4745 if (!df && f_zero_p(sf) && !of) {
4751 "cannot load complex into simple");
4763 d_lite_initialize_copy(
VALUE copy,
VALUE date)
4779 "cannot load complex into simple");
4790 d_lite_fill(
VALUE self)
4819 d_lite_ajd(
VALUE self)
4836 d_lite_amjd(
VALUE self)
4853 d_lite_jd(
VALUE self)
4856 return m_real_local_jd(dat);
4870 d_lite_mjd(
VALUE self)
4886 d_lite_ld(
VALUE self)
4902 d_lite_year(
VALUE self)
4905 return m_real_year(dat);
4917 d_lite_yday(
VALUE self)
4933 d_lite_mon(
VALUE self)
4949 d_lite_mday(
VALUE self)
4964 d_lite_day_fraction(
VALUE self)
4982 d_lite_cwyear(
VALUE self)
4985 return m_real_cwyear(dat);
4997 d_lite_cweek(
VALUE self)
5012 d_lite_cwday(
VALUE self)
5020 d_lite_wnum0(
VALUE self)
5027 d_lite_wnum1(
VALUE self)
5043 d_lite_wday(
VALUE self)
5056 d_lite_sunday_p(
VALUE self)
5069 d_lite_monday_p(
VALUE self)
5082 d_lite_tuesday_p(
VALUE self)
5095 d_lite_wednesday_p(
VALUE self)
5108 d_lite_thursday_p(
VALUE self)
5121 d_lite_friday_p(
VALUE self)
5134 d_lite_saturday_p(
VALUE self)
5148 if (
NUM2INT(k) != m_wday(dat))
5151 c_nth_kday_to_jd(m_year(dat), m_mon(dat),
5154 if (m_local_jd(dat) != rjd)
5169 d_lite_hour(
VALUE self)
5185 d_lite_min(
VALUE self)
5201 d_lite_sec(
VALUE self)
5217 d_lite_sec_fraction(
VALUE self)
5220 return m_sf_in_sec(dat);
5232 d_lite_offset(
VALUE self)
5235 return m_of_in_day(dat);
5247 d_lite_zone(
VALUE self)
5263 d_lite_julian_p(
VALUE self)
5279 d_lite_gregorian_p(
VALUE self)
5295 d_lite_leap_p(
VALUE self)
5297 int rjd, ns, ry, rm, rd;
5300 if (m_gregorian_p(dat))
5301 return f_boolcast(c_gregorian_leap_p(m_year(dat)));
5303 c_civil_to_jd(m_year(dat), 3, 1, m_virtual_sg(dat),
5305 c_jd_to_civil(rjd - 1, m_virtual_sg(dat), &ry, &rm, &rd);
5319 d_lite_start(
VALUE self)
5354 set_sg(
union DateData *x,
double sg)
5369 dup_obj_with_new_start(
VALUE obj,
double sg)
5371 volatile VALUE dup = dup_obj(obj);
5389 d_lite_new_start(
int argc,
VALUE *argv,
VALUE self)
5400 return dup_obj_with_new_start(
self, sg);
5410 d_lite_italy(
VALUE self)
5412 return dup_obj_with_new_start(
self,
ITALY);
5422 d_lite_england(
VALUE self)
5424 return dup_obj_with_new_start(
self,
ENGLAND);
5434 d_lite_julian(
VALUE self)
5436 return dup_obj_with_new_start(
self,
JULIAN);
5446 d_lite_gregorian(
VALUE self)
5448 return dup_obj_with_new_start(
self,
GREGORIAN);
5462 dup_obj_with_new_offset(
VALUE obj,
int of)
5464 volatile VALUE dup = dup_obj_as_complex(obj);
5483 d_lite_new_offset(
int argc,
VALUE *argv,
VALUE self)
5494 return dup_obj_with_new_offset(
self, rof);
5518 switch (
TYPE(other)) {
5535 jd = m_jd(dat) + (int)t;
5588 jd = m_jd(dat) +
jd;
5595 nth =
f_add(m_nth(dat), nth);
5667 sf =
f_add(m_sf(dat), sf);
5681 df = m_df(dat) + df;
5695 jd = m_jd(dat) +
jd;
5702 nth =
f_add(m_nth(dat), nth);
5704 if (!df && f_zero_p(sf) && !m_of(dat))
5716 m_of(dat), m_sg(dat),
5725 expect_numeric(other);
5727 #ifdef CANONICALIZATION_FOR_MATHN 5728 if (!k_rational_p(other))
5729 return d_lite_plus(
self, other);
5737 if (wholenum_p(other))
5769 sf =
f_add(m_sf(dat), sf);
5783 df = m_df(dat) + df;
5797 jd = m_jd(dat) +
jd;
5804 nth =
f_add(m_nth(dat), nth);
5806 if (!df && f_zero_p(sf) && !m_of(dat))
5818 m_of(dat), m_sg(dat),
5838 n =
f_sub(m_nth(adat), m_nth(bdat));
5839 d = m_jd(adat) - m_jd(bdat);
5840 df = m_df(adat) - m_df(bdat);
5841 sf =
f_sub(m_sf(adat), m_sf(bdat));
5870 r =
f_add(r, isec_to_day(df));
5872 r =
f_add(r, ns_to_day(sf));
5900 if (k_date_p(other))
5901 return minus_dd(
self, other);
5903 switch (
TYPE(other)) {
5909 expect_numeric(other);
5913 return d_lite_plus(
self,
f_negate(other));
5924 d_lite_next_day(
int argc,
VALUE *argv,
VALUE self)
5931 return d_lite_plus(
self, n);
5941 d_lite_prev_day(
int argc,
VALUE *argv,
VALUE self)
5948 return d_lite_minus(
self, n);
5959 d_lite_next(
VALUE self)
5961 return d_lite_next_day(0, (
VALUE *)
NULL,
self);
6015 if (valid_civil_p(y, m, d, sg,
6017 &rm, &rd, &rjd, &ns))
6022 encode_jd(nth, rjd, &rjd2);
6023 return d_lite_plus(
self,
f_sub(rjd2, m_real_local_jd(dat)));
6052 expect_numeric(other);
6053 return d_lite_rshift(
self,
f_negate(other));
6065 d_lite_next_month(
int argc,
VALUE *argv,
VALUE self)
6072 return d_lite_rshift(
self, n);
6084 d_lite_prev_month(
int argc,
VALUE *argv,
VALUE self)
6091 return d_lite_lshift(
self, n);
6107 d_lite_next_year(
int argc,
VALUE *argv,
VALUE self)
6130 d_lite_prev_year(
int argc,
VALUE *argv,
VALUE self)
6154 d_lite_step(
int argc,
VALUE *argv,
VALUE self)
6156 VALUE limit, step, date;
6173 while (
FIX2INT(d_lite_cmp(date, limit)) >= 0) {
6175 date = d_lite_plus(date, step);
6183 while (
FIX2INT(d_lite_cmp(date, limit)) <= 0) {
6185 date = d_lite_plus(date, step);
6209 while (
FIX2INT(d_lite_cmp(date, max)) <= 0) {
6211 date = d_lite_plus(date,
INT2FIX(1));
6231 while (
FIX2INT(d_lite_cmp(date, min)) >= 0) {
6233 date = d_lite_plus(date,
INT2FIX(-1));
6243 if (k_numeric_p(other))
6244 return f_cmp(m_ajd(dat), other);
6245 else if (k_date_p(other))
6246 return f_cmp(m_ajd(dat),
f_ajd(other));
6261 m_canonicalize_jd(
self, adat);
6262 m_canonicalize_jd(other, bdat);
6263 a_nth = m_nth(adat);
6264 b_nth = m_nth(bdat);
6265 if (f_eqeq_p(a_nth, b_nth)) {
6274 if (f_eqeq_p(a_sf, b_sf)) {
6277 else if (
f_lt_p(a_sf, b_sf)) {
6284 else if (a_df < b_df) {
6291 else if (a_jd < b_jd) {
6298 else if (
f_lt_p(a_nth, b_nth)) {
6326 if (!k_date_p(other))
6327 return cmp_gen(
self, other);
6333 m_gregorian_p(adat) == m_gregorian_p(bdat)))
6334 return cmp_dd(
self, other);
6340 m_canonicalize_jd(
self, adat);
6341 m_canonicalize_jd(other, bdat);
6342 a_nth = m_nth(adat);
6343 b_nth = m_nth(bdat);
6344 if (f_eqeq_p(a_nth, b_nth)) {
6350 else if (a_jd < b_jd) {
6357 else if (
f_lt_p(a_nth, b_nth)) {
6372 if (k_numeric_p(other))
6373 return f_eqeq_p(m_real_local_jd(dat), other);
6374 else if (k_date_p(other))
6375 return f_eqeq_p(m_real_local_jd(dat),
f_jd(other));
6399 if (!k_date_p(other))
6400 return equal_gen(
self, other);
6405 if (!(m_gregorian_p(adat) == m_gregorian_p(bdat)))
6406 return equal_gen(
self, other);
6412 m_canonicalize_jd(
self, adat);
6413 m_canonicalize_jd(other, bdat);
6414 a_nth = m_nth(adat);
6415 b_nth = m_nth(bdat);
6416 a_jd = m_local_jd(adat);
6417 b_jd = m_local_jd(bdat);
6418 if (f_eqeq_p(a_nth, b_nth) &&
6430 if (!k_date_p(other))
6432 return f_zero_p(d_lite_cmp(
self, other));
6437 d_lite_hash(
VALUE self)
6451 static void set_tmx(
VALUE,
struct tmx *);
6465 d_lite_to_s(
VALUE self)
6467 return strftimev(
"%Y-%m-%d",
self, set_tmx);
6491 x->
s.
year, x->
s.mon, x->
s.mday,
6503 "%dy%dm%dd %dh%dm%ds; %s>",
6508 x->
c.
year, x->
c.mon, x->
c.mday,
6509 x->
c.hour, x->
c.min, x->
c.sec,
6521 d_lite_inspect_raw(
VALUE self)
6535 m_real_jd(x), m_df(x), m_sf(x),
6551 d_lite_inspect(
VALUE self)
6560 size_t date_strftime(
char *s,
size_t maxsize,
const char *format,
6563 #define SMALLBUF 100 6565 date_strftime_alloc(
char **
buf,
const char *format,
6577 if (len != 0 || (**buf ==
'\0' &&
errno != ERANGE))
return len;
6578 for (size=1024; ; size*=2) {
6591 if (size >= 1024 * flen) {
6605 s = day_to_sec(
f_sub(m_real_jd(x),
6615 #define MILLISECOND_IN_NANOSECONDS 1000000 6622 s = sec_to_ms(tmx_m_secs(x));
6643 static const struct tmx_funcs tmx_funcs = {
6644 (
VALUE (*)(
void *))m_real_year,
6645 (int (*)(
void *))m_yday,
6646 (int (*)(
void *))m_mon,
6647 (int (*)(
void *))m_mday,
6648 (
VALUE (*)(
void *))m_real_cwyear,
6649 (int (*)(
void *))m_cweek,
6650 (int (*)(
void *))m_cwday,
6651 (int (*)(
void *))m_wnum0,
6652 (int (*)(
void *))m_wnum1,
6653 (int (*)(
void *))m_wday,
6654 (int (*)(
void *))m_hour,
6655 (int (*)(
void *))m_min,
6656 (int (*)(
void *))m_sec,
6657 (
VALUE (*)(
void *))m_sf_in_sec,
6658 (
VALUE (*)(
void *))tmx_m_secs,
6659 (
VALUE (*)(
void *))tmx_m_msecs,
6660 (int (*)(
void *))tmx_m_of,
6661 (
char *(*)(
void *))tmx_m_zone
6665 set_tmx(
VALUE self,
struct tmx *tmx)
6668 tmx->
dat = (
void *)dat;
6669 tmx->
funcs = &tmx_funcs;
6673 date_strftime_internal(
int argc,
VALUE *argv,
VALUE self,
6674 const char *default_fmt,
6675 void (*func)(
VALUE,
struct tmx *))
6692 "format should have ASCII compatible encoding");
6697 (*func)(
self, &tmx);
6698 if (memchr(fmt,
'\0', len)) {
6700 const char *p = fmt, *pe = fmt +
len;
6704 len = date_strftime_alloc(&buf, p, &tmx);
6707 if (buf != buffer) {
6711 for (fmt = p; p < pe && !*p; ++p);
6719 len = date_strftime_alloc(&buf, fmt, &tmx);
6722 if (buf != buffer)
xfree(buf);
6906 d_lite_strftime(
int argc,
VALUE *argv,
VALUE self)
6908 return date_strftime_internal(argc, argv,
self,
6909 "%Y-%m-%d", set_tmx);
6914 void (*func)(
VALUE,
struct tmx *))
6921 (*func)(
self, &tmx);
6922 len = date_strftime_alloc(&buf, fmt, &tmx);
6925 if (buf != buffer)
xfree(buf);
6940 d_lite_asctime(
VALUE self)
6942 return strftimev(
"%a %b %e %H:%M:%S %Y",
self, set_tmx);
6953 d_lite_iso8601(
VALUE self)
6955 return strftimev(
"%Y-%m-%d",
self, set_tmx);
6965 d_lite_rfc3339(
VALUE self)
6967 return strftimev(
"%Y-%m-%dT%H:%M:%S%:z",
self, set_tmx);
6978 d_lite_rfc2822(
VALUE self)
6980 return strftimev(
"%a, %-d %b %Y %T %z",
self, set_tmx);
6991 d_lite_httpdate(
VALUE self)
6993 volatile VALUE dup = dup_obj_with_new_offset(
self, 0);
6994 return strftimev(
"%a, %d %b %Y %T GMT", dup, set_tmx);
7015 else if (d < 2424875) {
7019 else if (d < 2447535) {
7042 d_lite_jisx0301(
VALUE self)
7048 fmt = jisx0301_date_format(fmtbuf,
sizeof(fmtbuf),
7049 m_real_local_jd(dat),
7056 d_lite_marshal_dump_old(
VALUE self)
7078 d_lite_marshal_dump(
VALUE self)
7125 if (!k_numeric_p(sg))
7134 old_to_new(ajd, of, sg,
7135 &nth, &jd, &df, &sf, &rof, &rsg);
7137 if (!df && f_zero_p(sf) && !rof) {
7142 "cannot load complex into simple");
7162 if (!df && f_zero_p(sf) && !of) {
7167 "cannot load complex into simple");
7195 obj = d_lite_s_alloc(klass);
7196 return d_lite_marshal_load(obj, a);
7214 datetime_s_jd(
int argc,
VALUE *argv,
VALUE klass)
7216 VALUE vjd, vh, vmin, vs, vof, vsg,
jd, fr, fr2, ret;
7220 rb_scan_args(argc, argv,
"06", &vjd, &vh, &vmin, &vs, &vof, &vsg);
7246 int rh, rmin, rs, rjd, rjd2;
7248 if (!c_valid_time_p(h, min, s, &rh, &rmin, &rs))
7252 decode_jd(jd, &nth, &rjd);
7253 rjd2 = jd_local_to_utc(rjd,
7254 time_to_df(rh, rmin, rs),
7257 ret = d_complex_new_internal(klass,
7282 datetime_s_ordinal(
int argc,
VALUE *argv,
VALUE klass)
7284 VALUE vy, vd, vh, vmin, vs, vof, vsg, y, fr, fr2, ret;
7285 int d, h, min, s, rof;
7288 rb_scan_args(argc, argv,
"07", &vy, &vd, &vh, &vmin, &vs, &vof, &vsg);
7317 int ry, rd, rh, rmin, rs, rjd, rjd2, ns;
7319 if (!valid_ordinal_p(y, d, sg,
7324 if (!c_valid_time_p(h, min, s, &rh, &rmin, &rs))
7328 rjd2 = jd_local_to_utc(rjd,
7329 time_to_df(rh, rmin, rs),
7332 ret = d_complex_new_internal(klass,
7358 datetime_s_civil(
int argc,
VALUE *argv,
VALUE klass)
7360 VALUE vy, vm, vd, vh, vmin, vs, vof, vsg, y, fr, fr2, ret;
7361 int m, d, h, min, s, rof;
7364 rb_scan_args(argc, argv,
"08", &vy, &vm, &vd, &vh, &vmin, &vs, &vof, &vsg);
7394 if (guess_style(y, sg) < 0) {
7396 int ry, rm, rd, rh, rmin, rs;
7398 if (!valid_gregorian_p(y, m, d,
7402 if (!c_valid_time_p(h, min, s, &rh, &rmin, &rs))
7406 ret = d_complex_new_internal(klass,
7416 int ry, rm, rd, rh, rmin, rs, rjd, rjd2, ns;
7418 if (!valid_civil_p(y, m, d, sg,
7423 if (!c_valid_time_p(h, min, s, &rh, &rmin, &rs))
7427 rjd2 = jd_local_to_utc(rjd,
7428 time_to_df(rh, rmin, rs),
7431 ret = d_complex_new_internal(klass,
7455 datetime_s_commercial(
int argc,
VALUE *argv,
VALUE klass)
7457 VALUE vy, vw, vd, vh, vmin, vs, vof, vsg, y, fr, fr2, ret;
7458 int w, d, h, min, s, rof;
7461 rb_scan_args(argc, argv,
"08", &vy, &vw, &vd, &vh, &vmin, &vs, &vof, &vsg);
7493 int ry, rw, rd, rh, rmin, rs, rjd, rjd2, ns;
7495 if (!valid_commercial_p(y, w, d, sg,
7500 if (!c_valid_time_p(h, min, s, &rh, &rmin, &rs))
7504 rjd2 = jd_local_to_utc(rjd,
7505 time_to_df(rh, rmin, rs),
7508 ret = d_complex_new_internal(klass,
7522 datetime_s_weeknum(
int argc,
VALUE *argv,
VALUE klass)
7524 VALUE vy, vw, vd, vf, vh, vmin, vs, vof, vsg, y, fr, fr2, ret;
7525 int w, d,
f, h, min, s, rof;
7529 &vh, &vmin, &vs, &vof, &vsg);
7564 int ry, rw, rd, rh, rmin, rs, rjd, rjd2, ns;
7566 if (!valid_weeknum_p(y, w, d, f, sg,
7571 if (!c_valid_time_p(h, min, s, &rh, &rmin, &rs))
7575 rjd2 = jd_local_to_utc(rjd,
7576 time_to_df(rh, rmin, rs),
7578 ret = d_complex_new_internal(klass,
7591 datetime_s_nth_kday(
int argc,
VALUE *argv,
VALUE klass)
7593 VALUE vy, vm, vn, vk, vh, vmin, vs, vof, vsg, y, fr, fr2, ret;
7594 int m, n, k, h, min, s, rof;
7598 &vh, &vmin, &vs, &vof, &vsg);
7633 int ry, rm, rn, rk, rh, rmin, rs, rjd, rjd2, ns;
7635 if (!valid_nth_kday_p(y, m, n, k, sg,
7637 &rm, &rn, &rk, &rjd,
7640 if (!c_valid_time_p(h, min, s, &rh, &rmin, &rs))
7644 rjd2 = jd_local_to_utc(rjd,
7645 time_to_df(rh, rmin, rs),
7647 ret = d_complex_new_internal(klass,
7669 datetime_s_now(
int argc,
VALUE *argv,
VALUE klass)
7673 #ifdef HAVE_CLOCK_GETTIME 7681 int y, ry, m, d, h, min, s;
7690 #ifdef HAVE_CLOCK_GETTIME 7700 if (!localtime_r(&sec, &tm))
7703 y = tm.tm_year + 1900;
7711 #ifdef HAVE_STRUCT_TM_TM_GMTOFF 7713 #elif defined(HAVE_TIMEZONE) 7715 of = (long)-((tm.tm_isdst > 0) ? altzone :
timezone);
7723 of += (long)difftime(sec2, sec);
7726 #elif defined(HAVE_TIMEGM) 7731 of = (long)difftime(sec2, sec);
7738 if (!gmtime_r(&sec, &tm2))
7740 tm2.tm_isdst = tm.tm_isdst;
7741 sec2 = mktime(&tm2);
7742 of = (long)difftime(sec, sec2);
7745 #ifdef HAVE_CLOCK_GETTIME 7756 decode_year(
INT2FIX(y), -1, &nth, &ry);
7758 ret = d_complex_new_internal(klass,
7778 if (!c_valid_start_p(
NUM2DBL(sg))) {
7791 jd = rt__valid_civil_p(
ref_hash(
"year"),
7805 hash = rt_rewrite_frags(hash);
7806 hash = rt_complete_frags(klass, hash);
7807 jd = rt__valid_date_frags_p(hash, sg);
7822 df = time_to_df(rh, rmin, rs);
7845 decode_jd(jd, &nth, &rjd);
7846 rjd2 = jd_local_to_utc(rjd, df, of);
7847 df = df_local_to_utc(df, of);
7849 return d_complex_new_internal(klass,
7870 datetime_s__strptime(
int argc,
VALUE *argv,
VALUE klass)
7872 return date_s__strptime_internal(argc, argv, klass,
"%FT%T%z");
7903 datetime_s_strptime(
int argc,
VALUE *argv,
VALUE klass)
7919 VALUE argv2[2], hash;
7923 hash = date_s__strptime(2, argv2, klass);
7924 return dt_new_by_frags(klass, hash, sg);
7946 datetime_s_parse(
int argc,
VALUE *argv,
VALUE klass)
7962 VALUE argv2[2], hash;
7966 hash = date_s__parse(2, argv2, klass);
7967 return dt_new_by_frags(klass, hash, sg);
7986 datetime_s_iso8601(
int argc,
VALUE *argv,
VALUE klass)
8000 VALUE hash = date_s__iso8601(klass, str);
8001 return dt_new_by_frags(klass, hash, sg);
8016 datetime_s_rfc3339(
int argc,
VALUE *argv,
VALUE klass)
8030 VALUE hash = date_s__rfc3339(klass, str);
8031 return dt_new_by_frags(klass, hash, sg);
8046 datetime_s_xmlschema(
int argc,
VALUE *argv,
VALUE klass)
8060 VALUE hash = date_s__xmlschema(klass, str);
8061 return dt_new_by_frags(klass, hash, sg);
8077 datetime_s_rfc2822(
int argc,
VALUE *argv,
VALUE klass)
8085 str =
rb_str_new2(
"Mon, 1 Jan -4712 00:00:00 +0000");
8091 VALUE hash = date_s__rfc2822(klass, str);
8092 return dt_new_by_frags(klass, hash, sg);
8107 datetime_s_httpdate(
int argc,
VALUE *argv,
VALUE klass)
8115 str =
rb_str_new2(
"Mon, 01 Jan -4712 00:00:00 GMT");
8121 VALUE hash = date_s__httpdate(klass, str);
8122 return dt_new_by_frags(klass, hash, sg);
8137 datetime_s_jisx0301(
int argc,
VALUE *argv,
VALUE klass)
8151 VALUE hash = date_s__jisx0301(klass, str);
8152 return dt_new_by_frags(klass, hash, sg);
8167 dt_lite_to_s(
VALUE self)
8169 return strftimev(
"%Y-%m-%dT%H:%M:%S%:z",
self, set_tmx);
8351 dt_lite_strftime(
int argc,
VALUE *argv,
VALUE self)
8353 return date_strftime_internal(argc, argv,
self,
8354 "%Y-%m-%dT%H:%M:%S%:z", set_tmx);
8358 iso8601_timediv(
VALUE self,
long n)
8360 static const char timefmt[] =
"T%H:%M:%S";
8361 static const char zone[] =
"%:z";
8362 char fmt[
sizeof(timefmt) +
sizeof(zone) +
rb_strlen_lit(
".%N") +
8366 memcpy(p, timefmt,
sizeof(timefmt)-1);
8367 p +=
sizeof(timefmt)-1;
8368 if (n > 0) p +=
snprintf(p, fmt+
sizeof(fmt)-p,
".%%%ldN", n);
8369 memcpy(p, zone,
sizeof(zone));
8385 dt_lite_iso8601(
int argc,
VALUE *argv,
VALUE self)
8394 iso8601_timediv(
self, n));
8408 dt_lite_rfc3339(
int argc,
VALUE *argv,
VALUE self)
8410 return dt_lite_iso8601(argc, argv,
self);
8424 dt_lite_jisx0301(
int argc,
VALUE *argv,
VALUE self)
8433 iso8601_timediv(
self, n));
8438 #define f_subsec(x) rb_funcall(x, rb_intern("subsec"), 0) 8439 #define f_utc_offset(x) rb_funcall(x, rb_intern("utc_offset"), 0) 8440 #define f_local3(x,y,m,d) rb_funcall(x, rb_intern("local"), 3, y, m, d) 8449 time_to_time(
VALUE self)
8461 time_to_date(
VALUE self)
8470 decode_year(y, -1, &nth, &ry);
8472 ret = d_simple_new_internal(cDate,
8491 time_to_datetime(
VALUE self)
8494 int ry, m, d, h, min, s, of;
8509 decode_year(y, -1, &nth, &ry);
8511 ret = d_complex_new_internal(cDateTime,
8532 date_to_time(
VALUE self)
8549 date_to_date(
VALUE self)
8561 date_to_datetime(
VALUE self)
8566 VALUE new = d_lite_s_alloc_simple(cDateTime);
8574 VALUE new = d_lite_s_alloc_complex(cDateTime);
8601 datetime_to_time(
VALUE self)
8603 volatile VALUE dup = dup_obj(
self);
8631 datetime_to_date(
VALUE self)
8636 VALUE new = d_lite_s_alloc_simple(cDate);
8640 bdat->s.jd = m_local_jd(adat);
8645 VALUE new = d_lite_s_alloc_simple(cDate);
8649 bdat->s.jd = m_local_jd(adat);
8663 datetime_to_datetime(
VALUE self)
8671 #define MIN_YEAR -4713 8672 #define MAX_YEAR 1000000 8674 #define MAX_JD 366963925 8677 test_civil(
int from,
int to,
double sg)
8681 fprintf(stderr,
"test_civil: %d...%d (%d) - %.0f\n",
8682 from, to, to - from, sg);
8683 for (j = from; j <= to; j++) {
8684 int y, m, d, rj, ns;
8686 c_jd_to_civil(j, sg, &y, &m, &d);
8687 c_civil_to_jd(y, m, d, sg, &rj, &ns);
8689 fprintf(stderr,
"%d != %d\n", j, rj);
8697 date_s_test_civil(
VALUE klass)
8699 if (!test_civil(MIN_JD, MIN_JD + 366,
GREGORIAN))
8701 if (!test_civil(2305814, 2598007,
GREGORIAN))
8703 if (!test_civil(MAX_JD - 366, MAX_JD,
GREGORIAN))
8706 if (!test_civil(MIN_JD, MIN_JD + 366,
ITALY))
8708 if (!test_civil(2305814, 2598007,
ITALY))
8710 if (!test_civil(MAX_JD - 366, MAX_JD,
ITALY))
8717 test_ordinal(
int from,
int to,
double sg)
8721 fprintf(stderr,
"test_ordinal: %d...%d (%d) - %.0f\n",
8722 from, to, to - from, sg);
8723 for (j = from; j <= to; j++) {
8726 c_jd_to_ordinal(j, sg, &y, &d);
8727 c_ordinal_to_jd(y, d, sg, &rj, &ns);
8729 fprintf(stderr,
"%d != %d\n", j, rj);
8737 date_s_test_ordinal(
VALUE klass)
8739 if (!test_ordinal(MIN_JD, MIN_JD + 366,
GREGORIAN))
8741 if (!test_ordinal(2305814, 2598007,
GREGORIAN))
8743 if (!test_ordinal(MAX_JD - 366, MAX_JD,
GREGORIAN))
8746 if (!test_ordinal(MIN_JD, MIN_JD + 366,
ITALY))
8748 if (!test_ordinal(2305814, 2598007,
ITALY))
8750 if (!test_ordinal(MAX_JD - 366, MAX_JD,
ITALY))
8757 test_commercial(
int from,
int to,
double sg)
8761 fprintf(stderr,
"test_commercial: %d...%d (%d) - %.0f\n",
8762 from, to, to - from, sg);
8763 for (j = from; j <= to; j++) {
8764 int y, w, d, rj, ns;
8766 c_jd_to_commercial(j, sg, &y, &w, &d);
8767 c_commercial_to_jd(y, w, d, sg, &rj, &ns);
8769 fprintf(stderr,
"%d != %d\n", j, rj);
8777 date_s_test_commercial(
VALUE klass)
8779 if (!test_commercial(MIN_JD, MIN_JD + 366,
GREGORIAN))
8781 if (!test_commercial(2305814, 2598007,
GREGORIAN))
8783 if (!test_commercial(MAX_JD - 366, MAX_JD,
GREGORIAN))
8786 if (!test_commercial(MIN_JD, MIN_JD + 366,
ITALY))
8788 if (!test_commercial(2305814, 2598007,
ITALY))
8790 if (!test_commercial(MAX_JD - 366, MAX_JD,
ITALY))
8797 test_weeknum(
int from,
int to,
int f,
double sg)
8801 fprintf(stderr,
"test_weeknum: %d...%d (%d) - %.0f\n",
8802 from, to, to - from, sg);
8803 for (j = from; j <= to; j++) {
8804 int y, w, d, rj, ns;
8806 c_jd_to_weeknum(j, f, sg, &y, &w, &d);
8807 c_weeknum_to_jd(y, w, d, f, sg, &rj, &ns);
8809 fprintf(stderr,
"%d != %d\n", j, rj);
8817 date_s_test_weeknum(
VALUE klass)
8821 for (f = 0; f <= 1; f++) {
8822 if (!test_weeknum(MIN_JD, MIN_JD + 366, f,
GREGORIAN))
8824 if (!test_weeknum(2305814, 2598007, f,
GREGORIAN))
8826 if (!test_weeknum(MAX_JD - 366, MAX_JD, f,
GREGORIAN))
8829 if (!test_weeknum(MIN_JD, MIN_JD + 366, f,
ITALY))
8831 if (!test_weeknum(2305814, 2598007, f,
ITALY))
8833 if (!test_weeknum(MAX_JD - 366, MAX_JD, f,
ITALY))
8841 test_nth_kday(
int from,
int to,
double sg)
8845 fprintf(stderr,
"test_nth_kday: %d...%d (%d) - %.0f\n",
8846 from, to, to - from, sg);
8847 for (j = from; j <= to; j++) {
8848 int y, m, n, k, rj, ns;
8850 c_jd_to_nth_kday(j, sg, &y, &m, &n, &k);
8851 c_nth_kday_to_jd(y, m, n, k, sg, &rj, &ns);
8853 fprintf(stderr,
"%d != %d\n", j, rj);
8861 date_s_test_nth_kday(
VALUE klass)
8863 if (!test_nth_kday(MIN_JD, MIN_JD + 366,
GREGORIAN))
8865 if (!test_nth_kday(2305814, 2598007,
GREGORIAN))
8867 if (!test_nth_kday(MAX_JD - 366, MAX_JD,
GREGORIAN))
8870 if (!test_nth_kday(MIN_JD, MIN_JD + 366,
ITALY))
8872 if (!test_nth_kday(2305814, 2598007,
ITALY))
8874 if (!test_nth_kday(MAX_JD - 366, MAX_JD,
ITALY))
8881 test_unit_v2v(
VALUE i,
8888 return f_eqeq_p(o, i);
8895 if (!test_unit_v2v(
INT2FIX(0), conv1, conv2))
8897 if (!test_unit_v2v(
INT2FIX(1), conv1, conv2))
8899 if (!test_unit_v2v(
INT2FIX(2), conv1, conv2))
8901 if (!test_unit_v2v(
INT2FIX(3), conv1, conv2))
8903 if (!test_unit_v2v(
INT2FIX(11), conv1, conv2))
8905 if (!test_unit_v2v(
INT2FIX(65535), conv1, conv2))
8907 if (!test_unit_v2v(
INT2FIX(1073741823), conv1, conv2))
8909 if (!test_unit_v2v(
INT2NUM(1073741824), conv1, conv2))
8926 if (!test_unit_v2v_iter2(conv1, conv2))
8928 if (!test_unit_v2v_iter2(conv2, conv1))
8934 date_s_test_unit_conv(
VALUE klass)
8936 if (!test_unit_v2v_iter(sec_to_day, day_to_sec))
8938 if (!test_unit_v2v_iter(ms_to_sec, sec_to_ms))
8940 if (!test_unit_v2v_iter(ns_to_day, day_to_ns))
8942 if (!test_unit_v2v_iter(ns_to_sec, sec_to_ns))
8948 date_s_test_all(
VALUE klass)
8950 if (date_s_test_civil(klass) ==
Qfalse)
8952 if (date_s_test_ordinal(klass) ==
Qfalse)
8954 if (date_s_test_commercial(klass) ==
Qfalse)
8956 if (date_s_test_weeknum(klass) ==
Qfalse)
8958 if (date_s_test_nth_kday(klass) ==
Qfalse)
8960 if (date_s_test_unit_conv(klass) ==
Qfalse)
8966 static const char *monthnames[] = {
8968 "January",
"February",
"March",
8969 "April",
"May",
"June",
8970 "July",
"August",
"September",
8971 "October",
"November",
"December" 8974 static const char *abbr_monthnames[] = {
8976 "Jan",
"Feb",
"Mar",
"Apr",
8977 "May",
"Jun",
"Jul",
"Aug",
8978 "Sep",
"Oct",
"Nov",
"Dec" 8981 static const char *daynames[] = {
8982 "Sunday",
"Monday",
"Tuesday",
"Wednesday",
8983 "Thursday",
"Friday",
"Saturday" 8986 static const char *abbr_daynames[] = {
8987 "Sun",
"Mon",
"Tue",
"Wed",
8992 mk_ary_of_str(
long len,
const char *a[])
8998 for (i = 0; i <
len; i++) {
9017 #define rb_intern(str) rb_intern_const(str) 9019 assert(fprintf(stderr,
"assert() is now active\n"));
9028 #if (LONG_MAX / DAY_IN_SECONDS) > SECOND_IN_NANOSECONDS 9031 #elif defined HAVE_LONG_LONG 9033 SECOND_IN_NANOSECONDS);
9036 INT2FIX(SECOND_IN_NANOSECONDS));
9204 mk_ary_of_str(13, abbr_monthnames));
9214 rb_define_const(cDate,
"ABBR_DAYNAMES", mk_ary_of_str(7, abbr_daynames));
9239 #define de_define_private_method rb_define_private_method 9240 de_define_private_method(
CLASS_OF(cDate),
"_valid_jd?",
9241 date_s__valid_jd_p, -1);
9242 de_define_private_method(
CLASS_OF(cDate),
"_valid_ordinal?",
9243 date_s__valid_ordinal_p, -1);
9244 de_define_private_method(
CLASS_OF(cDate),
"_valid_civil?",
9245 date_s__valid_civil_p, -1);
9246 de_define_private_method(
CLASS_OF(cDate),
"_valid_date?",
9247 date_s__valid_civil_p, -1);
9248 de_define_private_method(
CLASS_OF(cDate),
"_valid_commercial?",
9249 date_s__valid_commercial_p, -1);
9250 de_define_private_method(
CLASS_OF(cDate),
"_valid_weeknum?",
9251 date_s__valid_weeknum_p, -1);
9252 de_define_private_method(
CLASS_OF(cDate),
"_valid_nth_kday?",
9253 date_s__valid_nth_kday_p, -1);
9258 date_s_valid_ordinal_p, -1);
9262 date_s_valid_commercial_p, -1);
9265 de_define_private_method(
CLASS_OF(cDate),
"valid_weeknum?",
9266 date_s_valid_weeknum_p, -1);
9267 de_define_private_method(
CLASS_OF(cDate),
"valid_nth_kday?",
9268 date_s_valid_nth_kday_p, -1);
9269 de_define_private_method(
CLASS_OF(cDate),
"zone_to_diff",
9270 date_s_zone_to_diff, 1);
9275 date_s_gregorian_leap_p, 1);
9277 date_s_gregorian_leap_p, 1);
9280 #define de_define_singleton_method rb_define_singleton_method 9281 #define de_define_alias rb_define_alias 9282 de_define_singleton_method(cDate,
"new!", date_s_new_bang, -1);
9293 de_define_singleton_method(cDate,
"weeknum", date_s_weeknum, -1);
9294 de_define_singleton_method(cDate,
"nth_kday", date_s_nth_kday, -1);
9318 #define de_define_method rb_define_method 9319 de_define_method(cDate,
"initialize", d_lite_initialize, -1);
9324 de_define_method(cDate,
"fill", d_lite_fill, 0);
9346 de_define_private_method(cDate,
"wnum0", d_lite_wnum0, 0);
9347 de_define_private_method(cDate,
"wnum1", d_lite_wnum1, 0);
9361 de_define_method(cDate,
"nth_kday?", d_lite_nth_kday_p, 2);
9414 de_define_method(cDate,
"inspect_raw", d_lite_inspect_raw, 0);
9431 de_define_method(cDate,
"marshal_dump_old", d_lite_marshal_dump_old, 0);
9584 datetime_s_commercial, -1);
9587 de_define_singleton_method(cDateTime,
"weeknum",
9588 datetime_s_weeknum, -1);
9589 de_define_singleton_method(cDateTime,
"nth_kday",
9590 datetime_s_nth_kday, -1);
9597 datetime_s__strptime, -1);
9599 datetime_s_strptime, -1);
9601 datetime_s_parse, -1);
9603 datetime_s_iso8601, -1);
9605 datetime_s_rfc3339, -1);
9607 datetime_s_xmlschema, -1);
9609 datetime_s_rfc2822, -1);
9611 datetime_s_rfc2822, -1);
9613 datetime_s_httpdate, -1);
9615 datetime_s_jisx0301, -1);
9617 #define f_public(m,s) rb_funcall(m, rb_intern("public"), 1,\ 9618 ID2SYM(rb_intern(s))) 9625 f_public(cDateTime,
"sec_fraction");
9626 f_public(cDateTime,
"second_fraction");
9657 de_define_singleton_method(cDate,
"test_civil", date_s_test_civil, 0);
9658 de_define_singleton_method(cDate,
"test_ordinal", date_s_test_ordinal, 0);
9659 de_define_singleton_method(cDate,
"test_commercial",
9660 date_s_test_commercial, 0);
9661 de_define_singleton_method(cDate,
"test_weeknum", date_s_test_weeknum, 0);
9662 de_define_singleton_method(cDate,
"test_nth_kday", date_s_test_nth_kday, 0);
9663 de_define_singleton_method(cDate,
"test_unit_conv",
9664 date_s_test_unit_conv, 0);
9665 de_define_singleton_method(cDate,
"test_all", date_s_test_all, 0);
#define rb_rational_new2(x, y)
int gettimeofday(struct timeval *, struct timezone *)
void rb_enc_copy(VALUE obj1, VALUE obj2)
#define RUBY_TYPED_FREE_IMMEDIATELY
size_t strlen(const char *)
#define canonicalize_jd(_nth, _jd)
#define RB_OBJ_WRITTEN(a, oldv, b)
void rb_define_singleton_method(VALUE obj, const char *name, VALUE(*func)(ANYARGS), int argc)
Defines a singleton method for obj.
#define rb_usascii_str_new2
void rb_raise(VALUE exc, const char *fmt,...)
#define PACK5(m, d, h, min, s)
VALUE rb_str_cat(VALUE, const char *, long)
#define copy_complex_to_simple(obj, x, y)
#define DECIMAL_SIZE_OF_BITS(n)
void rb_define_private_method(VALUE klass, const char *name, VALUE(*func)(ANYARGS), int argc)
void rb_check_trusted(VALUE obj)
VALUE rb_ary_push(VALUE ary, VALUE item)
st_index_t rb_memhash(const void *ptr, long len)
#define decode_offset(of, s, h, m)
VALUE rb_funcall(VALUE, ID, int,...)
Calls a method.
void rb_define_alloc_func(VALUE, rb_alloc_func_t)
void rb_include_module(VALUE klass, VALUE module)
VALUE date__rfc3339(VALUE)
void rb_gc_mark(VALUE ptr)
size_t date_strftime(char *s, size_t maxsize, const char *format, const struct tmx *tmx)
void rb_undef_method(VALUE klass, const char *name)
VALUE rb_enc_sprintf(rb_encoding *enc, const char *format,...)
VALUE date__jisx0301(VALUE)
#define SECOND_IN_MILLISECONDS
VALUE rb_singleton_class(VALUE obj)
Returns the singleton class of obj.
#define REFORM_BEGIN_YEAR
VALUE rb_obj_class(VALUE)
call-seq: obj.class -> class
#define RB_TYPE_P(obj, type)
VALUE rb_obj_is_kind_of(VALUE, VALUE)
call-seq: obj.is_a?(class) -> true or false obj.kind_of?(class) -> true or false
#define RUBY_TYPED_WB_PROTECTED
void Init_date_core(void)
#define copy_simple_to_complex(obj, x, y)
RUBY_EXTERN VALUE rb_cObject
VALUE rb_define_class(const char *name, VALUE super)
Defines a top-level class.
void rb_define_const(VALUE, const char *, VALUE)
#define num2int_with_frac(s, n)
void rb_gc_register_mark_object(VALUE obj)
VALUE date__parse(VALUE str, VALUE comp)
RUBY_EXTERN int isinf(double)
#define rb_rational_new1(x)
void rb_sys_fail(const char *mesg)
const struct tmx_funcs * funcs
#define HALF_DAYS_IN_SECONDS
VALUE rb_obj_freeze(VALUE)
call-seq: obj.freeze -> obj
#define rb_strlen_lit(str)
VALUE date__iso8601(VALUE)
int rb_scan_args(int argc, const VALUE *argv, const char *fmt,...)
unsigned char buf[MIME_BUF_SIZE]
rb_encoding * rb_usascii_encoding(void)
#define rb_enc_str_asciicompat_p(str)
VALUE rb_rational_den(VALUE rat)
int clock_gettime(clockid_t, struct timespec *)
RUBY_EXTERN VALUE rb_cNumeric
register unsigned int len
#define set_to_simple(obj, x, _nth, _jd,_sg, _year, _mon, _mday, _flags)
#define f_local3(x, y, m, d)
#define RB_OBJ_WRITE(a, slot, b)
#define RARRAY_AREF(a, i)
VALUE rb_rational_num(VALUE rat)
RUBY_EXTERN double round(double)
#define val2off(vof, iof)
void rb_warning(const char *fmt,...)
#define set_to_complex(obj, x, _nth, _jd,_df, _sf, _of, _sg, _year, _mon, _mday, _hour, _min, _sec, _flags)
VALUE date__httpdate(VALUE)
VALUE rb_marshal_load(VALUE)
#define TypedData_Make_Struct(klass, type, data_type, sval)
#define RETURN_ENUMERATOR(obj, argc, argv)
#define SECOND_IN_NANOSECONDS
#define MILLISECOND_IN_NANOSECONDS
RUBY_EXTERN VALUE rb_cRational
VALUE date__rfc2822(VALUE)
#define rb_check_frozen(obj)
#define RUBY_TYPED_DEFAULT_FREE
void rb_copy_generic_ivar(VALUE, VALUE)
VALUE date__xmlschema(VALUE)
VALUE rb_usascii_str_new(const char *, long)
VALUE date__strptime(const char *str, size_t slen, const char *fmt, size_t flen, VALUE hash)
#define RB_INTEGER_TYPE_P(obj)
RUBY_EXTERN VALUE rb_cTime
void rb_define_method(VALUE klass, const char *name, VALUE(*func)(ANYARGS), int argc)
#define MINUTE_IN_SECONDS
VALUE rb_str_append(VALUE, VALUE)
VALUE date_zone_to_diff(VALUE)
#define num2num_with_frac(s, n)
#define UNIX_EPOCH_IN_CJD
#define strftimev(fmt, time, enc)
VALUE rb_num_coerce_cmp(VALUE, VALUE, ID)
VALUE rb_str_new(const char *, long)