12 #define _DEFAULT_SOURCE
14 #include "ruby/internal/config.h"
20 #include <sys/types.h>
30 #if defined(HAVE_SYS_TIME_H)
31 # include <sys/time.h>
36 #include "internal/array.h"
37 #include "internal/hash.h"
38 #include "internal/compar.h"
39 #include "internal/numeric.h"
40 #include "internal/rational.h"
41 #include "internal/string.h"
42 #include "internal/time.h"
43 #include "internal/variable.h"
50 static ID id_submicro, id_nano_num, id_nano_den, id_offset, id_zone;
51 static ID id_nanosecond, id_microsecond, id_millisecond, id_nsec, id_usec;
52 static ID id_local_to_utc, id_utc_to_local, id_find_timezone;
53 static ID id_year, id_mon, id_mday, id_hour, id_min, id_sec, id_isdst;
54 static VALUE str_utc, str_empty;
57 static VALUE sym_year, sym_month, sym_day, sym_yday, sym_wday;
58 static VALUE sym_hour, sym_min, sym_sec, sym_subsec, sym_dst, sym_zone;
62 #define id_divmod idDivmod
63 #define id_name idName
64 #define UTC_ZONE Qundef
66 #define NDIV(x,y) (-(-((x)+1)/(y))-1)
67 #define NMOD(x,y) ((y)-(-((x)+1)%(y))-1)
68 #define DIV(n,d) ((n)<0 ? NDIV((n),(d)) : (n)/(d))
69 #define MOD(n,d) ((n)<0 ? NMOD((n),(d)) : (n)%(d))
70 #define VTM_WDAY_INITVAL (7)
71 #define VTM_ISDST_INITVAL (3)
86 if ((
long)x < (
long)y)
88 if ((
long)x > (
long)y)
96 #define ne(x,y) (!eq((x),(y)))
97 #define lt(x,y) (cmp((x),(y)) < 0)
98 #define gt(x,y) (cmp((x),(y)) > 0)
99 #define le(x,y) (cmp((x),(y)) <= 0)
100 #define ge(x,y) (cmp((x),(y)) >= 0)
126 return rb_fix_mul_fix(x, y);
128 if (RB_BIGNUM_TYPE_P(x))
137 return rb_fix_div_fix(x, y);
139 if (RB_BIGNUM_TYPE_P(x))
149 if (
FIXNUM_P(x))
return rb_fix_mod_fix(x, y);
155 #define neg(x) (subv(INT2FIX(0), (x)))
171 return rb_numeric_quo(x, y);
177 VALUE ret = quor(x, y);
179 RRATIONAL(ret)->den ==
INT2FIX(1)) {
180 ret = RRATIONAL(ret)->num;
185 #define mulquov(x,y,z) (((y) == (z)) ? (x) : quov(mulv((x),(y)),(z)))
194 rb_fix_divmod_fix(n, d, q, r);
209 # define INT64toNUM(x) LONG2NUM(x)
210 #elif defined(HAVE_LONG_LONG) && SIZEOF_LONG_LONG == 8
211 # define INT64toNUM(x) LL2NUM(x)
214 #if defined(HAVE_UINT64_T) && SIZEOF_LONG*2 <= SIZEOF_UINT64_T
215 typedef uint64_t uwideint_t;
216 typedef int64_t wideint_t;
217 typedef uint64_t WIDEVALUE;
218 typedef int64_t SIGNED_WIDEVALUE;
219 # define WIDEVALUE_IS_WIDER 1
220 # define UWIDEINT_MAX UINT64_MAX
221 # define WIDEINT_MAX INT64_MAX
222 # define WIDEINT_MIN INT64_MIN
223 # define FIXWINT_P(tv) ((tv) & 1)
224 # define FIXWVtoINT64(tv) RSHIFT((SIGNED_WIDEVALUE)(tv), 1)
225 # define INT64toFIXWV(wi) ((WIDEVALUE)((SIGNED_WIDEVALUE)(wi) << 1 | FIXNUM_FLAG))
226 # define FIXWV_MAX (((int64_t)1 << 62) - 1)
227 # define FIXWV_MIN (-((int64_t)1 << 62))
228 # define FIXWVABLE(wi) (POSFIXWVABLE(wi) && NEGFIXWVABLE(wi))
229 # define WINT2FIXWV(i) WIDEVAL_WRAP(INT64toFIXWV(i))
230 # define FIXWV2WINT(w) FIXWVtoINT64(WIDEVAL_GET(w))
232 typedef unsigned long uwideint_t;
233 typedef long wideint_t;
234 typedef VALUE WIDEVALUE;
236 # define WIDEVALUE_IS_WIDER 0
237 # define UWIDEINT_MAX ULONG_MAX
238 # define WIDEINT_MAX LONG_MAX
239 # define WIDEINT_MIN LONG_MIN
240 # define FIXWINT_P(v) FIXNUM_P(v)
241 # define FIXWV_MAX FIXNUM_MAX
242 # define FIXWV_MIN FIXNUM_MIN
243 # define FIXWVABLE(i) FIXABLE(i)
244 # define WINT2FIXWV(i) WIDEVAL_WRAP(LONG2FIX(i))
245 # define FIXWV2WINT(w) FIX2LONG(WIDEVAL_GET(w))
248 #define POSFIXWVABLE(wi) ((wi) < FIXWV_MAX+1)
249 #define NEGFIXWVABLE(wi) ((wi) >= FIXWV_MIN)
250 #define FIXWV_P(w) FIXWINT_P(WIDEVAL_GET(w))
251 #define MUL_OVERFLOW_FIXWV_P(a, b) MUL_OVERFLOW_SIGNED_INTEGER_P(a, b, FIXWV_MIN, FIXWV_MAX)
254 #ifdef STRUCT_WIDEVAL
259 static inline wideval_t WIDEVAL_WRAP(WIDEVALUE v) { wideval_t w = { v };
return w; }
260 # define WIDEVAL_GET(w) ((w).value)
262 typedef WIDEVALUE wideval_t;
263 # define WIDEVAL_WRAP(v) (v)
264 # define WIDEVAL_GET(w) (w)
267 #if WIDEVALUE_IS_WIDER
268 static inline wideval_t
269 wint2wv(wideint_t wi)
272 return WINT2FIXWV(wi);
274 return WIDEVAL_WRAP(INT64toNUM(wi));
276 # define WINT2WV(wi) wint2wv(wi)
278 # define WINT2WV(wi) WIDEVAL_WRAP(LONG2NUM(wi))
284 #if WIDEVALUE_IS_WIDER
286 return INT64toNUM(FIXWV2WINT(w));
287 return (
VALUE)WIDEVAL_GET(w);
289 return WIDEVAL_GET(w);
293 #if WIDEVALUE_IS_WIDER
302 return WINT2FIXWV(0);
303 else if (sign == -1) {
305 return WINT2FIXWV(-(wideint_t)u);
307 else if (sign == +1) {
309 return WINT2FIXWV((wideint_t)u);
311 return WIDEVAL_WRAP(v);
315 static inline wideval_t
319 if (RRATIONAL(v)->den !=
LONG2FIX(1))
320 return WIDEVAL_WRAP(v);
321 v = RRATIONAL(v)->num;
323 #if WIDEVALUE_IS_WIDER
325 return WIDEVAL_WRAP((WIDEVALUE)(SIGNED_WIDEVALUE)(
long)v);
327 else if (RB_BIGNUM_TYPE_P(v) &&
329 return v2w_bignum(v);
332 return WIDEVAL_WRAP(v);
336 weq(wideval_t wx, wideval_t wy)
338 #if WIDEVALUE_IS_WIDER
339 if (FIXWV_P(wx) && FIXWV_P(wy)) {
340 return WIDEVAL_GET(wx) == WIDEVAL_GET(wy);
344 return eq(WIDEVAL_GET(wx), WIDEVAL_GET(wy));
349 wcmp(wideval_t wx, wideval_t wy)
352 #if WIDEVALUE_IS_WIDER
353 if (FIXWV_P(wx) && FIXWV_P(wy)) {
369 #define wne(x,y) (!weq((x),(y)))
370 #define wlt(x,y) (wcmp((x),(y)) < 0)
371 #define wgt(x,y) (wcmp((x),(y)) > 0)
372 #define wle(x,y) (wcmp((x),(y)) <= 0)
373 #define wge(x,y) (wcmp((x),(y)) >= 0)
376 wadd(wideval_t wx, wideval_t wy)
378 #if WIDEVALUE_IS_WIDER
379 if (FIXWV_P(wx) && FIXWV_P(wy)) {
380 wideint_t r = FIXWV2WINT(wx) + FIXWV2WINT(wy);
384 return v2w(addv(w2v(wx), w2v(wy)));
388 wsub(wideval_t wx, wideval_t wy)
390 #if WIDEVALUE_IS_WIDER
391 if (FIXWV_P(wx) && FIXWV_P(wy)) {
392 wideint_t r = FIXWV2WINT(wx) - FIXWV2WINT(wy);
396 return v2w(subv(w2v(wx), w2v(wy)));
400 wmul(wideval_t wx, wideval_t wy)
402 #if WIDEVALUE_IS_WIDER
403 if (FIXWV_P(wx) && FIXWV_P(wy)) {
404 if (!MUL_OVERFLOW_FIXWV_P(FIXWV2WINT(wx), FIXWV2WINT(wy)))
405 return WINT2WV(FIXWV2WINT(wx) * FIXWV2WINT(wy));
408 return v2w(mulv(w2v(wx), w2v(wy)));
412 wquo(wideval_t wx, wideval_t wy)
414 #if WIDEVALUE_IS_WIDER
415 if (FIXWV_P(wx) && FIXWV_P(wy)) {
426 return v2w(quov(w2v(wx), w2v(wy)));
429 #define wmulquo(x,y,z) ((WIDEVAL_GET(y) == WIDEVAL_GET(z)) ? (x) : wquo(wmul((x),(y)),(z)))
430 #define wmulquoll(x,y,z) (((y) == (z)) ? (x) : wquo(wmul((x),WINT2WV(y)),WINT2WV(z)))
432 #if WIDEVALUE_IS_WIDER
434 wdivmod0(wideval_t wn, wideval_t wd, wideval_t *wq, wideval_t *wr)
436 if (FIXWV_P(wn) && FIXWV_P(wd)) {
437 wideint_t n, d, q, r;
446 wideint_t xneg = -FIXWV2WINT(wn);
459 if (d > 0 ? r < 0 : r > 0) {
472 wdivmod(wideval_t wn, wideval_t wd, wideval_t *wq, wideval_t *wr)
475 #if WIDEVALUE_IS_WIDER
476 if (wdivmod0(wn, wd, wq, wr))
return;
478 divmodv(w2v(wn), w2v(wd), &vq, &vr);
484 wmuldivmod(wideval_t wx, wideval_t wy, wideval_t wz, wideval_t *wq, wideval_t *wr)
486 if (WIDEVAL_GET(wy) == WIDEVAL_GET(wz)) {
491 wdivmod(wmul(wx,wy), wz, wq, wr);
495 wdiv(wideval_t wx, wideval_t wy)
497 #if WIDEVALUE_IS_WIDER
499 if (wdivmod0(wx, wy, &q, &dmy))
return q;
501 return v2w(divv(w2v(wx), w2v(wy)));
505 wmod(wideval_t wx, wideval_t wy)
507 #if WIDEVALUE_IS_WIDER
509 if (wdivmod0(wx, wy, &dmy, &r))
return r;
511 return v2w(modv(w2v(wx), w2v(wy)));
515 num_exact_check(
VALUE v)
526 tmp = rb_rational_canonicalize(v);
540 tmp = rb_rational_canonicalize(tmp);
556 NORETURN(
static void num_exact_fail(
VALUE v));
558 num_exact_fail(
VALUE v)
567 VALUE num = num_exact_check(v);
568 if (
NIL_P(num)) num_exact_fail(v);
575 static const int TIME_SCALE_NUMDIGITS =
rb_strlen_lit(STRINGIZE(TIME_SCALE)) - 1;
578 rb_time_magnify(wideval_t w)
580 return wmul(w, WINT2FIXWV(TIME_SCALE));
584 rb_time_unmagnify_to_rational(wideval_t w)
586 return quor(w2v(w),
INT2FIX(TIME_SCALE));
590 rb_time_unmagnify(wideval_t w)
592 return v2w(rb_time_unmagnify_to_rational(w));
596 rb_time_unmagnify_to_float(wideval_t w)
599 #if WIDEVALUE_IS_WIDER
608 v =
DBL2NUM((
double)FIXWV2WINT(w));
609 return quov(v,
DBL2NUM(TIME_SCALE));
616 return quov(v,
DBL2NUM(TIME_SCALE));
620 split_second(wideval_t timew, wideval_t *timew_p,
VALUE *subsecx_p)
623 wdivmod(timew, WINT2FIXWV(TIME_SCALE), &q, &r);
631 #if WIDEVALUE_IS_WIDER
632 if (TIMET_MIN == 0) {
633 uwideint_t wi = (uwideint_t)t;
634 if (wi <= FIXWV_MAX) {
635 return WINT2FIXWV(wi);
639 wideint_t wi = (wideint_t)t;
640 if (FIXWV_MIN <= wi && wi <= FIXWV_MAX) {
641 return WINT2FIXWV(wi);
645 return v2w(TIMET2NUM(t));
647 #define TIMET2WV(t) timet2wv(t)
650 wv2timet(wideval_t w)
652 #if WIDEVALUE_IS_WIDER
654 wideint_t wi = FIXWV2WINT(w);
655 if (TIMET_MIN == 0) {
658 if (TIMET_MAX < (uwideint_t)wi)
662 if (wi < TIMET_MIN || TIMET_MAX < wi)
668 return NUM2TIMET(w2v(w));
670 #define WV2TIMET(t) wv2timet(t)
673 static VALUE rb_cTimeTM;
675 static int obj2int(
VALUE obj);
676 static uint32_t obj2ubits(
VALUE obj,
unsigned int bits);
678 static uint32_t month_arg(
VALUE arg);
679 static VALUE validate_utc_offset(
VALUE utc_offset);
680 static VALUE validate_zone_name(
VALUE zone_name);
681 static void validate_vtm(
struct vtm *
vtm);
682 static void vtm_add_day(
struct vtm *
vtm,
int day);
683 static uint32_t obj2subsecx(
VALUE obj,
VALUE *subsecx);
690 static time_t timegm_noleapsecond(
struct tm *tm);
691 static int tmcmp(
struct tm *a,
struct tm *b);
692 static int vtmcmp(
struct vtm *a,
struct vtm *b);
693 static const char *find_time_t(
struct tm *tptr,
int utc_p, time_t *tp);
695 static struct vtm *localtimew(wideval_t timew,
struct vtm *result);
697 static int leap_year_p(
long y);
698 #define leap_year_v_p(y) leap_year_p(NUM2LONG(modv((y), INT2FIX(400))))
702 bool ruby_tz_uptodate_p;
705 ruby_reset_timezone(
void)
707 ruby_tz_uptodate_p =
false;
708 ruby_reset_leap_second_info();
714 if (ruby_tz_uptodate_p)
return;
715 ruby_tz_uptodate_p =
true;
720 rb_localtime_r(
const time_t *t,
struct tm *result)
722 #if defined __APPLE__ && defined __LP64__
723 if (*t != (time_t)(
int)*t)
return NULL;
727 result = localtime_r(t, result);
730 struct tm *tmp = localtime(t);
731 if (tmp) *result = *tmp;
734 #if defined(HAVE_MKTIME) && defined(LOCALTIME_OVERFLOW_PROBLEM)
738 struct tm tmp = *result;
741 # if defined(HAVE_STRUCT_TM_TM_GMTOFF)
742 gmtoff1 = result->tm_gmtoff;
743 gmtoff2 = tmp.tm_gmtoff;
745 if (*t + gmtoff1 != t2 + gmtoff2)
751 #define LOCALTIME(tm, result) rb_localtime_r((tm), &(result))
753 #ifndef HAVE_STRUCT_TM_TM_GMTOFF
755 rb_gmtime_r(
const time_t *t,
struct tm *result)
758 result = gmtime_r(t, result);
760 struct tm *tmp = gmtime(t);
761 if (tmp) *result = *tmp;
763 #if defined(HAVE_TIMEGM) && defined(LOCALTIME_OVERFLOW_PROBLEM)
764 if (result && *t != timegm(result)) {
770 # define GMTIME(tm, result) rb_gmtime_r((tm), &(result))
773 static const int16_t common_year_yday_offset[] = {
778 -1 + 31 + 28 + 31 + 30,
779 -1 + 31 + 28 + 31 + 30 + 31,
780 -1 + 31 + 28 + 31 + 30 + 31 + 30,
781 -1 + 31 + 28 + 31 + 30 + 31 + 30 + 31,
782 -1 + 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31,
783 -1 + 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30,
784 -1 + 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31,
785 -1 + 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30
788 static const int16_t leap_year_yday_offset[] = {
793 -1 + 31 + 29 + 31 + 30,
794 -1 + 31 + 29 + 31 + 30 + 31,
795 -1 + 31 + 29 + 31 + 30 + 31 + 30,
796 -1 + 31 + 29 + 31 + 30 + 31 + 30 + 31,
797 -1 + 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31,
798 -1 + 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30,
799 -1 + 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31,
800 -1 + 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30
804 static const int8_t common_year_days_in_month[] = {
805 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
807 static const int8_t leap_year_days_in_month[] = {
808 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
811 #define days_in_month_of(leap) ((leap) ? leap_year_days_in_month : common_year_days_in_month)
812 #define days_in_month_in(y) days_in_month_of(leap_year_p(y))
813 #define days_in_month_in_v(y) days_in_month_of(leap_year_v_p(y))
816 (m),(m),(m),(m),(m),(m),(m),(m),(m),(m), \
817 (m),(m),(m),(m),(m),(m),(m),(m),(m),(m), \
818 (m),(m),(m),(m),(m),(m),(m),(m)
820 (m),(m),(m),(m),(m),(m),(m),(m),(m),(m), \
821 (m),(m),(m),(m),(m),(m),(m),(m),(m),(m), \
822 (m),(m),(m),(m),(m),(m),(m),(m),(m)
824 (m),(m),(m),(m),(m),(m),(m),(m),(m),(m), \
825 (m),(m),(m),(m),(m),(m),(m),(m),(m),(m), \
826 (m),(m),(m),(m),(m),(m),(m),(m),(m),(m)
828 (m),(m),(m),(m),(m),(m),(m),(m),(m),(m), \
829 (m),(m),(m),(m),(m),(m),(m),(m),(m),(m), \
830 (m),(m),(m),(m),(m),(m),(m),(m),(m),(m), (m)
832 static const uint8_t common_year_mon_of_yday[] = {
833 M31(1), M28(2), M31(3), M30(4), M31(5), M30(6),
834 M31(7), M31(8), M30(9), M31(10), M30(11), M31(12)
836 static const uint8_t leap_year_mon_of_yday[] = {
837 M31(1), M29(2), M31(3), M30(4), M31(5), M30(6),
838 M31(7), M31(8), M30(9), M31(10), M30(11), M31(12)
848 10,11,12,13,14,15,16,17,18,19, \
849 20,21,22,23,24,25,26,27,28
852 10,11,12,13,14,15,16,17,18,19, \
853 20,21,22,23,24,25,26,27,28,29
856 10,11,12,13,14,15,16,17,18,19, \
857 20,21,22,23,24,25,26,27,28,29,30
860 10,11,12,13,14,15,16,17,18,19, \
861 20,21,22,23,24,25,26,27,28,29,30,31
863 static const uint8_t common_year_mday_of_yday[] = {
865 D31, D28, D31, D30, D31, D30, D31, D31, D30, D31, D30, D31
867 static const uint8_t leap_year_mday_of_yday[] = {
868 D31, D29, D31, D30, D31, D30, D31, D31, D30, D31, D30, D31
877 calc_tm_yday(
long tm_year,
int tm_mon,
int tm_mday)
879 int tm_year_mod400 = (int)MOD(tm_year, 400);
880 int tm_yday = tm_mday;
882 if (leap_year_p(tm_year_mod400 + 1900))
883 tm_yday += leap_year_yday_offset[tm_mon];
885 tm_yday += common_year_yday_offset[tm_mon];
891 timegmw_noleapsecond(
struct vtm *
vtm)
903 divmodv(year1900,
INT2FIX(400), &q400, &r400);
906 yday = calc_tm_yday(year_mod400,
vtm->mon-1,
vtm->mday);
919 + DIV(year_mod400 - 69, 4)
920 - DIV(year_mod400 - 1, 100)
921 + (year_mod400 + 299) / 400;
923 vdays = addv(vdays, mulv(q400,
INT2FIX(97)));
924 vdays = addv(vdays, mulv(year1900,
INT2FIX(365)));
925 wret = wadd(rb_time_magnify(v2w(ret)), wmul(rb_time_magnify(v2w(vdays)), WINT2FIXWV(86400)));
926 wret = wadd(wret, v2w(
vtm->subsecx));
932 zone_str(
const char *zone)
940 return rb_fstring_lit(
"(NO-TIMEZONE-ABBREVIATION)");
943 for (p = zone; *p; p++)
948 len = p - zone + strlen(p);
955 return rb_fstring(str);
959 gmtimew_noleapsecond(wideval_t timew,
struct vtm *
vtm)
965 wideval_t timew2, w, w2;
970 split_second(timew, &timew2, &subsecx);
971 vtm->subsecx = subsecx;
973 wdivmod(timew2, WINT2FIXWV(86400), &w2, &w);
978 vtm->wday = (wday + 4) % 7;
981 vtm->sec = n % 60; n = n / 60;
982 vtm->min = n % 60; n = n / 60;
986 divmodv(timev,
INT2FIX(400*365 + 97), &timev, &v);
999 if (30*365+7+31+29-1 <= n) {
1013 x = n / (365*100 + 24);
1014 n = n % (365*100 + 24);
1016 if (30*365+7+31+29-1 <= n) {
1026 x = n / (365*4 + 1);
1027 n = n % (365*4 + 1);
1029 if (365*2+31+29-1 <= n) {
1030 if (n < 365*2+366) {
1047 if (leap_year_p(y)) {
1048 vtm->mon = leap_year_mon_of_yday[n];
1049 vtm->mday = leap_year_mday_of_yday[n];
1052 vtm->mon = common_year_mon_of_yday[n];
1053 vtm->mday = common_year_mday_of_yday[n];
1057 vtm->zone = str_utc;
1061 gmtime_with_leapsecond(
const time_t *timep,
struct tm *result)
1063 #if defined(HAVE_STRUCT_TM_TM_GMTOFF)
1067 int gmtoff_sec, gmtoff_min, gmtoff_hour, gmtoff_day;
1069 t = LOCALTIME(timep, *result);
1074 if (t->tm_gmtoff < 0) {
1076 gmtoff = -t->tm_gmtoff;
1080 gmtoff = t->tm_gmtoff;
1082 gmtoff_sec = (int)(gmtoff % 60);
1083 gmtoff = gmtoff / 60;
1084 gmtoff_min = (int)(gmtoff % 60);
1085 gmtoff = gmtoff / 60;
1086 gmtoff_hour = (int)gmtoff;
1090 gmtoff_hour *= sign;
1097 result->tm_sec += gmtoff_sec;
1098 if (result->tm_sec < 0) {
1099 result->tm_sec += 60;
1102 if (60 <= result->tm_sec) {
1103 result->tm_sec -= 60;
1108 result->tm_min += gmtoff_min;
1109 if (result->tm_min < 0) {
1110 result->tm_min += 60;
1113 if (60 <= result->tm_min) {
1114 result->tm_min -= 60;
1119 result->tm_hour += gmtoff_hour;
1120 if (result->tm_hour < 0) {
1121 result->tm_hour += 24;
1124 if (24 <= result->tm_hour) {
1125 result->tm_hour -= 24;
1131 if (gmtoff_day < 0) {
1132 if (result->tm_yday == 0) {
1133 result->tm_mday = 31;
1134 result->tm_mon = 11;
1136 result->tm_yday = leap_year_p(result->tm_year + 1900) ? 365 : 364;
1138 else if (result->tm_mday == 1) {
1139 const int8_t *days_in_month = days_in_month_in(result->tm_year + 1900);
1141 result->tm_mday = days_in_month[result->tm_mon];
1148 result->tm_wday = (result->tm_wday + 6) % 7;
1151 int leap = leap_year_p(result->tm_year + 1900);
1152 if (result->tm_yday == (leap ? 365 : 364)) {
1155 result->tm_mday = 1;
1156 result->tm_yday = 0;
1158 else if (result->tm_mday == days_in_month_of(leap)[result->tm_mon]) {
1160 result->tm_mday = 1;
1167 result->tm_wday = (result->tm_wday + 1) % 7;
1170 result->tm_isdst = 0;
1171 result->tm_gmtoff = 0;
1172 #if defined(HAVE_TM_ZONE)
1173 result->tm_zone = (
char *)
"UTC";
1177 return GMTIME(timep, *result);
1181 static long this_year = 0;
1182 static time_t known_leap_seconds_limit;
1183 static int number_of_leap_seconds_known;
1186 init_leap_second_info(
void)
1193 if (this_year == 0) {
1195 struct tm *tm, result;
1199 #ifdef HAVE_GMTIME_R
1200 gmtime_r(&now, &result);
1204 tm = gmtime_with_leapsecond(&now, &result);
1206 this_year = tm->tm_year;
1208 if (TIMET_MAX - now < (time_t)(366*86400))
1209 known_leap_seconds_limit = TIMET_MAX;
1211 known_leap_seconds_limit = now + (time_t)(366*86400);
1213 if (!gmtime_with_leapsecond(&known_leap_seconds_limit, &result))
1217 vtm.mon = result.tm_mon + 1;
1218 vtm.mday = result.tm_mday;
1219 vtm.hour = result.tm_hour;
1220 vtm.min = result.tm_min;
1221 vtm.sec = result.tm_sec;
1225 timew = timegmw_noleapsecond(&
vtm);
1227 number_of_leap_seconds_known =
NUM2INT(w2v(wsub(TIMET2WV(known_leap_seconds_limit), rb_time_unmagnify(timew))));
1233 ruby_reset_leap_second_info(
void)
1249 return timegmw_noleapsecond(
vtm);
1251 init_leap_second_info();
1253 timew = timegmw_noleapsecond(
vtm);
1256 if (number_of_leap_seconds_known == 0) {
1262 else if (wlt(rb_time_magnify(TIMET2WV(known_leap_seconds_limit)), timew)) {
1263 return wadd(timew, rb_time_magnify(WINT2WV(number_of_leap_seconds_known)));
1267 tm.tm_mon =
vtm->mon - 1;
1268 tm.tm_mday =
vtm->mday;
1269 tm.tm_hour =
vtm->hour;
1270 tm.tm_min =
vtm->min;
1271 tm.tm_sec =
vtm->sec;
1274 errmsg = find_time_t(&tm, 1, &t);
1277 return wadd(rb_time_magnify(TIMET2WV(t)), v2w(
vtm->subsecx));
1281 gmtimew(wideval_t timew,
struct vtm *result)
1288 if (wlt(timew, WINT2FIXWV(0))) {
1289 gmtimew_noleapsecond(timew, result);
1293 init_leap_second_info();
1295 if (number_of_leap_seconds_known == 0) {
1299 gmtimew_noleapsecond(timew, result);
1302 else if (wlt(rb_time_magnify(TIMET2WV(known_leap_seconds_limit)), timew)) {
1303 timew = wsub(timew, rb_time_magnify(WINT2WV(number_of_leap_seconds_known)));
1304 gmtimew_noleapsecond(timew, result);
1308 split_second(timew, &timew2, &subsecx);
1310 t = WV2TIMET(timew2);
1311 if (!gmtime_with_leapsecond(&t, &tm))
1314 result->year =
LONG2NUM((
long)tm.tm_year + 1900);
1315 result->mon = tm.tm_mon + 1;
1316 result->mday = tm.tm_mday;
1317 result->hour = tm.tm_hour;
1318 result->min = tm.tm_min;
1319 result->sec = tm.tm_sec;
1320 result->subsecx = subsecx;
1321 result->utc_offset =
INT2FIX(0);
1322 result->wday = tm.tm_wday;
1323 result->yday = tm.tm_yday+1;
1324 result->isdst = tm.tm_isdst;
1329 #define GMTIMEW(w, v) \
1330 (gmtimew(w, v) ? (void)0 : rb_raise(rb_eArgError, "gmtime error"))
1332 static struct tm *localtime_with_gmtoff_zone(
const time_t *t,
struct tm *result,
long *gmtoff,
VALUE *zone);
1367 static const int compat_common_month_table[12][7] = {
1369 { 2034, 2035, 2036, 2031, 2032, 2027, 2033 },
1370 { 2026, 2027, 2033, 2034, 2035, 2030, 2031 },
1371 { 2026, 2032, 2033, 2034, 2035, 2030, 2036 },
1372 { 2035, 2030, 2036, 2026, 2032, 2033, 2034 },
1373 { 2033, 2034, 2035, 2030, 2036, 2026, 2032 },
1374 { 2036, 2026, 2032, 2033, 2034, 2035, 2030 },
1375 { 2035, 2030, 2036, 2026, 2032, 2033, 2034 },
1376 { 2032, 2033, 2034, 2035, 2030, 2036, 2026 },
1377 { 2030, 2036, 2026, 2032, 2033, 2034, 2035 },
1378 { 2034, 2035, 2030, 2036, 2026, 2032, 2033 },
1379 { 2026, 2032, 2033, 2034, 2035, 2030, 2036 },
1380 { 2030, 2036, 2026, 2032, 2033, 2034, 2035 },
1408 static const int compat_leap_month_table[7] = {
1410 2032, 2016, 2028, 2012, 2024, 2036, 2020,
1414 calc_wday(
int year_mod400,
int month,
int day)
1419 a = (14 - month) / 12;
1420 y = year_mod400 + 4800 - a;
1421 m = month + 12 * a - 3;
1422 wday = day + (153*m+2)/5 + 365*y + y/4 - y/100 + y/400 + 2;
1428 guess_local_offset(
struct vtm *vtm_utc,
int *isdst_ret,
VALUE *zone_ret)
1436 int year_mod400, wday;
1440 if (lt(vtm_utc->year,
INT2FIX(1916))) {
1443 zone = rb_fstring_lit(
"UTC");
1445 # if defined(NEGATIVE_TIME_T)
1446 # if SIZEOF_TIME_T <= 4
1448 # define THE_TIME_OLD_ENOUGH ((time_t)0x80000000)
1452 # define THE_TIME_OLD_ENOUGH ((time_t)(1600-1970)*366*24*60*60)
1454 if (localtime_with_gmtoff_zone((t = THE_TIME_OLD_ENOUGH, &t), &tm, &gmtoff, &zone)) {
1456 isdst = tm.tm_isdst;
1461 if (localtime_with_gmtoff_zone((t = 0, &t), &tm, &gmtoff, &zone)) {
1463 isdst = tm.tm_isdst;
1479 wday = calc_wday(year_mod400, vtm_utc->mon, 1);
1480 if (vtm_utc->mon == 2 && leap_year_p(year_mod400))
1481 vtm2.year =
INT2FIX(compat_leap_month_table[wday]);
1483 vtm2.year =
INT2FIX(compat_common_month_table[vtm_utc->mon-1][wday]);
1485 timev = w2v(rb_time_unmagnify(timegmw(&vtm2)));
1486 t = NUM2TIMET(timev);
1488 if (localtime_with_gmtoff_zone(&t, &tm, &gmtoff, &zone)) {
1490 *isdst_ret = tm.tm_isdst;
1498 static time_t now = 0;
1499 static long now_gmtoff = 0;
1500 static int now_isdst = 0;
1501 static VALUE now_zone;
1505 localtime_with_gmtoff_zone(&now, &tm, &now_gmtoff, &zone);
1506 now_isdst = tm.tm_isdst;
1507 zone = rb_fstring(zone);
1508 rb_vm_register_global_object(zone);
1512 *isdst_ret = now_isdst;
1514 *zone_ret = now_zone;
1520 small_vtm_sub(
struct vtm *vtm1,
struct vtm *vtm2)
1524 off = vtm1->sec - vtm2->sec;
1525 off += (vtm1->min - vtm2->min) * 60;
1526 off += (vtm1->hour - vtm2->hour) * 3600;
1527 if (ne(vtm1->year, vtm2->year))
1528 off += lt(vtm1->year, vtm2->year) ? -24*3600 : 24*3600;
1529 else if (vtm1->mon != vtm2->mon)
1530 off += vtm1->mon < vtm2->mon ? -24*3600 : 24*3600;
1531 else if (vtm1->mday != vtm2->mday)
1532 off += vtm1->mday < vtm2->mday ? -24*3600 : 24*3600;
1538 timelocalw(
struct vtm *
vtm)
1543 wideval_t timew1, timew2;
1544 struct vtm vtm1, vtm2;
1549 if (l < INT_MIN || INT_MAX < l)
1551 tm.tm_year = (int)l;
1560 tm.tm_mon =
vtm->mon-1;
1561 tm.tm_mday =
vtm->mday;
1562 tm.tm_hour =
vtm->hour;
1563 tm.tm_min =
vtm->min;
1564 tm.tm_sec =
vtm->sec;
1565 tm.tm_isdst =
vtm->isdst == VTM_ISDST_INITVAL ? -1 :
vtm->isdst;
1567 if (find_time_t(&tm, 0, &t))
1569 return wadd(rb_time_magnify(TIMET2WV(t)), v2w(
vtm->subsecx));
1572 timew1 = timegmw(
vtm);
1574 if (!localtimew(timew1, &vtm1))
1577 n = vtmcmp(
vtm, &vtm1);
1579 timew1 = wsub(timew1, rb_time_magnify(WINT2FIXWV(12*3600)));
1580 if (!localtimew(timew1, &vtm1))
1588 timew1 = wsub(timew1, rb_time_magnify(WINT2FIXWV(24*3600)));
1589 if (!localtimew(timew1, &vtm1))
1593 timew2 = wadd(timew1, rb_time_magnify(WINT2FIXWV(24*3600)));
1594 if (!localtimew(timew2, &vtm2))
1597 timew1 = wadd(timew1, rb_time_magnify(v2w(small_vtm_sub(
vtm, &vtm1))));
1598 timew2 = wadd(timew2, rb_time_magnify(v2w(small_vtm_sub(
vtm, &vtm2))));
1600 if (weq(timew1, timew2))
1603 if (!localtimew(timew1, &vtm1))
1605 if (
vtm->hour != vtm1.hour ||
vtm->min != vtm1.min ||
vtm->sec != vtm1.sec)
1608 if (!localtimew(timew2, &vtm2))
1610 if (
vtm->hour != vtm2.hour ||
vtm->min != vtm2.min ||
vtm->sec != vtm2.sec)
1614 return lt(vtm1.utc_offset, vtm2.utc_offset) ? timew2 : timew1;
1616 return lt(vtm1.utc_offset, vtm2.utc_offset) ? timew1 : timew2;
1620 localtime_with_gmtoff_zone(
const time_t *t,
struct tm *result,
long *gmtoff,
VALUE *zone)
1624 if (LOCALTIME(t, tm)) {
1625 #if defined(HAVE_STRUCT_TM_TM_GMTOFF)
1626 *gmtoff = tm.tm_gmtoff;
1632 u = GMTIME(t, tmbuf);
1635 if (l->tm_year != u->tm_year)
1636 off = l->tm_year < u->tm_year ? -1 : 1;
1637 else if (l->tm_mon != u->tm_mon)
1638 off = l->tm_mon < u->tm_mon ? -1 : 1;
1639 else if (l->tm_mday != u->tm_mday)
1640 off = l->tm_mday < u->tm_mday ? -1 : 1;
1643 off =
off * 24 + l->tm_hour - u->tm_hour;
1644 off =
off * 60 + l->tm_min - u->tm_min;
1645 off =
off * 60 + l->tm_sec - u->tm_sec;
1650 #if defined(HAVE_TM_ZONE)
1651 *zone = zone_str(tm.tm_zone);
1652 #elif defined(HAVE_TZNAME) && defined(HAVE_DAYLIGHT)
1653 # if defined(RUBY_MSVCRT_VERSION) && RUBY_MSVCRT_VERSION >= 140
1654 # define tzname _tzname
1655 # define daylight _daylight
1658 *zone = zone_str(tzname[daylight && tm.tm_isdst]);
1662 strftime(buf,
sizeof(buf),
"%Z", &tm);
1663 *zone = zone_str(buf);
1675 timew_out_of_timet_range(wideval_t timew)
1678 #if WIDEVALUE_IS_WIDER && SIZEOF_TIME_T < SIZEOF_INT64_T
1679 if (FIXWV_P(timew)) {
1680 wideint_t t = FIXWV2WINT(timew);
1681 if (t < TIME_SCALE * (wideint_t)TIMET_MIN ||
1682 TIME_SCALE * (1 + (wideint_t)TIMET_MAX) <= t)
1687 #if SIZEOF_TIME_T == SIZEOF_INT64_T
1688 if (FIXWV_P(timew)) {
1689 wideint_t t = FIXWV2WINT(timew);
1690 if (~(time_t)0 <= 0) {
1700 timexv = w2v(timew);
1701 if (lt(timexv, mulv(
INT2FIX(TIME_SCALE), TIMET2NUM(TIMET_MIN))) ||
1702 le(mulv(
INT2FIX(TIME_SCALE), addv(TIMET2NUM(TIMET_MAX),
INT2FIX(1))), timexv))
1708 localtimew(wideval_t timew,
struct vtm *result)
1710 VALUE subsecx, offset;
1714 if (!timew_out_of_timet_range(timew)) {
1720 split_second(timew, &timew2, &subsecx);
1722 t = WV2TIMET(timew2);
1724 if (localtime_with_gmtoff_zone(&t, &tm, &gmtoff, &zone)) {
1725 result->year =
LONG2NUM((
long)tm.tm_year + 1900);
1726 result->mon = tm.tm_mon + 1;
1727 result->mday = tm.tm_mday;
1728 result->hour = tm.tm_hour;
1729 result->min = tm.tm_min;
1730 result->sec = tm.tm_sec;
1731 result->subsecx = subsecx;
1732 result->wday = tm.tm_wday;
1733 result->yday = tm.tm_yday+1;
1734 result->isdst = tm.tm_isdst;
1735 result->utc_offset =
LONG2NUM(gmtoff);
1736 result->zone = zone;
1741 if (!gmtimew(timew, result))
1744 offset = guess_local_offset(result, &isdst, &zone);
1746 if (!gmtimew(wadd(timew, rb_time_magnify(v2w(offset))), result))
1749 result->utc_offset = offset;
1750 result->isdst = isdst;
1751 result->zone = zone;
1756 #define TIME_TZMODE_LOCALTIME 0
1757 #define TIME_TZMODE_UTC 1
1758 #define TIME_TZMODE_FIXOFF 2
1759 #define TIME_TZMODE_UNINITIALIZED 3
1766 #define GetTimeval(obj, tobj) ((tobj) = get_timeval(obj))
1767 #define GetNewTimeval(obj, tobj) ((tobj) = get_new_timeval(obj))
1769 #define IsTimeval(obj) rb_typeddata_is_kind_of((obj), &time_data_type)
1770 #define TIME_INIT_P(tobj) ((tobj)->vtm.tzmode != TIME_TZMODE_UNINITIALIZED)
1772 #define TZMODE_UTC_P(tobj) ((tobj)->vtm.tzmode == TIME_TZMODE_UTC)
1773 #define TZMODE_SET_UTC(tobj) ((tobj)->vtm.tzmode = TIME_TZMODE_UTC)
1775 #define TZMODE_LOCALTIME_P(tobj) ((tobj)->vtm.tzmode == TIME_TZMODE_LOCALTIME)
1776 #define TZMODE_SET_LOCALTIME(tobj) ((tobj)->vtm.tzmode = TIME_TZMODE_LOCALTIME)
1778 #define TZMODE_FIXOFF_P(tobj) ((tobj)->vtm.tzmode == TIME_TZMODE_FIXOFF)
1779 #define TZMODE_SET_FIXOFF(time, tobj, off) do { \
1780 (tobj)->vtm.tzmode = TIME_TZMODE_FIXOFF; \
1781 RB_OBJ_WRITE_UNALIGNED(time, &(tobj)->vtm.utc_offset, off); \
1784 #define TZMODE_COPY(tobj1, tobj2) \
1785 ((tobj1)->vtm.tzmode = (tobj2)->vtm.tzmode, \
1786 (tobj1)->vtm.utc_offset = (tobj2)->vtm.utc_offset, \
1787 (tobj1)->vtm.zone = (tobj2)->vtm.zone)
1789 static int zone_localtime(
VALUE zone,
VALUE time);
1791 #define MAKE_TM(time, tobj) \
1793 if ((tobj)->vtm.tm_got == 0) { \
1794 time_get_tm((time), (tobj)); \
1797 #define MAKE_TM_ENSURE(time, tobj, cond) \
1799 MAKE_TM(time, tobj); \
1801 force_make_tm(time, tobj); \
1808 tobj->timew = timew;
1809 if (!FIXWV_P(timew)) {
1828 VALUE zone = tobj->vtm.zone;
1829 if (!
NIL_P(zone) && zone != str_empty && zone != str_utc) {
1830 if (zone_localtime(zone, time))
return;
1832 tobj->vtm.tm_got = 0;
1833 time_get_tm(time, tobj);
1837 time_mark(
void *
ptr)
1840 if (!FIXWV_P(tobj->timew))
1856 (RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_FROZEN_SHAREABLE | RUBY_TYPED_WB_PROTECTED | RUBY_TYPED_EMBEDDABLE),
1860 time_s_alloc(
VALUE klass)
1866 tobj->vtm.tzmode = TIME_TZMODE_UNINITIALIZED;
1867 tobj->vtm.tm_got = 0;
1868 time_set_timew(obj, tobj, WINT2FIXWV(0));
1869 tobj->vtm.zone =
Qnil;
1875 get_timeval(
VALUE obj)
1879 if (!TIME_INIT_P(tobj)) {
1886 get_new_timeval(
VALUE obj)
1890 if (TIME_INIT_P(tobj)) {
1897 time_modify(
VALUE time)
1899 rb_check_frozen(time);
1903 timenano2timew(time_t sec,
long nsec)
1907 timew = rb_time_magnify(TIMET2WV(sec));
1909 timew = wadd(timew, wmulquoll(WINT2WV(nsec), TIME_SCALE, 1000000000));
1914 timew2timespec(wideval_t timew)
1920 if (timew_out_of_timet_range(timew))
1922 split_second(timew, &timew2, &subsecx);
1923 ts.tv_sec = WV2TIMET(timew2);
1929 timew2timespec_exact(wideval_t timew,
struct timespec *ts)
1935 if (timew_out_of_timet_range(timew))
1937 split_second(timew, &timew2, &subsecx);
1938 ts->tv_sec = WV2TIMET(timew2);
1939 nsecv = mulquov(subsecx,
INT2FIX(1000000000),
INT2FIX(TIME_SCALE));
1949 #ifdef HAVE_CLOCK_GETTIME
1950 if (clock_gettime(CLOCK_REALTIME, ts) == -1) {
1956 if (gettimeofday(&tv, 0) < 0) {
1959 ts->tv_sec = tv.tv_sec;
1960 ts->tv_nsec = tv.tv_usec * 1000;
1976 GetNewTimeval(time, tobj);
1977 TZMODE_SET_LOCALTIME(tobj);
1980 time_set_timew(time, tobj, timenano2timew(ts.tv_sec, ts.tv_nsec));
1983 time_zonelocal(time, zone);
1991 VALUE t = time_s_alloc(klass);
1992 return time_init_now(ec, t, zone);
2002 GetTimeval(time, tobj);
2004 tobj->vtm.tm_got = 0;
2005 tobj->vtm.zone =
Qnil;
2006 TZMODE_SET_FIXOFF(time, tobj,
off);
2031 subsec = neg(subsec);
2040 vtm->subsecx = addv(
vtm->subsecx, w2v(rb_time_magnify(v2w(subsec))));
2089 vtm_add_day(
vtm, day);
2093 vtm_add_day(
struct vtm *
vtm,
int day)
2097 if (
vtm->mon == 1 &&
vtm->mday == 1) {
2102 vtm->yday = leap_year_v_p(
vtm->year) ? 366 : 365;
2104 else if (
vtm->mday == 1) {
2105 const int8_t *days_in_month = days_in_month_in_v(
vtm->year);
2107 vtm->mday = days_in_month[
vtm->mon-1];
2108 if (
vtm->yday != 0)
vtm->yday--;
2112 if (
vtm->yday != 0)
vtm->yday--;
2114 if (
vtm->wday != VTM_WDAY_INITVAL)
vtm->wday = (
vtm->wday + 6) % 7;
2117 int leap = leap_year_v_p(
vtm->year);
2118 if (
vtm->mon == 12 &&
vtm->mday == 31) {
2124 else if (
vtm->mday == days_in_month_of(leap)[
vtm->mon-1]) {
2127 if (
vtm->yday != 0)
vtm->yday++;
2131 if (
vtm->yday != 0)
vtm->yday++;
2133 if (
vtm->wday != VTM_WDAY_INITVAL)
vtm->wday = (
vtm->wday + 1) % 7;
2139 maybe_tzobj_p(
VALUE obj)
2141 if (
NIL_P(obj))
return FALSE;
2147 NORETURN(
static void invalid_utc_offset(
VALUE));
2149 invalid_utc_offset(
VALUE zone)
2152 "\"A\"..\"I\",\"K\"..\"Z\" expected for utc_offset: %"PRIsVALUE,
2156 #define have_2digits(ptr) (ISDIGIT((ptr)[0]) && ISDIGIT((ptr)[1]))
2157 #define num_from_2digits(ptr) ((ptr)[0] * 10 + (ptr)[1] - '0' * 11)
2160 utc_offset_arg(
VALUE arg)
2165 const char *s =
RSTRING_PTR(tmp), *min = NULL, *sec = NULL;
2167 goto invalid_utc_offset;
2175 if (s[0] >=
'A' && s[0] <=
'I') {
2176 n = (int)s[0] -
'A' + 1;
2179 else if (s[0] >=
'K' && s[0] <=
'M') {
2180 n = (int)s[0] -
'A';
2182 else if (s[0] >=
'N' && s[0] <=
'Y') {
2183 n =
'M' - (int)s[0];
2186 goto invalid_utc_offset;
2202 if (s[6] !=
':')
goto invalid_utc_offset;
2206 if (s[3] !=
':')
goto invalid_utc_offset;
2210 goto invalid_utc_offset;
2213 if (!have_2digits(sec))
goto invalid_utc_offset;
2214 if (sec[0] >
'5')
goto invalid_utc_offset;
2215 n += num_from_2digits(sec);
2219 if (!have_2digits(min))
goto invalid_utc_offset;
2220 if (min[0] >
'5')
goto invalid_utc_offset;
2221 n += num_from_2digits(min) * 60;
2223 if (s[0] !=
'+' && s[0] !=
'-')
goto invalid_utc_offset;
2224 if (!have_2digits(s+1))
goto invalid_utc_offset;
2225 n += num_from_2digits(s+1) * 3600;
2227 if (n == 0)
return UTC_ZONE;
2233 return num_exact(arg);
2241 wideval_t tlocal, wideval_t tutc)
2244 wideval_t w = wsub(tlocal, tutc);
2246 validate_utc_offset(
off);
2247 tobj->vtm.utc_offset =
off;
2248 tobj->vtm.zone = zone;
2249 TZMODE_SET_LOCALTIME(tobj);
2253 extract_time(
VALUE time)
2256 const ID id_to_i = idTo_i;
2258 #define EXTRACT_TIME() do { \
2259 t = v2w(rb_Integer(AREF(to_i))); \
2263 struct time_object *tobj = RTYPEDDATA_GET_DATA(time);
2266 t = rb_time_unmagnify(tobj->timew);
2271 #define AREF(x) rb_struct_aref(time, ID2SYM(id_##x))
2276 #define AREF(x) rb_funcallv(time, id_##x, 0, 0)
2289 const ID id_to_i = idTo_i;
2290 struct vtm *
vtm = &orig_tobj->vtm;
2292 #define EXTRACT_VTM() do { \
2294 vtm->year = obj2vint(AREF(year)); \
2295 vtm->mon = month_arg(AREF(mon)); \
2296 vtm->mday = obj2ubits(AREF(mday), 5); \
2297 vtm->hour = obj2ubits(AREF(hour), 5); \
2298 vtm->min = obj2ubits(AREF(min), 6); \
2299 vtm->sec = obj2subsecx(AREF(sec), &subsecx); \
2300 vtm->isdst = RTEST(AREF(isdst)); \
2301 vtm->utc_offset = Qnil; \
2302 t = v2w(rb_Integer(AREF(to_i))); \
2306 struct time_object *tobj = RTYPEDDATA_GET_DATA(time);
2308 time_get_tm(time, tobj);
2309 time_set_vtm(orig_time, orig_tobj, tobj->vtm);
2310 t = rb_time_unmagnify(tobj->timew);
2311 if (TZMODE_FIXOFF_P(tobj) &&
vtm->utc_offset !=
INT2FIX(0))
2312 t = wadd(t, v2w(
vtm->utc_offset));
2317 #define AREF(x) rb_struct_aref(time, ID2SYM(id_##x))
2323 struct vtm temp_vtm = *
vtm;
2324 GMTIMEW(rb_time_magnify(t), &temp_vtm);
2325 time_set_vtm(orig_time, orig_tobj, temp_vtm);
2328 #define AREF(x) rb_funcallv(time, id_##x, 0, 0)
2334 RB_OBJ_WRITE_UNALIGNED(orig_time, &
vtm->subsecx, subsecx);
2347 tobj->vtm.isdst = (!UNDEF_P(dst) &&
RTEST(dst));
2354 struct time_object *tobj = RTYPEDDATA_GET_DATA(time);
2357 wdivmod(tobj->timew, WINT2FIXWV(TIME_SCALE), &t, &s);
2358 tm = tm_from_time(rb_cTimeTM, time);
2360 if (UNDEF_P(utc))
return 0;
2362 s = extract_time(utc);
2363 zone_set_offset(zone, tobj, t, s);
2364 s = rb_time_magnify(s);
2365 if (tobj->vtm.subsecx !=
INT2FIX(0)) {
2366 s = wadd(s, v2w(tobj->vtm.subsecx));
2368 time_set_timew(time, tobj, s);
2370 zone_set_dst(zone, tobj, tm);
2380 VALUE local, tm, subsecx;
2381 struct time_object *tobj = RTYPEDDATA_GET_DATA(time);
2384 split_second(tobj->timew, &t, &subsecx);
2385 tm = tm_from_time(rb_cTimeTM, time);
2388 if (UNDEF_P(local))
return 0;
2390 s = extract_vtm(local, time, tobj, subsecx);
2391 tobj->vtm.tm_got = 1;
2392 zone_set_offset(zone, tobj, s, t);
2393 zone_set_dst(zone, tobj, tm);
2405 return rb_check_funcall_default(klass, id_find_timezone, 1, &zone,
Qnil);
2411 vtm_day_wraparound(
struct vtm *
vtm)
2413 if (
vtm->hour < 24)
return;
2418 vtm_add_day(
vtm, 1);
2433 vtm.wday = VTM_WDAY_INITVAL;
2435 vtm.zone = str_empty;
2437 vtm.year = obj2vint(year);
2439 vtm.mon =
NIL_P(mon) ? 1 : month_arg(mon);
2441 vtm.mday =
NIL_P(mday) ? 1 : obj2ubits(mday, 5);
2443 vtm.hour =
NIL_P(hour) ? 0 : obj2ubits(hour, 5);
2445 vtm.min =
NIL_P(min) ? 0 : obj2ubits(min, 6);
2453 vtm.sec = obj2subsecx(sec, &subsecx);
2454 vtm.subsecx = subsecx;
2457 return time_init_vtm(time,
vtm, zone);
2466 vtm.isdst = VTM_ISDST_INITVAL;
2468 const VALUE arg = zone;
2475 else if (maybe_tzobj_p(arg))
2477 else if (!
NIL_P(utc = utc_offset_arg(arg)))
2478 vtm.utc_offset = utc == UTC_ZONE ?
INT2FIX(0) : utc;
2479 else if (
NIL_P(zone = find_timezone(time, arg)))
2480 invalid_utc_offset(arg);
2486 GetNewTimeval(time, tobj);
2489 time_set_timew(time, tobj, timegmw(&
vtm));
2490 vtm_day_wraparound(&
vtm);
2491 time_set_vtm(time, tobj,
vtm);
2492 tobj->vtm.tm_got = 1;
2493 TZMODE_SET_LOCALTIME(tobj);
2494 if (zone_timelocal(zone, time)) {
2497 else if (
NIL_P(
vtm.utc_offset = utc_offset_arg(zone))) {
2498 if (
NIL_P(zone = find_timezone(time, zone)) || !zone_timelocal(zone, time))
2499 invalid_utc_offset(arg);
2503 if (utc == UTC_ZONE) {
2504 time_set_timew(time, tobj, timegmw(&
vtm));
2506 vtm_day_wraparound(&
vtm);
2507 time_set_vtm(time, tobj,
vtm);
2508 tobj->vtm.tm_got = 1;
2509 TZMODE_SET_UTC(tobj);
2513 TZMODE_SET_LOCALTIME(tobj);
2518 vtm_add_offset(&
vtm,
off, -1);
2520 time_set_timew(time, tobj, timegmw(&
vtm));
2522 return time_set_utc_offset(time,
off);
2525 time_set_timew(time, tobj, timelocalw(&
vtm));
2527 return time_localtime(time);
2532 two_digits(
const char *
ptr,
const char *end,
const char **endp,
const char *name)
2537 if (
ptr[-1] ==
'-' ||
ptr[-1] ==
':') {
2544 return num_from_2digits(
ptr);
2548 parse_int(
const char *
ptr,
const char *end,
const char **endp,
size_t *ndigits,
bool sign)
2550 ssize_t
len = (end -
ptr);
2551 int flags = sign ? RB_INT_PARSE_SIGN : 0;
2552 return rb_int_parse_cstr(
ptr,
len, (
char **)endp, ndigits, 10, flags);
2569 const char *
ptr = begin;
2571 int mon = -1, mday = -1, hour = -1, min = -1, sec = -1;
2573 size_t prec =
NIL_P(precision) ? SIZE_MAX :
NUM2SIZET(precision);
2578 year = parse_int(
ptr, end, &
ptr, &ndigits,
true);
2582 else if (ndigits < 4) {
2585 else if (
ptr == end) {
2589 #define peekable_p(n) ((ptrdiff_t)(n) < (end - ptr))
2590 #define peek_n(c, n) (peekable_p(n) && ((unsigned char)ptr[n] == (c)))
2591 #define peek(c) peek_n(c, 0)
2592 #define peekc_n(n) (peekable_p(n) ? (int)(unsigned char)ptr[n] : -1)
2593 #define peekc() peekc_n(0)
2594 #define expect_two_digits(x, bits) \
2595 (((unsigned int)(x = two_digits(ptr + 1, end, &ptr, #x)) > (1U << bits) - 1) ? \
2596 rb_raise(rb_eArgError, #x" out of range") : (void)0)
2597 if (!peek(
'-'))
break;
2598 expect_two_digits(mon, 4);
2599 if (!peek(
'-'))
break;
2600 expect_two_digits(mday, 5);
2601 if (!peek(
' ') && !peek(
'T'))
break;
2602 const char *
const time_part =
ptr + 1;
2603 if (!
ISDIGIT(peekc_n(1)))
break;
2604 #define nofraction(x) \
2606 rb_raise(rb_eArgError, "fraction " #x " is not supported: %.*s", \
2607 (int)(ptr + 1 - time_part), time_part); \
2609 #define need_colon(x) \
2611 rb_raise(rb_eArgError, "missing " #x " part: %.*s", \
2612 (int)(ptr + 1 - time_part), time_part); \
2614 expect_two_digits(hour, 5);
2617 expect_two_digits(min, 6);
2620 expect_two_digits(sec, 6);
2623 for (ndigits = 0; ndigits < prec &&
ISDIGIT(peekc_n(ndigits)); ++ndigits);
2626 if (clen < 0) clen = 0;
2628 (
int)(
ptr - time_part) + clen, time_part);
2630 subsec = parse_int(
ptr,
ptr + ndigits, &
ptr, &ndigits,
false);
2631 if (
NIL_P(subsec))
break;
2636 const char *
const zstr =
ptr;
2638 const char *
const zend =
ptr;
2648 else if (hour == -1) {
2651 if (!
NIL_P(subsec)) {
2653 if (ndigits < (
size_t)TIME_SCALE_NUMDIGITS) {
2655 subsec = rb_int_mul(subsec, mul);
2657 else if (ndigits > (
size_t)TIME_SCALE_NUMDIGITS) {
2667 .wday = VTM_WDAY_INITVAL,
2671 .mon = (mon < 0) ? 1 : mon,
2672 .mday = (mday < 0) ? 1 : mday,
2673 .hour = (hour < 0) ? 0 : hour,
2674 .min = (min < 0) ? 0 : min,
2675 .sec = (sec < 0) ? 0 : sec,
2678 return time_init_vtm(time,
vtm, zone);
2682 subsec_normalize(time_t *secp,
long *subsecp,
const long maxsubsec)
2685 long subsec = *subsecp;
2688 if (UNLIKELY(subsec >= maxsubsec)) {
2689 sec2 = subsec / maxsubsec;
2690 if (TIMET_MAX - sec2 < sec) {
2693 subsec -= sec2 * maxsubsec;
2696 else if (UNLIKELY(subsec < 0)) {
2697 sec2 = NDIV(subsec, maxsubsec);
2698 if (sec < TIMET_MIN - sec2) {
2701 subsec -= sec2 * maxsubsec;
2704 #ifndef NEGATIVE_TIME_T
2712 #define time_usec_normalize(secp, usecp) subsec_normalize(secp, usecp, 1000000)
2713 #define time_nsec_normalize(secp, nsecp) subsec_normalize(secp, nsecp, 1000000000)
2716 nsec2timew(time_t sec,
long nsec)
2718 time_nsec_normalize(&sec, &nsec);
2719 return timenano2timew(sec, nsec);
2723 time_new_timew(
VALUE klass, wideval_t timew)
2725 VALUE time = time_s_alloc(klass);
2728 tobj = RTYPEDDATA_GET_DATA(time);
2729 TZMODE_SET_LOCALTIME(tobj);
2730 time_set_timew(time, tobj, timew);
2738 time_usec_normalize(&sec, &usec);
2739 return time_new_timew(
rb_cTime, timenano2timew(sec, usec * 1000));
2746 return time_new_timew(
rb_cTime, nsec2timew(sec, nsec));
2753 VALUE time = time_new_timew(
rb_cTime, nsec2timew(ts->tv_sec, ts->tv_nsec));
2755 if (-86400 < offset && offset < 86400) {
2756 GetTimeval(time, tobj);
2757 TZMODE_SET_FIXOFF(time, tobj,
INT2FIX(offset));
2759 else if (offset == INT_MAX) {
2761 else if (offset == INT_MAX-1) {
2762 GetTimeval(time, tobj);
2763 TZMODE_SET_UTC(tobj);
2775 VALUE time = time_new_timew(
rb_cTime, rb_time_magnify(v2w(timev)));
2780 if (maybe_tzobj_p(zone)) {
2782 if (zone_timelocal(zone, time))
return time;
2786 if (
NIL_P(zone = find_timezone(time,
off))) invalid_utc_offset(
off);
2788 if (!zone_timelocal(zone, time)) invalid_utc_offset(
off);
2791 else if (
off == UTC_ZONE) {
2792 return time_gmtime(time);
2795 validate_utc_offset(
off);
2796 time_set_utc_offset(time,
off);
2804 time_timespec(
VALUE num, int interval)
2807 const char *
const tstr = interval ?
"time interval" :
"time";
2810 #ifndef NEGATIVE_TIME_T
2811 # define arg_range_check(v) \
2813 rb_raise(rb_eArgError, "%s must not be negative", tstr) : \
2816 # define arg_range_check(v) \
2817 ((interval && (v) < 0) ? \
2818 rb_raise(rb_eArgError, "time interval must not be negative") : \
2823 t.tv_sec = NUM2TIMET(num);
2824 arg_range_check(t.tv_sec);
2835 t.tv_nsec = (int)(d*1e9+0.5);
2836 if (t.tv_nsec >= 1000000000) {
2837 t.tv_nsec -= 1000000000;
2841 else if ((t.tv_nsec = (
int)(-d*1e9+0.5)) > 0) {
2842 t.tv_nsec = 1000000000 - t.tv_nsec;
2845 t.tv_sec = (time_t)f;
2846 if (f != t.tv_sec) {
2851 else if (RB_BIGNUM_TYPE_P(num)) {
2852 t.tv_sec = NUM2TIMET(num);
2853 arg_range_check(t.tv_sec);
2862 t.tv_sec = NUM2TIMET(i);
2863 arg_range_check(t.tv_sec);
2873 #undef arg_range_check
2877 time_timeval(
VALUE num, int interval)
2882 ts = time_timespec(num, interval);
2883 tv.tv_sec = (TYPEOF_TIMEVAL_TV_SEC)ts.tv_sec;
2884 tv.tv_usec = (TYPEOF_TIMEVAL_TV_USEC)(ts.tv_nsec / 1000);
2892 return time_timeval(num, TRUE);
2902 if (IsTimeval(time)) {
2903 GetTimeval(time, tobj);
2904 ts = timew2timespec(tobj->timew);
2905 t.tv_sec = (TYPEOF_TIMEVAL_TV_SEC)ts.tv_sec;
2906 t.tv_usec = (TYPEOF_TIMEVAL_TV_USEC)(ts.tv_nsec / 1000);
2909 return time_timeval(time, FALSE);
2918 if (IsTimeval(time)) {
2919 GetTimeval(time, tobj);
2920 t = timew2timespec(tobj->timew);
2923 return time_timespec(time, FALSE);
2929 return time_timespec(num, TRUE);
2933 get_scale(
VALUE unit)
2935 if (unit ==
ID2SYM(id_nanosecond) || unit ==
ID2SYM(id_nsec)) {
2938 else if (unit ==
ID2SYM(id_microsecond) || unit ==
ID2SYM(id_usec)) {
2941 else if (unit ==
ID2SYM(id_millisecond)) {
2956 int scale = get_scale(unit);
2957 time = num_exact(time);
2958 t = num_exact(subsec);
2959 timew = wadd(rb_time_magnify(v2w(time)), wmulquoll(v2w(t), TIME_SCALE, scale));
2960 t = time_new_timew(klass, timew);
2962 else if (IsTimeval(time)) {
2964 GetTimeval(time, tobj);
2965 t = time_new_timew(klass, tobj->timew);
2966 GetTimeval(t, tobj2);
2967 TZMODE_COPY(tobj2, tobj);
2970 timew = rb_time_magnify(v2w(num_exact(time)));
2971 t = time_new_timew(klass, timew);
2974 time_zonelocal(t, zone);
2986 static const char months[][4] = {
2987 "jan",
"feb",
"mar",
"apr",
"may",
"jun",
2988 "jul",
"aug",
"sep",
"oct",
"nov",
"dec",
3003 obj2ubits(
VALUE obj,
unsigned int bits)
3005 const unsigned int usable_mask = (1U << bits) - 1;
3006 unsigned int rv = (
unsigned int)obj2int(obj);
3008 if ((rv & usable_mask) != rv)
3010 return (uint32_t)rv;
3036 divmodv(num_exact(obj),
INT2FIX(1), &obj, &subsec);
3037 *subsecx = w2v(rb_time_magnify(v2w(subsec)));
3039 return obj2ubits(obj, 6);
3043 usec2subsecx(
VALUE obj)
3049 return mulquov(num_exact(obj),
INT2FIX(TIME_SCALE),
INT2FIX(1000000));
3053 month_arg(
VALUE arg)
3058 return obj2ubits(arg, 4);
3065 for (i=0; i<12; i++) {
3074 mon = obj2ubits(arg, 4);
3080 validate_utc_offset(
VALUE utc_offset)
3082 if (le(utc_offset,
INT2FIX(-86400)) || ge(utc_offset,
INT2FIX(86400)))
3088 validate_zone_name(
VALUE zone_name)
3095 validate_vtm(
struct vtm *
vtm)
3097 #define validate_vtm_range(mem, b, e) \
3098 ((vtm->mem < b || vtm->mem > e) ? \
3099 rb_raise(rb_eArgError, #mem" out of range") : (void)0)
3100 validate_vtm_range(mon, 1, 12);
3101 validate_vtm_range(mday, 1, 31);
3102 validate_vtm_range(hour, 0, 24);
3103 validate_vtm_range(min, 0, (
vtm->hour == 24 ? 0 : 59));
3104 validate_vtm_range(sec, 0, (
vtm->hour == 24 ? 0 : 60));
3107 if (!
NIL_P(
vtm->utc_offset)) validate_utc_offset(
vtm->utc_offset);
3108 #undef validate_vtm_range
3112 time_arg(
int argc,
const VALUE *argv,
struct vtm *
vtm)
3128 vtm->zone = str_empty;
3138 vtm->isdst =
RTEST(argv[8]) ? 1 : 0;
3141 rb_scan_args(argc, argv,
"17", &v[0],&v[1],&v[2],&v[3],&v[4],&v[5],&v[6],&v[7]);
3144 vtm->wday = VTM_WDAY_INITVAL;
3145 vtm->isdst = VTM_ISDST_INITVAL;
3148 vtm->year = obj2vint(v[0]);
3154 vtm->mon = month_arg(v[1]);
3161 vtm->mday = obj2ubits(v[2], 5);
3169 unsigned int mday2 = leap_year_v_p(
vtm->year) ? 29 : 28;
3170 if (
vtm->mday > mday2) {
3180 if (
vtm->mday == 31) {
3187 vtm->hour =
NIL_P(v[3])?0:obj2ubits(v[3], 5);
3189 vtm->min =
NIL_P(v[4])?0:obj2ubits(v[4], 6);
3191 if (!
NIL_P(v[6]) && argc == 7) {
3192 vtm->sec =
NIL_P(v[5])?0:obj2ubits(v[5],6);
3193 subsecx = usec2subsecx(v[6]);
3201 vtm->sec = obj2subsecx(v[5], &subsecx);
3204 vtm->subsecx = subsecx;
3216 unsigned long uy = (
unsigned long)(LIKELY(y >= 0) ? y : -y);
3218 if (LIKELY(uy % 4 != 0))
return 0;
3220 unsigned long century = uy / 100;
3221 if (LIKELY(uy != century * 100))
return 1;
3222 return century % 4 == 0;
3226 timegm_noleapsecond(
struct tm *tm)
3228 long tm_year = tm->tm_year;
3229 int tm_yday = calc_tm_yday(tm->tm_year, tm->tm_mon, tm->tm_mday);
3237 return tm->tm_sec + tm->tm_min*60 + tm->tm_hour*3600 +
3241 DIV(tm_year-1,100) +
3242 DIV(tm_year+299,400))*86400;
3246 #define DEBUG_FIND_TIME_NUMGUESS
3247 #define DEBUG_GUESSRANGE
3250 static const bool debug_guessrange =
3251 #ifdef DEBUG_GUESSRANGE
3257 #define DEBUG_REPORT_GUESSRANGE \
3258 (debug_guessrange ? debug_report_guessrange(guess_lo, guess_hi) : (void)0)
3261 debug_report_guessrange(time_t guess_lo, time_t guess_hi)
3263 time_t guess_diff = guess_hi - guess_lo;
3264 fprintf(stderr,
"find time guess range: %"PRI_TIMET_PREFIX
"d - "
3265 "%"PRI_TIMET_PREFIX
"d : %"PRI_TIMET_PREFIX
"u\n",
3266 guess_lo, guess_hi, guess_diff);
3269 static const bool debug_find_time_numguess =
3270 #ifdef DEBUG_FIND_TIME_NUMGUESS
3276 #define DEBUG_FIND_TIME_NUMGUESS_INC \
3277 (void)(debug_find_time_numguess && find_time_numguess++),
3278 static unsigned long long find_time_numguess;
3281 find_time_numguess_getter(
ID name,
VALUE *data)
3283 unsigned long long *numguess = (
void *)data;
3288 find_time_t(
struct tm *tptr,
int utc_p, time_t *tp)
3290 time_t guess, guess0, guess_lo, guess_hi;
3291 struct tm *tm, tm0, tm_lo, tm_hi;
3298 #define GUESS(p) (DEBUG_FIND_TIME_NUMGUESS_INC (utc_p ? gmtime_with_leapsecond((p), &result) : LOCALTIME((p), result)))
3300 guess_lo = TIMET_MIN;
3301 guess_hi = TIMET_MAX;
3303 find_dst = 0 < tptr->tm_isdst;
3309 if (tm0.tm_mon < 0) {
3316 else if (11 < tm0.tm_mon) {
3323 else if (tm0.tm_mday < 1) {
3329 else if ((d = days_in_month_in(1900 + tm0.tm_year)[tm0.tm_mon]) < tm0.tm_mday) {
3335 else if (tm0.tm_hour < 0) {
3340 else if (23 < tm0.tm_hour) {
3345 else if (tm0.tm_min < 0) {
3349 else if (59 < tm0.tm_min) {
3353 else if (tm0.tm_sec < 0) {
3356 else if (60 < tm0.tm_sec) {
3360 DEBUG_REPORT_GUESSRANGE;
3361 guess0 = guess = timegm_noleapsecond(&tm0);
3364 d = tmcmp(tptr, tm);
3365 if (d == 0) {
goto found; }
3368 guess -= 24 * 60 * 60;
3372 guess += 24 * 60 * 60;
3374 DEBUG_REPORT_GUESSRANGE;
3375 if (guess_lo < guess && guess < guess_hi && (tm = GUESS(&guess)) != NULL) {
3376 d = tmcmp(tptr, tm);
3377 if (d == 0) {
goto found; }
3382 DEBUG_REPORT_GUESSRANGE;
3386 tm = GUESS(&guess_lo);
3387 if (!tm)
goto error;
3388 d = tmcmp(tptr, tm);
3389 if (d < 0)
goto out_of_range;
3390 if (d == 0) { guess = guess_lo;
goto found; }
3393 tm = GUESS(&guess_hi);
3394 if (!tm)
goto error;
3395 d = tmcmp(tptr, tm);
3396 if (d > 0)
goto out_of_range;
3397 if (d == 0) { guess = guess_hi;
goto found; }
3400 DEBUG_REPORT_GUESSRANGE;
3404 while (guess_lo + 1 < guess_hi) {
3407 guess = guess_lo / 2 + guess_hi / 2;
3408 if (guess <= guess_lo)
3409 guess = guess_lo + 1;
3410 else if (guess >= guess_hi)
3411 guess = guess_hi - 1;
3416 time_t guess0_hi = timegm_noleapsecond(&tm_hi);
3417 guess = guess_hi - (guess0_hi - guess0);
3418 if (guess == guess_hi)
3422 else if (status == 2) {
3423 time_t guess0_lo = timegm_noleapsecond(&tm_lo);
3424 guess = guess_lo + (guess0 - guess0_lo);
3425 if (guess == guess_lo)
3429 if (guess <= guess_lo || guess_hi <= guess) {
3431 if (debug_guessrange) {
3432 if (guess <= guess_lo) {
3433 fprintf(stderr,
"too small guess: %"PRI_TIMET_PREFIX
"d"\
3434 " <= %"PRI_TIMET_PREFIX
"d\n", guess, guess_lo);
3436 if (guess_hi <= guess) {
3437 fprintf(stderr,
"too big guess: %"PRI_TIMET_PREFIX
"d"\
3438 " <= %"PRI_TIMET_PREFIX
"d\n", guess_hi, guess);
3447 if (!tm)
goto error;
3449 d = tmcmp(tptr, tm);
3454 DEBUG_REPORT_GUESSRANGE;
3459 DEBUG_REPORT_GUESSRANGE;
3474 tptr_tm_yday = calc_tm_yday(tptr->tm_year, tptr->tm_mon, tptr->tm_mday);
3477 ((tptr->tm_year - tm_lo.tm_year) * 365 +
3478 DIV((tptr->tm_year-69), 4) -
3479 DIV((tptr->tm_year-1), 100) +
3480 DIV((tptr->tm_year+299), 400) -
3481 DIV((tm_lo.tm_year-69), 4) +
3482 DIV((tm_lo.tm_year-1), 100) -
3483 DIV((tm_lo.tm_year+299), 400) +
3485 tm_lo.tm_yday) * 86400 +
3486 (tptr->tm_hour - tm_lo.tm_hour) * 3600 +
3487 (tptr->tm_min - tm_lo.tm_min) * 60 +
3488 (tptr->tm_sec - (tm_lo.tm_sec == 60 ? 59 : tm_lo.tm_sec));
3497 guess2 = guess - 2 * 60 * 60;
3498 tm = LOCALTIME(&guess2, result);
3500 if (tptr->tm_hour != (tm->tm_hour + 2) % 24 ||
3501 tptr->tm_min != tm->tm_min ||
3502 tptr->tm_sec != tm->tm_sec) {
3503 guess2 -= (tm->tm_hour - tptr->tm_hour) * 60 * 60 +
3504 (tm->tm_min - tptr->tm_min) * 60 +
3505 (tm->tm_sec - tptr->tm_sec);
3506 if (tptr->tm_mday != tm->tm_mday)
3507 guess2 += 24 * 60 * 60;
3508 if (guess != guess2) {
3509 tm = LOCALTIME(&guess2, result);
3510 if (tm && tmcmp(tptr, tm) == 0) {
3522 guess2 = guess + 2 * 60 * 60;
3523 tm = LOCALTIME(&guess2, result);
3525 if ((tptr->tm_hour + 2) % 24 != tm->tm_hour ||
3526 tptr->tm_min != tm->tm_min ||
3527 tptr->tm_sec != tm->tm_sec) {
3528 guess2 -= (tm->tm_hour - tptr->tm_hour) * 60 * 60 +
3529 (tm->tm_min - tptr->tm_min) * 60 +
3530 (tm->tm_sec - tptr->tm_sec);
3531 if (tptr->tm_mday != tm->tm_mday)
3532 guess2 -= 24 * 60 * 60;
3533 if (guess != guess2) {
3534 tm = LOCALTIME(&guess2, result);
3535 if (tm && tmcmp(tptr, tm) == 0) {
3551 return "time out of range";
3554 return "gmtime/localtime error";
3558 vtmcmp(
struct vtm *a,
struct vtm *b)
3560 if (ne(a->year, b->year))
3561 return lt(a->year, b->year) ? -1 : 1;
3562 else if (a->mon != b->mon)
3563 return a->mon < b->mon ? -1 : 1;
3564 else if (a->mday != b->mday)
3565 return a->mday < b->mday ? -1 : 1;
3566 else if (a->hour != b->hour)
3567 return a->hour < b->hour ? -1 : 1;
3568 else if (a->min != b->min)
3569 return a->min < b->min ? -1 : 1;
3570 else if (a->sec != b->sec)
3571 return a->sec < b->sec ? -1 : 1;
3572 else if (ne(a->subsecx, b->subsecx))
3573 return lt(a->subsecx, b->subsecx) ? -1 : 1;
3579 tmcmp(
struct tm *a,
struct tm *b)
3581 if (a->tm_year != b->tm_year)
3582 return a->tm_year < b->tm_year ? -1 : 1;
3583 else if (a->tm_mon != b->tm_mon)
3584 return a->tm_mon < b->tm_mon ? -1 : 1;
3585 else if (a->tm_mday != b->tm_mday)
3586 return a->tm_mday < b->tm_mday ? -1 : 1;
3587 else if (a->tm_hour != b->tm_hour)
3588 return a->tm_hour < b->tm_hour ? -1 : 1;
3589 else if (a->tm_min != b->tm_min)
3590 return a->tm_min < b->tm_min ? -1 : 1;
3591 else if (a->tm_sec != b->tm_sec)
3592 return a->tm_sec < b->tm_sec ? -1 : 1;
3698 time_s_mkutc(
int argc,
VALUE *argv,
VALUE klass)
3702 time_arg(argc, argv, &
vtm);
3703 return time_gmtime(time_new_timew(klass, timegmw(&
vtm)));
3724 time_s_mktime(
int argc,
VALUE *argv,
VALUE klass)
3728 time_arg(argc, argv, &
vtm);
3729 return time_localtime(time_new_timew(klass, timelocalw(&
vtm)));
3749 time_to_i(
VALUE time)
3753 GetTimeval(time, tobj);
3754 return w2v(wdiv(tobj->timew, WINT2FIXWV(TIME_SCALE)));
3778 time_to_f(
VALUE time)
3782 GetTimeval(time, tobj);
3783 return rb_Float(rb_time_unmagnify_to_float(tobj->timew));
3799 time_to_r(
VALUE time)
3804 GetTimeval(time, tobj);
3805 v = rb_time_unmagnify_to_rational(tobj->timew);
3827 time_usec(
VALUE time)
3832 GetTimeval(time, tobj);
3834 w = wmod(tobj->timew, WINT2WV(TIME_SCALE));
3835 wmuldivmod(w, WINT2FIXWV(1000000), WINT2FIXWV(TIME_SCALE), &q, &r);
3854 time_nsec(
VALUE time)
3858 GetTimeval(time, tobj);
3859 return rb_to_int(w2v(wmulquoll(wmod(tobj->timew, WINT2WV(TIME_SCALE)), 1000000000, TIME_SCALE)));
3880 time_subsec(
VALUE time)
3884 GetTimeval(time, tobj);
3885 return quov(w2v(wmod(tobj->timew, WINT2FIXWV(TIME_SCALE))),
INT2FIX(TIME_SCALE));
3922 GetTimeval(time1, tobj1);
3923 if (IsTimeval(time2)) {
3924 GetTimeval(time2, tobj2);
3925 n = wcmp(tobj1->timew, tobj2->timew);
3928 return rb_invcmp(time1, time2);
3930 if (n == 0)
return INT2FIX(0);
3948 GetTimeval(time1, tobj1);
3949 if (IsTimeval(time2)) {
3950 GetTimeval(time2, tobj2);
3951 return rb_equal(w2v(tobj1->timew), w2v(tobj2->timew));
3973 time_utc_p(
VALUE time)
3977 GetTimeval(time, tobj);
3978 return RBOOL(TZMODE_UTC_P(tobj));
3991 time_hash(
VALUE time)
3995 GetTimeval(time, tobj);
3996 return rb_hash(w2v(tobj->timew));
4006 GetTimeval(time, tobj);
4007 GetNewTimeval(copy, tcopy);
4014 time_dup(
VALUE time)
4017 time_init_copy(dup, time);
4022 time_localtime(
VALUE time)
4028 GetTimeval(time, tobj);
4029 if (TZMODE_LOCALTIME_P(tobj)) {
4030 if (tobj->vtm.tm_got)
4037 zone = tobj->vtm.zone;
4038 if (maybe_tzobj_p(zone) && zone_localtime(zone, time)) {
4042 if (!localtimew(tobj->timew, &
vtm))
4044 time_set_vtm(time, tobj,
vtm);
4046 tobj->vtm.tm_got = 1;
4047 TZMODE_SET_LOCALTIME(tobj);
4055 if (zone_localtime(zone, time))
return time;
4059 if (
NIL_P(zone = find_timezone(time,
off))) invalid_utc_offset(
off);
4060 if (!zone_localtime(zone, time)) invalid_utc_offset(
off);
4063 else if (
off == UTC_ZONE) {
4064 return time_gmtime(time);
4066 validate_utc_offset(
off);
4068 time_set_utc_offset(time,
off);
4069 return time_fixoff(time);
4098 time_localtime_m(
int argc,
VALUE *argv,
VALUE time)
4103 return time_zonelocal(time,
off);
4106 return time_localtime(time);
4124 time_gmtime(
VALUE time)
4129 GetTimeval(time, tobj);
4130 if (TZMODE_UTC_P(tobj)) {
4131 if (tobj->vtm.tm_got)
4139 GMTIMEW(tobj->timew, &
vtm);
4140 time_set_vtm(time, tobj,
vtm);
4142 tobj->vtm.tm_got = 1;
4143 TZMODE_SET_UTC(tobj);
4148 time_fixoff(
VALUE time)
4154 GetTimeval(time, tobj);
4155 if (TZMODE_FIXOFF_P(tobj)) {
4156 if (tobj->vtm.tm_got)
4163 if (TZMODE_FIXOFF_P(tobj))
4164 off = tobj->vtm.utc_offset;
4168 GMTIMEW(tobj->timew, &
vtm);
4170 zone = tobj->vtm.zone;
4171 vtm_add_offset(&
vtm,
off, +1);
4173 time_set_vtm(time, tobj,
vtm);
4174 RB_OBJ_WRITE_UNALIGNED(time, &tobj->vtm.zone, zone);
4176 tobj->vtm.tm_got = 1;
4177 TZMODE_SET_FIXOFF(time, tobj,
off);
4199 time_getlocaltime(
int argc,
VALUE *argv,
VALUE time)
4205 if (maybe_tzobj_p(zone)) {
4206 VALUE t = time_dup(time);
4207 if (zone_localtime(
off, t))
return t;
4212 if (
NIL_P(zone = find_timezone(time,
off))) invalid_utc_offset(
off);
4213 time = time_dup(time);
4214 if (!zone_localtime(zone, time)) invalid_utc_offset(
off);
4217 else if (
off == UTC_ZONE) {
4218 return time_gmtime(time_dup(time));
4220 validate_utc_offset(
off);
4222 time = time_dup(time);
4223 time_set_utc_offset(time,
off);
4224 return time_fixoff(time);
4227 return time_localtime(time_dup(time));
4246 time_getgmtime(
VALUE time)
4248 return time_gmtime(time_dup(time));
4254 if (TZMODE_UTC_P(tobj))
return time_gmtime(time);
4255 if (TZMODE_FIXOFF_P(tobj))
return time_fixoff(time);
4256 return time_localtime(time);
4260 #define strftimev(fmt, time, enc) strftime_cstr((fmt), rb_strlen_lit(fmt), (time), (enc))
4284 time_asctime(
VALUE time)
4306 time_to_s(
VALUE time)
4310 GetTimeval(time, tobj);
4311 if (TZMODE_UTC_P(tobj))
4334 time_inspect(
VALUE time)
4339 GetTimeval(time, tobj);
4341 subsec = w2v(wmod(tobj->timew, WINT2FIXWV(TIME_SCALE)));
4353 subsec = quov(subsec,
INT2FIX(TIME_SCALE));
4356 if (TZMODE_UTC_P(tobj)) {
4362 char sign = (
off < 0) ? (
off = -
off,
'-') :
'+';
4364 int min = (
off /= 60) % 60;
4378 offset = num_exact(offset);
4380 result = time_new_timew(klass, wsub(tobj->timew, rb_time_magnify(v2w(offset))));
4382 result = time_new_timew(klass, wadd(tobj->timew, rb_time_magnify(v2w(offset))));
4383 GetTimeval(result, result_tobj);
4384 TZMODE_COPY(result_tobj, tobj);
4392 return time_add0(
rb_cTime, tobj, torig, offset, sign);
4413 GetTimeval(time1, tobj);
4415 if (IsTimeval(time2)) {
4418 return time_add(tobj, time1, time2, 1);
4448 GetTimeval(time1, tobj);
4449 if (IsTimeval(time2)) {
4452 GetTimeval(time2, tobj2);
4453 return rb_Float(rb_time_unmagnify_to_float(wsub(tobj->timew, tobj2->timew)));
4455 return time_add(tobj, time1, time2, -1);
4459 ndigits_denominator(
VALUE ndigits)
4502 time_round(
int argc,
VALUE *argv,
VALUE time)
4504 VALUE ndigits, v, den;
4510 den = ndigits_denominator(ndigits);
4512 GetTimeval(time, tobj);
4513 v = w2v(rb_time_unmagnify(tobj->timew));
4516 if (lt(v, quov(den,
INT2FIX(2))))
4517 return time_add(tobj, time, v, -1);
4519 return time_add(tobj, time, subv(den, v), 1);
4550 time_floor(
int argc,
VALUE *argv,
VALUE time)
4552 VALUE ndigits, v, den;
4558 den = ndigits_denominator(ndigits);
4560 GetTimeval(time, tobj);
4561 v = w2v(rb_time_unmagnify(tobj->timew));
4564 return time_add(tobj, time, v, -1);
4597 VALUE ndigits, v, den;
4603 den = ndigits_denominator(ndigits);
4605 GetTimeval(time, tobj);
4606 v = w2v(rb_time_unmagnify(tobj->timew));
4612 return time_add(tobj, time, v, 1);
4633 time_sec(
VALUE time)
4637 GetTimeval(time, tobj);
4638 MAKE_TM(time, tobj);
4639 return INT2FIX(tobj->vtm.sec);
4657 time_min(
VALUE time)
4661 GetTimeval(time, tobj);
4662 MAKE_TM(time, tobj);
4663 return INT2FIX(tobj->vtm.min);
4681 time_hour(
VALUE time)
4685 GetTimeval(time, tobj);
4686 MAKE_TM(time, tobj);
4687 return INT2FIX(tobj->vtm.hour);
4705 time_mday(
VALUE time)
4709 GetTimeval(time, tobj);
4710 MAKE_TM(time, tobj);
4711 return INT2FIX(tobj->vtm.mday);
4729 time_mon(
VALUE time)
4733 GetTimeval(time, tobj);
4734 MAKE_TM(time, tobj);
4735 return INT2FIX(tobj->vtm.mon);
4752 time_year(
VALUE time)
4756 GetTimeval(time, tobj);
4757 MAKE_TM(time, tobj);
4758 return tobj->vtm.year;
4777 time_wday(
VALUE time)
4781 GetTimeval(time, tobj);
4782 MAKE_TM_ENSURE(time, tobj, tobj->vtm.wday != VTM_WDAY_INITVAL);
4783 return INT2FIX((
int)tobj->vtm.wday);
4786 #define wday_p(n) {\
4787 return RBOOL(time_wday(time) == INT2FIX(n)); \
4803 time_sunday(
VALUE time)
4821 time_monday(
VALUE time)
4839 time_tuesday(
VALUE time)
4857 time_wednesday(
VALUE time)
4875 time_thursday(
VALUE time)
4893 time_friday(
VALUE time)
4911 time_saturday(
VALUE time)
4927 time_yday(
VALUE time)
4931 GetTimeval(time, tobj);
4932 MAKE_TM_ENSURE(time, tobj, tobj->vtm.yday != 0);
4933 return INT2FIX(tobj->vtm.yday);
4952 time_isdst(
VALUE time)
4956 GetTimeval(time, tobj);
4957 MAKE_TM(time, tobj);
4958 if (tobj->vtm.isdst == VTM_ISDST_INITVAL) {
4961 return RBOOL(tobj->vtm.isdst);
4975 time_zone(
VALUE time)
4980 GetTimeval(time, tobj);
4981 MAKE_TM(time, tobj);
4983 if (TZMODE_UTC_P(tobj)) {
4986 zone = tobj->vtm.zone;
5011 GetTimeval(time, tobj);
5013 if (TZMODE_UTC_P(tobj)) {
5017 MAKE_TM(time, tobj);
5018 return tobj->vtm.utc_offset;
5038 time_to_a(
VALUE time)
5042 GetTimeval(time, tobj);
5043 MAKE_TM_ENSURE(time, tobj, tobj->vtm.yday != 0);
5053 RBOOL(tobj->vtm.isdst),
5093 time_deconstruct_keys(
VALUE time,
VALUE keys)
5099 GetTimeval(time, tobj);
5100 MAKE_TM_ENSURE(time, tobj, tobj->vtm.yday != 0);
5103 h = rb_hash_new_with_size(11);
5114 quov(w2v(wmod(tobj->timew, WINT2FIXWV(TIME_SCALE))),
INT2FIX(TIME_SCALE)));
5122 "wrong argument type %"PRIsVALUE
" (expected Array or nil)",
5132 if (sym_year == key)
rb_hash_aset(h, key, tobj->vtm.year);
5140 if (sym_subsec == key) {
5141 rb_hash_aset(h, key, quov(w2v(wmod(tobj->timew, WINT2FIXWV(TIME_SCALE))),
INT2FIX(TIME_SCALE)));
5143 if (sym_dst == key)
rb_hash_aset(h, key, RBOOL(tobj->vtm.isdst));
5144 if (sym_zone == key)
rb_hash_aset(h, key, time_zone(time));
5150 rb_strftime_alloc(
const char *format,
size_t format_len,
rb_encoding *enc,
5151 VALUE time,
struct vtm *
vtm, wideval_t timew,
int gmt)
5156 if (!timew2timespec_exact(timew, &ts))
5157 timev = w2v(rb_time_unmagnify(timew));
5160 return rb_strftime_timespec(format, format_len, enc, time,
vtm, &ts, gmt);
5163 return rb_strftime(format, format_len, enc, time,
vtm, timev, gmt);
5173 GetTimeval(time, tobj);
5174 MAKE_TM(time, tobj);
5175 str = rb_strftime_alloc(fmt,
len, enc, time, &tobj->vtm, tobj->timew, TZMODE_UTC_P(tobj));
5198 GetTimeval(time, tobj);
5199 MAKE_TM_ENSURE(time, tobj, tobj->vtm.yday != 0);
5204 tmp = rb_str_tmp_frozen_acquire(format);
5209 rb_warning(
"strftime called with empty format string");
5213 VALUE str = rb_strftime_alloc(fmt,
len, enc, time, &tobj->vtm, tobj->timew,
5214 TZMODE_UTC_P(tobj));
5215 rb_str_tmp_frozen_release(format, tmp);
5222 time_xmlschema(
int argc,
VALUE *argv,
VALUE time)
5224 long fraction_digits = 0;
5227 fraction_digits =
NUM2LONG(argv[0]);
5228 if (fraction_digits < 0) {
5229 fraction_digits = 0;
5235 GetTimeval(time, tobj);
5236 MAKE_TM(time, tobj);
5238 const long size_after_year =
sizeof(
"-MM-DDTHH:MM:SS+ZH:ZM") + fraction_digits
5239 + (fraction_digits > 0);
5243 # define fill_digits_long(len, prec, n) \
5244 for (int fill_it = 1, written = snprintf(ptr, len, "%0*ld", prec, n); \
5245 fill_it; ptr += written, fill_it = 0)
5248 long year =
FIX2LONG(tobj->vtm.year);
5250 int w = (year >= -9999 && year <= 9999 ? year_width : (year < 0) + (
int)
DECIMAL_SIZE_OF(year));
5253 fill_digits_long(w + 1, year_width, year) {
5254 if (year >= -9999 && year <= 9999) {
5264 str = rb_int2str(tobj->vtm.year, 10);
5269 # define fill_2(c, n) (*ptr++ = c, *ptr++ = '0' + (n) / 10, *ptr++ = '0' + (n) % 10)
5270 fill_2(
'-', tobj->vtm.mon);
5271 fill_2(
'-', tobj->vtm.mday);
5272 fill_2(
'T', tobj->vtm.hour);
5273 fill_2(
':', tobj->vtm.min);
5274 fill_2(
':', tobj->vtm.sec);
5276 if (fraction_digits > 0) {
5277 VALUE subsecx = tobj->vtm.subsecx;
5281 if (fraction_digits <= TIME_SCALE_NUMDIGITS) {
5282 digits = TIME_SCALE_NUMDIGITS - (int)fraction_digits;
5285 long w = fraction_digits - TIME_SCALE_NUMDIGITS;
5292 if (digits >= 0 && fraction_digits < INT_MAX) {
5294 if (digits > 0) subsec /= (long)pow(10, digits);
5295 fill_digits_long(fraction_digits + 1, (
int)fraction_digits, subsec) {
5300 subsecx = rb_int2str(subsecx, 10);
5302 if (fraction_digits >
len) {
5303 memset(
ptr,
'0', fraction_digits -
len);
5306 len = fraction_digits;
5308 ptr += fraction_digits;
5313 if (TZMODE_UTC_P(tobj)) {
5319 char sign = offset < 0 ?
'-' :
'+';
5320 if (offset < 0) offset = -offset;
5322 fill_2(sign, offset / 60);
5323 fill_2(
':', offset % 60);
5330 int ruby_marshal_write_long(
long x,
char *buf);
5332 enum {base_dump_size = 8};
5336 time_mdump(
VALUE time)
5340 char buf[base_dump_size +
sizeof(long) + 1];
5347 VALUE subsecx, nano, subnano, v, zone;
5350 const int max_year = 1900+0xffff;
5352 GetTimeval(time, tobj);
5354 gmtimew(tobj->timew, &
vtm);
5358 if (year > max_year) {
5359 year_extend =
INT2FIX(year - max_year);
5362 else if (year < 1900) {
5363 year_extend =
LONG2NUM(1900 - year);
5368 if (rb_int_positive_p(
vtm.year)) {
5369 year_extend = rb_int_minus(
vtm.year,
INT2FIX(max_year));
5373 year_extend = rb_int_minus(
INT2FIX(1900),
vtm.year);
5378 subsecx =
vtm.subsecx;
5380 nano = mulquov(subsecx,
INT2FIX(1000000000),
INT2FIX(TIME_SCALE));
5381 divmodv(nano,
INT2FIX(1), &v, &subnano);
5386 nano = addv(
LONG2FIX(nsec), subnano);
5389 TZMODE_UTC_P(tobj) << 30 |
5394 s = (
unsigned long)
vtm.min << 26 |
5398 for (i=0; i<4; i++) {
5399 buf[i] = (
unsigned char)p;
5402 for (i=4; i<8; i++) {
5403 buf[i] = (
unsigned char)s;
5407 if (!
NIL_P(year_extend)) {
5415 char *p, *
const buf_year_extend = buf + base_dump_size;
5416 if (ysize > LONG_MAX ||
5417 (i = ruby_marshal_write_long((
long)ysize, buf_year_extend)) < 0) {
5419 (year == 1900 ?
"small" :
"big"),
vtm.year);
5421 i += base_dump_size;
5434 rb_ivar_set(str, id_nano_num, RRATIONAL(nano)->num);
5435 rb_ivar_set(str, id_nano_den, RRATIONAL(nano)->den);
5451 int len = (int)
sizeof(buf);
5452 buf[1] = (char)((nsec % 10) << 4);
5454 buf[0] = (char)(nsec % 10);
5456 buf[0] |= (char)((nsec % 10) << 4);
5461 if (!TZMODE_UTC_P(tobj)) {
5468 zone = tobj->vtm.zone;
5469 if (maybe_tzobj_p(zone)) {
5483 str = time_mdump(time);
5489 mload_findzone(
VALUE arg)
5492 VALUE time = argp[0], zone = argp[1];
5493 return find_timezone(time, zone);
5503 if (
NIL_P(z))
return rb_fstring(zone);
5508 long ruby_marshal_read_long(
const char **buf,
long len);
5522 VALUE submicro, nano_num, nano_den, offset, zone, year;
5527 #define get_attr(attr, iffound) \
5528 attr = rb_attr_delete(str, id_##attr); \
5529 if (!NIL_P(attr)) { \
5533 get_attr(nano_num, {});
5534 get_attr(nano_den, {});
5535 get_attr(submicro, {});
5536 get_attr(offset, (offset =
rb_rescue(validate_utc_offset, offset, 0,
Qnil)));
5537 get_attr(zone, (zone =
rb_rescue(validate_zone_name, zone, 0,
Qnil)));
5547 goto invalid_format;
5551 for (i=0; i<4; i++) {
5552 p |= (
unsigned long)buf[i]<<(8*i);
5554 for (i=4; i<8; i++) {
5555 s |= (
unsigned long)buf[i]<<(8*(i-4));
5558 if ((p & (1UL<<31)) == 0) {
5564 timew = wadd(rb_time_magnify(TIMET2WV(sec)), wmulquoll(WINT2FIXWV(usec), TIME_SCALE, 1000000));
5568 gmt = (int)((p >> 30) & 0x1);
5571 year =
INT2FIX(((
int)(p >> 14) & 0xffff) + 1900);
5577 const char *ybuf = (
const char *)(buf += base_dump_size);
5578 ysize = ruby_marshal_read_long(&ybuf,
len);
5579 len -= ybuf - (
const char *)buf;
5580 if (ysize < 0 || ysize >
len)
goto invalid_format;
5583 year = rb_int_minus(year, year_extend);
5586 year = rb_int_plus(year, year_extend);
5589 unsigned int mon = ((int)(p >> 10) & 0xf);
5596 vtm.mday = (int)(p >> 5) & 0x1f;
5597 vtm.hour = (int) p & 0x1f;
5598 vtm.min = (int)(s >> 26) & 0x3f;
5599 vtm.sec = (int)(s >> 20) & 0x3f;
5603 vtm.zone = str_empty;
5605 usec = (long)(s & 0xfffff);
5610 if (nano_num !=
Qnil) {
5611 VALUE nano = quov(num_exact(nano_num), num_exact(nano_den));
5614 else if (submicro !=
Qnil) {
5622 if (10 <= (digit =
ptr[0] >> 4))
goto end_submicro;
5623 nsec += digit * 100;
5624 if (10 <= (digit =
ptr[0] & 0xf))
goto end_submicro;
5628 if (10 <= (digit =
ptr[1] >> 4))
goto end_submicro;
5634 timew = timegmw(&
vtm);
5637 GetNewTimeval(time, tobj);
5638 TZMODE_SET_LOCALTIME(tobj);
5639 tobj->vtm.tm_got = 0;
5640 time_set_timew(time, tobj, timew);
5643 TZMODE_SET_UTC(tobj);
5645 else if (!
NIL_P(offset)) {
5646 time_set_utc_offset(time, offset);
5650 zone = mload_zone(time, zone);
5651 tobj->vtm.zone = zone;
5652 zone_localtime(zone, time);
5666 VALUE time = time_s_alloc(klass);
5668 time_mload(time, str);
5694 GetTimeval(time, tobj);
5695 tm = time_s_alloc(klass);
5696 ttm = RTYPEDDATA_GET_DATA(tm);
5698 GMTIMEW(ttm->timew = tobj->timew, v);
5699 ttm->timew = wsub(ttm->timew, v->subsecx);
5702 time_set_vtm(tm, ttm, *v);
5704 ttm->vtm.tm_got = 1;
5705 TZMODE_SET_UTC(ttm);
5718 tm_initialize(
int argc,
VALUE *argv,
VALUE time)
5724 time_arg(argc, argv, &
vtm);
5726 struct time_object *tobj = RTYPEDDATA_GET_DATA(time);
5727 TZMODE_SET_UTC(tobj);
5728 time_set_timew(time, tobj, t);
5729 time_set_vtm(time, tobj,
vtm);
5742 tm_to_time(
VALUE tm)
5746 struct time_object *tobj = RTYPEDDATA_GET_DATA(dup);
5754 return time_add0(
rb_obj_class(tm), get_timeval(tm), tm, offset, +1);
5760 return time_add0(
rb_obj_class(tm), get_timeval(tm), tm, offset, -1);
5764 Init_tm(
VALUE outer,
const char *name)
5810 rb_time_zone_abbreviation(
VALUE zone,
VALUE time)
5812 VALUE tm, abbr, strftime_args[2];
5815 if (!
NIL_P(abbr))
return abbr;
5817 tm = tm_from_time(rb_cTimeTM, time);
5819 if (!UNDEF_P(abbr)) {
5822 #ifdef SUPPORT_TZINFO_ZONE_ABBREVIATION
5824 if (!UNDEF_P(abbr)) {
5829 strftime_args[0] = rb_fstring_lit(
"%Z");
5830 strftime_args[1] = tm;
5832 if (!UNDEF_P(abbr)) {
5835 abbr = rb_check_funcall_default(zone, idName, 0, 0,
Qnil);
5877 str_utc = rb_fstring_lit(
"UTC");
5878 rb_vm_register_global_object(str_utc);
5879 str_empty = rb_fstring_lit(
"");
5880 rb_vm_register_global_object(str_empty);
5964 if (debug_find_time_numguess) {
5966 find_time_numguess_getter, 0);
5969 rb_cTimeTM = Init_tm(
rb_cTime,
"tm");
5972 #include "timev.rbinc"
#define RUBY_ASSERT(...)
Asserts that the given expression is truthy if and only if RUBY_DEBUG is truthy.
#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.
void rb_include_module(VALUE klass, VALUE module)
Includes a module to a class.
VALUE rb_define_class(const char *name, VALUE super)
Defines a top-level class.
VALUE rb_singleton_class(VALUE obj)
Finds or creates the singleton class of the passed object.
VALUE rb_define_class_under(VALUE outer, const char *name, VALUE super)
Defines a class under the namespace of outer.
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,...)
Retrieves argument from argc and argv to given VALUE references according to the format string.
void rb_define_method(VALUE klass, const char *name, VALUE(*func)(ANYARGS), int argc)
Defines a method.
#define TYPE(_)
Old name of rb_type.
#define RB_INTEGER_TYPE_P
Old name of rb_integer_type_p.
#define OBJ_INIT_COPY(obj, orig)
Old name of RB_OBJ_INIT_COPY.
#define ISSPACE
Old name of rb_isspace.
#define RFLOAT_VALUE
Old name of rb_float_value.
#define T_STRING
Old name of RUBY_T_STRING.
#define Qundef
Old name of RUBY_Qundef.
#define INT2FIX
Old name of RB_INT2FIX.
#define T_NIL
Old name of RUBY_T_NIL.
#define ID2SYM
Old name of RB_ID2SYM.
#define T_BIGNUM
Old name of RUBY_T_BIGNUM.
#define T_STRUCT
Old name of RUBY_T_STRUCT.
#define T_FIXNUM
Old name of RUBY_T_FIXNUM.
#define UNREACHABLE_RETURN
Old name of RBIMPL_UNREACHABLE_RETURN.
#define CLASS_OF
Old name of rb_class_of.
#define LONG2FIX
Old name of RB_INT2FIX.
#define FIX2INT
Old name of RB_FIX2INT.
#define ISDIGIT
Old name of rb_isdigit.
#define ASSUME
Old name of RBIMPL_ASSUME.
#define T_RATIONAL
Old name of RUBY_T_RATIONAL.
#define rb_ary_new3
Old name of rb_ary_new_from_args.
#define LONG2NUM
Old name of RB_LONG2NUM.
#define STRNCASECMP
Old name of st_locale_insensitive_strncasecmp.
#define ISASCII
Old name of rb_isascii.
#define ULL2NUM
Old name of RB_ULL2NUM.
#define FIXNUM_MIN
Old name of RUBY_FIXNUM_MIN.
#define NUM2INT
Old name of RB_NUM2INT.
#define INT2NUM
Old name of RB_INT2NUM.
#define Qnil
Old name of RUBY_Qnil.
#define Qfalse
Old name of RUBY_Qfalse.
#define FIX2LONG
Old name of RB_FIX2LONG.
#define T_ARRAY
Old name of RUBY_T_ARRAY.
#define NIL_P
Old name of RB_NIL_P.
#define DBL2NUM
Old name of rb_float_new.
#define NUM2LONG
Old name of RB_NUM2LONG.
#define FIXNUM_P
Old name of RB_FIXNUM_P.
#define CONST_ID
Old name of RUBY_CONST_ID.
#define NUM2SIZET
Old name of RB_NUM2SIZE.
void rb_raise(VALUE exc, const char *fmt,...)
Exception entry point.
void rb_exc_raise(VALUE mesg)
Raises an exception in the current thread.
int rb_typeddata_is_kind_of(VALUE obj, const rb_data_type_t *data_type)
Checks if the given object is of given kind.
void rb_sys_fail(const char *mesg)
Converts a C errno into a Ruby exception, then raises it.
VALUE rb_eRangeError
RangeError exception.
VALUE rb_eTypeError
TypeError exception.
VALUE rb_eRuntimeError
RuntimeError exception.
VALUE rb_exc_new_str(VALUE etype, VALUE str)
Identical to rb_exc_new_cstr(), except it takes a Ruby's string instead of C's.
VALUE rb_eArgError
ArgumentError exception.
VALUE rb_rescue(VALUE(*b_proc)(VALUE), VALUE data1, VALUE(*r_proc)(VALUE, VALUE), VALUE data2)
Identical to rb_rescue2(), except it does not take a list of exception classes.
void rb_warning(const char *fmt,...)
Issues a warning.
VALUE rb_cTime
Time class.
VALUE rb_Float(VALUE val)
This is the logic behind Kernel#Float.
VALUE rb_check_to_int(VALUE val)
Identical to rb_check_to_integer(), except it uses #to_int for conversion.
VALUE rb_Integer(VALUE val)
This is the logic behind Kernel#Integer.
VALUE rb_obj_class(VALUE obj)
Queries the class of an object.
VALUE rb_equal(VALUE lhs, VALUE rhs)
This function is an optimised version of calling #==.
VALUE rb_mComparable
Comparable module.
VALUE rb_to_int(VALUE val)
Identical to rb_check_to_int(), except it raises in case of conversion mismatch.
#define RB_OBJ_WRITTEN(old, oldv, young)
Identical to RB_OBJ_WRITE(), except it doesn't write any values, but only a WB declaration.
rb_encoding * rb_locale_encoding(void)
Queries the encoding that represents the current locale.
int rb_enc_precise_mbclen(const char *p, const char *e, rb_encoding *enc)
Queries the number of bytes of the character at the passed pointer.
rb_encoding * rb_usascii_encoding(void)
Queries the encoding that represents US-ASCII.
rb_encoding * rb_enc_get(VALUE obj)
Identical to rb_enc_get_index(), except the return type.
static bool rb_enc_str_asciicompat_p(VALUE str)
Queries if the passed string is in an ASCII-compatible encoding.
VALUE rb_enc_str_new(const char *ptr, long len, rb_encoding *enc)
Identical to rb_str_new(), except it additionally takes an encoding.
VALUE rb_funcall(VALUE recv, ID mid, int n,...)
Calls a method.
VALUE rb_funcallv(VALUE recv, ID mid, int argc, const VALUE *argv)
Identical to rb_funcall(), except it takes the method arguments as a C array.
void rb_gc_mark(VALUE obj)
Marks an object.
Defines RBIMPL_HAS_BUILTIN.
VALUE rb_check_array_type(VALUE obj)
Try converting an object to its array representation using its to_ary method, if any.
VALUE rb_ary_entry(VALUE ary, long off)
Queries an element of an array.
int rb_integer_pack(VALUE val, void *words, size_t numwords, size_t wordsize, size_t nails, int flags)
Exports an integer into a buffer.
VALUE rb_big_minus(VALUE x, VALUE y)
Performs subtraction of the passed two objects.
VALUE rb_big_modulo(VALUE x, VALUE y)
Performs modulo of the passed two objects.
#define INTEGER_PACK_NATIVE_BYTE_ORDER
Means either INTEGER_PACK_MSBYTE_FIRST or INTEGER_PACK_LSBYTE_FIRST, depending on the host processor'...
VALUE rb_big_plus(VALUE x, VALUE y)
Performs addition of the passed two objects.
VALUE rb_str_to_inum(VALUE str, int base, int badcheck)
Identical to rb_cstr2inum(), except it takes Ruby's strings instead of C's.
size_t rb_absint_size(VALUE val, int *nlz_bits_ret)
Calculates the number of bytes needed to represent the absolute value of the passed integer.
VALUE rb_integer_unpack(const void *words, size_t numwords, size_t wordsize, size_t nails, int flags)
Import an integer from a buffer.
#define INTEGER_PACK_LITTLE_ENDIAN
Little endian combination.
VALUE rb_big_cmp(VALUE lhs, VALUE rhs)
Compares the passed two bignums.
VALUE rb_big_mul(VALUE x, VALUE y)
Performs multiplication of the passed two objects.
VALUE rb_big_div(VALUE x, VALUE y)
Performs division of the passed two objects.
int rb_cmpint(VALUE val, VALUE a, VALUE b)
Canonicalises the passed val, which is the return value of a <=> b, into C's {-1, 0,...
static int rb_check_arity(int argc, int min, int max)
Ensures that the passed integer is in the passed range.
VALUE rb_hash_aset(VALUE hash, VALUE key, VALUE val)
Inserts or replaces ("upsert"s) the objects into the given hash table.
VALUE rb_hash(VALUE obj)
Calculates a message authentication code of the passed object.
void rb_num_zerodiv(void)
Just always raises an exception.
VALUE rb_int_positive_pow(long x, unsigned long y)
Raises the passed x to the power of y.
VALUE rb_rational_new(VALUE num, VALUE den)
Constructs a Rational, with reduction.
#define rb_Rational1(x)
Shorthand of (x/1)r.
VALUE rb_str_subseq(VALUE str, long beg, long len)
Identical to rb_str_substr(), except the numbers are interpreted as byte offsets instead of character...
VALUE rb_str_dup(VALUE str)
Duplicates a string.
VALUE rb_str_cat(VALUE dst, const char *src, long srclen)
Destructively appends the passed contents to the string.
VALUE rb_usascii_str_new(const char *ptr, long len)
Identical to rb_str_new(), except it generates a string of "US ASCII" encoding.
VALUE rb_usascii_str_new_cstr(const char *ptr)
Identical to rb_str_new_cstr(), except it generates a string of "US ASCII" encoding.
void rb_str_set_len(VALUE str, long len)
Overwrites the length of the string.
VALUE rb_str_concat(VALUE dst, VALUE src)
Identical to rb_str_append(), except it also accepts an integer as a codepoint.
#define rb_strlen_lit(str)
Length of a string literal.
VALUE rb_str_new(const char *ptr, long len)
Allocates an instance of rb_cString.
VALUE rb_check_string_type(VALUE obj)
Try converting an object to its stringised representation using its to_str method,...
VALUE rb_str_new_cstr(const char *ptr)
Identical to rb_str_new(), except it assumes the passed pointer is a pointer to a C string.
VALUE rb_str_resize(VALUE str, long len)
Overwrites the length of the string.
void rb_str_modify_expand(VALUE str, long capa)
Identical to rb_str_modify(), except it additionally expands the capacity of the receiver.
VALUE rb_str_cat_cstr(VALUE dst, const char *src)
Identical to rb_str_cat(), except it assumes the passed pointer is a pointer to a C string.
VALUE rb_obj_as_string(VALUE obj)
Try converting an object to its stringised representation using its to_s method, if any.
VALUE rb_time_nano_new(time_t sec, long nsec)
Identical to rb_time_new(), except it accepts the time in nanoseconds resolution.
void rb_timespec_now(struct timespec *ts)
Fills the current time into the given struct.
VALUE rb_time_timespec_new(const struct timespec *ts, int offset)
Creates an instance of rb_cTime, with given time and offset.
struct timespec rb_time_timespec(VALUE time)
Identical to rb_time_timeval(), except for return type.
VALUE rb_time_new(time_t sec, long usec)
Creates an instance of rb_cTime with the given time and the local timezone.
struct timeval rb_time_timeval(VALUE time)
Converts an instance of rb_cTime to a struct timeval that represents the identical point of time.
struct timeval rb_time_interval(VALUE num)
Creates a "time interval".
VALUE rb_time_num_new(VALUE timev, VALUE off)
Identical to rb_time_timespec_new(), except it takes Ruby values instead of C structs.
VALUE rb_time_utc_offset(VALUE time)
Queries the offset, in seconds between the time zone of the time and the UTC.
struct timespec rb_time_timespec_interval(VALUE num)
Identical to rb_time_interval(), except for return type.
VALUE rb_ivar_set(VALUE obj, ID name, VALUE val)
Identical to rb_iv_set(), except it accepts the name as an ID instead of a C string.
int rb_respond_to(VALUE obj, ID mid)
Queries if the object responds to the method.
void rb_alias(VALUE klass, ID dst, ID src)
Resembles alias.
VALUE rb_check_funcall(VALUE recv, ID mid, int argc, const VALUE *argv)
Identical to rb_funcallv(), except it returns RUBY_Qundef instead of raising rb_eNoMethodError.
void rb_define_alloc_func(VALUE klass, rb_alloc_func_t func)
Sets the allocator function of a class.
static ID rb_intern_const(const char *str)
This is a "tiny optimisation" over rb_intern().
ID rb_intern(const char *name)
Finds or creates a symbol of the given name.
void rb_define_hooked_variable(const char *name, VALUE *var, rb_gvar_getter_t *getter, rb_gvar_setter_t *setter)
Identical to rb_define_virtual_variable(), but can also specify a storage.
char * ptr
Pointer to the underlying memory region, of at least capa bytes.
int off
Offset inside of ptr.
int len
Length of the buffer.
#define DECIMAL_SIZE_OF(expr)
An approximation of decimal representation size.
VALUE rb_sprintf(const char *fmt,...)
Ruby's extended sprintf(3).
VALUE rb_str_catf(VALUE dst, const char *fmt,...)
Identical to rb_sprintf(), except it renders the output to the specified object rather than creating ...
#define rb_long2int
Just another name of rb_long2int_inline.
#define MEMCPY(p1, p2, type, n)
Handy macro to call memcpy.
#define RB_GC_GUARD(v)
Prevents premature destruction of local objects.
void rb_copy_generic_ivar(VALUE clone, VALUE obj)
Copies the list of instance variables.
#define RARRAY_LEN
Just another name of rb_array_len.
#define RARRAY_AREF(a, i)
#define StringValue(v)
Ensures that the parameter object is a String.
#define StringValuePtr(v)
Identical to StringValue, except it returns a char*.
static char * RSTRING_END(VALUE str)
Queries the end of the contents pointer of the string.
static char * RSTRING_PTR(VALUE str)
Queries the contents pointer of the string.
static long RSTRING_LEN(VALUE str)
Queries the length of the string.
#define StringValueCStr(v)
Identical to StringValuePtr, except it additionally checks for the contents for viability as a C stri...
#define RUBY_TYPED_DEFAULT_FREE
This is a value you can set to rb_data_type_struct::dfree.
#define TypedData_Get_Struct(obj, type, data_type, sval)
Obtains a C struct from inside of a wrapper Ruby object.
#define TypedData_Make_Struct(klass, type, data_type, sval)
Identical to TypedData_Wrap_Struct, except it allocates a new data region internally instead of takin...
#define RTEST
This is an old name of RB_TEST.
This is the struct that holds necessary info for a struct.
intptr_t SIGNED_VALUE
A signed integer type that has the same width with VALUE.
uintptr_t ID
Type that represents a Ruby identifier such as a variable name.
uintptr_t VALUE
Type that represents a Ruby object.
static bool RB_FLOAT_TYPE_P(VALUE obj)
Queries if the object is an instance of rb_cFloat.
static bool rb_integer_type_p(VALUE obj)
Queries if the object is an instance of rb_cInteger.
static bool RB_TYPE_P(VALUE obj, enum ruby_value_type t)
Queries if the given object is of given type.