12#define _DEFAULT_SOURCE
14#include "ruby/internal/config.h"
30#if defined(HAVE_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"
49static ID id_submicro, id_nano_num, id_nano_den, id_offset, id_zone;
50static ID id_nanosecond, id_microsecond, id_millisecond, id_nsec, id_usec;
51static ID id_local_to_utc, id_utc_to_local, id_find_timezone;
52static ID id_year, id_mon, id_mday, id_hour, id_min, id_sec, id_isdst;
53static VALUE str_utc, str_empty;
56static VALUE sym_year, sym_month, sym_day, sym_yday, sym_wday;
57static VALUE sym_hour, sym_min, sym_sec, sym_subsec, sym_dst, sym_zone;
61#define id_divmod idDivmod
63#define UTC_ZONE Qundef
69#define NDIV(x,y) (-(-((x)+1)/(y))-1)
70#define NMOD(x,y) ((y)-(-((x)+1)%(y))-1)
71#define DIV(n,d) ((n)<0 ? NDIV((n),(d)) : (n)/(d))
72#define MOD(n,d) ((n)<0 ? NMOD((n),(d)) : (n)%(d))
73#define VTM_WDAY_INITVAL (7)
74#define VTM_ISDST_INITVAL (3)
89 if ((
long)x < (
long)y)
91 if ((
long)x > (
long)y)
95 if (RB_BIGNUM_TYPE_P(x))
return FIX2INT(rb_big_cmp(x, y));
96 return rb_cmpint(
rb_funcall(x, idCmp, 1, y), x, y);
99#define ne(x,y) (!eq((x),(y)))
100#define lt(x,y) (cmp((x),(y)) < 0)
101#define gt(x,y) (cmp((x),(y)) > 0)
102#define le(x,y) (cmp((x),(y)) <= 0)
103#define ge(x,y) (cmp((x),(y)) >= 0)
111 if (RB_BIGNUM_TYPE_P(x))
return rb_big_plus(x, y);
121 if (RB_BIGNUM_TYPE_P(x))
return rb_big_minus(x, y);
129 return rb_fix_mul_fix(x, y);
131 if (RB_BIGNUM_TYPE_P(x))
132 return rb_big_mul(x, y);
140 return rb_fix_div_fix(x, y);
142 if (RB_BIGNUM_TYPE_P(x))
143 return rb_big_div(x, y);
152 if (
FIXNUM_P(x))
return rb_fix_mod_fix(x, y);
154 if (RB_BIGNUM_TYPE_P(x))
return rb_big_modulo(x, y);
158#define neg(x) (subv(INT2FIX(0), (x)))
174 return rb_numeric_quo(x, y);
180 VALUE ret = quor(x, y);
182 RRATIONAL(ret)->den ==
INT2FIX(1)) {
183 ret = RRATIONAL(ret)->num;
188#define mulquov(x,y,z) (((y) == (z)) ? (x) : quov(mulv((x),(y)),(z)))
197 rb_fix_divmod_fix(n, d, q, r);
202 ary = rb_check_array_type(tmp);
207 *q = rb_ary_entry(ary, 0);
208 *r = rb_ary_entry(ary, 1);
212# define INT64toNUM(x) LONG2NUM(x)
213#elif defined(HAVE_LONG_LONG) && SIZEOF_LONG_LONG == 8
214# define INT64toNUM(x) LL2NUM(x)
217#if defined(HAVE_UINT64_T) && SIZEOF_LONG*2 <= SIZEOF_UINT64_T
218 typedef uint64_t uwideint_t;
219 typedef int64_t wideint_t;
220 typedef uint64_t WIDEVALUE;
221 typedef int64_t SIGNED_WIDEVALUE;
222# define WIDEVALUE_IS_WIDER 1
223# define UWIDEINT_MAX UINT64_MAX
224# define WIDEINT_MAX INT64_MAX
225# define WIDEINT_MIN INT64_MIN
226# define FIXWINT_P(tv) ((tv) & 1)
227# define FIXWVtoINT64(tv) RSHIFT((SIGNED_WIDEVALUE)(tv), 1)
228# define INT64toFIXWV(wi) ((WIDEVALUE)((SIGNED_WIDEVALUE)(wi) << 1 | FIXNUM_FLAG))
229# define FIXWV_MAX (((int64_t)1 << 62) - 1)
230# define FIXWV_MIN (-((int64_t)1 << 62))
231# define FIXWVABLE(wi) (POSFIXWVABLE(wi) && NEGFIXWVABLE(wi))
232# define WINT2FIXWV(i) WIDEVAL_WRAP(INT64toFIXWV(i))
233# define FIXWV2WINT(w) FIXWVtoINT64(WIDEVAL_GET(w))
235 typedef unsigned long uwideint_t;
236 typedef long wideint_t;
237 typedef VALUE WIDEVALUE;
239# define WIDEVALUE_IS_WIDER 0
240# define UWIDEINT_MAX ULONG_MAX
241# define WIDEINT_MAX LONG_MAX
242# define WIDEINT_MIN LONG_MIN
243# define FIXWINT_P(v) FIXNUM_P(v)
244# define FIXWV_MAX FIXNUM_MAX
245# define FIXWV_MIN FIXNUM_MIN
246# define FIXWVABLE(i) FIXABLE(i)
247# define WINT2FIXWV(i) WIDEVAL_WRAP(LONG2FIX(i))
248# define FIXWV2WINT(w) FIX2LONG(WIDEVAL_GET(w))
251#define POSFIXWVABLE(wi) ((wi) < FIXWV_MAX+1)
252#define NEGFIXWVABLE(wi) ((wi) >= FIXWV_MIN)
253#define FIXWV_P(w) FIXWINT_P(WIDEVAL_GET(w))
254#define MUL_OVERFLOW_FIXWV_P(a, b) MUL_OVERFLOW_SIGNED_INTEGER_P(a, b, FIXWV_MIN, FIXWV_MAX)
262 static inline wideval_t WIDEVAL_WRAP(WIDEVALUE v) { wideval_t w = { v };
return w; }
263# define WIDEVAL_GET(w) ((w).value)
265 typedef WIDEVALUE wideval_t;
266# define WIDEVAL_WRAP(v) (v)
267# define WIDEVAL_GET(w) (w)
270#if WIDEVALUE_IS_WIDER
271 static inline wideval_t
272 wint2wv(wideint_t wi)
275 return WINT2FIXWV(wi);
277 return WIDEVAL_WRAP(INT64toNUM(wi));
279# define WINT2WV(wi) wint2wv(wi)
281# define WINT2WV(wi) WIDEVAL_WRAP(LONG2NUM(wi))
287#if WIDEVALUE_IS_WIDER
289 return INT64toNUM(FIXWV2WINT(w));
290 return (
VALUE)WIDEVAL_GET(w);
292 return WIDEVAL_GET(w);
296#if WIDEVALUE_IS_WIDER
302 sign = rb_integer_pack(v, &u, 1,
sizeof(u), 0,
305 return WINT2FIXWV(0);
306 else if (sign == -1) {
308 return WINT2FIXWV(-(wideint_t)u);
310 else if (sign == +1) {
312 return WINT2FIXWV((wideint_t)u);
314 return WIDEVAL_WRAP(v);
318static inline wideval_t
322 if (RRATIONAL(v)->den !=
LONG2FIX(1))
323 return WIDEVAL_WRAP(v);
324 v = RRATIONAL(v)->num;
326#if WIDEVALUE_IS_WIDER
328 return WIDEVAL_WRAP((WIDEVALUE)(SIGNED_WIDEVALUE)(
long)v);
330 else if (RB_BIGNUM_TYPE_P(v) &&
331 rb_absint_size(v, NULL) <=
sizeof(WIDEVALUE)) {
332 return v2w_bignum(v);
335 return WIDEVAL_WRAP(v);
339weq(wideval_t wx, wideval_t wy)
341#if WIDEVALUE_IS_WIDER
342 if (FIXWV_P(wx) && FIXWV_P(wy)) {
343 return WIDEVAL_GET(wx) == WIDEVAL_GET(wy);
347 return eq(WIDEVAL_GET(wx), WIDEVAL_GET(wy));
352wcmp(wideval_t wx, wideval_t wy)
355#if WIDEVALUE_IS_WIDER
356 if (FIXWV_P(wx) && FIXWV_P(wy)) {
372#define wne(x,y) (!weq((x),(y)))
373#define wlt(x,y) (wcmp((x),(y)) < 0)
374#define wgt(x,y) (wcmp((x),(y)) > 0)
375#define wle(x,y) (wcmp((x),(y)) <= 0)
376#define wge(x,y) (wcmp((x),(y)) >= 0)
379wadd(wideval_t wx, wideval_t wy)
381#if WIDEVALUE_IS_WIDER
382 if (FIXWV_P(wx) && FIXWV_P(wy)) {
383 wideint_t r = FIXWV2WINT(wx) + FIXWV2WINT(wy);
387 return v2w(addv(w2v(wx), w2v(wy)));
391wsub(wideval_t wx, wideval_t wy)
393#if WIDEVALUE_IS_WIDER
394 if (FIXWV_P(wx) && FIXWV_P(wy)) {
395 wideint_t r = FIXWV2WINT(wx) - FIXWV2WINT(wy);
399 return v2w(subv(w2v(wx), w2v(wy)));
403wmul(wideval_t wx, wideval_t wy)
405#if WIDEVALUE_IS_WIDER
406 if (FIXWV_P(wx) && FIXWV_P(wy)) {
407 if (!MUL_OVERFLOW_FIXWV_P(FIXWV2WINT(wx), FIXWV2WINT(wy)))
408 return WINT2WV(FIXWV2WINT(wx) * FIXWV2WINT(wy));
411 return v2w(mulv(w2v(wx), w2v(wy)));
415wquo(wideval_t wx, wideval_t wy)
417#if WIDEVALUE_IS_WIDER
418 if (FIXWV_P(wx) && FIXWV_P(wy)) {
429 return v2w(quov(w2v(wx), w2v(wy)));
432#define wmulquo(x,y,z) ((WIDEVAL_GET(y) == WIDEVAL_GET(z)) ? (x) : wquo(wmul((x),(y)),(z)))
433#define wmulquoll(x,y,z) (((y) == (z)) ? (x) : wquo(wmul((x),WINT2WV(y)),WINT2WV(z)))
435#if WIDEVALUE_IS_WIDER
437wdivmod0(wideval_t wn, wideval_t wd, wideval_t *wq, wideval_t *wr)
439 if (FIXWV_P(wn) && FIXWV_P(wd)) {
440 wideint_t n, d, q, r;
449 wideint_t xneg = -FIXWV2WINT(wn);
462 if (d > 0 ? r < 0 : r > 0) {
475wdivmod(wideval_t wn, wideval_t wd, wideval_t *wq, wideval_t *wr)
478#if WIDEVALUE_IS_WIDER
479 if (wdivmod0(wn, wd, wq, wr))
return;
481 divmodv(w2v(wn), w2v(wd), &vq, &vr);
487wmuldivmod(wideval_t wx, wideval_t wy, wideval_t wz, wideval_t *wq, wideval_t *wr)
489 if (WIDEVAL_GET(wy) == WIDEVAL_GET(wz)) {
494 wdivmod(wmul(wx,wy), wz, wq, wr);
498wdiv(wideval_t wx, wideval_t wy)
500#if WIDEVALUE_IS_WIDER
502 if (wdivmod0(wx, wy, &q, &dmy))
return q;
504 return v2w(divv(w2v(wx), w2v(wy)));
508wmod(wideval_t wx, wideval_t wy)
510#if WIDEVALUE_IS_WIDER
512 if (wdivmod0(wx, wy, &dmy, &r))
return r;
514 return v2w(modv(w2v(wx), w2v(wy)));
518num_exact_check(
VALUE v)
529 tmp = rb_rational_canonicalize(v);
543 tmp = rb_rational_canonicalize(tmp);
559NORETURN(
static void num_exact_fail(
VALUE v));
561num_exact_fail(
VALUE v)
570 VALUE num = num_exact_check(v);
571 if (
NIL_P(num)) num_exact_fail(v);
578rb_time_magnify(wideval_t w)
580 return wmul(w, WINT2FIXWV(TIME_SCALE));
584rb_time_unmagnify_to_rational(wideval_t w)
586 return quor(w2v(w),
INT2FIX(TIME_SCALE));
590rb_time_unmagnify(wideval_t w)
592 return v2w(rb_time_unmagnify_to_rational(w));
596rb_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));
620split_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)
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)
673static VALUE rb_cTimeTM;
675static int obj2int(
VALUE obj);
676static uint32_t obj2ubits(
VALUE obj,
unsigned int bits);
678static uint32_t month_arg(
VALUE arg);
679static VALUE validate_utc_offset(
VALUE utc_offset);
680static VALUE validate_zone_name(
VALUE zone_name);
681static void validate_vtm(
struct vtm *
vtm);
682static void vtm_add_day(
struct vtm *
vtm,
int day);
683static uint32_t obj2subsecx(
VALUE obj,
VALUE *subsecx);
690static time_t timegm_noleapsecond(
struct tm *tm);
691static int tmcmp(
struct tm *a,
struct tm *b);
692static int vtmcmp(
struct vtm *a,
struct vtm *b);
693static const char *find_time_t(
struct tm *tptr,
int utc_p, time_t *tp);
695static struct vtm *localtimew(wideval_t timew,
struct vtm *result);
697static int leap_year_p(
long y);
698#define leap_year_v_p(y) leap_year_p(NUM2LONG(modv((y), INT2FIX(400))))
702bool ruby_tz_uptodate_p;
705ruby_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;
720rb_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
755rb_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))
773static 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
788static 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
804static const int8_t common_year_days_in_month[] = {
805 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
807static 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)
832static 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)
836static 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
863static const uint8_t common_year_mday_of_yday[] = {
865 D31, D28, D31, D30, D31, D30, D31, D31, D30, D31, D30, D31
867static const uint8_t leap_year_mday_of_yday[] = {
868 D31, D29, D31, D30, D31, D30, D31, D31, D30, D31, D30, D31
877calc_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];
891timegmw_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));
932zone_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);
959gmtimew_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;
1061gmtime_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);
1181static long this_year = 0;
1182static time_t known_leap_seconds_limit;
1183static int number_of_leap_seconds_known;
1186init_leap_second_info(
void)
1193 if (this_year == 0) {
1195 struct tm *tm, result;
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))));
1233ruby_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));
1281gmtimew(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;
1326 result->zone = rb_fstring_lit(
"UTC");
1332#define GMTIMEW(w, v) \
1333 (gmtimew(w, v) ? (void)0 : rb_raise(rb_eArgError, "gmtime error"))
1335static struct tm *localtime_with_gmtoff_zone(
const time_t *t,
struct tm *result,
long *gmtoff,
VALUE *zone);
1370static const int compat_common_month_table[12][7] = {
1372 { 2034, 2035, 2036, 2031, 2032, 2027, 2033 },
1373 { 2026, 2027, 2033, 2034, 2035, 2030, 2031 },
1374 { 2026, 2032, 2033, 2034, 2035, 2030, 2036 },
1375 { 2035, 2030, 2036, 2026, 2032, 2033, 2034 },
1376 { 2033, 2034, 2035, 2030, 2036, 2026, 2032 },
1377 { 2036, 2026, 2032, 2033, 2034, 2035, 2030 },
1378 { 2035, 2030, 2036, 2026, 2032, 2033, 2034 },
1379 { 2032, 2033, 2034, 2035, 2030, 2036, 2026 },
1380 { 2030, 2036, 2026, 2032, 2033, 2034, 2035 },
1381 { 2034, 2035, 2030, 2036, 2026, 2032, 2033 },
1382 { 2026, 2032, 2033, 2034, 2035, 2030, 2036 },
1383 { 2030, 2036, 2026, 2032, 2033, 2034, 2035 },
1411static const int compat_leap_month_table[7] = {
1413 2032, 2016, 2028, 2012, 2024, 2036, 2020,
1417calc_wday(
int year_mod400,
int month,
int day)
1422 a = (14 - month) / 12;
1423 y = year_mod400 + 4800 - a;
1424 m = month + 12 * a - 3;
1425 wday = day + (153*m+2)/5 + 365*y + y/4 - y/100 + y/400 + 2;
1431guess_local_offset(
struct vtm *vtm_utc,
int *isdst_ret,
VALUE *zone_ret)
1439 int year_mod400, wday;
1443 if (lt(vtm_utc->year,
INT2FIX(1916))) {
1446 zone = rb_fstring_lit(
"UTC");
1448# if defined(NEGATIVE_TIME_T)
1449# if SIZEOF_TIME_T <= 4
1451# define THE_TIME_OLD_ENOUGH ((time_t)0x80000000)
1455# define THE_TIME_OLD_ENOUGH ((time_t)(1600-1970)*366*24*60*60)
1457 if (localtime_with_gmtoff_zone((t = THE_TIME_OLD_ENOUGH, &t), &tm, &gmtoff, &zone)) {
1459 isdst = tm.tm_isdst;
1464 if (localtime_with_gmtoff_zone((t = 0, &t), &tm, &gmtoff, &zone)) {
1466 isdst = tm.tm_isdst;
1482 wday = calc_wday(year_mod400, vtm_utc->mon, 1);
1483 if (vtm_utc->mon == 2 && leap_year_p(year_mod400))
1484 vtm2.year =
INT2FIX(compat_leap_month_table[wday]);
1486 vtm2.year =
INT2FIX(compat_common_month_table[vtm_utc->mon-1][wday]);
1488 timev = w2v(rb_time_unmagnify(timegmw(&vtm2)));
1489 t = NUM2TIMET(timev);
1491 if (localtime_with_gmtoff_zone(&t, &tm, &gmtoff, &zone)) {
1493 *isdst_ret = tm.tm_isdst;
1501 static time_t now = 0;
1502 static long now_gmtoff = 0;
1503 static int now_isdst = 0;
1504 static VALUE now_zone;
1508 localtime_with_gmtoff_zone(&now, &tm, &now_gmtoff, &zone);
1509 now_isdst = tm.tm_isdst;
1510 zone = rb_fstring(zone);
1511 rb_gc_register_mark_object(zone);
1515 *isdst_ret = now_isdst;
1517 *zone_ret = now_zone;
1523small_vtm_sub(
struct vtm *vtm1,
struct vtm *vtm2)
1527 off = vtm1->sec - vtm2->sec;
1528 off += (vtm1->min - vtm2->min) * 60;
1529 off += (vtm1->hour - vtm2->hour) * 3600;
1530 if (ne(vtm1->year, vtm2->year))
1531 off += lt(vtm1->year, vtm2->year) ? -24*3600 : 24*3600;
1532 else if (vtm1->mon != vtm2->mon)
1533 off += vtm1->mon < vtm2->mon ? -24*3600 : 24*3600;
1534 else if (vtm1->mday != vtm2->mday)
1535 off += vtm1->mday < vtm2->mday ? -24*3600 : 24*3600;
1541timelocalw(
struct vtm *
vtm)
1546 wideval_t timew1, timew2;
1547 struct vtm vtm1, vtm2;
1552 if (l < INT_MIN || INT_MAX < l)
1554 tm.tm_year = (int)l;
1563 tm.tm_mon =
vtm->mon-1;
1564 tm.tm_mday =
vtm->mday;
1565 tm.tm_hour =
vtm->hour;
1566 tm.tm_min =
vtm->min;
1567 tm.tm_sec =
vtm->sec;
1568 tm.tm_isdst =
vtm->isdst == VTM_ISDST_INITVAL ? -1 :
vtm->isdst;
1570 if (find_time_t(&tm, 0, &t))
1572 return wadd(rb_time_magnify(TIMET2WV(t)), v2w(
vtm->subsecx));
1575 timew1 = timegmw(
vtm);
1577 if (!localtimew(timew1, &vtm1))
1580 n = vtmcmp(
vtm, &vtm1);
1582 timew1 = wsub(timew1, rb_time_magnify(WINT2FIXWV(12*3600)));
1583 if (!localtimew(timew1, &vtm1))
1591 timew1 = wsub(timew1, rb_time_magnify(WINT2FIXWV(24*3600)));
1592 if (!localtimew(timew1, &vtm1))
1596 timew2 = wadd(timew1, rb_time_magnify(WINT2FIXWV(24*3600)));
1597 if (!localtimew(timew2, &vtm2))
1600 timew1 = wadd(timew1, rb_time_magnify(v2w(small_vtm_sub(
vtm, &vtm1))));
1601 timew2 = wadd(timew2, rb_time_magnify(v2w(small_vtm_sub(
vtm, &vtm2))));
1603 if (weq(timew1, timew2))
1606 if (!localtimew(timew1, &vtm1))
1608 if (
vtm->hour != vtm1.hour ||
vtm->min != vtm1.min ||
vtm->sec != vtm1.sec)
1611 if (!localtimew(timew2, &vtm2))
1613 if (
vtm->hour != vtm2.hour ||
vtm->min != vtm2.min ||
vtm->sec != vtm2.sec)
1617 return lt(vtm1.utc_offset, vtm2.utc_offset) ? timew2 : timew1;
1619 return lt(vtm1.utc_offset, vtm2.utc_offset) ? timew1 : timew2;
1623localtime_with_gmtoff_zone(
const time_t *t,
struct tm *result,
long *gmtoff,
VALUE *zone)
1627 if (LOCALTIME(t, tm)) {
1628#if defined(HAVE_STRUCT_TM_TM_GMTOFF)
1629 *gmtoff = tm.tm_gmtoff;
1635 u = GMTIME(t, tmbuf);
1638 if (l->tm_year != u->tm_year)
1639 off = l->tm_year < u->tm_year ? -1 : 1;
1640 else if (l->tm_mon != u->tm_mon)
1641 off = l->tm_mon < u->tm_mon ? -1 : 1;
1642 else if (l->tm_mday != u->tm_mday)
1643 off = l->tm_mday < u->tm_mday ? -1 : 1;
1646 off = off * 24 + l->tm_hour - u->tm_hour;
1647 off = off * 60 + l->tm_min - u->tm_min;
1648 off = off * 60 + l->tm_sec - u->tm_sec;
1653#if defined(HAVE_TM_ZONE)
1654 *zone = zone_str(tm.tm_zone);
1655#elif defined(HAVE_TZNAME) && defined(HAVE_DAYLIGHT)
1656# if defined(RUBY_MSVCRT_VERSION) && RUBY_MSVCRT_VERSION >= 140
1657# define tzname _tzname
1658# define daylight _daylight
1661 *zone = zone_str(tzname[daylight && tm.tm_isdst]);
1665 strftime(buf,
sizeof(buf),
"%Z", &tm);
1666 *zone = zone_str(buf);
1678timew_out_of_timet_range(wideval_t timew)
1681#if WIDEVALUE_IS_WIDER && SIZEOF_TIME_T < SIZEOF_INT64_T
1682 if (FIXWV_P(timew)) {
1683 wideint_t t = FIXWV2WINT(timew);
1684 if (t < TIME_SCALE * (wideint_t)TIMET_MIN ||
1685 TIME_SCALE * (1 + (wideint_t)TIMET_MAX) <= t)
1690#if SIZEOF_TIME_T == SIZEOF_INT64_T
1691 if (FIXWV_P(timew)) {
1692 wideint_t t = FIXWV2WINT(timew);
1693 if (~(time_t)0 <= 0) {
1703 timexv = w2v(timew);
1704 if (lt(timexv, mulv(
INT2FIX(TIME_SCALE), TIMET2NUM(TIMET_MIN))) ||
1705 le(mulv(
INT2FIX(TIME_SCALE), addv(TIMET2NUM(TIMET_MAX),
INT2FIX(1))), timexv))
1711localtimew(wideval_t timew,
struct vtm *result)
1713 VALUE subsecx, offset;
1717 if (!timew_out_of_timet_range(timew)) {
1723 split_second(timew, &timew2, &subsecx);
1725 t = WV2TIMET(timew2);
1727 if (localtime_with_gmtoff_zone(&t, &tm, &gmtoff, &zone)) {
1728 result->year =
LONG2NUM((
long)tm.tm_year + 1900);
1729 result->mon = tm.tm_mon + 1;
1730 result->mday = tm.tm_mday;
1731 result->hour = tm.tm_hour;
1732 result->min = tm.tm_min;
1733 result->sec = tm.tm_sec;
1734 result->subsecx = subsecx;
1735 result->wday = tm.tm_wday;
1736 result->yday = tm.tm_yday+1;
1737 result->isdst = tm.tm_isdst;
1738 result->utc_offset =
LONG2NUM(gmtoff);
1739 result->zone = zone;
1744 if (!gmtimew(timew, result))
1747 offset = guess_local_offset(result, &isdst, &zone);
1749 if (!gmtimew(wadd(timew, rb_time_magnify(v2w(offset))), result))
1752 result->utc_offset = offset;
1753 result->isdst = isdst;
1754 result->zone = zone;
1759#define TIME_TZMODE_LOCALTIME 0
1760#define TIME_TZMODE_UTC 1
1761#define TIME_TZMODE_FIXOFF 2
1762#define TIME_TZMODE_UNINITIALIZED 3
1769#define GetTimeval(obj, tobj) ((tobj) = get_timeval(obj))
1770#define GetNewTimeval(obj, tobj) ((tobj) = get_new_timeval(obj))
1772#define IsTimeval(obj) rb_typeddata_is_kind_of((obj), &time_data_type)
1773#define TIME_INIT_P(tobj) ((tobj)->vtm.tzmode != TIME_TZMODE_UNINITIALIZED)
1775#define TZMODE_UTC_P(tobj) ((tobj)->vtm.tzmode == TIME_TZMODE_UTC)
1776#define TZMODE_SET_UTC(tobj) ((tobj)->vtm.tzmode = TIME_TZMODE_UTC)
1778#define TZMODE_LOCALTIME_P(tobj) ((tobj)->vtm.tzmode == TIME_TZMODE_LOCALTIME)
1779#define TZMODE_SET_LOCALTIME(tobj) ((tobj)->vtm.tzmode = TIME_TZMODE_LOCALTIME)
1781#define TZMODE_FIXOFF_P(tobj) ((tobj)->vtm.tzmode == TIME_TZMODE_FIXOFF)
1782#define TZMODE_SET_FIXOFF(tobj, off) \
1783 ((tobj)->vtm.tzmode = TIME_TZMODE_FIXOFF, \
1784 (tobj)->vtm.utc_offset = (off))
1786#define TZMODE_COPY(tobj1, tobj2) \
1787 ((tobj1)->vtm.tzmode = (tobj2)->vtm.tzmode, \
1788 (tobj1)->vtm.utc_offset = (tobj2)->vtm.utc_offset, \
1789 (tobj1)->vtm.zone = (tobj2)->vtm.zone)
1791static int zone_localtime(
VALUE zone,
VALUE time);
1793#define MAKE_TM(time, tobj) \
1795 if ((tobj)->vtm.tm_got == 0) { \
1796 time_get_tm((time), (tobj)); \
1799#define MAKE_TM_ENSURE(time, tobj, cond) \
1801 MAKE_TM(time, tobj); \
1803 force_make_tm(time, tobj); \
1810 VALUE zone = tobj->vtm.zone;
1811 if (!
NIL_P(zone) && zone != str_empty && zone != str_utc) {
1812 if (zone_localtime(zone, time))
return;
1814 tobj->vtm.tm_got = 0;
1815 time_get_tm(time, tobj);
1822 if (!FIXWV_P(tobj->timew))
1823 rb_gc_mark(w2v(tobj->timew));
1824 rb_gc_mark(tobj->vtm.year);
1825 rb_gc_mark(tobj->vtm.subsecx);
1826 rb_gc_mark(tobj->vtm.utc_offset);
1827 rb_gc_mark(tobj->vtm.zone);
1831time_memsize(
const void *tobj)
1840 (RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_FROZEN_SHAREABLE),
1844time_s_alloc(
VALUE klass)
1850 tobj->vtm.tzmode = TIME_TZMODE_UNINITIALIZED;
1851 tobj->vtm.tm_got = 0;
1852 tobj->timew = WINT2FIXWV(0);
1853 tobj->vtm.zone =
Qnil;
1859get_timeval(
VALUE obj)
1863 if (!TIME_INIT_P(tobj)) {
1870get_new_timeval(
VALUE obj)
1874 if (TIME_INIT_P(tobj)) {
1881time_modify(
VALUE time)
1887timenano2timew(time_t sec,
long nsec)
1891 timew = rb_time_magnify(TIMET2WV(sec));
1893 timew = wadd(timew, wmulquoll(WINT2WV(nsec), TIME_SCALE, 1000000000));
1898timew2timespec(wideval_t timew)
1904 if (timew_out_of_timet_range(timew))
1906 split_second(timew, &timew2, &subsecx);
1907 ts.tv_sec = WV2TIMET(timew2);
1913timew2timespec_exact(wideval_t timew,
struct timespec *ts)
1919 if (timew_out_of_timet_range(timew))
1921 split_second(timew, &timew2, &subsecx);
1922 ts->tv_sec = WV2TIMET(timew2);
1923 nsecv = mulquov(subsecx,
INT2FIX(1000000000),
INT2FIX(TIME_SCALE));
1933#ifdef HAVE_CLOCK_GETTIME
1934 if (clock_gettime(CLOCK_REALTIME, ts) == -1) {
1940 if (gettimeofday(&tv, 0) < 0) {
1943 ts->tv_sec = tv.tv_sec;
1944 ts->tv_nsec = tv.tv_usec * 1000;
1956 GetNewTimeval(time, tobj);
1957 TZMODE_SET_LOCALTIME(tobj);
1959 tobj->timew = WINT2FIXWV(0);
1961 tobj->timew = timenano2timew(ts.tv_sec, ts.tv_nsec);
1964 time_zonelocal(time, zone);
1972 VALUE t = time_s_alloc(klass);
1973 return time_init_now(ec, t, zone);
1980 off = num_exact(off);
1983 GetTimeval(time, tobj);
1985 tobj->vtm.tm_got = 0;
1986 tobj->vtm.zone =
Qnil;
1987 TZMODE_SET_FIXOFF(tobj, off);
1993vtm_add_offset(
struct vtm *
vtm,
VALUE off,
int sign)
2003 divmodv(off,
INT2FIX(1), &off, &subsec);
2004 divmodv(off,
INT2FIX(60), &off, &v);
2006 divmodv(off,
INT2FIX(60), &off, &v);
2008 divmodv(off,
INT2FIX(24), &off, &v);
2012 subsec = neg(subsec);
2021 vtm->subsecx = addv(
vtm->subsecx, w2v(rb_time_magnify(v2w(subsec))));
2070 vtm_add_day(
vtm, day);
2074vtm_add_day(
struct vtm *
vtm,
int day)
2078 if (
vtm->mon == 1 &&
vtm->mday == 1) {
2083 vtm->yday = leap_year_v_p(
vtm->year) ? 366 : 365;
2085 else if (
vtm->mday == 1) {
2086 const int8_t *days_in_month = days_in_month_in_v(
vtm->year);
2088 vtm->mday = days_in_month[
vtm->mon-1];
2089 if (
vtm->yday != 0)
vtm->yday--;
2093 if (
vtm->yday != 0)
vtm->yday--;
2095 if (
vtm->wday != VTM_WDAY_INITVAL)
vtm->wday = (
vtm->wday + 6) % 7;
2098 int leap = leap_year_v_p(
vtm->year);
2099 if (
vtm->mon == 12 &&
vtm->mday == 31) {
2105 else if (
vtm->mday == days_in_month_of(leap)[
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 + 1) % 7;
2120maybe_tzobj_p(
VALUE obj)
2122 if (
NIL_P(obj))
return FALSE;
2128NORETURN(
static void invalid_utc_offset(
VALUE));
2130invalid_utc_offset(
VALUE zone)
2133 "\"A\"..\"I\",\"K\"..\"Z\" expected for utc_offset: %"PRIsVALUE,
2138utc_offset_arg(
VALUE arg)
2143 const char *s =
RSTRING_PTR(tmp), *min = NULL, *sec = NULL;
2145 goto invalid_utc_offset;
2153 if (s[0] >=
'A' && s[0] <=
'I') {
2154 n = (int)s[0] -
'A' + 1;
2157 else if (s[0] >=
'K' && s[0] <=
'M') {
2158 n = (int)s[0] -
'A';
2160 else if (s[0] >=
'N' && s[0] <=
'Y') {
2161 n =
'M' - (int)s[0];
2164 goto invalid_utc_offset;
2180 if (s[6] !=
':')
goto invalid_utc_offset;
2184 if (s[3] !=
':')
goto invalid_utc_offset;
2188 goto invalid_utc_offset;
2191 if (!
ISDIGIT(sec[0]) || !
ISDIGIT(sec[1]))
goto invalid_utc_offset;
2192 n += (sec[0] * 10 + sec[1] -
'0' * 11);
2196 if (!
ISDIGIT(min[0]) || !
ISDIGIT(min[1]))
goto invalid_utc_offset;
2197 if (min[0] >
'5')
goto invalid_utc_offset;
2198 n += (min[0] * 10 + min[1] -
'0' * 11) * 60;
2200 if (s[0] !=
'+' && s[0] !=
'-')
goto invalid_utc_offset;
2202 n += (s[1] * 10 + s[2] -
'0' * 11) * 3600;
2204 if (n == 0)
return UTC_ZONE;
2210 return num_exact(arg);
2218 wideval_t tlocal, wideval_t tutc)
2221 wideval_t w = wsub(tlocal, tutc);
2223 validate_utc_offset(off);
2224 tobj->vtm.utc_offset = off;
2225 tobj->vtm.zone = zone;
2226 TZMODE_SET_LOCALTIME(tobj);
2230extract_time(
VALUE time)
2233 const ID id_to_i = idTo_i;
2235#define EXTRACT_TIME() do { \
2236 t = v2w(rb_Integer(AREF(to_i))); \
2243 t = rb_time_unmagnify(tobj->timew);
2246#define AREF(x) rb_struct_aref(time, ID2SYM(id_##x))
2251#define AREF(x) rb_funcallv(time, id_##x, 0, 0)
2264 const ID id_to_i = idTo_i;
2266#define EXTRACT_VTM() do { \
2268 vtm->year = obj2vint(AREF(year)); \
2269 vtm->mon = month_arg(AREF(mon)); \
2270 vtm->mday = obj2ubits(AREF(mday), 5); \
2271 vtm->hour = obj2ubits(AREF(hour), 5); \
2272 vtm->min = obj2ubits(AREF(min), 6); \
2273 vtm->sec = obj2subsecx(AREF(sec), &subsecx); \
2274 vtm->isdst = RTEST(AREF(isdst)); \
2275 vtm->utc_offset = Qnil; \
2276 t = v2w(rb_Integer(AREF(to_i))); \
2282 time_get_tm(time, tobj);
2284 t = rb_time_unmagnify(tobj->timew);
2285 if (TZMODE_FIXOFF_P(tobj) &&
vtm->utc_offset !=
INT2FIX(0))
2286 t = wadd(t, v2w(
vtm->utc_offset));
2289#define AREF(x) rb_struct_aref(time, ID2SYM(id_##x))
2295 GMTIMEW(rb_time_magnify(t),
vtm);
2298#define AREF(x) rb_funcallv(time, id_##x, 0, 0)
2303 vtm->subsecx = subsecx;
2315 tobj->vtm.isdst = (!UNDEF_P(dst) &&
RTEST(dst));
2325 split_second(tobj->timew, &t, &s);
2326 tm = tm_from_time(rb_cTimeTM, time);
2328 if (UNDEF_P(utc))
return 0;
2330 s = extract_time(utc);
2331 zone_set_offset(zone, tobj, t, s);
2332 s = rb_time_magnify(s);
2333 if (tobj->vtm.subsecx !=
INT2FIX(0)) {
2334 s = wadd(s, v2w(tobj->vtm.subsecx));
2337 zone_set_dst(zone, tobj, tm);
2344 VALUE local, tm, subsecx;
2348 split_second(tobj->timew, &t, &subsecx);
2349 tm = tm_from_time(rb_cTimeTM, time);
2352 if (UNDEF_P(local))
return 0;
2354 s = extract_vtm(local, &tobj->vtm, subsecx);
2355 tobj->vtm.tm_got = 1;
2356 zone_set_offset(zone, tobj, s, t);
2357 zone_set_dst(zone, tobj, tm);
2366 return rb_check_funcall_default(klass, id_find_timezone, 1, &zone,
Qnil);
2372vtm_day_wraparound(
struct vtm *
vtm)
2374 if (
vtm->hour < 24)
return;
2379 vtm_add_day(
vtm, 1);
2390 vtm.wday = VTM_WDAY_INITVAL;
2392 vtm.zone = str_empty;
2394 vtm.year = obj2vint(year);
2396 vtm.mon =
NIL_P(mon) ? 1 : month_arg(mon);
2398 vtm.mday =
NIL_P(mday) ? 1 : obj2ubits(mday, 5);
2400 vtm.hour =
NIL_P(hour) ? 0 : obj2ubits(hour, 5);
2402 vtm.min =
NIL_P(min) ? 0 : obj2ubits(min, 6);
2410 vtm.sec = obj2subsecx(sec, &subsecx);
2411 vtm.subsecx = subsecx;
2414 return time_init_vtm(time,
vtm, zone);
2423 vtm.isdst = VTM_ISDST_INITVAL;
2425 const VALUE arg = zone;
2432 else if (maybe_tzobj_p(arg))
2434 else if (!
NIL_P(utc = utc_offset_arg(arg)))
2435 vtm.utc_offset = utc == UTC_ZONE ?
INT2FIX(0) : utc;
2436 else if (
NIL_P(zone = find_timezone(time, arg)))
2437 invalid_utc_offset(arg);
2443 GetNewTimeval(time, tobj);
2446 tobj->timew = timegmw(&
vtm);
2447 vtm_day_wraparound(&
vtm);
2449 tobj->vtm.tm_got = 1;
2450 TZMODE_SET_LOCALTIME(tobj);
2451 if (zone_timelocal(zone, time)) {
2454 else if (
NIL_P(
vtm.utc_offset = utc_offset_arg(zone))) {
2455 if (
NIL_P(zone = find_timezone(time, zone)) || !zone_timelocal(zone, time))
2456 invalid_utc_offset(arg);
2460 if (utc == UTC_ZONE) {
2461 tobj->timew = timegmw(&
vtm);
2463 vtm_day_wraparound(&
vtm);
2465 tobj->vtm.tm_got = 1;
2466 TZMODE_SET_UTC(tobj);
2470 TZMODE_SET_LOCALTIME(tobj);
2472 tobj->timew = WINT2FIXWV(0);
2476 vtm_add_offset(&
vtm, off, -1);
2478 tobj->timew = timegmw(&
vtm);
2479 return time_set_utc_offset(time, off);
2482 tobj->timew = timelocalw(&
vtm);
2483 return time_localtime(time);
2488two_digits(
const char *ptr,
const char *end,
const char **endp,
const char *name)
2490 ssize_t len = end - ptr;
2492 ((len > 2) &&
ISDIGIT(ptr[2]))) {
2494 if (ptr[-1] ==
'-' || ptr[-1] ==
':') {
2497 rb_str_catf(mesg,
": %.*s", ((len > 10) ? 10 : (int)(end - ptr)) + 1, ptr - 1);
2501 return (ptr[0] -
'0') * 10 + (ptr[1] -
'0');
2505parse_int(
const char *ptr,
const char *end,
const char **endp,
size_t *ndigits,
bool sign)
2507 ssize_t len = (end - ptr);
2508 int flags = sign ? RB_INT_PARSE_SIGN : 0;
2509 return rb_int_parse_cstr(ptr, len, (
char **)endp, ndigits, 10, flags);
2522 const char *ptr = begin;
2524 int mon = -1, mday = -1, hour = -1, min = -1, sec = -1;
2526 size_t prec =
NIL_P(precision) ? SIZE_MAX :
NUM2SIZET(precision);
2531 year = parse_int(ptr, end, &ptr, &ndigits,
true);
2535 else if (ndigits < 4) {
2538 else if (ptr == end) {
2542#define peekable_p(n) ((ptrdiff_t)(n) < (end - ptr))
2543#define peek_n(c, n) (peekable_p(n) && ((unsigned char)ptr[n] == (c)))
2544#define peek(c) peek_n(c, 0)
2545#define peekc_n(n) (peekable_p(n) ? (int)(unsigned char)ptr[n] : -1)
2546#define peekc() peekc_n(0)
2547#define expect_two_digits(x, bits) \
2548 (((unsigned int)(x = two_digits(ptr + 1, end, &ptr, #x)) > (1U << bits) - 1) ? \
2549 rb_raise(rb_eArgError, #x" out of range") : (void)0)
2550 if (!peek(
'-'))
break;
2551 expect_two_digits(mon, 4);
2552 if (!peek(
'-'))
break;
2553 expect_two_digits(mday, 5);
2554 if (!peek(
' ') && !peek(
'T'))
break;
2555 const char *
const time_part = ptr + 1;
2556 if (!
ISDIGIT(peekc_n(1)))
break;
2557#define nofraction(x) \
2559 rb_raise(rb_eArgError, "fraction " #x " is not supported: %.*s", \
2560 (int)(ptr + 1 - time_part), time_part); \
2562#define need_colon(x) \
2564 rb_raise(rb_eArgError, "missing " #x " part: %.*s", \
2565 (int)(ptr + 1 - time_part), time_part); \
2567 expect_two_digits(hour, 5);
2570 expect_two_digits(min, 6);
2573 expect_two_digits(sec, 6);
2576 for (ndigits = 0; ndigits < prec &&
ISDIGIT(peekc_n(ndigits)); ++ndigits);
2578 int clen = rb_enc_precise_mbclen(ptr, end, rb_enc_get(str));
2579 if (clen < 0) clen = 0;
2581 (
int)(ptr - time_part) + clen, time_part);
2583 subsec = parse_int(ptr, ptr + ndigits, &ptr, &ndigits,
false);
2584 if (
NIL_P(subsec))
break;
2585 while (ptr < end &&
ISDIGIT(*ptr)) ptr++;
2588 while (ptr < end &&
ISSPACE(*ptr)) ptr++;
2589 const char *
const zstr = ptr;
2590 while (ptr < end && !
ISSPACE(*ptr)) ptr++;
2591 const char *
const zend = ptr;
2592 while (ptr < end &&
ISSPACE(*ptr)) ptr++;
2601 else if (hour == -1) {
2604 if (!
NIL_P(subsec)) {
2606 static const size_t TIME_SCALE_NUMDIGITS =
2610 if (ndigits < TIME_SCALE_NUMDIGITS) {
2612 subsec = rb_int_mul(subsec, mul);
2614 else if (ndigits > TIME_SCALE_NUMDIGITS) {
2624 .wday = VTM_WDAY_INITVAL,
2628 .mon = (mon < 0) ? 1 : mon,
2629 .mday = (mday < 0) ? 1 : mday,
2630 .hour = (hour < 0) ? 0 : hour,
2631 .min = (min < 0) ? 0 : min,
2632 .sec = (sec < 0) ? 0 : sec,
2635 return time_init_vtm(klass,
vtm, zone);
2639subsec_normalize(time_t *secp,
long *subsecp,
const long maxsubsec)
2642 long subsec = *subsecp;
2645 if (UNLIKELY(subsec >= maxsubsec)) {
2646 sec2 = subsec / maxsubsec;
2647 if (TIMET_MAX - sec2 < sec) {
2650 subsec -= sec2 * maxsubsec;
2653 else if (UNLIKELY(subsec < 0)) {
2654 sec2 = NDIV(subsec, maxsubsec);
2655 if (sec < TIMET_MIN - sec2) {
2658 subsec -= sec2 * maxsubsec;
2661#ifndef NEGATIVE_TIME_T
2669#define time_usec_normalize(secp, usecp) subsec_normalize(secp, usecp, 1000000)
2670#define time_nsec_normalize(secp, nsecp) subsec_normalize(secp, nsecp, 1000000000)
2673nsec2timew(time_t sec,
long nsec)
2675 time_nsec_normalize(&sec, &nsec);
2676 return timenano2timew(sec, nsec);
2680time_new_timew(
VALUE klass, wideval_t timew)
2682 VALUE time = time_s_alloc(klass);
2686 TZMODE_SET_LOCALTIME(tobj);
2687 tobj->timew = timew;
2695 time_usec_normalize(&sec, &usec);
2696 return time_new_timew(
rb_cTime, timenano2timew(sec, usec * 1000));
2703 return time_new_timew(
rb_cTime, nsec2timew(sec, nsec));
2710 VALUE time = time_new_timew(
rb_cTime, nsec2timew(ts->tv_sec, ts->tv_nsec));
2712 if (-86400 < offset && offset < 86400) {
2713 GetTimeval(time, tobj);
2714 TZMODE_SET_FIXOFF(tobj,
INT2FIX(offset));
2716 else if (offset == INT_MAX) {
2718 else if (offset == INT_MAX-1) {
2719 GetTimeval(time, tobj);
2720 TZMODE_SET_UTC(tobj);
2732 VALUE time = time_new_timew(
rb_cTime, rb_time_magnify(v2w(timev)));
2737 if (maybe_tzobj_p(zone)) {
2739 if (zone_timelocal(zone, time))
return time;
2741 if (
NIL_P(off = utc_offset_arg(off))) {
2743 if (
NIL_P(zone = find_timezone(time, off))) invalid_utc_offset(off);
2745 if (!zone_timelocal(zone, time)) invalid_utc_offset(off);
2748 else if (off == UTC_ZONE) {
2749 return time_gmtime(time);
2752 validate_utc_offset(off);
2753 time_set_utc_offset(time, off);
2761time_timespec(
VALUE num, int interval)
2764 const char *
const tstr = interval ?
"time interval" :
"time";
2767#ifndef NEGATIVE_TIME_T
2768# define arg_range_check(v) \
2770 rb_raise(rb_eArgError, "%s must not be negative", tstr) : \
2773# define arg_range_check(v) \
2774 ((interval && (v) < 0) ? \
2775 rb_raise(rb_eArgError, "time interval must not be negative") : \
2780 t.tv_sec = NUM2TIMET(num);
2781 arg_range_check(t.tv_sec);
2792 t.tv_nsec = (int)(d*1e9+0.5);
2793 if (t.tv_nsec >= 1000000000) {
2794 t.tv_nsec -= 1000000000;
2798 else if ((t.tv_nsec = (
int)(-d*1e9+0.5)) > 0) {
2799 t.tv_nsec = 1000000000 - t.tv_nsec;
2802 t.tv_sec = (time_t)f;
2803 if (f != t.tv_sec) {
2808 else if (RB_BIGNUM_TYPE_P(num)) {
2809 t.tv_sec = NUM2TIMET(num);
2810 arg_range_check(t.tv_sec);
2816 if (!UNDEF_P(ary) && !
NIL_P(ary = rb_check_array_type(ary))) {
2817 i = rb_ary_entry(ary, 0);
2818 f = rb_ary_entry(ary, 1);
2819 t.tv_sec = NUM2TIMET(i);
2820 arg_range_check(t.tv_sec);
2830#undef arg_range_check
2834time_timeval(
VALUE num, int interval)
2839 ts = time_timespec(num, interval);
2840 tv.tv_sec = (TYPEOF_TIMEVAL_TV_SEC)ts.tv_sec;
2841 tv.tv_usec = (TYPEOF_TIMEVAL_TV_USEC)(ts.tv_nsec / 1000);
2849 return time_timeval(num, TRUE);
2859 if (IsTimeval(time)) {
2860 GetTimeval(time, tobj);
2861 ts = timew2timespec(tobj->timew);
2862 t.tv_sec = (TYPEOF_TIMEVAL_TV_SEC)ts.tv_sec;
2863 t.tv_usec = (TYPEOF_TIMEVAL_TV_USEC)(ts.tv_nsec / 1000);
2866 return time_timeval(time, FALSE);
2875 if (IsTimeval(time)) {
2876 GetTimeval(time, tobj);
2877 t = timew2timespec(tobj->timew);
2880 return time_timespec(time, FALSE);
2886 return time_timespec(num, TRUE);
2890get_scale(
VALUE unit)
2892 if (unit ==
ID2SYM(id_nanosecond) || unit ==
ID2SYM(id_nsec)) {
2895 else if (unit ==
ID2SYM(id_microsecond) || unit ==
ID2SYM(id_usec)) {
2898 else if (unit ==
ID2SYM(id_millisecond)) {
2913 int scale = get_scale(unit);
2914 time = num_exact(time);
2915 t = num_exact(subsec);
2916 timew = wadd(rb_time_magnify(v2w(time)), wmulquoll(v2w(t), TIME_SCALE, scale));
2917 t = time_new_timew(klass, timew);
2919 else if (IsTimeval(time)) {
2921 GetTimeval(time, tobj);
2922 t = time_new_timew(klass, tobj->timew);
2923 GetTimeval(t, tobj2);
2924 TZMODE_COPY(tobj2, tobj);
2927 timew = rb_time_magnify(v2w(num_exact(time)));
2928 t = time_new_timew(klass, timew);
2931 time_zonelocal(t, zone);
2943static const char months[][4] = {
2944 "jan",
"feb",
"mar",
"apr",
"may",
"jun",
2945 "jul",
"aug",
"sep",
"oct",
"nov",
"dec",
2952 obj = rb_str_to_inum(obj, 10, TRUE);
2960obj2ubits(
VALUE obj,
unsigned int bits)
2962 const unsigned int usable_mask = (1U << bits) - 1;
2963 unsigned int rv = (
unsigned int)obj2int(obj);
2965 if ((rv & usable_mask) != rv)
2967 return (uint32_t)rv;
2974 obj = rb_str_to_inum(obj, 10, TRUE);
2989 obj = rb_str_to_inum(obj, 10, TRUE);
2993 divmodv(num_exact(obj),
INT2FIX(1), &obj, &subsec);
2994 *subsecx = w2v(rb_time_magnify(v2w(subsec)));
2996 return obj2ubits(obj, 6);
3000usec2subsecx(
VALUE obj)
3003 obj = rb_str_to_inum(obj, 10, TRUE);
3006 return mulquov(num_exact(obj),
INT2FIX(TIME_SCALE),
INT2FIX(1000000));
3015 return obj2ubits(arg, 4);
3022 for (i=0; i<12; i++) {
3031 mon = obj2ubits(arg, 4);
3037validate_utc_offset(
VALUE utc_offset)
3039 if (le(utc_offset,
INT2FIX(-86400)) || ge(utc_offset,
INT2FIX(86400)))
3045validate_zone_name(
VALUE zone_name)
3052validate_vtm(
struct vtm *
vtm)
3054#define validate_vtm_range(mem, b, e) \
3055 ((vtm->mem < b || vtm->mem > e) ? \
3056 rb_raise(rb_eArgError, #mem" out of range") : (void)0)
3057 validate_vtm_range(mon, 1, 12);
3058 validate_vtm_range(mday, 1, 31);
3059 validate_vtm_range(hour, 0, 24);
3060 validate_vtm_range(min, 0, (
vtm->hour == 24 ? 0 : 59));
3061 validate_vtm_range(sec, 0, (
vtm->hour == 24 ? 0 : 60));
3064 if (!
NIL_P(
vtm->utc_offset)) validate_utc_offset(
vtm->utc_offset);
3065#undef validate_vtm_range
3069time_arg(
int argc,
const VALUE *argv,
struct vtm *
vtm)
3085 vtm->zone = str_empty;
3095 vtm->isdst =
RTEST(argv[8]) ? 1 : 0;
3098 rb_scan_args(argc, argv,
"17", &v[0],&v[1],&v[2],&v[3],&v[4],&v[5],&v[6],&v[7]);
3101 vtm->wday = VTM_WDAY_INITVAL;
3102 vtm->isdst = VTM_ISDST_INITVAL;
3105 vtm->year = obj2vint(v[0]);
3111 vtm->mon = month_arg(v[1]);
3118 vtm->mday = obj2ubits(v[2], 5);
3126 unsigned int mday2 = leap_year_v_p(
vtm->year) ? 29 : 28;
3127 if (
vtm->mday > mday2) {
3137 if (
vtm->mday == 31) {
3144 vtm->hour =
NIL_P(v[3])?0:obj2ubits(v[3], 5);
3146 vtm->min =
NIL_P(v[4])?0:obj2ubits(v[4], 6);
3148 if (!
NIL_P(v[6]) && argc == 7) {
3149 vtm->sec =
NIL_P(v[5])?0:obj2ubits(v[5],6);
3150 subsecx = usec2subsecx(v[6]);
3158 vtm->sec = obj2subsecx(v[5], &subsecx);
3161 vtm->subsecx = subsecx;
3173 unsigned long uy = (
unsigned long)(LIKELY(y >= 0) ? y : -y);
3175 if (LIKELY(uy % 4 != 0))
return 0;
3177 unsigned long century = uy / 100;
3178 if (LIKELY(uy != century * 100))
return 1;
3179 return century % 4 == 0;
3183timegm_noleapsecond(
struct tm *tm)
3185 long tm_year = tm->tm_year;
3186 int tm_yday = calc_tm_yday(tm->tm_year, tm->tm_mon, tm->tm_mday);
3194 return tm->tm_sec + tm->tm_min*60 + tm->tm_hour*3600 +
3198 DIV(tm_year-1,100) +
3199 DIV(tm_year+299,400))*86400;
3203#define DEBUG_FIND_TIME_NUMGUESS
3204#define DEBUG_GUESSRANGE
3207static const bool debug_guessrange =
3208#ifdef DEBUG_GUESSRANGE
3214#define DEBUG_REPORT_GUESSRANGE \
3215 (debug_guessrange ? debug_report_guessrange(guess_lo, guess_hi) : (void)0)
3218debug_report_guessrange(time_t guess_lo, time_t guess_hi)
3220 time_t guess_diff = guess_hi - guess_lo;
3221 fprintf(stderr,
"find time guess range: %"PRI_TIMET_PREFIX
"d - "
3222 "%"PRI_TIMET_PREFIX
"d : %"PRI_TIMET_PREFIX
"u\n",
3223 guess_lo, guess_hi, guess_diff);
3226static const bool debug_find_time_numguess =
3227#ifdef DEBUG_FIND_TIME_NUMGUESS
3233#define DEBUG_FIND_TIME_NUMGUESS_INC \
3234 (void)(debug_find_time_numguess && find_time_numguess++),
3235static unsigned long long find_time_numguess;
3238find_time_numguess_getter(
ID name,
VALUE *data)
3240 unsigned long long *numguess = (
void *)data;
3245find_time_t(
struct tm *tptr,
int utc_p, time_t *tp)
3247 time_t guess, guess0, guess_lo, guess_hi;
3248 struct tm *tm, tm0, tm_lo, tm_hi;
3255#define GUESS(p) (DEBUG_FIND_TIME_NUMGUESS_INC (utc_p ? gmtime_with_leapsecond((p), &result) : LOCALTIME((p), result)))
3257 guess_lo = TIMET_MIN;
3258 guess_hi = TIMET_MAX;
3260 find_dst = 0 < tptr->tm_isdst;
3266 if (tm0.tm_mon < 0) {
3273 else if (11 < tm0.tm_mon) {
3280 else if (tm0.tm_mday < 1) {
3286 else if ((d = days_in_month_in(1900 + tm0.tm_year)[tm0.tm_mon]) < tm0.tm_mday) {
3292 else if (tm0.tm_hour < 0) {
3297 else if (23 < tm0.tm_hour) {
3302 else if (tm0.tm_min < 0) {
3306 else if (59 < tm0.tm_min) {
3310 else if (tm0.tm_sec < 0) {
3313 else if (60 < tm0.tm_sec) {
3317 DEBUG_REPORT_GUESSRANGE;
3318 guess0 = guess = timegm_noleapsecond(&tm0);
3321 d = tmcmp(tptr, tm);
3322 if (d == 0) {
goto found; }
3325 guess -= 24 * 60 * 60;
3329 guess += 24 * 60 * 60;
3331 DEBUG_REPORT_GUESSRANGE;
3332 if (guess_lo < guess && guess < guess_hi && (tm = GUESS(&guess)) != NULL) {
3333 d = tmcmp(tptr, tm);
3334 if (d == 0) {
goto found; }
3339 DEBUG_REPORT_GUESSRANGE;
3343 tm = GUESS(&guess_lo);
3344 if (!tm)
goto error;
3345 d = tmcmp(tptr, tm);
3346 if (d < 0)
goto out_of_range;
3347 if (d == 0) { guess = guess_lo;
goto found; }
3350 tm = GUESS(&guess_hi);
3351 if (!tm)
goto error;
3352 d = tmcmp(tptr, tm);
3353 if (d > 0)
goto out_of_range;
3354 if (d == 0) { guess = guess_hi;
goto found; }
3357 DEBUG_REPORT_GUESSRANGE;
3361 while (guess_lo + 1 < guess_hi) {
3364 guess = guess_lo / 2 + guess_hi / 2;
3365 if (guess <= guess_lo)
3366 guess = guess_lo + 1;
3367 else if (guess >= guess_hi)
3368 guess = guess_hi - 1;
3373 time_t guess0_hi = timegm_noleapsecond(&tm_hi);
3374 guess = guess_hi - (guess0_hi - guess0);
3375 if (guess == guess_hi)
3379 else if (status == 2) {
3380 time_t guess0_lo = timegm_noleapsecond(&tm_lo);
3381 guess = guess_lo + (guess0 - guess0_lo);
3382 if (guess == guess_lo)
3386 if (guess <= guess_lo || guess_hi <= guess) {
3388 if (debug_guessrange) {
3389 if (guess <= guess_lo) {
3390 fprintf(stderr,
"too small guess: %"PRI_TIMET_PREFIX
"d"\
3391 " <= %"PRI_TIMET_PREFIX
"d\n", guess, guess_lo);
3393 if (guess_hi <= guess) {
3394 fprintf(stderr,
"too big guess: %"PRI_TIMET_PREFIX
"d"\
3395 " <= %"PRI_TIMET_PREFIX
"d\n", guess_hi, guess);
3404 if (!tm)
goto error;
3406 d = tmcmp(tptr, tm);
3411 DEBUG_REPORT_GUESSRANGE;
3416 DEBUG_REPORT_GUESSRANGE;
3431 tptr_tm_yday = calc_tm_yday(tptr->tm_year, tptr->tm_mon, tptr->tm_mday);
3434 ((tptr->tm_year - tm_lo.tm_year) * 365 +
3435 DIV((tptr->tm_year-69), 4) -
3436 DIV((tptr->tm_year-1), 100) +
3437 DIV((tptr->tm_year+299), 400) -
3438 DIV((tm_lo.tm_year-69), 4) +
3439 DIV((tm_lo.tm_year-1), 100) -
3440 DIV((tm_lo.tm_year+299), 400) +
3442 tm_lo.tm_yday) * 86400 +
3443 (tptr->tm_hour - tm_lo.tm_hour) * 3600 +
3444 (tptr->tm_min - tm_lo.tm_min) * 60 +
3445 (tptr->tm_sec - (tm_lo.tm_sec == 60 ? 59 : tm_lo.tm_sec));
3454 guess2 = guess - 2 * 60 * 60;
3455 tm = LOCALTIME(&guess2, result);
3457 if (tptr->tm_hour != (tm->tm_hour + 2) % 24 ||
3458 tptr->tm_min != tm->tm_min ||
3459 tptr->tm_sec != tm->tm_sec) {
3460 guess2 -= (tm->tm_hour - tptr->tm_hour) * 60 * 60 +
3461 (tm->tm_min - tptr->tm_min) * 60 +
3462 (tm->tm_sec - tptr->tm_sec);
3463 if (tptr->tm_mday != tm->tm_mday)
3464 guess2 += 24 * 60 * 60;
3465 if (guess != guess2) {
3466 tm = LOCALTIME(&guess2, result);
3467 if (tm && tmcmp(tptr, tm) == 0) {
3479 guess2 = guess + 2 * 60 * 60;
3480 tm = LOCALTIME(&guess2, result);
3482 if ((tptr->tm_hour + 2) % 24 != tm->tm_hour ||
3483 tptr->tm_min != tm->tm_min ||
3484 tptr->tm_sec != tm->tm_sec) {
3485 guess2 -= (tm->tm_hour - tptr->tm_hour) * 60 * 60 +
3486 (tm->tm_min - tptr->tm_min) * 60 +
3487 (tm->tm_sec - tptr->tm_sec);
3488 if (tptr->tm_mday != tm->tm_mday)
3489 guess2 -= 24 * 60 * 60;
3490 if (guess != guess2) {
3491 tm = LOCALTIME(&guess2, result);
3492 if (tm && tmcmp(tptr, tm) == 0) {
3508 return "time out of range";
3511 return "gmtime/localtime error";
3515vtmcmp(
struct vtm *a,
struct vtm *b)
3517 if (ne(a->year, b->year))
3518 return lt(a->year, b->year) ? -1 : 1;
3519 else if (a->mon != b->mon)
3520 return a->mon < b->mon ? -1 : 1;
3521 else if (a->mday != b->mday)
3522 return a->mday < b->mday ? -1 : 1;
3523 else if (a->hour != b->hour)
3524 return a->hour < b->hour ? -1 : 1;
3525 else if (a->min != b->min)
3526 return a->min < b->min ? -1 : 1;
3527 else if (a->sec != b->sec)
3528 return a->sec < b->sec ? -1 : 1;
3529 else if (ne(a->subsecx, b->subsecx))
3530 return lt(a->subsecx, b->subsecx) ? -1 : 1;
3536tmcmp(
struct tm *a,
struct tm *b)
3538 if (a->tm_year != b->tm_year)
3539 return a->tm_year < b->tm_year ? -1 : 1;
3540 else if (a->tm_mon != b->tm_mon)
3541 return a->tm_mon < b->tm_mon ? -1 : 1;
3542 else if (a->tm_mday != b->tm_mday)
3543 return a->tm_mday < b->tm_mday ? -1 : 1;
3544 else if (a->tm_hour != b->tm_hour)
3545 return a->tm_hour < b->tm_hour ? -1 : 1;
3546 else if (a->tm_min != b->tm_min)
3547 return a->tm_min < b->tm_min ? -1 : 1;
3548 else if (a->tm_sec != b->tm_sec)
3549 return a->tm_sec < b->tm_sec ? -1 : 1;
3657time_s_mkutc(
int argc,
VALUE *argv,
VALUE klass)
3661 time_arg(argc, argv, &
vtm);
3662 return time_gmtime(time_new_timew(klass, timegmw(&
vtm)));
3683time_s_mktime(
int argc,
VALUE *argv,
VALUE klass)
3687 time_arg(argc, argv, &
vtm);
3688 return time_localtime(time_new_timew(klass, timelocalw(&
vtm)));
3710time_to_i(
VALUE time)
3714 GetTimeval(time, tobj);
3715 return w2v(wdiv(tobj->timew, WINT2FIXWV(TIME_SCALE)));
3739time_to_f(
VALUE time)
3743 GetTimeval(time, tobj);
3744 return rb_Float(rb_time_unmagnify_to_float(tobj->timew));
3760time_to_r(
VALUE time)
3765 GetTimeval(time, tobj);
3766 v = rb_time_unmagnify_to_rational(tobj->timew);
3790time_usec(
VALUE time)
3795 GetTimeval(time, tobj);
3797 w = wmod(tobj->timew, WINT2WV(TIME_SCALE));
3798 wmuldivmod(w, WINT2FIXWV(1000000), WINT2FIXWV(TIME_SCALE), &q, &r);
3819time_nsec(
VALUE time)
3823 GetTimeval(time, tobj);
3824 return rb_to_int(w2v(wmulquoll(wmod(tobj->timew, WINT2WV(TIME_SCALE)), 1000000000, TIME_SCALE)));
3845time_subsec(
VALUE time)
3849 GetTimeval(time, tobj);
3850 return quov(w2v(wmod(tobj->timew, WINT2FIXWV(TIME_SCALE))),
INT2FIX(TIME_SCALE));
3887 GetTimeval(time1, tobj1);
3888 if (IsTimeval(time2)) {
3889 GetTimeval(time2, tobj2);
3890 n = wcmp(tobj1->timew, tobj2->timew);
3893 return rb_invcmp(time1, time2);
3895 if (n == 0)
return INT2FIX(0);
3913 GetTimeval(time1, tobj1);
3914 if (IsTimeval(time2)) {
3915 GetTimeval(time2, tobj2);
3916 return rb_equal(w2v(tobj1->timew), w2v(tobj2->timew));
3940time_utc_p(
VALUE time)
3944 GetTimeval(time, tobj);
3945 return RBOOL(TZMODE_UTC_P(tobj));
3958time_hash(
VALUE time)
3962 GetTimeval(time, tobj);
3963 return rb_hash(w2v(tobj->timew));
3973 GetTimeval(time, tobj);
3974 GetNewTimeval(copy, tcopy);
3984 time_init_copy(dup, time);
3989time_localtime(
VALUE time)
3995 GetTimeval(time, tobj);
3996 if (TZMODE_LOCALTIME_P(tobj)) {
3997 if (tobj->vtm.tm_got)
4004 zone = tobj->vtm.zone;
4005 if (maybe_tzobj_p(zone) && zone_localtime(zone, time)) {
4009 if (!localtimew(tobj->timew, &
vtm))
4013 tobj->vtm.tm_got = 1;
4014 TZMODE_SET_LOCALTIME(tobj);
4022 if (zone_localtime(zone, time))
return time;
4024 if (
NIL_P(off = utc_offset_arg(off))) {
4026 if (
NIL_P(zone = find_timezone(time, off))) invalid_utc_offset(off);
4027 if (!zone_localtime(zone, time)) invalid_utc_offset(off);
4030 else if (off == UTC_ZONE) {
4031 return time_gmtime(time);
4033 validate_utc_offset(off);
4035 time_set_utc_offset(time, off);
4036 return time_fixoff(time);
4065time_localtime_m(
int argc,
VALUE *argv,
VALUE time)
4070 return time_zonelocal(time, off);
4073 return time_localtime(time);
4093time_gmtime(
VALUE time)
4098 GetTimeval(time, tobj);
4099 if (TZMODE_UTC_P(tobj)) {
4100 if (tobj->vtm.tm_got)
4108 GMTIMEW(tobj->timew, &
vtm);
4111 tobj->vtm.tm_got = 1;
4112 TZMODE_SET_UTC(tobj);
4117time_fixoff(
VALUE time)
4123 GetTimeval(time, tobj);
4124 if (TZMODE_FIXOFF_P(tobj)) {
4125 if (tobj->vtm.tm_got)
4132 if (TZMODE_FIXOFF_P(tobj))
4133 off = tobj->vtm.utc_offset;
4137 GMTIMEW(tobj->timew, &
vtm);
4139 zone = tobj->vtm.zone;
4141 tobj->vtm.zone = zone;
4142 vtm_add_offset(&tobj->vtm, off, +1);
4144 tobj->vtm.tm_got = 1;
4145 TZMODE_SET_FIXOFF(tobj, off);
4167time_getlocaltime(
int argc,
VALUE *argv,
VALUE time)
4173 if (maybe_tzobj_p(zone)) {
4174 VALUE t = time_dup(time);
4175 if (zone_localtime(off, t))
return t;
4178 if (
NIL_P(off = utc_offset_arg(off))) {
4180 if (
NIL_P(zone = find_timezone(time, off))) invalid_utc_offset(off);
4181 time = time_dup(time);
4182 if (!zone_localtime(zone, time)) invalid_utc_offset(off);
4185 else if (off == UTC_ZONE) {
4186 return time_gmtime(time_dup(time));
4188 validate_utc_offset(off);
4190 time = time_dup(time);
4191 time_set_utc_offset(time, off);
4192 return time_fixoff(time);
4195 return time_localtime(time_dup(time));
4215time_getgmtime(
VALUE time)
4217 return time_gmtime(time_dup(time));
4223 if (TZMODE_UTC_P(tobj))
return time_gmtime(time);
4224 if (TZMODE_FIXOFF_P(tobj))
return time_fixoff(time);
4225 return time_localtime(time);
4229#define strftimev(fmt, time, enc) strftime_cstr((fmt), rb_strlen_lit(fmt), (time), (enc))
4255time_asctime(
VALUE time)
4257 return strftimev(
"%a %b %e %T %Y", time, rb_usascii_encoding());
4277time_to_s(
VALUE time)
4281 GetTimeval(time, tobj);
4282 if (TZMODE_UTC_P(tobj))
4283 return strftimev(
"%Y-%m-%d %H:%M:%S UTC", time, rb_usascii_encoding());
4285 return strftimev(
"%Y-%m-%d %H:%M:%S %z", time, rb_usascii_encoding());
4305time_inspect(
VALUE time)
4310 GetTimeval(time, tobj);
4311 str = strftimev(
"%Y-%m-%d %H:%M:%S", time, rb_usascii_encoding());
4312 subsec = w2v(wmod(tobj->timew, WINT2FIXWV(TIME_SCALE)));
4324 subsec = quov(subsec,
INT2FIX(TIME_SCALE));
4327 if (TZMODE_UTC_P(tobj)) {
4333 char sign = (off < 0) ? (off = -off,
'-') :
'+';
4335 int min = (off /= 60) % 60;
4337 rb_str_catf(str,
" %c%.2d%.2d", sign, (
int)off, min);
4349 offset = num_exact(offset);
4351 result = time_new_timew(klass, wsub(tobj->timew, rb_time_magnify(v2w(offset))));
4353 result = time_new_timew(klass, wadd(tobj->timew, rb_time_magnify(v2w(offset))));
4354 GetTimeval(result, result_tobj);
4355 TZMODE_COPY(result_tobj, tobj);
4363 return time_add0(
rb_cTime, tobj, torig, offset, sign);
4384 GetTimeval(time1, tobj);
4386 if (IsTimeval(time2)) {
4389 return time_add(tobj, time1, time2, 1);
4419 GetTimeval(time1, tobj);
4420 if (IsTimeval(time2)) {
4423 GetTimeval(time2, tobj2);
4424 return rb_Float(rb_time_unmagnify_to_float(wsub(tobj->timew, tobj2->timew)));
4426 return time_add(tobj, time1, time2, -1);
4430ndigits_denominator(
VALUE ndigits)
4475 VALUE ndigits, v, den;
4481 den = ndigits_denominator(ndigits);
4483 GetTimeval(time, tobj);
4484 v = w2v(rb_time_unmagnify(tobj->timew));
4487 if (lt(v, quov(den,
INT2FIX(2))))
4488 return time_add(tobj, time, v, -1);
4490 return time_add(tobj, time, subv(den, v), 1);
4523 VALUE ndigits, v, den;
4529 den = ndigits_denominator(ndigits);
4531 GetTimeval(time, tobj);
4532 v = w2v(rb_time_unmagnify(tobj->timew));
4535 return time_add(tobj, time, v, -1);
4568 VALUE ndigits, v, den;
4574 den = ndigits_denominator(ndigits);
4576 GetTimeval(time, tobj);
4577 v = w2v(rb_time_unmagnify(tobj->timew));
4583 return time_add(tobj, time, v, 1);
4608 GetTimeval(time, tobj);
4609 MAKE_TM(time, tobj);
4610 return INT2FIX(tobj->vtm.sec);
4632 GetTimeval(time, tobj);
4633 MAKE_TM(time, tobj);
4634 return INT2FIX(tobj->vtm.min);
4652time_hour(
VALUE time)
4656 GetTimeval(time, tobj);
4657 MAKE_TM(time, tobj);
4658 return INT2FIX(tobj->vtm.hour);
4678time_mday(
VALUE time)
4682 GetTimeval(time, tobj);
4683 MAKE_TM(time, tobj);
4684 return INT2FIX(tobj->vtm.mday);
4708 GetTimeval(time, tobj);
4709 MAKE_TM(time, tobj);
4710 return INT2FIX(tobj->vtm.mon);
4727time_year(
VALUE time)
4731 GetTimeval(time, tobj);
4732 MAKE_TM(time, tobj);
4733 return tobj->vtm.year;
4752time_wday(
VALUE time)
4756 GetTimeval(time, tobj);
4757 MAKE_TM_ENSURE(time, tobj, tobj->vtm.wday != VTM_WDAY_INITVAL);
4758 return INT2FIX((
int)tobj->vtm.wday);
4762 return RBOOL(time_wday(time) == INT2FIX(n)); \
4778time_sunday(
VALUE time)
4796time_monday(
VALUE time)
4814time_tuesday(
VALUE time)
4832time_wednesday(
VALUE time)
4850time_thursday(
VALUE time)
4868time_friday(
VALUE time)
4886time_saturday(
VALUE time)
4902time_yday(
VALUE time)
4906 GetTimeval(time, tobj);
4907 MAKE_TM_ENSURE(time, tobj, tobj->vtm.yday != 0);
4908 return INT2FIX(tobj->vtm.yday);
4928time_isdst(
VALUE time)
4932 GetTimeval(time, tobj);
4933 MAKE_TM(time, tobj);
4934 if (tobj->vtm.isdst == VTM_ISDST_INITVAL) {
4937 return RBOOL(tobj->vtm.isdst);
4951time_zone(
VALUE time)
4956 GetTimeval(time, tobj);
4957 MAKE_TM(time, tobj);
4959 if (TZMODE_UTC_P(tobj)) {
4962 zone = tobj->vtm.zone;
4988 GetTimeval(time, tobj);
4990 if (TZMODE_UTC_P(tobj)) {
4994 MAKE_TM(time, tobj);
4995 return tobj->vtm.utc_offset;
5015time_to_a(
VALUE time)
5019 GetTimeval(time, tobj);
5020 MAKE_TM_ENSURE(time, tobj, tobj->vtm.yday != 0);
5030 RBOOL(tobj->vtm.isdst),
5076 GetTimeval(time, tobj);
5077 MAKE_TM_ENSURE(time, tobj, tobj->vtm.yday != 0);
5080 h = rb_hash_new_with_size(11);
5082 rb_hash_aset(h, sym_year, tobj->vtm.year);
5083 rb_hash_aset(h, sym_month,
INT2FIX(tobj->vtm.mon));
5084 rb_hash_aset(h, sym_day,
INT2FIX(tobj->vtm.mday));
5085 rb_hash_aset(h, sym_yday,
INT2FIX(tobj->vtm.yday));
5086 rb_hash_aset(h, sym_wday,
INT2FIX(tobj->vtm.wday));
5087 rb_hash_aset(h, sym_hour,
INT2FIX(tobj->vtm.hour));
5088 rb_hash_aset(h, sym_min,
INT2FIX(tobj->vtm.min));
5089 rb_hash_aset(h, sym_sec,
INT2FIX(tobj->vtm.sec));
5090 rb_hash_aset(h, sym_subsec,
5091 quov(w2v(wmod(tobj->timew, WINT2FIXWV(TIME_SCALE))),
INT2FIX(TIME_SCALE)));
5092 rb_hash_aset(h, sym_dst, RBOOL(tobj->vtm.isdst));
5093 rb_hash_aset(h, sym_zone, time_zone(time));
5099 "wrong argument type %"PRIsVALUE
" (expected Array or nil)",
5109 if (sym_year == key) rb_hash_aset(h, key, tobj->vtm.year);
5110 if (sym_month == key) rb_hash_aset(h, key,
INT2FIX(tobj->vtm.mon));
5111 if (sym_day == key) rb_hash_aset(h, key,
INT2FIX(tobj->vtm.mday));
5112 if (sym_yday == key) rb_hash_aset(h, key,
INT2FIX(tobj->vtm.yday));
5113 if (sym_wday == key) rb_hash_aset(h, key,
INT2FIX(tobj->vtm.wday));
5114 if (sym_hour == key) rb_hash_aset(h, key,
INT2FIX(tobj->vtm.hour));
5115 if (sym_min == key) rb_hash_aset(h, key,
INT2FIX(tobj->vtm.min));
5116 if (sym_sec == key) rb_hash_aset(h, key,
INT2FIX(tobj->vtm.sec));
5117 if (sym_subsec == key) {
5118 rb_hash_aset(h, key, quov(w2v(wmod(tobj->timew, WINT2FIXWV(TIME_SCALE))),
INT2FIX(TIME_SCALE)));
5120 if (sym_dst == key) rb_hash_aset(h, key, RBOOL(tobj->vtm.isdst));
5121 if (sym_zone == key) rb_hash_aset(h, key, time_zone(time));
5127rb_strftime_alloc(
const char *format,
size_t format_len,
rb_encoding *enc,
5128 VALUE time,
struct vtm *
vtm, wideval_t timew,
int gmt)
5133 if (!timew2timespec_exact(timew, &ts))
5134 timev = w2v(rb_time_unmagnify(timew));
5137 return rb_strftime_timespec(format, format_len, enc, time,
vtm, &ts, gmt);
5140 return rb_strftime(format, format_len, enc, time,
vtm, timev, gmt);
5150 GetTimeval(time, tobj);
5151 MAKE_TM(time, tobj);
5152 str = rb_strftime_alloc(fmt, len, enc, time, &tobj->vtm, tobj->timew, TZMODE_UTC_P(tobj));
5175 GetTimeval(time, tobj);
5176 MAKE_TM_ENSURE(time, tobj, tobj->vtm.yday != 0);
5181 tmp = rb_str_tmp_frozen_acquire(format);
5184 enc = rb_enc_get(format);
5186 rb_warning(
"strftime called with empty format string");
5190 VALUE str = rb_strftime_alloc(fmt, len, enc, time, &tobj->vtm, tobj->timew,
5191 TZMODE_UTC_P(tobj));
5192 rb_str_tmp_frozen_release(format, tmp);
5198int ruby_marshal_write_long(
long x,
char *buf);
5200enum {base_dump_size = 8};
5204time_mdump(
VALUE time)
5208 char buf[base_dump_size +
sizeof(long) + 1];
5215 VALUE subsecx, nano, subnano, v, zone;
5218 const int max_year = 1900+0xffff;
5220 GetTimeval(time, tobj);
5222 gmtimew(tobj->timew, &
vtm);
5226 if (year > max_year) {
5227 year_extend =
INT2FIX(year - max_year);
5230 else if (year < 1900) {
5231 year_extend =
LONG2NUM(1900 - year);
5236 if (rb_int_positive_p(
vtm.year)) {
5237 year_extend = rb_int_minus(
vtm.year,
INT2FIX(max_year));
5241 year_extend = rb_int_minus(
INT2FIX(1900),
vtm.year);
5246 subsecx =
vtm.subsecx;
5248 nano = mulquov(subsecx,
INT2FIX(1000000000),
INT2FIX(TIME_SCALE));
5249 divmodv(nano,
INT2FIX(1), &v, &subnano);
5254 nano = addv(
LONG2FIX(nsec), subnano);
5257 TZMODE_UTC_P(tobj) << 30 |
5262 s = (
unsigned long)
vtm.min << 26 |
5266 for (i=0; i<4; i++) {
5267 buf[i] = (
unsigned char)p;
5270 for (i=4; i<8; i++) {
5271 buf[i] = (
unsigned char)s;
5275 if (!
NIL_P(year_extend)) {
5282 size_t ysize = rb_absint_size(year_extend, NULL);
5283 char *p, *
const buf_year_extend = buf + base_dump_size;
5284 if (ysize > LONG_MAX ||
5285 (i = ruby_marshal_write_long((
long)ysize, buf_year_extend)) < 0) {
5287 (year == 1900 ?
"small" :
"big"),
vtm.year);
5289 i += base_dump_size;
5302 rb_ivar_set(str, id_nano_num, RRATIONAL(nano)->num);
5303 rb_ivar_set(str, id_nano_den, RRATIONAL(nano)->den);
5319 int len = (int)
sizeof(buf);
5320 buf[1] = (char)((nsec % 10) << 4);
5322 buf[0] = (char)(nsec % 10);
5324 buf[0] |= (char)((nsec % 10) << 4);
5329 if (!TZMODE_UTC_P(tobj)) {
5331 divmodv(off,
INT2FIX(1), &div, &mod);
5336 zone = tobj->vtm.zone;
5337 if (maybe_tzobj_p(zone)) {
5338 zone = rb_funcallv(zone, id_name, 0, 0);
5351 str = time_mdump(time);
5357mload_findzone(
VALUE arg)
5360 VALUE time = argp[0], zone = argp[1];
5361 return find_timezone(time, zone);
5371 if (
NIL_P(z))
return rb_fstring(zone);
5376long ruby_marshal_read_long(
const char **buf,
long len);
5390 VALUE submicro, nano_num, nano_den, offset, zone, year;
5395#define get_attr(attr, iffound) \
5396 attr = rb_attr_delete(str, id_##attr); \
5397 if (!NIL_P(attr)) { \
5401 get_attr(nano_num, {});
5402 get_attr(nano_den, {});
5403 get_attr(submicro, {});
5404 get_attr(offset, (offset =
rb_rescue(validate_utc_offset, offset, 0,
Qnil)));
5405 get_attr(zone, (zone =
rb_rescue(validate_zone_name, zone, 0,
Qnil)));
5415 goto invalid_format;
5419 for (i=0; i<4; i++) {
5420 p |= (
unsigned long)buf[i]<<(8*i);
5422 for (i=4; i<8; i++) {
5423 s |= (
unsigned long)buf[i]<<(8*(i-4));
5426 if ((p & (1UL<<31)) == 0) {
5432 timew = wadd(rb_time_magnify(TIMET2WV(sec)), wmulquoll(WINT2FIXWV(usec), TIME_SCALE, 1000000));
5436 gmt = (int)((p >> 30) & 0x1);
5439 year =
INT2FIX(((
int)(p >> 14) & 0xffff) + 1900);
5445 const char *ybuf = (
const char *)(buf += base_dump_size);
5446 ysize = ruby_marshal_read_long(&ybuf, len);
5447 len -= ybuf - (
const char *)buf;
5448 if (ysize < 0 || ysize > len)
goto invalid_format;
5451 year = rb_int_minus(year, year_extend);
5454 year = rb_int_plus(year, year_extend);
5457 unsigned int mon = ((int)(p >> 10) & 0xf);
5464 vtm.mday = (int)(p >> 5) & 0x1f;
5465 vtm.hour = (int) p & 0x1f;
5466 vtm.min = (int)(s >> 26) & 0x3f;
5467 vtm.sec = (int)(s >> 20) & 0x3f;
5471 vtm.zone = str_empty;
5473 usec = (long)(s & 0xfffff);
5478 if (nano_num !=
Qnil) {
5479 VALUE nano = quov(num_exact(nano_num), num_exact(nano_den));
5482 else if (submicro !=
Qnil) {
5490 if (10 <= (digit = ptr[0] >> 4))
goto end_submicro;
5491 nsec += digit * 100;
5492 if (10 <= (digit = ptr[0] & 0xf))
goto end_submicro;
5496 if (10 <= (digit = ptr[1] >> 4))
goto end_submicro;
5502 timew = timegmw(&
vtm);
5505 GetNewTimeval(time, tobj);
5506 TZMODE_SET_LOCALTIME(tobj);
5507 tobj->vtm.tm_got = 0;
5508 tobj->timew = timew;
5511 TZMODE_SET_UTC(tobj);
5513 else if (!
NIL_P(offset)) {
5514 time_set_utc_offset(time, offset);
5518 zone = mload_zone(time, zone);
5519 tobj->vtm.zone = zone;
5520 zone_localtime(zone, time);
5534 VALUE time = time_s_alloc(klass);
5536 time_mload(time, str);
5563 GetTimeval(time, tobj);
5564 tm = time_s_alloc(klass);
5567 GMTIMEW(ttm->timew = tobj->timew, v);
5568 ttm->timew = wsub(ttm->timew, v->subsecx);
5573 ttm->vtm.tm_got = 1;
5574 TZMODE_SET_UTC(ttm);
5580 GetTimeval(time, tobj);
5581 if (tobj->tm_got && TZMODE_UTC_P(tobj))
5584 GMTIMEW(tobj->timew, v = &
vtm);
5585 args[i++] = v->year;
5592 case 0: args[i++] =
Qfalse;
break;
5593 case 1: args[i++] =
Qtrue;
break;
5594 default: args[i++] =
Qnil;
break;
5596 args[i++] = w2v(rb_time_unmagnify(tobj->timew));
5610tm_initialize(
int argc,
VALUE *argv,
VALUE tm)
5616 time_arg(argc, argv, &
vtm);
5621 TZMODE_SET_UTC(tobj);
5631 RSTRUCT_SET(tm, i++,
vtm.year);
5632 RSTRUCT_SET(tm, i++, w2v(rb_time_unmagnify(t)));
5656 const VALUE *p = RSTRUCT_CONST_PTR(tm);
5659 for (i = 0; i < numberof(t); ++i) {
5660 t[i] = p[numberof(t) - 1 - i];
5662 return time_s_mkutc(numberof(t), t,
rb_cTime);
5673#define tm_subsec tm_zero
5674#define tm_utc_offset tm_zero
5685 const VALUE *p = RSTRUCT_CONST_PTR(tm);
5687 return rb_sprintf(
"%.4"PRIsVALUE
"-%.2"PRIsVALUE
"-%.2"PRIsVALUE
" "
5688 "%.2"PRIsVALUE
":%.2"PRIsVALUE
":%.2"PRIsVALUE
" "
5690 p[5], p[4], p[3], p[2], p[1], p[0]);
5696 return time_add0(
rb_obj_class(tm), get_timeval(tm), tm, offset, +1);
5702 return time_add0(
rb_obj_class(tm), get_timeval(tm), tm, offset, -1);
5707Init_tm(
VALUE outer,
const char *name)
5746 "sec",
"min",
"hour",
5747 "mday",
"mon",
"year",
5766rb_time_zone_abbreviation(
VALUE zone,
VALUE time)
5768 VALUE tm, abbr, strftime_args[2];
5771 if (!
NIL_P(abbr))
return abbr;
5773 tm = tm_from_time(rb_cTimeTM, time);
5775 if (!UNDEF_P(abbr)) {
5778#ifdef SUPPORT_TZINFO_ZONE_ABBREVIATION
5780 if (!UNDEF_P(abbr)) {
5781 abbr = rb_funcallv(abbr,
rb_intern(
"abbreviation"), 0, 0);
5785 strftime_args[0] = rb_fstring_lit(
"%Z");
5786 strftime_args[1] = tm;
5788 if (!UNDEF_P(abbr)) {
5791 abbr = rb_check_funcall_default(zone, idName, 0, 0,
Qnil);
5843 str_utc = rb_fstring_lit(
"UTC");
5844 rb_gc_register_mark_object(str_utc);
5845 str_empty = rb_fstring_lit(
"");
5846 rb_gc_register_mark_object(str_empty);
5933 if (debug_find_time_numguess) {
5935 find_time_numguess_getter, NULL);
5938 rb_cTimeTM = Init_tm(
rb_cTime,
"tm");
5941#include "timev.rbinc"
#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.
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.
#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 Qtrue
Old name of RUBY_Qtrue.
#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.
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_class_new_instance(int argc, const VALUE *argv, VALUE klass)
Allocates, then initialises an instance of the given class.
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.
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_enc_str_new(), except it additionally takes an encoding.
VALUE rb_funcall(VALUE recv, ID mid, int n,...)
Calls a method.
#define INTEGER_PACK_NATIVE_BYTE_ORDER
Means either INTEGER_PACK_MSBYTE_FIRST or INTEGER_PACK_LSBYTE_FIRST, depending on the host processor'...
#define INTEGER_PACK_LITTLE_ENDIAN
Little endian combination.
#define rb_check_frozen
Just another name of rb_check_frozen.
static int rb_check_arity(int argc, int min, int max)
Ensures that the passed integer is in the passed range.
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...
#define rb_str_new(str, len)
Allocates an instance of rb_cString.
#define rb_usascii_str_new(str, len)
Identical to rb_str_new, except it generates a string of "US ASCII" encoding.
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.
#define rb_usascii_str_new_cstr(str)
Identical to rb_str_new_cstr, except it generates a string of "US ASCII" encoding.
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_check_string_type(VALUE obj)
Try converting an object to its stringised representation using its to_str method,...
#define rb_str_cat_cstr(buf, str)
Identical to rb_str_cat(), 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.
#define rb_str_new_cstr(str)
Identical to rb_str_new, 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_struct_define_under(VALUE space, const char *name,...)
Identical to rb_struct_define(), except it defines the class under the specified namespace instead of...
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.
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_define_hooked_variable(const char *q, VALUE *w, type *e, void_type *r)
Define a function-backended global variable.
VALUE rb_rescue(type *q, VALUE w, type *e, VALUE r)
An equivalent of rescue clause.
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 DATA_PTR(obj)
Convenient getter macro.
#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 long RSTRING_LEN(VALUE str)
Queries the length of the string.
static char * RSTRING_PTR(VALUE str)
Queries the contents pointer 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.