10#if defined(HAVE_SYS_TIME_H)
19#include RUBY_EXTCONF_H
24static ID id_cmp, id_le_p, id_ge_p, id_eqeq_p;
25static VALUE cDate, cDateTime;
26static VALUE eDateError;
27static VALUE half_days_in_day, day_in_nanoseconds;
28static double positive_inf, negative_inf;
30#define f_boolcast(x) ((x) ? Qtrue : Qfalse)
32#define f_abs(x) rb_funcall(x, rb_intern("abs"), 0)
33#define f_negate(x) rb_funcall(x, rb_intern("-@"), 0)
34#define f_add(x,y) rb_funcall(x, '+', 1, y)
35#define f_sub(x,y) rb_funcall(x, '-', 1, y)
36#define f_mul(x,y) rb_funcall(x, '*', 1, y)
37#define f_div(x,y) rb_funcall(x, '/', 1, y)
38#define f_quo(x,y) rb_funcall(x, rb_intern("quo"), 1, y)
39#define f_idiv(x,y) rb_funcall(x, rb_intern("div"), 1, y)
40#define f_mod(x,y) rb_funcall(x, '%', 1, y)
41#define f_remainder(x,y) rb_funcall(x, rb_intern("remainder"), 1, y)
42#define f_expt(x,y) rb_funcall(x, rb_intern("**"), 1, y)
43#define f_floor(x) rb_funcall(x, rb_intern("floor"), 0)
44#define f_ceil(x) rb_funcall(x, rb_intern("ceil"), 0)
45#define f_truncate(x) rb_funcall(x, rb_intern("truncate"), 0)
46#define f_round(x) rb_funcall(x, rb_intern("round"), 0)
48#define f_to_i(x) rb_funcall(x, rb_intern("to_i"), 0)
49#define f_to_r(x) rb_funcall(x, rb_intern("to_r"), 0)
50#define f_to_s(x) rb_funcall(x, rb_intern("to_s"), 0)
51#define f_inspect(x) rb_funcall(x, rb_intern("inspect"), 0)
53#define f_add3(x,y,z) f_add(f_add(x, y), z)
54#define f_sub3(x,y,z) f_sub(f_sub(x, y), z)
56#define f_frozen_ary(...) rb_obj_freeze(rb_ary_new3(__VA_ARGS__))
61#define RETURN_FALSE_UNLESS_NUMERIC(obj) if(!RTEST(rb_obj_is_kind_of((obj), rb_cNumeric))) return Qfalse
63check_numeric(
VALUE obj,
const char* field) {
140#define f_nonzero_p(x) (!f_zero_p(x))
150#define f_positive_p(x) (!f_negative_p(x))
152#define f_ajd(x) rb_funcall(x, rb_intern("ajd"), 0)
153#define f_jd(x) rb_funcall(x, rb_intern("jd"), 0)
154#define f_year(x) rb_funcall(x, rb_intern("year"), 0)
155#define f_mon(x) rb_funcall(x, rb_intern("mon"), 0)
156#define f_mday(x) rb_funcall(x, rb_intern("mday"), 0)
157#define f_wday(x) rb_funcall(x, rb_intern("wday"), 0)
158#define f_hour(x) rb_funcall(x, rb_intern("hour"), 0)
159#define f_min(x) rb_funcall(x, rb_intern("min"), 0)
160#define f_sec(x) rb_funcall(x, rb_intern("sec"), 0)
163#define NDIV(x,y) (-(-((x)+1)/(y))-1)
164#define NMOD(x,y) ((y)-(-((x)+1)%(y))-1)
165#define DIV(n,d) ((n)<0 ? NDIV((n),(d)) : (n)/(d))
166#define MOD(n,d) ((n)<0 ? NMOD((n),(d)) : (n)%(d))
168#define HAVE_JD (1 << 0)
169#define HAVE_DF (1 << 1)
170#define HAVE_CIVIL (1 << 2)
171#define HAVE_TIME (1 << 3)
172#define COMPLEX_DAT (1 << 7)
174#define have_jd_p(x) ((x)->flags & HAVE_JD)
175#define have_df_p(x) ((x)->flags & HAVE_DF)
176#define have_civil_p(x) ((x)->flags & HAVE_CIVIL)
177#define have_time_p(x) ((x)->flags & HAVE_TIME)
178#define complex_dat_p(x) ((x)->flags & COMPLEX_DAT)
179#define simple_dat_p(x) (!complex_dat_p(x))
182#define ENGLAND 2361222
183#define JULIAN positive_inf
184#define GREGORIAN negative_inf
185#define DEFAULT_SG ITALY
187#define UNIX_EPOCH_IN_CJD INT2FIX(2440588)
189#define MINUTE_IN_SECONDS 60
190#define HOUR_IN_SECONDS 3600
191#define DAY_IN_SECONDS 86400
192#define SECOND_IN_MILLISECONDS 1000
193#define SECOND_IN_NANOSECONDS 1000000000
195#define JC_PERIOD0 1461
196#define GC_PERIOD0 146097
197#define CM_PERIOD0 71149239
198#define CM_PERIOD (0xfffffff / CM_PERIOD0 * CM_PERIOD0)
199#define CM_PERIOD_JCY (CM_PERIOD / JC_PERIOD0 * 4)
200#define CM_PERIOD_GCY (CM_PERIOD / GC_PERIOD0 * 400)
202#define REFORM_BEGIN_YEAR 1582
203#define REFORM_END_YEAR 1930
204#define REFORM_BEGIN_JD 2298874
205#define REFORM_END_JD 2426355
215#define MIN_SHIFT SEC_WIDTH
216#define HOUR_SHIFT (MIN_WIDTH + SEC_WIDTH)
217#define MDAY_SHIFT (HOUR_WIDTH + MIN_WIDTH + SEC_WIDTH)
218#define MON_SHIFT (MDAY_WIDTH + HOUR_WIDTH + MIN_WIDTH + SEC_WIDTH)
220#define PK_MASK(x) ((1 << (x)) - 1)
222#define EX_SEC(x) (((x) >> SEC_SHIFT) & PK_MASK(SEC_WIDTH))
223#define EX_MIN(x) (((x) >> MIN_SHIFT) & PK_MASK(MIN_WIDTH))
224#define EX_HOUR(x) (((x) >> HOUR_SHIFT) & PK_MASK(HOUR_WIDTH))
225#define EX_MDAY(x) (((x) >> MDAY_SHIFT) & PK_MASK(MDAY_WIDTH))
226#define EX_MON(x) (((x) >> MON_SHIFT) & PK_MASK(MON_WIDTH))
228#define PACK5(m,d,h,min,s) \
229 (((m) << MON_SHIFT) | ((d) << MDAY_SHIFT) |\
230 ((h) << HOUR_SHIFT) | ((min) << MIN_SHIFT) | ((s) << SEC_SHIFT))
233 (((m) << MON_SHIFT) | ((d) << MDAY_SHIFT))
240#if defined(FLT_RADIX) && defined(FLT_MANT_DIG) && FLT_RADIX == 2 && FLT_MANT_DIG > 22
241#define date_sg_t float
243#define date_sg_t double
301 union DateData *dat;\
302 TypedData_Get_Struct(x, union DateData, &d_lite_type, dat);
305 union DateData *adat;\
306 TypedData_Get_Struct(x, union DateData, &d_lite_type, adat);
309 union DateData *bdat;\
310 TypedData_Get_Struct(x, union DateData, &d_lite_type, bdat);
313 union DateData *adat, *bdat;\
314 TypedData_Get_Struct(x, union DateData, &d_lite_type, adat);\
315 TypedData_Get_Struct(y, union DateData, &d_lite_type, bdat);
329#define set_to_simple(obj, x, _nth, _jd ,_sg, _year, _mon, _mday, _flags) \
331 RB_OBJ_WRITE((obj), &(x)->nth, canon(_nth)); \
333 (x)->sg = (date_sg_t)(_sg);\
337 (x)->flags = (_flags) & ~COMPLEX_DAT;\
340#define set_to_simple(obj, x, _nth, _jd ,_sg, _year, _mon, _mday, _flags) \
342 RB_OBJ_WRITE((obj), &(x)->nth, canon(_nth)); \
344 (x)->sg = (date_sg_t)(_sg);\
346 (x)->pc = PACK2(_mon, _mday);\
347 (x)->flags = (_flags) & ~COMPLEX_DAT;\
352#define set_to_complex(obj, x, _nth, _jd ,_df, _sf, _of, _sg,\
353_year, _mon, _mday, _hour, _min, _sec, _flags) \
355 RB_OBJ_WRITE((obj), &(x)->nth, canon(_nth));\
358 RB_OBJ_WRITE((obj), &(x)->sf, canon(_sf));\
360 (x)->sg = (date_sg_t)(_sg);\
367 (x)->flags = (_flags) | COMPLEX_DAT;\
370#define set_to_complex(obj, x, _nth, _jd ,_df, _sf, _of, _sg,\
371_year, _mon, _mday, _hour, _min, _sec, _flags) \
373 RB_OBJ_WRITE((obj), &(x)->nth, canon(_nth));\
376 RB_OBJ_WRITE((obj), &(x)->sf, canon(_sf));\
378 (x)->sg = (date_sg_t)(_sg);\
380 (x)->pc = PACK5(_mon, _mday, _hour, _min, _sec);\
381 (x)->flags = (_flags) | COMPLEX_DAT;\
386#define copy_simple_to_complex(obj, x, y) \
388 RB_OBJ_WRITE((obj), &(x)->nth, (y)->nth);\
391 (x)->sf = INT2FIX(0);\
393 (x)->sg = (date_sg_t)((y)->sg);\
394 (x)->year = (y)->year;\
395 (x)->mon = (y)->mon;\
396 (x)->mday = (y)->mday;\
400 (x)->flags = (y)->flags;\
403#define copy_simple_to_complex(obj, x, y) \
405 RB_OBJ_WRITE((obj), &(x)->nth, (y)->nth);\
408 RB_OBJ_WRITE((obj), &(x)->sf, INT2FIX(0));\
410 (x)->sg = (date_sg_t)((y)->sg);\
411 (x)->year = (y)->year;\
412 (x)->pc = PACK5(EX_MON((y)->pc), EX_MDAY((y)->pc), 0, 0, 0);\
413 (x)->flags = (y)->flags;\
418#define copy_complex_to_simple(obj, x, y) \
420 RB_OBJ_WRITE((obj), &(x)->nth, (y)->nth);\
422 (x)->sg = (date_sg_t)((y)->sg);\
423 (x)->year = (y)->year;\
424 (x)->mon = (y)->mon;\
425 (x)->mday = (y)->mday;\
426 (x)->flags = (y)->flags;\
429#define copy_complex_to_simple(obj, x, y) \
431 RB_OBJ_WRITE((obj), &(x)->nth, (y)->nth);\
433 (x)->sg = (date_sg_t)((y)->sg);\
434 (x)->year = (y)->year;\
435 (x)->pc = PACK2(EX_MON((y)->pc), EX_MDAY((y)->pc));\
436 (x)->flags = (y)->flags;\
442static int c_valid_civil_p(
int,
int,
int,
double,
443 int *,
int *,
int *,
int *);
446c_find_fdoy(
int y,
double sg,
int *rjd,
int *ns)
450 for (d = 1; d < 31; d++)
451 if (c_valid_civil_p(y, 1, d,
sg, &rm, &rd, rjd, ns))
457c_find_ldoy(
int y,
double sg,
int *rjd,
int *ns)
461 for (i = 0; i < 30; i++)
462 if (c_valid_civil_p(y, 12, 31 - i,
sg, &rm, &rd, rjd, ns))
469c_find_fdom(
int y,
int m,
double sg,
int *rjd,
int *ns)
473 for (d = 1; d < 31; d++)
474 if (c_valid_civil_p(y, m, d,
sg, &rm, &rd, rjd, ns))
481c_find_ldom(
int y,
int m,
double sg,
int *rjd,
int *ns)
485 for (i = 0; i < 30; i++)
486 if (c_valid_civil_p(y, m, 31 - i,
sg, &rm, &rd, rjd, ns))
492c_civil_to_jd(
int y,
int m,
int d,
double sg,
int *rjd,
int *ns)
500 a = floor(y / 100.0);
501 b = 2 - a + floor(a / 4.0);
502 jd = floor(365.25 * (y + 4716)) +
503 floor(30.6001 * (m + 1)) +
516c_jd_to_civil(
int jd,
double sg,
int *ry,
int *rm,
int *rdom)
518 double x, a, b, c, d, e, y, m, dom;
523 x = floor((
jd - 1867216.25) / 36524.25);
524 a =
jd + 1 + x - floor(x / 4.0);
527 c = floor((b - 122.1) / 365.25);
528 d = floor(365.25 * c);
529 e = floor((b - d) / 30.6001);
530 dom = b - d - floor(30.6001 * e);
546c_ordinal_to_jd(
int y,
int d,
double sg,
int *rjd,
int *ns)
550 c_find_fdoy(y,
sg, rjd, &ns2);
552 *ns = (*rjd <
sg) ? 0 : 1;
556c_jd_to_ordinal(
int jd,
double sg,
int *ry,
int *rd)
558 int rm2, rd2, rjd, ns;
560 c_jd_to_civil(
jd,
sg, ry, &rm2, &rd2);
561 c_find_fdoy(*ry,
sg, &rjd, &ns);
562 *rd = (
jd - rjd) + 1;
566c_commercial_to_jd(
int y,
int w,
int d,
double sg,
int *rjd,
int *ns)
570 c_find_fdoy(y,
sg, &rjd2, &ns2);
573 (rjd2 -
MOD((rjd2 - 1) + 1, 7)) +
576 *ns = (*rjd <
sg) ? 0 : 1;
580c_jd_to_commercial(
int jd,
double sg,
int *ry,
int *rw,
int *rd)
582 int ry2, rm2, rd2, a, rjd2, ns2;
584 c_jd_to_civil(
jd - 3,
sg, &ry2, &rm2, &rd2);
586 c_commercial_to_jd(a + 1, 1, 1,
sg, &rjd2, &ns2);
590 c_commercial_to_jd(a, 1, 1,
sg, &rjd2, &ns2);
593 *rw = 1 +
DIV(
jd - rjd2, 7);
594 *rd =
MOD(
jd + 1, 7);
600c_weeknum_to_jd(
int y,
int w,
int d,
int f,
double sg,
int *rjd,
int *ns)
604 c_find_fdoy(y,
sg, &rjd2, &ns2);
606 *rjd = (rjd2 -
MOD(((rjd2 -
f) + 1), 7) - 7) + 7 * w + d;
607 *ns = (*rjd <
sg) ? 0 : 1;
611c_jd_to_weeknum(
int jd,
int f,
double sg,
int *ry,
int *rw,
int *rd)
613 int rm, rd2, rjd, ns, j;
615 c_jd_to_civil(
jd,
sg, ry, &rm, &rd2);
616 c_find_fdoy(*ry,
sg, &rjd, &ns);
618 j =
jd - (rjd -
MOD((rjd -
f) + 1, 7)) + 7;
625c_nth_kday_to_jd(
int y,
int m,
int n,
int k,
double sg,
int *rjd,
int *ns)
630 c_find_fdom(y, m,
sg, &rjd2, &ns2);
634 c_find_ldom(y, m,
sg, &rjd2, &ns2);
637 *rjd = (rjd2 -
MOD((rjd2 - k) + 1, 7)) + 7 * n;
638 *ns = (*rjd <
sg) ? 0 : 1;
645 return MOD(
jd + 1, 7);
650c_jd_to_nth_kday(
int jd,
double sg,
int *ry,
int *rm,
int *rn,
int *rk)
654 c_jd_to_civil(
jd,
sg, ry, rm, &rd);
655 c_find_fdom(*ry, *rm,
sg, &rjd, &ns2);
656 *rn =
DIV(
jd - rjd, 7) + 1;
657 *rk = c_jd_to_wday(
jd);
662c_valid_ordinal_p(
int y,
int d,
double sg,
663 int *rd,
int *rjd,
int *ns)
670 if (!c_find_ldoy(y,
sg, &rjd2, &ns2))
672 c_jd_to_ordinal(rjd2 + d + 1,
sg, &ry2, &rd2);
677 c_ordinal_to_jd(y, d,
sg, rjd, ns);
678 c_jd_to_ordinal(*rjd,
sg, &ry2, &rd2);
679 if (ry2 != y || rd2 != d)
684static const int monthtab[2][13] = {
685 { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
686 { 0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
690c_julian_leap_p(
int y)
692 return MOD(y, 4) == 0;
696c_gregorian_leap_p(
int y)
698 return (
MOD(y, 4) == 0 && y % 100 != 0) ||
MOD(y, 400) == 0;
702c_julian_last_day_of_month(
int y,
int m)
704 assert(m >= 1 && m <= 12);
705 return monthtab[c_julian_leap_p(y) ? 1 : 0][m];
709c_gregorian_last_day_of_month(
int y,
int m)
711 assert(m >= 1 && m <= 12);
712 return monthtab[c_gregorian_leap_p(y) ? 1 : 0][m];
716c_valid_julian_p(
int y,
int m,
int d,
int *rm,
int *rd)
724 last = c_julian_last_day_of_month(y, m);
727 if (d < 1 || d >
last)
735c_valid_gregorian_p(
int y,
int m,
int d,
int *rm,
int *rd)
743 last = c_gregorian_last_day_of_month(y, m);
746 if (d < 1 || d >
last)
754c_valid_civil_p(
int y,
int m,
int d,
double sg,
755 int *rm,
int *rd,
int *rjd,
int *ns)
762 if (!c_find_ldom(y, m,
sg, rjd, ns))
764 c_jd_to_civil(*rjd + d + 1,
sg, &ry, rm, rd);
765 if (ry != y || *rm != m)
769 c_civil_to_jd(y, m, d,
sg, rjd, ns);
770 c_jd_to_civil(*rjd,
sg, &ry, rm, rd);
771 if (ry != y || *rm != m || *rd != d)
777c_valid_commercial_p(
int y,
int w,
int d,
double sg,
778 int *rw,
int *rd,
int *rjd,
int *ns)
780 int ns2, ry2, rw2, rd2;
787 c_commercial_to_jd(y + 1, 1, 1,
sg, &rjd2, &ns2);
788 c_jd_to_commercial(rjd2 + w * 7,
sg, &ry2, &rw2, &rd2);
793 c_commercial_to_jd(y, w, d,
sg, rjd, ns);
794 c_jd_to_commercial(*rjd,
sg, &ry2, rw, rd);
795 if (y != ry2 || w != *rw || d != *rd)
801c_valid_weeknum_p(
int y,
int w,
int d,
int f,
double sg,
802 int *rw,
int *rd,
int *rjd,
int *ns)
804 int ns2, ry2, rw2, rd2;
811 c_weeknum_to_jd(y + 1, 1,
f,
f,
sg, &rjd2, &ns2);
812 c_jd_to_weeknum(rjd2 + w * 7,
f,
sg, &ry2, &rw2, &rd2);
817 c_weeknum_to_jd(y, w, d,
f,
sg, rjd, ns);
818 c_jd_to_weeknum(*rjd,
f,
sg, &ry2, rw, rd);
819 if (y != ry2 || w != *rw || d != *rd)
826c_valid_nth_kday_p(
int y,
int m,
int n,
int k,
double sg,
827 int *rm,
int *rn,
int *rk,
int *rjd,
int *ns)
829 int ns2, ry2, rm2, rn2, rk2;
840 c_nth_kday_to_jd(ny, nm, 1, k,
sg, &rjd2, &ns2);
841 c_jd_to_nth_kday(rjd2 + n * 7,
sg, &ry2, &rm2, &rn2, &rk2);
842 if (ry2 != y || rm2 != m)
846 c_nth_kday_to_jd(y, m, n, k,
sg, rjd, ns);
847 c_jd_to_nth_kday(*rjd,
sg, &ry2, rm, rn, rk);
848 if (y != ry2 || m != *rm || n != *rn || k != *rk)
855c_valid_time_p(
int h,
int min,
int s,
int *rh,
int *rmin,
int *rs)
866 return !(h < 0 || h > 24 ||
867 min < 0 || min > 59 ||
869 (h == 24 && (min > 0 || s > 0)));
873c_valid_start_p(
double sg)
885df_local_to_utc(
int df,
int of)
896df_utc_to_local(
int df,
int of)
907jd_local_to_utc(
int jd,
int df,
int of)
918jd_utc_to_local(
int jd,
int df,
int of)
929time_to_df(
int h,
int min,
int s)
935df_to_time(
int df,
int *h,
int *min,
int *s)
962 return f_quo(n, day_in_nanoseconds);
992safe_mul_p(
VALUE x,
long m)
1022 return f_mul(d, day_in_nanoseconds);
1061 VALUE s = day_to_sec(d);
1072 VALUE n = sec_to_ns(s);
1085 *
jd = div_day(d, &
f);
1086 *
df = div_df(
f, &
f);
1095 if (f_zero_p(x->
s.
nth))
1097 else if (f_negative_p(x->
s.
nth))
1098 return positive_inf;
1099 return negative_inf;
1107 if (f_zero_p(x->
c.
nth))
1109 else if (f_negative_p(x->
c.
nth))
1110 return positive_inf;
1111 return negative_inf;
1118 return s_virtual_sg(x);
1120 return c_virtual_sg(x);
1123#define canonicalize_jd(_nth, _jd) \
1126 _nth = f_sub(_nth, INT2FIX(1));\
1129 if (_jd >= CM_PERIOD) {\
1130 _nth = f_add(_nth, INT2FIX(1));\
1144 x->
flags &= ~HAVE_CIVIL;
1156 c_civil_to_jd(x->
s.
year, x->
s.mon, x->
s.mday,
1157 s_virtual_sg(x), &
jd, &ns);
1160 s_virtual_sg(x), &
jd, &ns);
1175 c_jd_to_civil(x->
s.
jd, s_virtual_sg(x), &y, &m, &d);
1194 x->
c.
df = df_local_to_utc(time_to_df(x->
c.hour, x->
c.min, x->
c.sec),
1214 r = df_utc_to_local(x->
c.
df, x->
c.
of);
1215 df_to_time(r, &x->
c.hour, &x->
c.min, &x->
c.sec);
1218 int r, m, d, h, min, s;
1223 r = df_utc_to_local(x->
c.
df, x->
c.
of);
1224 df_to_time(r, &h, &min, &s);
1240 x->
flags &= ~HAVE_CIVIL;
1252 c_civil_to_jd(x->
c.
year, x->
c.mon, x->
c.mday,
1253 c_virtual_sg(x), &
jd, &ns);
1256 c_virtual_sg(x), &
jd, &ns);
1261 x->
c.
jd = jd_local_to_utc(
jd,
1262 time_to_df(x->
c.hour, x->
c.min, x->
c.sec),
1265 x->
c.
jd = jd_local_to_utc(
jd,
1283 int jd, y, m, d, h, min, s;
1289 c_jd_to_civil(
jd, c_virtual_sg(x), &y, &m, &d);
1310 return jd_utc_to_local(x->
c.
jd, x->
c.
df, x->
c.
of);
1318 return df_utc_to_local(x->
c.
df, x->
c.
of);
1322decode_year(
VALUE y,
double style,
1328 period = (style < 0) ?
1338 inth =
DIV(it, ((
long)period));
1341 it =
MOD(it, ((
long)period));
1342 *ry = (
int)it - 4712;
1354encode_year(
VALUE nth,
int y,
double style,
1360 period = (style < 0) ?
1376 if (f_zero_p(*
nth)) {
1386 if (f_zero_p(
nth)) {
1394guess_style(
VALUE y,
double sg)
1407 style = positive_inf;
1409 style = negative_inf;
1419 canonicalize_s_jd(obj, x);
1423 canonicalize_c_jd(obj, x);
1460 encode_jd(
nth,
jd, &rjd);
1487 encode_jd(
nth,
jd, &rjd);
1506 return isec_to_day(m_df(x));
1523m_local_df_in_day(
union DateData *x)
1525 return isec_to_day(m_local_df(x));
1542 return ns_to_day(m_sf(x));
1549 return ns_to_sec(m_sf(x));
1563 fr = isec_to_day(
df);
1565 fr =
f_add(fr, ns_to_day(
sf));
1570#define HALF_DAYS_IN_SECONDS (DAY_IN_SECONDS / 2)
1596 r =
f_add(r, isec_to_day(
df));
1625 r =
f_add(r, isec_to_day(
df));
1647 return isec_to_day(m_of(x));
1670 sg = s_virtual_sg(x);
1675 sg = c_virtual_sg(x);
1678 return sg == positive_inf;
1685 return !m_julian_p(x);
1689m_proleptic_julian_p(
union DateData *x)
1700m_proleptic_gregorian_p(
union DateData *x)
1736 m_gregorian_p(x) ? -1 : +1,
1783static const int yeartab[2][13] = {
1784 { 0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 },
1785 { 0, 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335 }
1789c_julian_to_yday(
int y,
int m,
int d)
1791 assert(m >= 1 && m <= 12);
1792 return yeartab[c_julian_leap_p(y) ? 1 : 0][m] + d;
1796c_gregorian_to_yday(
int y,
int m,
int d)
1798 assert(m >= 1 && m <= 12);
1799 return yeartab[c_gregorian_leap_p(y) ? 1 : 0][m] + d;
1809 sg = m_virtual_sg(x);
1811 if (m_proleptic_gregorian_p(x) ||
1813 return c_gregorian_to_yday(m_year(x), m_mon(x), m_mday(x));
1814 if (m_proleptic_julian_p(x))
1815 return c_julian_to_yday(m_year(x), m_mon(x), m_mday(x));
1816 c_jd_to_ordinal(
jd,
sg, &ry, &rd);
1823 return c_jd_to_wday(m_local_jd(x));
1831 c_jd_to_commercial(m_local_jd(x), m_virtual_sg(x),
1849 m_gregorian_p(x) ? -1 : +1,
1859 c_jd_to_commercial(m_local_jd(x), m_virtual_sg(x),
1880 c_jd_to_weeknum(m_local_jd(x),
f, m_virtual_sg(x),
1888 return m_wnumx(x, 0);
1894 return m_wnumx(x, 1);
1942#define decode_offset(of,s,h,m)\
1945 s = (of < 0) ? '-' : '+';\
1946 a = (of < 0) ? -of : of;\
1947 h = a / HOUR_IN_SECONDS;\
1948 m = a % HOUR_IN_SECONDS / MINUTE_IN_SECONDS;\
1965 return of2str(m_of(x));
1977 return f_kind_of_p(x, cDate);
1987k_rational_p(
VALUE x)
1993expect_numeric(
VALUE x)
1995 if (!k_numeric_p(x))
2001civil_to_jd(
VALUE y,
int m,
int d,
double sg,
2006 double style = guess_style(y,
sg);
2017 decode_year(y, *ns ? -1 : +1, &nth2, ry);
2021 decode_year(y, style,
nth, ry);
2022 c_civil_to_jd(*ry, m, d, style, rjd, ns);
2029 int *ry,
int *rm,
int *rd)
2031 decode_jd(
jd,
nth, rjd);
2032 c_jd_to_civil(*rjd,
sg, ry, rm, rd);
2036ordinal_to_jd(
VALUE y,
int d,
double sg,
2041 double style = guess_style(y,
sg);
2052 decode_year(y, *ns ? -1 : +1, &nth2, ry);
2056 decode_year(y, style,
nth, ry);
2057 c_ordinal_to_jd(*ry, d, style, rjd, ns);
2066 decode_jd(
jd,
nth, rjd);
2067 c_jd_to_ordinal(*rjd,
sg, ry, rd);
2071commercial_to_jd(
VALUE y,
int w,
int d,
double sg,
2076 double style = guess_style(y,
sg);
2081 c_commercial_to_jd(
FIX2INT(y), w, d,
sg, &
jd, ns);
2087 decode_year(y, *ns ? -1 : +1, &nth2, ry);
2091 decode_year(y, style,
nth, ry);
2092 c_commercial_to_jd(*ry, w, d, style, rjd, ns);
2099 int *ry,
int *rw,
int *rd)
2101 decode_jd(
jd,
nth, rjd);
2102 c_jd_to_commercial(*rjd,
sg, ry, rw, rd);
2106weeknum_to_jd(
VALUE y,
int w,
int d,
int f,
double sg,
2111 double style = guess_style(y,
sg);
2122 decode_year(y, *ns ? -1 : +1, &nth2, ry);
2126 decode_year(y, style,
nth, ry);
2127 c_weeknum_to_jd(*ry, w, d,
f, style, rjd, ns);
2134 int *ry,
int *rw,
int *rd)
2136 decode_jd(
jd,
nth, rjd);
2137 c_jd_to_weeknum(*rjd,
f,
sg, ry, rw, rd);
2141nth_kday_to_jd(
VALUE y,
int m,
int n,
int k,
double sg,
2146 double style = guess_style(y,
sg);
2151 c_nth_kday_to_jd(
FIX2INT(y), m, n, k,
sg, &
jd, ns);
2157 decode_year(y, *ns ? -1 : +1, &nth2, ry);
2161 decode_year(y, style,
nth, ry);
2162 c_nth_kday_to_jd(*ry, m, n, k, style, rjd, ns);
2169 int *ry,
int *rm,
int *rn,
int *rk)
2171 decode_jd(
jd,
nth, rjd);
2172 c_jd_to_nth_kday(*rjd,
sg, ry, rm, rn, rk);
2177valid_ordinal_p(
VALUE y,
int d,
double sg,
2182 double style = guess_style(y,
sg);
2188 r = c_valid_ordinal_p(
FIX2INT(y), d,
sg, rd, &
jd, ns);
2196 decode_year(y, *ns ? -1 : +1, &nth2, ry);
2200 decode_year(y, style,
nth, ry);
2201 r = c_valid_ordinal_p(*ry, d, style, rd, rjd, ns);
2207valid_gregorian_p(
VALUE y,
int m,
int d,
2211 decode_year(y, -1,
nth, ry);
2212 return c_valid_gregorian_p(*ry, m, d, rm, rd);
2216valid_civil_p(
VALUE y,
int m,
int d,
double sg,
2218 int *rm,
int *rd,
int *rjd,
2221 double style = guess_style(y,
sg);
2227 r = c_valid_civil_p(
FIX2INT(y), m, d,
sg, rm, rd, &
jd, ns);
2235 decode_year(y, *ns ? -1 : +1, &nth2, ry);
2239 decode_year(y, style,
nth, ry);
2241 r = c_valid_gregorian_p(*ry, m, d, rm, rd);
2243 r = c_valid_julian_p(*ry, m, d, rm, rd);
2246 c_civil_to_jd(*ry, *rm, *rd, style, rjd, ns);
2252valid_commercial_p(
VALUE y,
int w,
int d,
double sg,
2254 int *rw,
int *rd,
int *rjd,
2257 double style = guess_style(y,
sg);
2263 r = c_valid_commercial_p(
FIX2INT(y), w, d,
sg, rw, rd, &
jd, ns);
2271 decode_year(y, *ns ? -1 : +1, &nth2, ry);
2275 decode_year(y, style,
nth, ry);
2276 r = c_valid_commercial_p(*ry, w, d, style, rw, rd, rjd, ns);
2282valid_weeknum_p(
VALUE y,
int w,
int d,
int f,
double sg,
2284 int *rw,
int *rd,
int *rjd,
2287 double style = guess_style(y,
sg);
2293 r = c_valid_weeknum_p(
FIX2INT(y), w, d,
f,
sg, rw, rd, &
jd, ns);
2301 decode_year(y, *ns ? -1 : +1, &nth2, ry);
2305 decode_year(y, style,
nth, ry);
2306 r = c_valid_weeknum_p(*ry, w, d,
f, style, rw, rd, rjd, ns);
2313valid_nth_kday_p(
VALUE y,
int m,
int n,
int k,
double sg,
2315 int *rm,
int *rn,
int *rk,
int *rjd,
2318 double style = guess_style(y,
sg);
2324 r = c_valid_nth_kday_p(
FIX2INT(y), m, n, k,
sg, rm, rn, rk, &
jd, ns);
2332 decode_year(y, *ns ? -1 : +1, &nth2, ry);
2336 decode_year(y, style,
nth, ry);
2337 r = c_valid_nth_kday_p(*ry, m, n, k, style, rm, rn, rk, rjd, ns);
2346offset_to_sec(
VALUE vof,
int *rof)
2348 int try_rational = 1;
2351 switch (
TYPE(vof)) {
2357 if (n != -1 && n != 0 && n != 1)
2375 expect_numeric(vof);
2377 if (!k_rational_p(vof)) {
2378 if (!try_rational) Check_Type(vof,
T_RATIONAL);
2388 vs = day_to_sec(vof);
2390 if (!k_rational_p(vs)) {
2401 if (!f_eqeq_p(vn, vs))
2432#define valid_sg(sg) \
2434 if (!c_valid_start_p(sg)) {\
2436 rb_warning("invalid start is ignored");\
2463 return valid_jd_sub(2, argv2, klass, 1);
2492 if (
NIL_P(valid_jd_sub(2, argv2, klass, 0)))
2501 int m, d, ry, rm, rd;
2511 if (!need_jd && (guess_style(y,
sg) < 0)) {
2512 if (!valid_gregorian_p(y, m, d,
2522 if (!valid_civil_p(y, m, d,
sg,
2529 encode_jd(
nth, rjd, &rjd2);
2538 VALUE vy, vm, vd, vsg;
2551 return valid_civil_sub(4, argv2, klass, 1);
2573 VALUE vy, vm, vd, vsg;
2589 if (
NIL_P(valid_civil_sub(4, argv2, klass, 0)))
2611 if (!valid_ordinal_p(y, d,
sg,
2618 encode_jd(
nth, rjd, &rjd2);
2639 return valid_ordinal_sub(3, argv2, klass, 1);
2671 if (
NIL_P(valid_ordinal_sub(3, argv2, klass, 0)))
2680 int w, d, ry, rw, rd;
2694 if (!valid_commercial_p(y, w, d,
sg,
2701 encode_jd(
nth, rjd, &rjd2);
2710 VALUE vy, vw, vd, vsg;
2723 return valid_commercial_sub(4, argv2, klass, 1);
2741 VALUE vy, vw, vd, vsg;
2757 if (
NIL_P(valid_commercial_sub(4, argv2, klass, 0)))
2767 int w, d,
f, ry, rw, rd;
2782 if (!valid_weeknum_p(y, w, d,
f,
sg,
2789 encode_jd(
nth, rjd, &rjd2);
2797 VALUE vy, vw, vd, vf, vsg;
2811 return valid_weeknum_sub(5, argv2, klass, 1);
2817 VALUE vy, vw, vd, vf, vsg;
2831 if (
NIL_P(valid_weeknum_sub(5, argv2, klass, 0)))
2840 int m, n, k, ry, rm, rn, rk;
2853 if (!valid_nth_kday_p(y, m, n, k,
sg,
2855 &rm, &rn, &rk, &rjd,
2860 encode_jd(
nth, rjd, &rjd2);
2868 VALUE vy, vm, vn, vk, vsg;
2882 return valid_nth_kday_sub(5, argv2, klass, 1);
2888 VALUE vy, vm, vn, vk, vsg;
2902 if (
NIL_P(valid_nth_kday_sub(5, argv2, klass, 0)))
2930 check_numeric(y,
"year");
2931 decode_year(y, +1, &
nth, &ry);
2952 check_numeric(y,
"year");
2953 decode_year(y, -1, &
nth, &ry);
2958d_lite_gc_mark(
void *
ptr)
2970d_lite_memsize(
const void *
ptr)
2976#ifndef HAVE_RB_EXT_RACTOR_SAFE
2977# define RUBY_TYPED_FROZEN_SHAREABLE 0
2988d_simple_new_internal(
VALUE klass,
2991 int y,
int m,
int d,
3007d_complex_new_internal(
VALUE klass,
3011 int y,
int m,
int d,
3012 int h,
int min,
int s,
3021 y, m, d, h, min, s,
flags);
3030d_lite_s_alloc_simple(
VALUE klass)
3032 return d_simple_new_internal(klass,
3040d_lite_s_alloc_complex(
VALUE klass)
3042 return d_complex_new_internal(klass,
3052d_lite_s_alloc(
VALUE klass)
3054 return d_lite_s_alloc_complex(klass);
3060 int *rof,
double *rsg)
3064 decode_day(
f_add(ajd, half_days_in_day),
3069 if (!f_eqeq_p(of2,
t))
3072 decode_jd(
jd, rnth, rjd);
3080 rb_raise(eDateError,
"invalid day fraction");
3090 if (!c_valid_start_p(*rsg)) {
3115 old_to_new(ajd,
of,
sg,
3118 if (!
df && f_zero_p(
sf) && !rof)
3119 return d_simple_new_internal(klass,
3125 return d_complex_new_internal(klass,
3146 return round(d) == d;
3172 if (wholenum_p(d)) {
3183#define jd_trunc d_trunc
3184#define k_trunc d_trunc
3191 if (wholenum_p(h)) {
3208 if (wholenum_p(min)) {
3209 rmin = to_integer(min);
3225 if (wholenum_p(s)) {
3237#define num2num_with_frac(s,n) \
3239 s = s##_trunc(v##s, &fr);\
3240 if (f_nonzero_p(fr)) {\
3242 rb_raise(eDateError, "invalid fraction");\
3247#define num2int_with_frac(s,n) \
3249 s = NUM2INT(s##_trunc(v##s, &fr));\
3250 if (f_nonzero_p(fr)) {\
3252 rb_raise(eDateError, "invalid fraction");\
3257#define canon24oc() \
3261 fr2 = f_add(fr2, INT2FIX(1));\
3267 if (f_nonzero_p(fr2))\
3268 ret = d_lite_plus(ret, fr2);\
3271#define val2sg(vsg,dsg) \
3273 dsg = NUM2DBL(vsg);\
3274 if (!c_valid_start_p(dsg)) {\
3276 rb_warning("invalid start is ignored");\
3298 VALUE vjd, vsg,
jd, fr, fr2, ret;
3311 check_numeric(vjd,
"jd");
3319 decode_jd(
jd, &
nth, &rjd);
3320 ret = d_simple_new_internal(klass,
3349 VALUE vy, vd, vsg, y, fr, fr2, ret;
3364 check_numeric(vd,
"yday");
3367 check_numeric(vy,
"year");
3373 int ry, rd, rjd, ns;
3375 if (!valid_ordinal_p(y, d,
sg,
3379 rb_raise(eDateError,
"invalid date");
3381 ret = d_simple_new_internal(klass,
3419 return date_initialize(
argc,
argv, d_lite_s_alloc_simple(klass));
3425 VALUE vy, vm, vd, vsg, y, fr, fr2, ret;
3446 check_numeric(vd,
"day");
3449 check_numeric(vm,
"month");
3452 check_numeric(vy,
"year");
3456 if (guess_style(y,
sg) < 0) {
3460 if (!valid_gregorian_p(y, m, d,
3463 rb_raise(eDateError,
"invalid date");
3469 int ry, rm, rd, rjd, ns;
3471 if (!valid_civil_p(y, m, d,
sg,
3475 rb_raise(eDateError,
"invalid date");
3503 VALUE vy, vw, vd, vsg, y, fr, fr2, ret;
3519 check_numeric(vd,
"cwday");
3522 check_numeric(vw,
"cweek");
3525 check_numeric(vy,
"year");
3531 int ry, rw, rd, rjd, ns;
3533 if (!valid_commercial_p(y, w, d,
sg,
3537 rb_raise(eDateError,
"invalid date");
3539 ret = d_simple_new_internal(klass,
3553 VALUE vy, vw, vd, vf, vsg, y, fr, fr2, ret;
3581 int ry, rw, rd, rjd, ns;
3583 if (!valid_weeknum_p(y, w, d,
f,
sg,
3587 rb_raise(eDateError,
"invalid date");
3589 ret = d_simple_new_internal(klass,
3602 VALUE vy, vm, vn, vk, vsg, y, fr, fr2, ret;
3630 int ry, rm, rn, rk, rjd, ns;
3632 if (!valid_nth_kday_p(y, m, n, k,
sg,
3634 &rm, &rn, &rk, &rjd,
3636 rb_raise(eDateError,
"invalid date");
3638 ret = d_simple_new_internal(klass,
3649#if !defined(HAVE_GMTIME_R)
3651gmtime_r(
const time_t *
t,
struct tm *tm)
3653 auto struct tm *tmp = gmtime(
t);
3660localtime_r(
const time_t *
t,
struct tm *tm)
3662 auto struct tm *tmp = localtime(
t);
3669static void set_sg(
union DateData *,
double);
3682 VALUE vsg, nth, ret;
3698 if (!localtime_r(&
t, &tm))
3701 y = tm.tm_year + 1900;
3705 decode_year(
INT2FIX(y), -1, &nth, &ry);
3707 ret = d_simple_new_internal(klass,
3719#define set_hash0(k,v) rb_hash_aset(hash, k, v)
3720#define ref_hash0(k) rb_hash_aref(hash, k)
3721#define del_hash0(k) rb_hash_delete(hash, k)
3723#define sym(x) ID2SYM(rb_intern(x""))
3725#define set_hash(k,v) set_hash0(sym(k), v)
3726#define ref_hash(k) ref_hash0(sym(k))
3727#define del_hash(k) del_hash0(sym(k))
3730rt_rewrite_frags(
VALUE hash)
3735 if (!
NIL_P(seconds)) {
3869 long i, eno = 0, idx = 0;
3902 if (k ==
sym(
"ordinal")) {
3905 d = date_s_today(0, (
VALUE *)0, cDate);
3911 else if (k ==
sym(
"civil")) {
3920 d = date_s_today(0, (
VALUE *)0, cDate);
3928 else if (k ==
sym(
"commercial")) {
3937 d = date_s_today(0, (
VALUE *)0, cDate);
3945 else if (k ==
sym(
"wday")) {
3947 d = date_s_today(0, (
VALUE *)0, cDate);
3952 else if (k ==
sym(
"wnum0")) {
3961 d = date_s_today(0, (
VALUE *)0, cDate);
3969 else if (k ==
sym(
"wnum1")) {
3978 d = date_s_today(0, (
VALUE *)0, cDate);
3988 if (
g && k ==
sym(
"time")) {
3989 if (
f_le_p(klass, cDateTime)) {
3991 d = date_s_today(0, (
VALUE *)0, cDate);
4019 int ry, rd, rjd, ns;
4026 encode_jd(nth, rjd, &rjd2);
4034 int ry, rm, rd, rjd, ns;
4041 encode_jd(nth, rjd, &rjd2);
4049 int ry, rw, rd, rjd, ns;
4056 encode_jd(nth, rjd, &rjd2);
4064 int ry, rw, rd, rjd, ns;
4071 encode_jd(nth, rjd, &rjd2);
4082 VALUE jd = rt__valid_jd_p(vjd, sg);
4093 VALUE jd = rt__valid_ordinal_p(year, yday, sg);
4100 VALUE year, mon, mday;
4105 VALUE jd = rt__valid_civil_p(year, mon, mday, sg);
4112 VALUE year, week, wday;
4125 VALUE jd = rt__valid_commercial_p(year, week, wday, sg);
4132 VALUE year, week, wday;
4138 if (f_eqeq_p(wday,
INT2FIX(7)))
4145 VALUE jd = rt__valid_weeknum_p(year, week, wday,
INT2FIX(0), sg);
4152 VALUE year, week, wday;
4164 VALUE jd = rt__valid_weeknum_p(year, week, wday,
INT2FIX(1), sg);
4177 if (!c_valid_start_p(
NUM2DBL(sg))) {
4183 rb_raise(eDateError,
"invalid date");
4190 jd = rt__valid_civil_p(
ref_hash(
"year"),
4194 hash = rt_rewrite_frags(hash);
4195 hash = rt_complete_frags(klass, hash);
4196 jd = rt__valid_date_frags_p(hash, sg);
4200 rb_raise(eDateError,
"invalid date");
4205 decode_jd(jd, &nth, &rjd);
4206 return d_simple_new_internal(klass,
4215 const char *fmt,
size_t flen,
VALUE hash);
4219 const char *default_fmt)
4221 VALUE vstr, vfmt, hash;
4222 const char *
str, *fmt;
4230 "string should have ASCII compatible encoding");
4235 flen =
strlen(default_fmt);
4241 "format should have ASCII compatible encoding");
4282 return date_s__strptime_internal(
argc,
argv, klass,
"%F");
4320 VALUE argv2[2], hash;
4324 hash = date_s__strptime(2, argv2, klass);
4325 return d_new_by_frags(klass, hash, sg);
4350 size_t limit = get_limit(opt);
4360 VALUE vstr, vcomp, hash, opt;
4364 check_limit(vstr, opt);
4368 "string should have ASCII compatible encoding");
4402 return date_s__parse_internal(
argc,
argv, klass);
4451 if (!
NIL_P(opt)) argv2[argc2++] = opt;
4452 VALUE hash = date_s__parse(argc2, argv2, klass);
4453 return d_new_by_frags(klass, hash, sg);
4480 check_limit(
str, opt);
4519 if (!
NIL_P(opt)) argv2[argc2++] = opt;
4520 VALUE hash = date_s__iso8601(argc2, argv2, klass);
4521 return d_new_by_frags(klass, hash, sg);
4541 check_limit(
str, opt);
4578 if (!
NIL_P(opt)) argv2[argc2++] = opt;
4579 VALUE hash = date_s__rfc3339(argc2, argv2, klass);
4580 return d_new_by_frags(klass, hash, sg);
4600 check_limit(
str, opt);
4637 if (!
NIL_P(opt)) argv2[argc2++] = opt;
4638 VALUE hash = date_s__xmlschema(argc2, argv2, klass);
4639 return d_new_by_frags(klass, hash, sg);
4660 check_limit(
str, opt);
4698 if (!
NIL_P(opt)) argv2[argc2++] = opt;
4699 VALUE hash = date_s__rfc2822(argc2, argv2, klass);
4700 return d_new_by_frags(klass, hash, sg);
4720 check_limit(
str, opt);
4757 if (!
NIL_P(opt)) argv2[argc2++] = opt;
4758 VALUE hash = date_s__httpdate(argc2, argv2, klass);
4759 return d_new_by_frags(klass, hash, sg);
4779 check_limit(
str, opt);
4820 if (!
NIL_P(opt)) argv2[argc2++] = opt;
4821 VALUE hash = date_s__jisx0301(argc2, argv2, klass);
4822 return d_new_by_frags(klass, hash, sg);
4853dup_obj_as_complex(
VALUE self)
4878#define val2off(vof,iof) \
4880 if (!offset_to_sec(vof, &iof)) {\
4882 rb_warning("invalid offset is ignored");\
4890 VALUE jd, vjd, vdf, sf, vsf, vof, vsg;
4913 rb_raise(eDateError,
"invalid second fraction");
4917 rb_raise(eDateError,
"invalid day fraction");
4928 decode_jd(jd, &nth, &rjd);
4929 if (!df && f_zero_p(sf) && !of) {
4935 "cannot load complex into simple");
4947d_lite_initialize_copy(
VALUE copy,
VALUE date)
4961 adat->c.nth = bdat->s.nth;
4962 adat->c.jd = bdat->s.jd;
4966 adat->c.sg = bdat->s.sg;
4967 adat->c.year = bdat->s.year;
4969 adat->c.mon = bdat->s.mon;
4970 adat->c.mday = bdat->s.mday;
4971 adat->c.hour = bdat->s.hour;
4972 adat->c.min = bdat->s.min;
4973 adat->c.sec = bdat->s.sec;
4975 adat->c.pc = bdat->s.pc;
4982 "cannot load complex into simple");
4992d_lite_fill(
VALUE self)
5021d_lite_ajd(
VALUE self)
5038d_lite_amjd(
VALUE self)
5055d_lite_jd(
VALUE self)
5058 return m_real_local_jd(dat);
5072d_lite_mjd(
VALUE self)
5088d_lite_ld(
VALUE self)
5104d_lite_year(
VALUE self)
5107 return m_real_year(dat);
5119d_lite_yday(
VALUE self)
5135d_lite_mon(
VALUE self)
5151d_lite_mday(
VALUE self)
5166d_lite_day_fraction(
VALUE self)
5184d_lite_cwyear(
VALUE self)
5187 return m_real_cwyear(dat);
5199d_lite_cweek(
VALUE self)
5214d_lite_cwday(
VALUE self)
5222d_lite_wnum0(
VALUE self)
5229d_lite_wnum1(
VALUE self)
5245d_lite_wday(
VALUE self)
5258d_lite_sunday_p(
VALUE self)
5271d_lite_monday_p(
VALUE self)
5284d_lite_tuesday_p(
VALUE self)
5297d_lite_wednesday_p(
VALUE self)
5310d_lite_thursday_p(
VALUE self)
5323d_lite_friday_p(
VALUE self)
5336d_lite_saturday_p(
VALUE self)
5350 if (
NUM2INT(k) != m_wday(dat))
5353 c_nth_kday_to_jd(m_year(dat), m_mon(dat),
5356 if (m_local_jd(dat) != rjd)
5371d_lite_hour(
VALUE self)
5387d_lite_min(
VALUE self)
5403d_lite_sec(
VALUE self)
5419d_lite_sec_fraction(
VALUE self)
5422 return m_sf_in_sec(dat);
5434d_lite_offset(
VALUE self)
5437 return m_of_in_day(dat);
5449d_lite_zone(
VALUE self)
5465d_lite_julian_p(
VALUE self)
5481d_lite_gregorian_p(
VALUE self)
5497d_lite_leap_p(
VALUE self)
5499 int rjd, ns, ry, rm, rd;
5502 if (m_gregorian_p(dat))
5503 return f_boolcast(c_gregorian_leap_p(m_year(dat)));
5505 c_civil_to_jd(m_year(dat), 3, 1, m_virtual_sg(dat),
5507 c_jd_to_civil(rjd - 1, m_virtual_sg(dat), &ry, &rm, &rd);
5521d_lite_start(
VALUE self)
5538 x->
s.
flags &= ~HAVE_CIVIL;
5556set_sg(
union DateData *x,
double sg)
5571dup_obj_with_new_start(
VALUE obj,
double sg)
5573 volatile VALUE dup = dup_obj(obj);
5602 return dup_obj_with_new_start(self, sg);
5612d_lite_italy(
VALUE self)
5614 return dup_obj_with_new_start(self,
ITALY);
5624d_lite_england(
VALUE self)
5626 return dup_obj_with_new_start(self,
ENGLAND);
5636d_lite_julian(
VALUE self)
5638 return dup_obj_with_new_start(self,
JULIAN);
5648d_lite_gregorian(
VALUE self)
5650 return dup_obj_with_new_start(self,
GREGORIAN);
5664dup_obj_with_new_offset(
VALUE obj,
int of)
5666 volatile VALUE dup = dup_obj_as_complex(obj);
5696 return dup_obj_with_new_offset(self, rof);
5718 int try_rational = 1;
5722 switch (
TYPE(other)) {
5739 jd = m_jd(dat) + (
int)
t;
5753 dat->c.df, dat->c.sf,
5754 dat->c.of, dat->c.
sg,
5792 jd = m_jd(dat) + jd;
5799 nth =
f_add(m_nth(dat), nth);
5811 dat->c.df, dat->c.sf,
5812 dat->c.of, dat->c.
sg,
5871 sf =
f_add(m_sf(dat), sf);
5885 df = m_df(dat) + df;
5899 jd = m_jd(dat) + jd;
5906 nth =
f_add(m_nth(dat), nth);
5908 if (!df && f_zero_p(sf) && !m_of(dat))
5920 m_of(dat), m_sg(dat),
5929 expect_numeric(other);
5931 if (!k_rational_p(other)) {
5932 if (!try_rational) Check_Type(other,
T_RATIONAL);
5942 if (wholenum_p(other)) {
5976 sf =
f_add(m_sf(dat), sf);
5990 df = m_df(dat) + df;
6004 jd = m_jd(dat) + jd;
6011 nth =
f_add(m_nth(dat), nth);
6013 if (!df && f_zero_p(sf) && !m_of(dat))
6025 m_of(dat), m_sg(dat),
6045 n =
f_sub(m_nth(adat), m_nth(bdat));
6046 d = m_jd(adat) - m_jd(bdat);
6047 df = m_df(adat) - m_df(bdat);
6048 sf =
f_sub(m_sf(adat), m_sf(bdat));
6077 r =
f_add(r, isec_to_day(df));
6079 r =
f_add(r, ns_to_day(sf));
6107 if (k_date_p(other))
6108 return minus_dd(self, other);
6110 switch (
TYPE(other)) {
6116 expect_numeric(other);
6120 return d_lite_plus(self,
f_negate(other));
6138 return d_lite_plus(self, n);
6155 return d_lite_minus(self, n);
6166d_lite_next(
VALUE self)
6168 return d_lite_next_day(0, (
VALUE *)
NULL, self);
6222 if (valid_civil_p(y, m, d, sg,
6224 &rm, &rd, &rjd, &ns))
6227 rb_raise(eDateError,
"invalid date");
6229 encode_jd(nth, rjd, &rjd2);
6230 return d_lite_plus(self,
f_sub(rjd2, m_real_local_jd(dat)));
6259 expect_numeric(other);
6260 return d_lite_rshift(self,
f_negate(other));
6279 return d_lite_rshift(self, n);
6298 return d_lite_lshift(self, n);
6363 VALUE limit, step, date;
6381 while (
FIX2INT(d_lite_cmp(date, limit)) >= 0) {
6383 date = d_lite_plus(date, step);
6391 while (
FIX2INT(d_lite_cmp(date, limit)) <= 0) {
6393 date = d_lite_plus(date, step);
6414 while (
FIX2INT(d_lite_cmp(date,
max)) <= 0) {
6416 date = d_lite_plus(date,
INT2FIX(1));
6436 while (
FIX2INT(d_lite_cmp(date, min)) >= 0) {
6438 date = d_lite_plus(date,
INT2FIX(-1));
6448 if (k_numeric_p(other))
6449 return INT2FIX(f_cmp(m_ajd(dat), other));
6450 else if (k_date_p(other))
6466 m_canonicalize_jd(self, adat);
6467 m_canonicalize_jd(other, bdat);
6468 a_nth = m_nth(adat);
6469 b_nth = m_nth(bdat);
6470 if (f_eqeq_p(a_nth, b_nth)) {
6479 if (f_eqeq_p(a_sf, b_sf)) {
6482 else if (
f_lt_p(a_sf, b_sf)) {
6489 else if (a_df < b_df) {
6496 else if (a_jd < b_jd) {
6503 else if (
f_lt_p(a_nth, b_nth)) {
6531 if (!k_date_p(other))
6532 return cmp_gen(self, other);
6538 m_gregorian_p(adat) == m_gregorian_p(bdat)))
6539 return cmp_dd(self, other);
6545 m_canonicalize_jd(self, adat);
6546 m_canonicalize_jd(other, bdat);
6547 a_nth = m_nth(adat);
6548 b_nth = m_nth(bdat);
6549 if (f_eqeq_p(a_nth, b_nth)) {
6555 else if (a_jd < b_jd) {
6562 else if (
f_lt_p(a_nth, b_nth)) {
6577 if (k_numeric_p(other))
6578 return f_eqeq_p(m_real_local_jd(dat), other);
6579 else if (k_date_p(other))
6580 return f_eqeq_p(m_real_local_jd(dat),
f_jd(other));
6604 if (!k_date_p(other))
6605 return equal_gen(self, other);
6610 if (!(m_gregorian_p(adat) == m_gregorian_p(bdat)))
6611 return equal_gen(self, other);
6617 m_canonicalize_jd(self, adat);
6618 m_canonicalize_jd(other, bdat);
6619 a_nth = m_nth(adat);
6620 b_nth = m_nth(bdat);
6621 a_jd = m_local_jd(adat);
6622 b_jd = m_local_jd(bdat);
6623 if (f_eqeq_p(a_nth, b_nth) &&
6635 if (!k_date_p(other))
6637 return f_zero_p(d_lite_cmp(self, other));
6642d_lite_hash(
VALUE self)
6656static void set_tmx(
VALUE,
struct tmx *);
6670d_lite_to_s(
VALUE self)
6672 return strftimev(
"%Y-%m-%d", self, set_tmx);
6696 x->
s.
year, x->
s.mon, x->
s.mday,
6708 "%dy%dm%dd %dh%dm%ds; %s>",
6713 x->
c.
year, x->
c.mon, x->
c.mday,
6714 x->
c.hour, x->
c.min, x->
c.sec,
6726d_lite_inspect_raw(
VALUE self)
6740 m_real_jd(x), m_df(x), m_sf(x),
6756d_lite_inspect(
VALUE self)
6765size_t date_strftime(
char *s,
size_t maxsize,
const char *format,
6770date_strftime_alloc(
char **
buf,
const char *format,
6782 if (
len != 0 || (**
buf ==
'\0' && errno != ERANGE))
return len;
6796 if (
size >= 1024 * flen) {
6810 s = day_to_sec(
f_sub(m_real_jd(x),
6820#define MILLISECOND_IN_NANOSECONDS 1000000
6827 s = sec_to_ms(tmx_m_secs(x));
6851 (
VALUE (*)(
void *))m_real_year,
6852 (
int (*)(
void *))m_yday,
6853 (
int (*)(
void *))m_mon,
6854 (
int (*)(
void *))m_mday,
6855 (
VALUE (*)(
void *))m_real_cwyear,
6856 (
int (*)(
void *))m_cweek,
6857 (
int (*)(
void *))m_cwday,
6858 (
int (*)(
void *))m_wnum0,
6859 (
int (*)(
void *))m_wnum1,
6860 (
int (*)(
void *))m_wday,
6861 (
int (*)(
void *))m_hour,
6862 (
int (*)(
void *))m_min,
6863 (
int (*)(
void *))m_sec,
6864 (
VALUE (*)(
void *))m_sf_in_sec,
6865 (
VALUE (*)(
void *))tmx_m_secs,
6866 (
VALUE (*)(
void *))tmx_m_msecs,
6867 (
int (*)(
void *))tmx_m_of,
6868 (
char *(*)(
void *))tmx_m_zone
6881 const char *default_fmt,
6899 "format should have ASCII compatible encoding");
6904 (*func)(self, &
tmx);
6905 if (memchr(fmt,
'\0',
len)) {
6907 const char *p = fmt, *pe = fmt +
len;
6911 len = date_strftime_alloc(&
buf, p, &
tmx);
6914 if (
buf != buffer) {
6918 for (fmt = p; p < pe && !*p; ++p);
6925 len = date_strftime_alloc(&
buf, fmt, &
tmx);
7113 return date_strftime_internal(
argc,
argv, self,
7114 "%Y-%m-%d", set_tmx);
7126 (*func)(self, &
tmx);
7127 len = date_strftime_alloc(&
buf, fmt, &
tmx);
7145d_lite_asctime(
VALUE self)
7147 return strftimev(
"%a %b %e %H:%M:%S %Y", self, set_tmx);
7158d_lite_iso8601(
VALUE self)
7160 return strftimev(
"%Y-%m-%d", self, set_tmx);
7170d_lite_rfc3339(
VALUE self)
7172 return strftimev(
"%Y-%m-%dT%H:%M:%S%:z", self, set_tmx);
7183d_lite_rfc2822(
VALUE self)
7185 return strftimev(
"%a, %-d %b %Y %T %z", self, set_tmx);
7196d_lite_httpdate(
VALUE self)
7198 volatile VALUE dup = dup_obj_with_new_offset(self, 0);
7199 return strftimev(
"%a, %d %b %Y %T GMT", dup, set_tmx);
7220 else if (d < 2424875) {
7224 else if (d < 2447535) {
7228 else if (d < 2458605) {
7251d_lite_jisx0301(
VALUE self)
7257 fmt = jisx0301_date_format(fmtbuf,
sizeof(fmtbuf),
7258 m_real_local_jd(
dat),
7265d_lite_marshal_dump_old(
VALUE self)
7287d_lite_marshal_dump(
VALUE self)
7328 VALUE ajd, vof, vsg;
7334 if (!k_numeric_p(vsg))
7343 old_to_new(ajd, vof, vsg,
7344 &nth, &jd, &df, &sf, &of, &sg);
7363 if (df || !f_zero_p(sf) || of) {
7392 obj = d_lite_s_alloc(klass);
7393 return d_lite_marshal_load(obj, a);
7413 VALUE vjd, vh, vmin, vs, vof, vsg, jd, fr, fr2, ret;
7432 check_numeric(vs,
"second");
7435 check_numeric(vmin,
"minute");
7438 check_numeric(vh,
"hour");
7441 check_numeric(vjd,
"jd");
7447 int rh, rmin, rs, rjd, rjd2;
7449 if (!c_valid_time_p(h, min, s, &rh, &rmin, &rs))
7450 rb_raise(eDateError,
"invalid date");
7453 decode_jd(jd, &nth, &rjd);
7454 rjd2 = jd_local_to_utc(rjd,
7455 time_to_df(rh, rmin, rs),
7458 ret = d_complex_new_internal(klass,
7485 VALUE vy, vd, vh, vmin, vs, vof, vsg, y, fr, fr2, ret;
7486 int d, h, min, s, rof;
7505 check_numeric(vs,
"second");
7508 check_numeric(vmin,
"minute");
7511 check_numeric(vh,
"hour");
7514 check_numeric(vd,
"yday");
7517 check_numeric(vy,
"year");
7523 int ry, rd, rh, rmin, rs, rjd, rjd2, ns;
7525 if (!valid_ordinal_p(y, d, sg,
7529 rb_raise(eDateError,
"invalid date");
7530 if (!c_valid_time_p(h, min, s, &rh, &rmin, &rs))
7531 rb_raise(eDateError,
"invalid date");
7534 rjd2 = jd_local_to_utc(rjd,
7535 time_to_df(rh, rmin, rs),
7538 ret = d_complex_new_internal(klass,
7566 return datetime_initialize(
argc,
argv, d_lite_s_alloc_complex(klass));
7572 VALUE vy, vm, vd, vh, vmin, vs, vof, vsg, y, fr, fr2, ret;
7573 int m, d, h, min, s, rof;
7581 rb_scan_args(
argc,
argv,
"08", &vy, &vm, &vd, &vh, &vmin, &vs, &vof, &vsg);
7598 check_numeric(vs,
"second");
7601 check_numeric(vmin,
"minute");
7604 check_numeric(vh,
"hour");
7607 check_numeric(vd,
"day");
7610 check_numeric(vm,
"month");
7613 check_numeric(vy,
"year");
7617 if (guess_style(y,
sg) < 0) {
7619 int ry, rm, rd, rh, rmin, rs;
7621 if (!valid_gregorian_p(y, m, d,
7624 rb_raise(eDateError,
"invalid date");
7625 if (!c_valid_time_p(h, min, s, &rh, &rmin, &rs))
7626 rb_raise(eDateError,
"invalid date");
7639 int ry, rm, rd, rh, rmin, rs, rjd, rjd2, ns;
7641 if (!valid_civil_p(y, m, d,
sg,
7645 rb_raise(eDateError,
"invalid date");
7646 if (!c_valid_time_p(h, min, s, &rh, &rmin, &rs))
7647 rb_raise(eDateError,
"invalid date");
7650 rjd2 = jd_local_to_utc(rjd,
7651 time_to_df(rh, rmin, rs),
7681 VALUE vy, vw, vd, vh, vmin, vs, vof, vsg, y, fr, fr2, ret;
7682 int w, d, h, min, s, rof;
7685 rb_scan_args(
argc,
argv,
"08", &vy, &vw, &vd, &vh, &vmin, &vs, &vof, &vsg);
7702 check_numeric(vs,
"second");
7705 check_numeric(vmin,
"minute");
7708 check_numeric(vh,
"hour");
7711 check_numeric(vd,
"cwday");
7714 check_numeric(vw,
"cweek");
7717 check_numeric(vy,
"year");
7723 int ry, rw, rd, rh, rmin, rs, rjd, rjd2, ns;
7725 if (!valid_commercial_p(y, w, d,
sg,
7729 rb_raise(eDateError,
"invalid date");
7730 if (!c_valid_time_p(h, min, s, &rh, &rmin, &rs))
7731 rb_raise(eDateError,
"invalid date");
7734 rjd2 = jd_local_to_utc(rjd,
7735 time_to_df(rh, rmin, rs),
7738 ret = d_complex_new_internal(klass,
7754 VALUE vy, vw, vd, vf, vh, vmin, vs, vof, vsg, y, fr, fr2, ret;
7755 int w, d,
f, h, min, s, rof;
7759 &vh, &vmin, &vs, &vof, &vsg);
7794 int ry, rw, rd, rh, rmin, rs, rjd, rjd2, ns;
7796 if (!valid_weeknum_p(y, w, d,
f,
sg,
7800 rb_raise(eDateError,
"invalid date");
7801 if (!c_valid_time_p(h, min, s, &rh, &rmin, &rs))
7802 rb_raise(eDateError,
"invalid date");
7805 rjd2 = jd_local_to_utc(rjd,
7806 time_to_df(rh, rmin, rs),
7808 ret = d_complex_new_internal(klass,
7823 VALUE vy, vm, vn, vk, vh, vmin, vs, vof, vsg, y, fr, fr2, ret;
7824 int m, n, k, h, min, s, rof;
7828 &vh, &vmin, &vs, &vof, &vsg);
7863 int ry, rm, rn, rk, rh, rmin, rs, rjd, rjd2, ns;
7865 if (!valid_nth_kday_p(y, m, n, k,
sg,
7867 &rm, &rn, &rk, &rjd,
7869 rb_raise(eDateError,
"invalid date");
7870 if (!c_valid_time_p(h, min, s, &rh, &rmin, &rs))
7871 rb_raise(eDateError,
"invalid date");
7874 rjd2 = jd_local_to_utc(rjd,
7875 time_to_df(rh, rmin, rs),
7877 ret = d_complex_new_internal(klass,
7903#ifdef HAVE_CLOCK_GETTIME
7911 int y, ry, m, d, h, min, s;
7920#ifdef HAVE_CLOCK_GETTIME
7930 if (!localtime_r(&sec, &tm))
7933 y = tm.tm_year + 1900;
7941#ifdef HAVE_STRUCT_TM_TM_GMTOFF
7943#elif defined(HAVE_TIMEZONE)
7953 of += (
long)difftime(sec2, sec);
7956#elif defined(HAVE_TIMEGM)
7961 of = (
long)difftime(sec2, sec);
7968 if (!gmtime_r(&sec, &tm2))
7970 tm2.tm_isdst = tm.tm_isdst;
7971 sec2 = mktime(&tm2);
7972 of = (
long)difftime(sec, sec2);
7975#ifdef HAVE_CLOCK_GETTIME
7978 sf = tv.tv_usec * 1000;
7986 decode_year(
INT2FIX(y), -1, &nth, &ry);
7988 ret = d_complex_new_internal(klass,
8008 if (!c_valid_start_p(
NUM2DBL(sg))) {
8014 rb_raise(eDateError,
"invalid date");
8021 jd = rt__valid_civil_p(
ref_hash(
"year"),
8035 hash = rt_rewrite_frags(hash);
8036 hash = rt_complete_frags(klass, hash);
8037 jd = rt__valid_date_frags_p(hash, sg);
8041 rb_raise(eDateError,
"invalid date");
8050 rb_raise(eDateError,
"invalid date");
8052 df = time_to_df(rh, rmin, rs);
8075 decode_jd(jd, &nth, &rjd);
8076 rjd2 = jd_local_to_utc(rjd, df, of);
8077 df = df_local_to_utc(df, of);
8079 return d_complex_new_internal(klass,
8102 return date_s__strptime_internal(
argc,
argv, klass,
"%FT%T%z");
8149 VALUE argv2[2], hash;
8153 hash = date_s__strptime(2, argv2, klass);
8154 return dt_new_by_frags(klass, hash, sg);
8207 if (!
NIL_P(opt)) argc2++;
8208 VALUE hash = date_s__parse(argc2, argv2, klass);
8209 return dt_new_by_frags(klass, hash, sg);
8251 if (!
NIL_P(opt)) argc2--;
8252 VALUE hash = date_s__iso8601(argc2, argv2, klass);
8253 return dt_new_by_frags(klass, hash, sg);
8291 if (!
NIL_P(opt)) argc2++;
8292 VALUE hash = date_s__rfc3339(argc2, argv2, klass);
8293 return dt_new_by_frags(klass, hash, sg);
8331 if (!
NIL_P(opt)) argc2++;
8332 VALUE hash = date_s__xmlschema(argc2, argv2, klass);
8333 return dt_new_by_frags(klass, hash, sg);
8372 if (!
NIL_P(opt)) argc2++;
8373 VALUE hash = date_s__rfc2822(argc2, argv2, klass);
8374 return dt_new_by_frags(klass, hash, sg);
8412 if (!
NIL_P(opt)) argc2++;
8413 VALUE hash = date_s__httpdate(argc2, argv2, klass);
8414 return dt_new_by_frags(klass, hash, sg);
8457 if (!
NIL_P(opt)) argc2++;
8458 VALUE hash = date_s__jisx0301(argc2, argv2, klass);
8459 return dt_new_by_frags(klass, hash, sg);
8474dt_lite_to_s(
VALUE self)
8476 return strftimev(
"%Y-%m-%dT%H:%M:%S%:z", self, set_tmx);
8660 return date_strftime_internal(
argc,
argv, self,
8661 "%Y-%m-%dT%H:%M:%S%:z", set_tmx);
8665iso8601_timediv(
VALUE self,
long n)
8667 static const char timefmt[] =
"T%H:%M:%S";
8668 static const char zone[] =
"%:z";
8673 memcpy(p, timefmt,
sizeof(timefmt)-1);
8674 p +=
sizeof(timefmt)-1;
8675 if (n > 0) p +=
snprintf(p, fmt+
sizeof(fmt)-p,
".%%%ldN", n);
8701 iso8601_timediv(self, n));
8717 return dt_lite_iso8601(
argc,
argv, self);
8740 iso8601_timediv(self, n));
8745#define f_subsec(x) rb_funcall(x, rb_intern("subsec"), 0)
8746#define f_utc_offset(x) rb_funcall(x, rb_intern("utc_offset"), 0)
8747#define f_local3(x,y,m,d) rb_funcall(x, rb_intern("local"), 3, y, m, d)
8756time_to_time(
VALUE self)
8768time_to_date(
VALUE self)
8777 decode_year(y, -1, &nth, &ry);
8779 ret = d_simple_new_internal(cDate,
8798time_to_datetime(
VALUE self)
8800 VALUE y, sf, nth, ret;
8801 int ry, m, d, h, min, s, of;
8816 decode_year(y, -1, &nth, &ry);
8818 ret = d_complex_new_internal(cDateTime,
8840date_to_time(
VALUE self)
8844 if (m_julian_p(adat)) {
8845 VALUE tmp = d_lite_gregorian(self);
8863date_to_date(
VALUE self)
8875date_to_datetime(
VALUE self)
8880 VALUE new = d_lite_s_alloc_simple(cDateTime);
8888 VALUE new = d_lite_s_alloc_complex(cDateTime);
8915datetime_to_time(
VALUE self)
8917 volatile VALUE dup = dup_obj(self);
8945datetime_to_date(
VALUE self)
8950 VALUE new = d_lite_s_alloc_simple(cDate);
8954 bdat->s.jd = m_local_jd(adat);
8959 VALUE new = d_lite_s_alloc_simple(cDate);
8963 bdat->s.jd = m_local_jd(adat);
8977datetime_to_datetime(
VALUE self)
8985#define MIN_YEAR -4713
8986#define MAX_YEAR 1000000
8988#define MAX_JD 366963925
8991test_civil(
int from,
int to,
double sg)
8995 fprintf(stderr,
"test_civil: %d...%d (%d) - %.0f\n",
8996 from, to, to - from, sg);
8997 for (j = from; j <= to; j++) {
8998 int y, m, d, rj, ns;
9000 c_jd_to_civil(j, sg, &y, &m, &d);
9001 c_civil_to_jd(y, m, d, sg, &rj, &ns);
9003 fprintf(stderr,
"%d != %d\n", j, rj);
9011date_s_test_civil(
VALUE klass)
9013 if (!test_civil(MIN_JD, MIN_JD + 366,
GREGORIAN))
9015 if (!test_civil(2305814, 2598007,
GREGORIAN))
9017 if (!test_civil(MAX_JD - 366, MAX_JD,
GREGORIAN))
9020 if (!test_civil(MIN_JD, MIN_JD + 366,
ITALY))
9022 if (!test_civil(2305814, 2598007,
ITALY))
9024 if (!test_civil(MAX_JD - 366, MAX_JD,
ITALY))
9031test_ordinal(
int from,
int to,
double sg)
9035 fprintf(stderr,
"test_ordinal: %d...%d (%d) - %.0f\n",
9036 from, to, to - from, sg);
9037 for (j = from; j <= to; j++) {
9040 c_jd_to_ordinal(j, sg, &y, &d);
9041 c_ordinal_to_jd(y, d, sg, &rj, &ns);
9043 fprintf(stderr,
"%d != %d\n", j, rj);
9051date_s_test_ordinal(
VALUE klass)
9053 if (!test_ordinal(MIN_JD, MIN_JD + 366,
GREGORIAN))
9055 if (!test_ordinal(2305814, 2598007,
GREGORIAN))
9057 if (!test_ordinal(MAX_JD - 366, MAX_JD,
GREGORIAN))
9060 if (!test_ordinal(MIN_JD, MIN_JD + 366,
ITALY))
9062 if (!test_ordinal(2305814, 2598007,
ITALY))
9064 if (!test_ordinal(MAX_JD - 366, MAX_JD,
ITALY))
9071test_commercial(
int from,
int to,
double sg)
9075 fprintf(stderr,
"test_commercial: %d...%d (%d) - %.0f\n",
9076 from, to, to - from, sg);
9077 for (j = from; j <= to; j++) {
9078 int y, w, d, rj, ns;
9080 c_jd_to_commercial(j, sg, &y, &w, &d);
9081 c_commercial_to_jd(y, w, d, sg, &rj, &ns);
9083 fprintf(stderr,
"%d != %d\n", j, rj);
9091date_s_test_commercial(
VALUE klass)
9093 if (!test_commercial(MIN_JD, MIN_JD + 366,
GREGORIAN))
9095 if (!test_commercial(2305814, 2598007,
GREGORIAN))
9097 if (!test_commercial(MAX_JD - 366, MAX_JD,
GREGORIAN))
9100 if (!test_commercial(MIN_JD, MIN_JD + 366,
ITALY))
9102 if (!test_commercial(2305814, 2598007,
ITALY))
9104 if (!test_commercial(MAX_JD - 366, MAX_JD,
ITALY))
9111test_weeknum(
int from,
int to,
int f,
double sg)
9115 fprintf(stderr,
"test_weeknum: %d...%d (%d) - %.0f\n",
9116 from, to, to - from, sg);
9117 for (j = from; j <= to; j++) {
9118 int y, w, d, rj, ns;
9120 c_jd_to_weeknum(j,
f, sg, &y, &w, &d);
9121 c_weeknum_to_jd(y, w, d,
f, sg, &rj, &ns);
9123 fprintf(stderr,
"%d != %d\n", j, rj);
9131date_s_test_weeknum(
VALUE klass)
9135 for (
f = 0;
f <= 1;
f++) {
9136 if (!test_weeknum(MIN_JD, MIN_JD + 366,
f,
GREGORIAN))
9138 if (!test_weeknum(2305814, 2598007,
f,
GREGORIAN))
9140 if (!test_weeknum(MAX_JD - 366, MAX_JD,
f,
GREGORIAN))
9143 if (!test_weeknum(MIN_JD, MIN_JD + 366,
f,
ITALY))
9145 if (!test_weeknum(2305814, 2598007,
f,
ITALY))
9147 if (!test_weeknum(MAX_JD - 366, MAX_JD,
f,
ITALY))
9155test_nth_kday(
int from,
int to,
double sg)
9159 fprintf(stderr,
"test_nth_kday: %d...%d (%d) - %.0f\n",
9160 from, to, to - from, sg);
9161 for (j = from; j <= to; j++) {
9162 int y, m, n, k, rj, ns;
9164 c_jd_to_nth_kday(j, sg, &y, &m, &n, &k);
9165 c_nth_kday_to_jd(y, m, n, k, sg, &rj, &ns);
9167 fprintf(stderr,
"%d != %d\n", j, rj);
9175date_s_test_nth_kday(
VALUE klass)
9177 if (!test_nth_kday(MIN_JD, MIN_JD + 366,
GREGORIAN))
9179 if (!test_nth_kday(2305814, 2598007,
GREGORIAN))
9181 if (!test_nth_kday(MAX_JD - 366, MAX_JD,
GREGORIAN))
9184 if (!test_nth_kday(MIN_JD, MIN_JD + 366,
ITALY))
9186 if (!test_nth_kday(2305814, 2598007,
ITALY))
9188 if (!test_nth_kday(MAX_JD - 366, MAX_JD,
ITALY))
9195test_unit_v2v(
VALUE i,
9202 return f_eqeq_p(o, i);
9209 if (!test_unit_v2v(
INT2FIX(0), conv1, conv2))
9211 if (!test_unit_v2v(
INT2FIX(1), conv1, conv2))
9213 if (!test_unit_v2v(
INT2FIX(2), conv1, conv2))
9215 if (!test_unit_v2v(
INT2FIX(3), conv1, conv2))
9217 if (!test_unit_v2v(
INT2FIX(11), conv1, conv2))
9219 if (!test_unit_v2v(
INT2FIX(65535), conv1, conv2))
9221 if (!test_unit_v2v(
INT2FIX(1073741823), conv1, conv2))
9223 if (!test_unit_v2v(
INT2NUM(1073741824), conv1, conv2))
9240 if (!test_unit_v2v_iter2(conv1, conv2))
9242 if (!test_unit_v2v_iter2(conv2, conv1))
9248date_s_test_unit_conv(
VALUE klass)
9250 if (!test_unit_v2v_iter(sec_to_day, day_to_sec))
9252 if (!test_unit_v2v_iter(ms_to_sec, sec_to_ms))
9254 if (!test_unit_v2v_iter(ns_to_day, day_to_ns))
9256 if (!test_unit_v2v_iter(ns_to_sec, sec_to_ns))
9262date_s_test_all(
VALUE klass)
9264 if (date_s_test_civil(klass) ==
Qfalse)
9266 if (date_s_test_ordinal(klass) ==
Qfalse)
9268 if (date_s_test_commercial(klass) ==
Qfalse)
9270 if (date_s_test_weeknum(klass) ==
Qfalse)
9272 if (date_s_test_nth_kday(klass) ==
Qfalse)
9274 if (date_s_test_unit_conv(klass) ==
Qfalse)
9280static const char *monthnames[] = {
9282 "January",
"February",
"March",
9283 "April",
"May",
"June",
9284 "July",
"August",
"September",
9285 "October",
"November",
"December"
9288static const char *abbr_monthnames[] = {
9290 "Jan",
"Feb",
"Mar",
"Apr",
9291 "May",
"Jun",
"Jul",
"Aug",
9292 "Sep",
"Oct",
"Nov",
"Dec"
9295static const char *daynames[] = {
9296 "Sunday",
"Monday",
"Tuesday",
"Wednesday",
9297 "Thursday",
"Friday",
"Saturday"
9300static const char *abbr_daynames[] = {
9301 "Sun",
"Mon",
"Tue",
"Wed",
9306mk_ary_of_str(
long len,
const char *a[])
9312 for (i = 0; i <
len; i++) {
9336 #ifdef HAVE_RB_EXT_RACTOR_SAFE
9339 id_cmp = rb_intern_const(
"<=>");
9340 id_le_p = rb_intern_const(
"<=");
9341 id_ge_p = rb_intern_const(
">=");
9342 id_eqeq_p = rb_intern_const(
"==");
9346#if (LONG_MAX / DAY_IN_SECONDS) > SECOND_IN_NANOSECONDS
9349#elif defined HAVE_LONG_LONG
9523 mk_ary_of_str(13, abbr_monthnames));
9533 rb_define_const(cDate,
"ABBR_DAYNAMES", mk_ary_of_str(7, abbr_daynames));
9559 date_s__valid_jd_p, -1);
9561 date_s__valid_ordinal_p, -1);
9563 date_s__valid_civil_p, -1);
9565 date_s__valid_civil_p, -1);
9567 date_s__valid_commercial_p, -1);
9569 date_s__valid_weeknum_p, -1);
9571 date_s__valid_nth_kday_p, -1);
9576 date_s_valid_ordinal_p, -1);
9580 date_s_valid_commercial_p, -1);
9584 date_s_valid_weeknum_p, -1);
9586 date_s_valid_nth_kday_p, -1);
9588 date_s_zone_to_diff, 1);
9593 date_s_gregorian_leap_p, 1);
9595 date_s_gregorian_leap_p, 1);
9893 datetime_s_commercial, -1);
9897 datetime_s_weeknum, -1);
9899 datetime_s_nth_kday, -1);
9906 datetime_s__strptime, -1);
9908 datetime_s_strptime, -1);
9910 datetime_s_parse, -1);
9912 datetime_s_iso8601, -1);
9914 datetime_s_rfc3339, -1);
9916 datetime_s_xmlschema, -1);
9918 datetime_s_rfc2822, -1);
9920 datetime_s_rfc2822, -1);
9922 datetime_s_httpdate, -1);
9924 datetime_s_jisx0301, -1);
9966 date_s_test_commercial, 0);
9970 date_s_test_unit_conv, 0);
VALUE rb_ary_push(VALUE ary, VALUE item)
#define rb_define_method(klass, mid, func, arity)
Defines klass#mid.
#define rb_define_singleton_method(klass, mid, func, arity)
Defines klass.mid.
#define rb_define_private_method(klass, mid, func, arity)
Defines klass#mid and makes it private.
#define MILLISECOND_IN_NANOSECONDS
#define val2off(vof, iof)
#define num2num_with_frac(s, n)
VALUE date__iso8601(VALUE)
VALUE date__rfc3339(VALUE)
#define copy_complex_to_simple(obj, x, y)
#define HALF_DAYS_IN_SECONDS
#define f_frozen_ary(...)
#define MINUTE_IN_SECONDS
#define set_to_complex(obj, x, _nth, _jd,_df, _sf, _of, _sg, _year, _mon, _mday, _hour, _min, _sec, _flags)
size_t date_strftime(char *s, size_t maxsize, const char *format, const struct tmx *tmx)
VALUE date__xmlschema(VALUE)
VALUE date__parse(VALUE str, VALUE comp)
#define REFORM_BEGIN_YEAR
#define SECOND_IN_NANOSECONDS
void Init_date_core(void)
#define UNIX_EPOCH_IN_CJD
VALUE date__strptime(const char *str, size_t slen, const char *fmt, size_t flen, VALUE hash)
#define num2int_with_frac(s, n)
#define PACK5(m, d, h, min, s)
VALUE date_zone_to_diff(VALUE)
#define set_to_simple(obj, x, _nth, _jd,_sg, _year, _mon, _mday, _flags)
#define decode_offset(of, s, h, m)
#define copy_simple_to_complex(obj, x, y)
VALUE date__jisx0301(VALUE)
#define RETURN_FALSE_UNLESS_NUMERIC(obj)
#define canonicalize_jd(_nth, _jd)
#define f_local3(x, y, m, d)
VALUE date__httpdate(VALUE)
VALUE date__rfc2822(VALUE)
#define SECOND_IN_MILLISECONDS
void rb_enc_copy(VALUE obj1, VALUE obj2)
rb_encoding * rb_usascii_encoding(void)
#define rb_cmpint(cmp, a, b)
char str[HTML_ESCAPE_MAX_LEN+1]
#define RSTRING_LEN(string)
#define RSTRING_PTR(string)
void rb_gc_mark(VALUE ptr)
void * ruby_xrealloc(void *ptr, size_t new_size)
Resize the storage instance.
void rb_gc_register_mark_object(VALUE obj)
Inform the garbage collector that object is a live Ruby object that should not be moved.
void rb_include_module(VALUE klass, VALUE module)
VALUE rb_define_class(const char *name, VALUE super)
Defines a top-level class.
VALUE rb_singleton_class(VALUE obj)
Returns the singleton class of obj.
VALUE rb_define_class_under(VALUE outer, const char *name, VALUE super)
Defines a class under the namespace of outer.
void rb_undef_method(VALUE klass, const char *name)
void rb_define_alias(VALUE klass, const char *name1, const char *name2)
Defines an alias of a method.
int rb_scan_args(int argc, const VALUE *argv, const char *fmt,...)
void rb_raise(VALUE exc, const char *fmt,...)
void * rb_check_typeddata(VALUE obj, const rb_data_type_t *data_type)
void rb_sys_fail(const char *mesg)
void rb_warning(const char *fmt,...)
VALUE rb_cObject
Object class.
VALUE rb_obj_class(VALUE)
VALUE rb_obj_is_kind_of(VALUE, VALUE)
Determines if obj is a kind of c.
VALUE rb_obj_freeze(VALUE)
Make the object unmodifiable.
VALUE rb_hash_aref(VALUE hash, VALUE key)
#define rb_enc_str_asciicompat_p(str)
VALUE rb_enc_sprintf(rb_encoding *, const char *,...)
VALUE rb_funcall(VALUE, ID, int,...)
Calls a method.
#define RETURN_ENUMERATOR(obj, argc, argv)
#define RB_EXT_RACTOR_SAFE(f)
VALUE rb_num_coerce_cmp(VALUE, VALUE, ID)
VALUE rb_rational_num(VALUE rat)
#define rb_rational_new2(x, y)
#define rb_rational_new1(x)
VALUE rb_rational_den(VALUE rat)
st_index_t rb_memhash(const void *ptr, long len)
#define rb_str_new(str, len)
VALUE rb_str_cat(VALUE, const char *, long)
#define rb_usascii_str_new(str, len)
#define rb_usascii_str_new2
#define rb_strlen_lit(str)
VALUE rb_str_append(VALUE, VALUE)
void rb_copy_generic_ivar(VALUE, VALUE)
void rb_define_alloc_func(VALUE, rb_alloc_func_t)
ID rb_intern(const char *)
void rb_define_const(VALUE, const char *, VALUE)
#define DECIMAL_SIZE_OF_BITS(n)
typedef long(ZCALLBACK *tell_file_func) OF((voidpf opaque
typedef int(ZCALLBACK *close_file_func) OF((voidpf opaque
VALUE rb_marshal_load(VALUE)
Internal header for Math.
#define RARRAY_AREF(a, i)
#define RB_OBJ_WRITE(a, slot, b)
WB for new reference from ‘a’ to ‘b’.
#define RB_OBJ_WRITTEN(a, oldv, b)
WB for new reference from ‘a’ to ‘b’.
#define RUBY_TYPED_DEFAULT_FREE
@ RUBY_TYPED_FREE_IMMEDIATELY
@ RUBY_TYPED_FROZEN_SHAREABLE
@ RUBY_TYPED_WB_PROTECTED
#define TypedData_Make_Struct(klass, type, data_type, sval)
#define RB_INTEGER_TYPE_P(obj)
size_t strlen(const char *)
const struct tmx_funcs * funcs
#define strftimev(fmt, time, enc)
int gettimeofday(struct timeval *, struct timezone *)
int clock_gettime(clockid_t, struct timespec *)