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/error.h"
40#include "internal/numeric.h"
41#include "internal/rational.h"
42#include "internal/string.h"
43#include "internal/time.h"
44#include "internal/variable.h"
50# include <timezoneapi.h>
55static ID id_submicro, id_nano_num, id_nano_den, id_offset, id_zone;
56static ID id_nanosecond, id_microsecond, id_millisecond, id_nsec, id_usec;
57static ID id_local_to_utc, id_utc_to_local, id_find_timezone;
58static ID id_year, id_mon, id_mday, id_hour, id_min, id_sec, id_isdst;
59static VALUE str_utc, str_empty;
62static VALUE sym_year, sym_month, sym_day, sym_yday, sym_wday;
63static VALUE sym_hour, sym_min, sym_sec, sym_subsec, sym_dst, sym_zone;
67#define id_divmod idDivmod
69#define UTC_ZONE Qundef
71#define NDIV(x,y) (-(-((x)+1)/(y))-1)
72#define NMOD(x,y) ((y)-(-((x)+1)%(y))-1)
73#define DIV(n,d) ((n)<0 ? NDIV((n),(d)) : (n)/(d))
74#define MOD(n,d) ((n)<0 ? NMOD((n),(d)) : (n)%(d))
75#define VTM_WDAY_INITVAL (7)
76#define VTM_ISDST_INITVAL (3)
91 if ((
long)x < (
long)y)
93 if ((
long)x > (
long)y)
97 if (RB_BIGNUM_TYPE_P(x))
return FIX2INT(rb_big_cmp(x, y));
98 return rb_cmpint(
rb_funcall(x, idCmp, 1, y), x, y);
101#define ne(x,y) (!eq((x),(y)))
102#define lt(x,y) (cmp((x),(y)) < 0)
103#define gt(x,y) (cmp((x),(y)) > 0)
104#define le(x,y) (cmp((x),(y)) <= 0)
105#define ge(x,y) (cmp((x),(y)) >= 0)
113 if (RB_BIGNUM_TYPE_P(x))
return rb_big_plus(x, y);
123 if (RB_BIGNUM_TYPE_P(x))
return rb_big_minus(x, y);
131 return rb_fix_mul_fix(x, y);
133 if (RB_BIGNUM_TYPE_P(x))
134 return rb_big_mul(x, y);
142 return rb_fix_div_fix(x, y);
144 if (RB_BIGNUM_TYPE_P(x))
145 return rb_big_div(x, y);
154 if (
FIXNUM_P(x))
return rb_fix_mod_fix(x, y);
156 if (RB_BIGNUM_TYPE_P(x))
return rb_big_modulo(x, y);
160#define neg(x) (subv(INT2FIX(0), (x)))
176 return rb_numeric_quo(x, y);
182 VALUE ret = quor(x, y);
184 RRATIONAL(ret)->den ==
INT2FIX(1)) {
185 ret = RRATIONAL(ret)->num;
190#define mulquov(x,y,z) (((y) == (z)) ? (x) : quov(mulv((x),(y)),(z)))
199 rb_fix_divmod_fix(n, d, q, r);
206 rb_raise(
rb_eTypeError,
"unexpected divmod result: into %"PRIsVALUE,
214# define INT64toNUM(x) LONG2NUM(x)
215#elif defined(HAVE_LONG_LONG) && SIZEOF_LONG_LONG == 8
216# define INT64toNUM(x) LL2NUM(x)
219#if defined(HAVE_UINT64_T) && SIZEOF_LONG*2 <= SIZEOF_UINT64_T
220 typedef uint64_t uwideint_t;
221 typedef int64_t wideint_t;
222 typedef uint64_t WIDEVALUE;
223 typedef int64_t SIGNED_WIDEVALUE;
224# define WIDEVALUE_IS_WIDER 1
225# define UWIDEINT_MAX UINT64_MAX
226# define WIDEINT_MAX INT64_MAX
227# define WIDEINT_MIN INT64_MIN
228# define FIXWINT_P(tv) ((tv) & 1)
229# define FIXWVtoINT64(tv) RSHIFT((SIGNED_WIDEVALUE)(tv), 1)
230# define INT64toFIXWV(wi) ((WIDEVALUE)((SIGNED_WIDEVALUE)(wi) << 1 | FIXNUM_FLAG))
231# define FIXWV_MAX (((int64_t)1 << 62) - 1)
232# define FIXWV_MIN (-((int64_t)1 << 62))
233# define FIXWVABLE(wi) (POSFIXWVABLE(wi) && NEGFIXWVABLE(wi))
234# define WINT2FIXWV(i) WIDEVAL_WRAP(INT64toFIXWV(i))
235# define FIXWV2WINT(w) FIXWVtoINT64(WIDEVAL_GET(w))
237 typedef unsigned long uwideint_t;
238 typedef long wideint_t;
239 typedef VALUE WIDEVALUE;
241# define WIDEVALUE_IS_WIDER 0
242# define UWIDEINT_MAX ULONG_MAX
243# define WIDEINT_MAX LONG_MAX
244# define WIDEINT_MIN LONG_MIN
245# define FIXWINT_P(v) FIXNUM_P(v)
246# define FIXWV_MAX FIXNUM_MAX
247# define FIXWV_MIN FIXNUM_MIN
248# define FIXWVABLE(i) FIXABLE(i)
249# define WINT2FIXWV(i) WIDEVAL_WRAP(LONG2FIX(i))
250# define FIXWV2WINT(w) FIX2LONG(WIDEVAL_GET(w))
253#define SIZEOF_WIDEINT SIZEOF_INT64_T
254#define POSFIXWVABLE(wi) ((wi) < FIXWV_MAX+1)
255#define NEGFIXWVABLE(wi) ((wi) >= FIXWV_MIN)
256#define FIXWV_P(w) FIXWINT_P(WIDEVAL_GET(w))
257#define MUL_OVERFLOW_FIXWV_P(a, b) MUL_OVERFLOW_SIGNED_INTEGER_P(a, b, FIXWV_MIN, FIXWV_MAX)
260#if WIDEVALUE_IS_WIDER && SIZEOF_VALUE < SIZEOF_INT64_T
266 WIDEVAL_WRAP(WIDEVALUE v)
269 w.value = (
VALUE)(uint32_t)v;
270 w.hi = (uint32_t)(v >> 32);
273 static inline WIDEVALUE
276 return ((WIDEVALUE)w.hi << 32) | (uint32_t)w.value;
283 WIDEVAL_WRAP(WIDEVALUE v)
289 static inline WIDEVALUE
292 return (WIDEVALUE)w.value;
296#if WIDEVALUE_IS_WIDER
298 wint2wv(wideint_t wi)
301 return WINT2FIXWV(wi);
303 return WIDEVAL_WRAP(INT64toNUM(wi));
305# define WINT2WV(wi) wint2wv(wi)
307# define WINT2WV(wi) WIDEVAL_WRAP(LONG2NUM(wi))
313#if WIDEVALUE_IS_WIDER
315 return INT64toNUM(FIXWV2WINT(w));
316 return (
VALUE)WIDEVAL_GET(w);
318 return WIDEVAL_GET(w);
322#if WIDEVALUE_IS_WIDER
328 sign = rb_integer_pack(v, &u, 1,
sizeof(u), 0,
331 return WINT2FIXWV(0);
332 else if (sign == -1) {
334 return WINT2FIXWV(-(wideint_t)u);
336 else if (sign == +1) {
338 return WINT2FIXWV((wideint_t)u);
340 return WIDEVAL_WRAP(v);
348 if (RRATIONAL(v)->den !=
LONG2FIX(1))
349 return WIDEVAL_WRAP(v);
350 v = RRATIONAL(v)->num;
352#if WIDEVALUE_IS_WIDER
354 return WIDEVAL_WRAP((WIDEVALUE)(SIGNED_WIDEVALUE)(
long)v);
356 else if (RB_BIGNUM_TYPE_P(v) &&
357 rb_absint_size(v, NULL) <=
sizeof(WIDEVALUE)) {
358 return v2w_bignum(v);
361 return WIDEVAL_WRAP(v);
364#define NUM2WV(v) v2w(rb_Integer(v))
369#if WIDEVALUE_IS_WIDER
370 if (FIXWV_P(wx) && FIXWV_P(wy)) {
371 return WIDEVAL_GET(wx) == WIDEVAL_GET(wy);
375 return eq(WIDEVAL_GET(wx), WIDEVAL_GET(wy));
383#if WIDEVALUE_IS_WIDER
384 if (FIXWV_P(wx) && FIXWV_P(wy)) {
400#define wne(x,y) (!weq((x),(y)))
401#define wlt(x,y) (wcmp((x),(y)) < 0)
402#define wgt(x,y) (wcmp((x),(y)) > 0)
403#define wle(x,y) (wcmp((x),(y)) <= 0)
404#define wge(x,y) (wcmp((x),(y)) >= 0)
409#if WIDEVALUE_IS_WIDER
410 if (FIXWV_P(wx) && FIXWV_P(wy)) {
411 wideint_t r = FIXWV2WINT(wx) + FIXWV2WINT(wy);
415 return v2w(addv(w2v(wx), w2v(wy)));
421#if WIDEVALUE_IS_WIDER
422 if (FIXWV_P(wx) && FIXWV_P(wy)) {
423 wideint_t r = FIXWV2WINT(wx) - FIXWV2WINT(wy);
427 return v2w(subv(w2v(wx), w2v(wy)));
433#if WIDEVALUE_IS_WIDER
434 if (FIXWV_P(wx) && FIXWV_P(wy)) {
435 if (!MUL_OVERFLOW_FIXWV_P(FIXWV2WINT(wx), FIXWV2WINT(wy)))
436 return WINT2WV(FIXWV2WINT(wx) * FIXWV2WINT(wy));
439 return v2w(mulv(w2v(wx), w2v(wy)));
445#if WIDEVALUE_IS_WIDER
446 if (FIXWV_P(wx) && FIXWV_P(wy)) {
457 return v2w(quov(w2v(wx), w2v(wy)));
460#define wmulquo(x,y,z) ((WIDEVAL_GET(y) == WIDEVAL_GET(z)) ? (x) : wquo(wmul((x),(y)),(z)))
461#define wmulquoll(x,y,z) (((y) == (z)) ? (x) : wquo(wmul((x),WINT2WV(y)),WINT2WV(z)))
463#if WIDEVALUE_IS_WIDER
467 if (FIXWV_P(wn) && FIXWV_P(wd)) {
468 wideint_t n, d, q, r;
477 wideint_t xneg = -FIXWV2WINT(wn);
490 if (d > 0 ? r < 0 : r > 0) {
506#if WIDEVALUE_IS_WIDER
507 if (wdivmod0(wn, wd, wq, wr))
return;
509 divmodv(w2v(wn), w2v(wd), &vq, &vr);
517 if (WIDEVAL_GET(wy) == WIDEVAL_GET(wz)) {
522 wdivmod(wmul(wx,wy), wz, wq, wr);
528#if WIDEVALUE_IS_WIDER
530 if (wdivmod0(wx, wy, &q, &dmy))
return q;
532 return v2w(divv(w2v(wx), w2v(wy)));
538#if WIDEVALUE_IS_WIDER
540 if (wdivmod0(wx, wy, &dmy, &r))
return r;
542 return v2w(modv(w2v(wx), w2v(wy)));
546num_exact_check(
VALUE v)
557 tmp = rb_rational_canonicalize(v);
571 tmp = rb_rational_canonicalize(tmp);
587NORETURN(
static void num_exact_fail(
VALUE v));
589num_exact_fail(
VALUE v)
591 rb_cant_convert(v,
"an exact number");
597 VALUE num = num_exact_check(v);
598 if (
NIL_P(num)) num_exact_fail(v);
605static const int TIME_SCALE_NUMDIGITS =
rb_strlen_lit(STRINGIZE(TIME_SCALE)) - 1;
610 return wmul(w, WINT2FIXWV(TIME_SCALE));
614rb_time_unmagnify_to_rational(
wideval_t w)
616 return quor(w2v(w),
INT2FIX(TIME_SCALE));
622 return v2w(rb_time_unmagnify_to_rational(w));
629#if WIDEVALUE_IS_WIDER
638 v =
DBL2NUM((
double)FIXWV2WINT(w));
639 return quov(v,
DBL2NUM(TIME_SCALE));
646 return quov(v,
DBL2NUM(TIME_SCALE));
653 wdivmod(timew, WINT2FIXWV(TIME_SCALE), &q, &r);
661#if WIDEVALUE_IS_WIDER
662 if (TIMET_MIN == 0) {
663 uwideint_t wi = (uwideint_t)t;
664 if (wi <= FIXWV_MAX) {
665 return WINT2FIXWV(wi);
669 wideint_t wi = (wideint_t)t;
670 if (FIXWV_MIN <= wi && wi <= FIXWV_MAX) {
671 return WINT2FIXWV(wi);
675 return v2w(TIMET2NUM(t));
677#define TIMET2WV(t) timet2wv(t)
682#if WIDEVALUE_IS_WIDER
684 wideint_t wi = FIXWV2WINT(w);
685 if (TIMET_MIN == 0) {
687 rb_raise(
rb_eRangeError,
"negative value to convert into 'time_t'");
688 if (TIMET_MAX < (uwideint_t)wi)
692 if (wi < TIMET_MIN || TIMET_MAX < wi)
698 return NUM2TIMET(w2v(w));
700#define WV2TIMET(t) wv2timet(t)
703static VALUE rb_cTimeTM;
705static int obj2int(
VALUE obj);
706static uint32_t obj2ubits(
VALUE obj,
unsigned int bits);
708static uint32_t month_arg(
VALUE arg);
709static VALUE validate_utc_offset(
VALUE utc_offset);
710static VALUE validate_zone_name(
VALUE zone_name);
711static void validate_vtm(
struct vtm *
vtm);
712static void vtm_add_day(
struct vtm *
vtm,
int day);
713static uint32_t obj2subsecx(
VALUE obj,
VALUE *subsecx);
720static time_t timegm_noleapsecond(
struct tm *tm);
721static int tmcmp(
struct tm *a,
struct tm *b);
722static int vtmcmp(
struct vtm *a,
struct vtm *b);
723static const char *find_time_t(
struct tm *tptr,
int utc_p, time_t *tp);
727static int leap_year_p(
long y);
728#define leap_year_v_p(y) leap_year_p(NUM2LONG(modv((y), INT2FIX(400))))
732bool ruby_tz_uptodate_p;
735enum {tzkey_max = numberof(((DYNAMIC_TIME_ZONE_INFORMATION *)NULL)->TimeZoneKeyName)};
738 char name[tzkey_max * 4 + 1];
745 if (w32_tz.use_tzkey) {
746 if (w32_tz.name[0]) {
756 DYNAMIC_TIME_ZONE_INFORMATION tzi;
757 WCHAR *
const wtzkey = tzi.TimeZoneKeyName;
758 DWORD tzret = GetDynamicTimeZoneInformation(&tzi);
759 if (tzret != TIME_ZONE_ID_INVALID && *wtzkey) {
760 int wlen = (int)wcsnlen(wtzkey, tzkey_max);
761 int clen = WideCharToMultiByte(CP_UTF8, 0, wtzkey, wlen,
762 w32_tz.name,
sizeof(w32_tz.name) - 1,
764 w32_tz.name[clen] =
'\0';
771 char cp[(
sizeof(UINT) * 8 / 3) + 4];
772 snprintf(cp,
sizeof(cp),
"CP%u", GetACP());
773 *enc = rb_enc_find(cp);
774 return _tzname[_daylight && dst];
778static void ruby_reset_leap_second_info(
void);
781ruby_reset_timezone(
const char *val)
783 ruby_tz_uptodate_p =
false;
785 w32_tz.use_tzkey = !val || !*val;
787 ruby_reset_leap_second_info();
793 if (ruby_tz_uptodate_p)
return;
794 ruby_tz_uptodate_p =
true;
799rb_localtime_r(
const time_t *t,
struct tm *result)
801#if defined __APPLE__ && defined __LP64__
802 if (*t != (time_t)(
int)*t)
return NULL;
806 result = localtime_r(t, result);
809 struct tm *tmp = localtime(t);
810 if (tmp) *result = *tmp;
813#if defined(HAVE_MKTIME) && defined(LOCALTIME_OVERFLOW_PROBLEM)
817 struct tm tmp = *result;
820# if defined(HAVE_STRUCT_TM_TM_GMTOFF)
821 gmtoff1 = result->tm_gmtoff;
822 gmtoff2 = tmp.tm_gmtoff;
824 if (*t + gmtoff1 != t2 + gmtoff2)
830#define LOCALTIME(tm, result) rb_localtime_r((tm), &(result))
832#ifndef HAVE_STRUCT_TM_TM_GMTOFF
834rb_gmtime_r(
const time_t *t,
struct tm *result)
837 result = gmtime_r(t, result);
839 struct tm *tmp = gmtime(t);
840 if (tmp) *result = *tmp;
842#if defined(HAVE_TIMEGM) && defined(LOCALTIME_OVERFLOW_PROBLEM)
843 if (result && *t != timegm(result)) {
849# define GMTIME(tm, result) rb_gmtime_r((tm), &(result))
852static const int16_t common_year_yday_offset[] = {
857 -1 + 31 + 28 + 31 + 30,
858 -1 + 31 + 28 + 31 + 30 + 31,
859 -1 + 31 + 28 + 31 + 30 + 31 + 30,
860 -1 + 31 + 28 + 31 + 30 + 31 + 30 + 31,
861 -1 + 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31,
862 -1 + 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30,
863 -1 + 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31,
864 -1 + 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30
867static const int16_t leap_year_yday_offset[] = {
872 -1 + 31 + 29 + 31 + 30,
873 -1 + 31 + 29 + 31 + 30 + 31,
874 -1 + 31 + 29 + 31 + 30 + 31 + 30,
875 -1 + 31 + 29 + 31 + 30 + 31 + 30 + 31,
876 -1 + 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31,
877 -1 + 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30,
878 -1 + 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31,
879 -1 + 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30
883static const int8_t common_year_days_in_month[] = {
884 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
886static const int8_t leap_year_days_in_month[] = {
887 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
890#define days_in_month_of(leap) ((leap) ? leap_year_days_in_month : common_year_days_in_month)
891#define days_in_month_in(y) days_in_month_of(leap_year_p(y))
892#define days_in_month_in_v(y) days_in_month_of(leap_year_v_p(y))
895 (m),(m),(m),(m),(m),(m),(m),(m),(m),(m), \
896 (m),(m),(m),(m),(m),(m),(m),(m),(m),(m), \
897 (m),(m),(m),(m),(m),(m),(m),(m)
899 (m),(m),(m),(m),(m),(m),(m),(m),(m),(m), \
900 (m),(m),(m),(m),(m),(m),(m),(m),(m),(m), \
901 (m),(m),(m),(m),(m),(m),(m),(m),(m)
903 (m),(m),(m),(m),(m),(m),(m),(m),(m),(m), \
904 (m),(m),(m),(m),(m),(m),(m),(m),(m),(m), \
905 (m),(m),(m),(m),(m),(m),(m),(m),(m),(m)
907 (m),(m),(m),(m),(m),(m),(m),(m),(m),(m), \
908 (m),(m),(m),(m),(m),(m),(m),(m),(m),(m), \
909 (m),(m),(m),(m),(m),(m),(m),(m),(m),(m), (m)
911static const uint8_t common_year_mon_of_yday[] = {
912 M31(1), M28(2), M31(3), M30(4), M31(5), M30(6),
913 M31(7), M31(8), M30(9), M31(10), M30(11), M31(12)
915static const uint8_t leap_year_mon_of_yday[] = {
916 M31(1), M29(2), M31(3), M30(4), M31(5), M30(6),
917 M31(7), M31(8), M30(9), M31(10), M30(11), M31(12)
927 10,11,12,13,14,15,16,17,18,19, \
928 20,21,22,23,24,25,26,27,28
931 10,11,12,13,14,15,16,17,18,19, \
932 20,21,22,23,24,25,26,27,28,29
935 10,11,12,13,14,15,16,17,18,19, \
936 20,21,22,23,24,25,26,27,28,29,30
939 10,11,12,13,14,15,16,17,18,19, \
940 20,21,22,23,24,25,26,27,28,29,30,31
942static const uint8_t common_year_mday_of_yday[] = {
944 D31, D28, D31, D30, D31, D30, D31, D31, D30, D31, D30, D31
946static const uint8_t leap_year_mday_of_yday[] = {
947 D31, D29, D31, D30, D31, D30, D31, D31, D30, D31, D30, D31
956calc_tm_yday(
long tm_year,
int tm_mon,
int tm_mday)
958 int tm_year_mod400 = (int)MOD(tm_year, 400);
959 int tm_yday = tm_mday;
961 if (leap_year_p(tm_year_mod400 + 1900))
962 tm_yday += leap_year_yday_offset[tm_mon];
964 tm_yday += common_year_yday_offset[tm_mon];
970timegmw_noleapsecond(
struct vtm *
vtm)
982 divmodv(year1900,
INT2FIX(400), &q400, &r400);
985 yday = calc_tm_yday(year_mod400,
vtm->mon-1,
vtm->mday);
998 + DIV(year_mod400 - 69, 4)
999 - DIV(year_mod400 - 1, 100)
1000 + (year_mod400 + 299) / 400;
1002 vdays = addv(vdays, mulv(q400,
INT2FIX(97)));
1003 vdays = addv(vdays, mulv(year1900,
INT2FIX(365)));
1004 wret = wadd(rb_time_magnify(v2w(ret)), wmul(rb_time_magnify(v2w(vdays)), WINT2FIXWV(86400)));
1005 wret = wadd(wret, v2w(
vtm->subsecx));
1018 return rb_fstring_lit(
"(NO-TIMEZONE-ABBREVIATION)");
1021 for (p = zone; *p; p++) {
1040 return rb_fstring(str);
1047#define zone_str(zone) zone_str_enc(zone, NULL)
1061 split_second(timew, &timew2, &subsecx);
1062 vtm->subsecx = subsecx;
1064 wdivmod(timew2, WINT2FIXWV(86400), &w2, &w);
1069 vtm->wday = (wday + 4) % 7;
1072 vtm->sec = n % 60; n = n / 60;
1073 vtm->min = n % 60; n = n / 60;
1077 divmodv(timev,
INT2FIX(400*365 + 97), &timev, &v);
1090 if (30*365+7+31+29-1 <= n) {
1104 x = n / (365*100 + 24);
1105 n = n % (365*100 + 24);
1107 if (30*365+7+31+29-1 <= n) {
1117 x = n / (365*4 + 1);
1118 n = n % (365*4 + 1);
1120 if (365*2+31+29-1 <= n) {
1121 if (n < 365*2+366) {
1138 if (leap_year_p(y)) {
1139 vtm->mon = leap_year_mon_of_yday[n];
1140 vtm->mday = leap_year_mday_of_yday[n];
1143 vtm->mon = common_year_mon_of_yday[n];
1144 vtm->mday = common_year_mday_of_yday[n];
1148 vtm->zone = str_utc;
1152gmtime_with_leapsecond(
const time_t *timep,
struct tm *result)
1154#if defined(HAVE_STRUCT_TM_TM_GMTOFF)
1158 int gmtoff_sec, gmtoff_min, gmtoff_hour, gmtoff_day;
1160 t = LOCALTIME(timep, *result);
1165 if (t->tm_gmtoff < 0) {
1167 gmtoff = -t->tm_gmtoff;
1171 gmtoff = t->tm_gmtoff;
1173 gmtoff_sec = (int)(gmtoff % 60);
1174 gmtoff = gmtoff / 60;
1175 gmtoff_min = (int)(gmtoff % 60);
1176 gmtoff = gmtoff / 60;
1177 gmtoff_hour = (int)gmtoff;
1181 gmtoff_hour *= sign;
1188 result->tm_sec += gmtoff_sec;
1189 if (result->tm_sec < 0) {
1190 result->tm_sec += 60;
1193 if (60 <= result->tm_sec) {
1194 result->tm_sec -= 60;
1199 result->tm_min += gmtoff_min;
1200 if (result->tm_min < 0) {
1201 result->tm_min += 60;
1204 if (60 <= result->tm_min) {
1205 result->tm_min -= 60;
1210 result->tm_hour += gmtoff_hour;
1211 if (result->tm_hour < 0) {
1212 result->tm_hour += 24;
1215 if (24 <= result->tm_hour) {
1216 result->tm_hour -= 24;
1222 if (gmtoff_day < 0) {
1223 if (result->tm_yday == 0) {
1224 result->tm_mday = 31;
1225 result->tm_mon = 11;
1227 result->tm_yday = leap_year_p(result->tm_year + 1900) ? 365 : 364;
1229 else if (result->tm_mday == 1) {
1230 const int8_t *days_in_month = days_in_month_in(result->tm_year + 1900);
1232 result->tm_mday = days_in_month[result->tm_mon];
1239 result->tm_wday = (result->tm_wday + 6) % 7;
1242 int leap = leap_year_p(result->tm_year + 1900);
1243 if (result->tm_yday == (leap ? 365 : 364)) {
1246 result->tm_mday = 1;
1247 result->tm_yday = 0;
1249 else if (result->tm_mday == days_in_month_of(leap)[result->tm_mon]) {
1251 result->tm_mday = 1;
1258 result->tm_wday = (result->tm_wday + 1) % 7;
1261 result->tm_isdst = 0;
1262 result->tm_gmtoff = 0;
1263#if defined(HAVE_TM_ZONE)
1264 result->tm_zone = (
char *)
"UTC";
1268 return GMTIME(timep, *result);
1272static long this_year = 0;
1273static time_t known_leap_seconds_limit;
1274static int number_of_leap_seconds_known;
1277init_leap_second_info(
void)
1284 if (this_year == 0) {
1286 struct tm *tm, result;
1291 gmtime_r(&now, &result);
1295 tm = gmtime_with_leapsecond(&now, &result);
1297 this_year = tm->tm_year;
1299 if (TIMET_MAX - now < (time_t)(366*86400))
1300 known_leap_seconds_limit = TIMET_MAX;
1302 known_leap_seconds_limit = now + (time_t)(366*86400);
1304 if (!gmtime_with_leapsecond(&known_leap_seconds_limit, &result))
1308 vtm.mon = result.tm_mon + 1;
1309 vtm.mday = result.tm_mday;
1310 vtm.hour = result.tm_hour;
1311 vtm.min = result.tm_min;
1312 vtm.sec = result.tm_sec;
1316 timew = timegmw_noleapsecond(&
vtm);
1318 number_of_leap_seconds_known =
NUM2INT(w2v(wsub(TIMET2WV(known_leap_seconds_limit), rb_time_unmagnify(timew))));
1324ruby_reset_leap_second_info(
void)
1340 return timegmw_noleapsecond(
vtm);
1342 init_leap_second_info();
1344 timew = timegmw_noleapsecond(
vtm);
1347 if (number_of_leap_seconds_known == 0) {
1353 else if (wlt(rb_time_magnify(TIMET2WV(known_leap_seconds_limit)), timew)) {
1354 return wadd(timew, rb_time_magnify(WINT2WV(number_of_leap_seconds_known)));
1358 tm.tm_mon =
vtm->mon - 1;
1359 tm.tm_mday =
vtm->mday;
1360 tm.tm_hour =
vtm->hour;
1361 tm.tm_min =
vtm->min;
1362 tm.tm_sec =
vtm->sec;
1365 errmsg = find_time_t(&tm, 1, &t);
1367 rb_raise(rb_eArgError,
"%s", errmsg);
1368 return wadd(rb_time_magnify(TIMET2WV(t)), v2w(
vtm->subsecx));
1379 if (wlt(timew, WINT2FIXWV(0))) {
1380 gmtimew_noleapsecond(timew, result);
1384 init_leap_second_info();
1386 if (number_of_leap_seconds_known == 0) {
1390 gmtimew_noleapsecond(timew, result);
1393 else if (wlt(rb_time_magnify(TIMET2WV(known_leap_seconds_limit)), timew)) {
1394 timew = wsub(timew, rb_time_magnify(WINT2WV(number_of_leap_seconds_known)));
1395 gmtimew_noleapsecond(timew, result);
1399 split_second(timew, &timew2, &subsecx);
1401 t = WV2TIMET(timew2);
1402 if (!gmtime_with_leapsecond(&t, &tm))
1405 result->year =
LONG2NUM((
long)tm.tm_year + 1900);
1406 result->mon = tm.tm_mon + 1;
1407 result->mday = tm.tm_mday;
1408 result->hour = tm.tm_hour;
1409 result->min = tm.tm_min;
1410 result->sec = tm.tm_sec;
1411 result->subsecx = subsecx;
1412 result->utc_offset =
INT2FIX(0);
1413 result->wday = tm.tm_wday;
1414 result->yday = tm.tm_yday+1;
1415 result->isdst = tm.tm_isdst;
1420#define GMTIMEW(w, v) \
1421 (gmtimew(w, v) ? (void)0 : rb_raise(rb_eArgError, "gmtime error"))
1423static struct tm *localtime_with_gmtoff_zone(
const time_t *t,
struct tm *result,
long *gmtoff,
VALUE *zone);
1458static const int compat_common_month_table[12][7] = {
1460 { 2034, 2035, 2036, 2031, 2032, 2027, 2033 },
1461 { 2026, 2027, 2033, 2034, 2035, 2030, 2031 },
1462 { 2026, 2032, 2033, 2034, 2035, 2030, 2036 },
1463 { 2035, 2030, 2036, 2026, 2032, 2033, 2034 },
1464 { 2033, 2034, 2035, 2030, 2036, 2026, 2032 },
1465 { 2036, 2026, 2032, 2033, 2034, 2035, 2030 },
1466 { 2035, 2030, 2036, 2026, 2032, 2033, 2034 },
1467 { 2032, 2033, 2034, 2035, 2030, 2036, 2026 },
1468 { 2030, 2036, 2026, 2032, 2033, 2034, 2035 },
1469 { 2034, 2035, 2030, 2036, 2026, 2032, 2033 },
1470 { 2026, 2032, 2033, 2034, 2035, 2030, 2036 },
1471 { 2030, 2036, 2026, 2032, 2033, 2034, 2035 },
1499static const int compat_leap_month_table[7] = {
1501 2032, 2016, 2028, 2012, 2024, 2036, 2020,
1505calc_wday(
int year_mod400,
int month,
int day)
1510 a = (14 - month) / 12;
1511 y = year_mod400 + 4800 - a;
1512 m = month + 12 * a - 3;
1513 wday = day + (153*m+2)/5 + 365*y + y/4 - y/100 + y/400 + 2;
1519guess_local_offset(
struct vtm *vtm_utc,
int *isdst_ret,
VALUE *zone_ret)
1527 int year_mod400, wday;
1531 if (lt(vtm_utc->year,
INT2FIX(1916))) {
1536# if defined(NEGATIVE_TIME_T)
1537# if SIZEOF_TIME_T <= 4
1539# define THE_TIME_OLD_ENOUGH ((time_t)0x80000000)
1543# define THE_TIME_OLD_ENOUGH ((time_t)(1600-1970)*366*24*60*60)
1545 if (localtime_with_gmtoff_zone((t = THE_TIME_OLD_ENOUGH, &t), &tm, &gmtoff, &zone)) {
1547 isdst = tm.tm_isdst;
1552 if (localtime_with_gmtoff_zone((t = 0, &t), &tm, &gmtoff, &zone)) {
1554 isdst = tm.tm_isdst;
1570 wday = calc_wday(year_mod400, vtm_utc->mon, 1);
1571 if (vtm_utc->mon == 2 && leap_year_p(year_mod400))
1572 vtm2.year =
INT2FIX(compat_leap_month_table[wday]);
1574 vtm2.year =
INT2FIX(compat_common_month_table[vtm_utc->mon-1][wday]);
1576 timev = w2v(rb_time_unmagnify(timegmw(&vtm2)));
1577 t = NUM2TIMET(timev);
1579 if (localtime_with_gmtoff_zone(&t, &tm, &gmtoff, &zone)) {
1581 *isdst_ret = tm.tm_isdst;
1589 static time_t now = 0;
1590 static long now_gmtoff = 0;
1591 static int now_isdst = 0;
1592 static VALUE now_zone;
1596 localtime_with_gmtoff_zone(&now, &tm, &now_gmtoff, &zone);
1597 now_isdst = tm.tm_isdst;
1598 zone = rb_fstring(zone);
1599 rb_vm_register_global_object(zone);
1603 *isdst_ret = now_isdst;
1605 *zone_ret = now_zone;
1611small_vtm_sub(
struct vtm *vtm1,
struct vtm *vtm2)
1615 off = vtm1->sec - vtm2->sec;
1616 off += (vtm1->min - vtm2->min) * 60;
1617 off += (vtm1->hour - vtm2->hour) * 3600;
1618 if (ne(vtm1->year, vtm2->year))
1619 off += lt(vtm1->year, vtm2->year) ? -24*3600 : 24*3600;
1620 else if (vtm1->mon != vtm2->mon)
1621 off += vtm1->mon < vtm2->mon ? -24*3600 : 24*3600;
1622 else if (vtm1->mday != vtm2->mday)
1623 off += vtm1->mday < vtm2->mday ? -24*3600 : 24*3600;
1629timelocalw(
struct vtm *
vtm)
1635 struct vtm vtm1, vtm2;
1640 if (l < INT_MIN || INT_MAX < l)
1642 tm.tm_year = (int)l;
1651 tm.tm_mon =
vtm->mon-1;
1652 tm.tm_mday =
vtm->mday;
1653 tm.tm_hour =
vtm->hour;
1654 tm.tm_min =
vtm->min;
1655 tm.tm_sec =
vtm->sec;
1656 tm.tm_isdst =
vtm->isdst == VTM_ISDST_INITVAL ? -1 :
vtm->isdst;
1658 if (find_time_t(&tm, 0, &t))
1660 return wadd(rb_time_magnify(TIMET2WV(t)), v2w(
vtm->subsecx));
1663 timew1 = timegmw(
vtm);
1665 if (!localtimew(timew1, &vtm1))
1666 rb_raise(rb_eArgError,
"localtimew error");
1668 n = vtmcmp(
vtm, &vtm1);
1670 timew1 = wsub(timew1, rb_time_magnify(WINT2FIXWV(12*3600)));
1671 if (!localtimew(timew1, &vtm1))
1672 rb_raise(rb_eArgError,
"localtimew error");
1679 timew1 = wsub(timew1, rb_time_magnify(WINT2FIXWV(24*3600)));
1680 if (!localtimew(timew1, &vtm1))
1681 rb_raise(rb_eArgError,
"localtimew error");
1684 timew2 = wadd(timew1, rb_time_magnify(WINT2FIXWV(24*3600)));
1685 if (!localtimew(timew2, &vtm2))
1686 rb_raise(rb_eArgError,
"localtimew error");
1688 timew1 = wadd(timew1, rb_time_magnify(v2w(small_vtm_sub(
vtm, &vtm1))));
1689 timew2 = wadd(timew2, rb_time_magnify(v2w(small_vtm_sub(
vtm, &vtm2))));
1691 if (weq(timew1, timew2))
1694 if (!localtimew(timew1, &vtm1))
1695 rb_raise(rb_eArgError,
"localtimew error");
1696 if (
vtm->hour != vtm1.hour ||
vtm->min != vtm1.min ||
vtm->sec != vtm1.sec)
1699 if (!localtimew(timew2, &vtm2))
1700 rb_raise(rb_eArgError,
"localtimew error");
1701 if (
vtm->hour != vtm2.hour ||
vtm->min != vtm2.min ||
vtm->sec != vtm2.sec)
1705 return lt(vtm1.utc_offset, vtm2.utc_offset) ? timew2 : timew1;
1707 return lt(vtm1.utc_offset, vtm2.utc_offset) ? timew1 : timew2;
1711localtime_with_gmtoff_zone(
const time_t *t,
struct tm *result,
long *gmtoff,
VALUE *zone)
1715 if (LOCALTIME(t, tm)) {
1716#if defined(HAVE_STRUCT_TM_TM_GMTOFF)
1717 *gmtoff = tm.tm_gmtoff;
1723 u = GMTIME(t, tmbuf);
1726 if (l->tm_year != u->tm_year)
1727 off = l->tm_year < u->tm_year ? -1 : 1;
1728 else if (l->tm_mon != u->tm_mon)
1729 off = l->tm_mon < u->tm_mon ? -1 : 1;
1730 else if (l->tm_mday != u->tm_mday)
1731 off = l->tm_mday < u->tm_mday ? -1 : 1;
1734 off =
off * 24 + l->tm_hour - u->tm_hour;
1735 off =
off * 60 + l->tm_min - u->tm_min;
1736 off =
off * 60 + l->tm_sec - u->tm_sec;
1741#if defined(HAVE_TM_ZONE)
1742 *zone = zone_str(tm.tm_zone);
1743#elif defined(_WIN32)
1745 const char *name = get_tzname(tm.tm_isdst, &enc);
1746 *zone = zone_str_enc(name, enc);
1747#elif defined(HAVE_TZNAME) && defined(HAVE_DAYLIGHT)
1749 *zone = zone_str(tzname[daylight && tm.tm_isdst]);
1753 strftime(buf,
sizeof(buf),
"%Z", &tm);
1754 *zone = zone_str(buf);
1766timew_out_of_timet_range(
wideval_t timew)
1769#if WIDEVALUE_IS_WIDER && SIZEOF_TIME_T < SIZEOF_INT64_T
1770 if (FIXWV_P(timew)) {
1771 wideint_t t = FIXWV2WINT(timew);
1772 if (t < TIME_SCALE * (wideint_t)TIMET_MIN ||
1773 TIME_SCALE * (1 + (wideint_t)TIMET_MAX) <= t)
1778#if SIZEOF_TIME_T == SIZEOF_INT64_T
1779 if (FIXWV_P(timew)) {
1780 wideint_t t = FIXWV2WINT(timew);
1781 if (~(time_t)0 <= 0) {
1791 timexv = w2v(timew);
1792 if (lt(timexv, mulv(
INT2FIX(TIME_SCALE), TIMET2NUM(TIMET_MIN))) ||
1793 le(mulv(
INT2FIX(TIME_SCALE), addv(TIMET2NUM(TIMET_MAX),
INT2FIX(1))), timexv))
1801 VALUE subsecx, offset;
1805 if (!timew_out_of_timet_range(timew)) {
1811 split_second(timew, &timew2, &subsecx);
1813 t = WV2TIMET(timew2);
1815 if (localtime_with_gmtoff_zone(&t, &tm, &gmtoff, &zone)) {
1816 result->year =
LONG2NUM((
long)tm.tm_year + 1900);
1817 result->mon = tm.tm_mon + 1;
1818 result->mday = tm.tm_mday;
1819 result->hour = tm.tm_hour;
1820 result->min = tm.tm_min;
1821 result->sec = tm.tm_sec;
1822 result->subsecx = subsecx;
1823 result->wday = tm.tm_wday;
1824 result->yday = tm.tm_yday+1;
1825 result->isdst = tm.tm_isdst;
1826 result->utc_offset =
LONG2NUM(gmtoff);
1827 result->zone = zone;
1832 if (!gmtimew(timew, result))
1835 offset = guess_local_offset(result, &isdst, &zone);
1837 if (!gmtimew(wadd(timew, rb_time_magnify(v2w(offset))), result))
1840 result->utc_offset = offset;
1841 result->isdst = isdst;
1842 result->zone = zone;
1847#define TIME_TZMODE_LOCALTIME 0
1848#define TIME_TZMODE_UTC 1
1849#define TIME_TZMODE_FIXOFF 2
1850#define TIME_TZMODE_UNINITIALIZED 3
1857#define GetTimeval(obj, tobj) ((tobj) = get_timeval(obj))
1858#define GetNewTimeval(obj, tobj) ((tobj) = get_new_timeval(obj))
1860#define IsTimeval(obj) rb_typeddata_is_kind_of((obj), &time_data_type)
1861#define TIME_INIT_P(tobj) ((tobj)->vtm.tzmode != TIME_TZMODE_UNINITIALIZED)
1863#define TZMODE_UTC_P(tobj) ((tobj)->vtm.tzmode == TIME_TZMODE_UTC)
1864#define TZMODE_SET_UTC(tobj) ((tobj)->vtm.tzmode = TIME_TZMODE_UTC)
1866#define TZMODE_LOCALTIME_P(tobj) ((tobj)->vtm.tzmode == TIME_TZMODE_LOCALTIME)
1867#define TZMODE_SET_LOCALTIME(tobj) ((tobj)->vtm.tzmode = TIME_TZMODE_LOCALTIME)
1869#define TZMODE_FIXOFF_P(tobj) ((tobj)->vtm.tzmode == TIME_TZMODE_FIXOFF)
1870#define TZMODE_SET_FIXOFF(time, tobj, off) do { \
1871 (tobj)->vtm.tzmode = TIME_TZMODE_FIXOFF; \
1872 RB_OBJ_WRITE_UNALIGNED(time, &(tobj)->vtm.utc_offset, off); \
1875#define TZMODE_COPY(tobj1, tobj2) \
1876 ((tobj1)->vtm.tzmode = (tobj2)->vtm.tzmode, \
1877 (tobj1)->vtm.utc_offset = (tobj2)->vtm.utc_offset, \
1878 (tobj1)->vtm.zone = (tobj2)->vtm.zone)
1880static int zone_localtime(
VALUE zone,
VALUE time);
1882#define MAKE_TM(time, tobj) \
1884 if ((tobj)->vtm.tm_got == 0) { \
1885 time_get_tm((time), (tobj)); \
1888#define MAKE_TM_ENSURE(time, tobj, cond) \
1890 MAKE_TM(time, tobj); \
1892 force_make_tm(time, tobj); \
1899 tobj->timew = timew;
1900 if (!FIXWV_P(timew)) {
1919 VALUE zone = tobj->vtm.zone;
1920 if (!
NIL_P(zone) && zone != str_empty && zone != str_utc) {
1921 if (zone_localtime(zone, time))
return;
1923 tobj->vtm.tm_got = 0;
1924 time_get_tm(time, tobj);
1927RUBY_REFERENCES(time_refs) = {
1939 RUBY_REFS_LIST_PTR(time_refs),
1943 .flags = RUBY_TYPED_THREAD_SAFE_FREE | RUBY_TYPED_FROZEN_SHAREABLE | RUBY_TYPED_WB_PROTECTED | RUBY_TYPED_EMBEDDABLE | RUBY_TYPED_DECL_MARKING,
1947time_s_alloc(
VALUE klass)
1953 tobj->vtm.tzmode = TIME_TZMODE_UNINITIALIZED;
1954 tobj->vtm.tm_got = 0;
1955 time_set_timew(obj, tobj, WINT2FIXWV(0));
1956 tobj->vtm.zone =
Qnil;
1962get_timeval(
VALUE obj)
1966 if (!TIME_INIT_P(tobj)) {
1973get_new_timeval(
VALUE obj)
1977 if (TIME_INIT_P(tobj)) {
1984time_modify(
VALUE time)
1986 rb_check_frozen(time);
1990timenano2timew(wideint_t sec,
long nsec)
1994 timew = rb_time_magnify(WINT2WV(sec));
1996 timew = wadd(timew, wmulquoll(WINT2WV(nsec), TIME_SCALE, 1000000000));
2007 if (timew_out_of_timet_range(timew))
2008 rb_raise(rb_eArgError,
"time out of system range");
2009 split_second(timew, &timew2, &subsecx);
2010 ts.tv_sec = WV2TIMET(timew2);
2022 if (timew_out_of_timet_range(timew))
2024 split_second(timew, &timew2, &subsecx);
2025 ts->tv_sec = WV2TIMET(timew2);
2026 nsecv = mulquov(subsecx,
INT2FIX(1000000000),
INT2FIX(TIME_SCALE));
2036#ifdef HAVE_CLOCK_GETTIME
2037 if (clock_gettime(CLOCK_REALTIME, ts) == -1) {
2038 rb_sys_fail(
"clock_gettime");
2043 if (gettimeofday(&tv, 0) < 0) {
2044 rb_sys_fail(
"gettimeofday");
2046 ts->tv_sec = tv.tv_sec;
2047 ts->tv_nsec = tv.tv_usec * 1000;
2063 GetNewTimeval(time, tobj);
2064 TZMODE_SET_LOCALTIME(tobj);
2067 time_set_timew(time, tobj, timenano2timew(ts.tv_sec, ts.tv_nsec));
2070 time_zonelocal(time, zone);
2078 VALUE t = time_s_alloc(klass);
2079 return time_init_now(ec, t, zone);
2089 GetTimeval(time, tobj);
2091 tobj->vtm.tm_got = 0;
2092 tobj->vtm.zone =
Qnil;
2093 TZMODE_SET_FIXOFF(time, tobj,
off);
2118 subsec = neg(subsec);
2127 vtm->subsecx = addv(
vtm->subsecx, w2v(rb_time_magnify(v2w(subsec))));
2176 vtm_add_day(
vtm, day);
2180vtm_add_day(
struct vtm *
vtm,
int day)
2184 if (
vtm->mon == 1 &&
vtm->mday == 1) {
2189 vtm->yday = leap_year_v_p(
vtm->year) ? 366 : 365;
2191 else if (
vtm->mday == 1) {
2192 const int8_t *days_in_month = days_in_month_in_v(
vtm->year);
2194 vtm->mday = days_in_month[
vtm->mon-1];
2195 if (
vtm->yday != 0)
vtm->yday--;
2199 if (
vtm->yday != 0)
vtm->yday--;
2201 if (
vtm->wday != VTM_WDAY_INITVAL)
vtm->wday = (
vtm->wday + 6) % 7;
2204 int leap = leap_year_v_p(
vtm->year);
2205 if (
vtm->mon == 12 &&
vtm->mday == 31) {
2211 else if (
vtm->mday == days_in_month_of(leap)[
vtm->mon-1]) {
2214 if (
vtm->yday != 0)
vtm->yday++;
2218 if (
vtm->yday != 0)
vtm->yday++;
2220 if (
vtm->wday != VTM_WDAY_INITVAL)
vtm->wday = (
vtm->wday + 1) % 7;
2226maybe_tzobj_p(
VALUE obj)
2228 if (
NIL_P(obj))
return FALSE;
2234NORETURN(
static void invalid_utc_offset(
VALUE));
2236invalid_utc_offset(
VALUE zone)
2238 rb_raise(rb_eArgError,
"\"+HH:MM\", \"-HH:MM\", \"UTC\" or "
2239 "\"A\"..\"I\",\"K\"..\"Z\" expected for utc_offset: %"PRIsVALUE,
2243#define have_2digits(ptr) (ISDIGIT((ptr)[0]) && ISDIGIT((ptr)[1]))
2244#define num_from_2digits(ptr) ((ptr)[0] * 10 + (ptr)[1] - '0' * 11)
2247utc_offset_arg(
VALUE arg)
2256 const char *s = RSTRING_PTR(tmp), *min = NULL, *sec = NULL;
2258 goto invalid_utc_offset;
2260 switch (RSTRING_LEN(tmp)) {
2266 if (s[0] >=
'A' && s[0] <=
'I') {
2267 n = (int)s[0] -
'A' + 1;
2270 else if (s[0] >=
'K' && s[0] <=
'M') {
2271 n = (int)s[0] -
'A';
2273 else if (s[0] >=
'N' && s[0] <=
'Y') {
2274 n =
'M' - (int)s[0];
2277 goto invalid_utc_offset;
2293 if (s[6] !=
':')
goto invalid_utc_offset;
2297 if (s[3] !=
':')
goto invalid_utc_offset;
2301 goto invalid_utc_offset;
2304 if (!have_2digits(sec))
goto invalid_utc_offset;
2305 if (sec[0] >
'5')
goto invalid_utc_offset;
2306 n += num_from_2digits(sec);
2310 if (!have_2digits(min))
goto invalid_utc_offset;
2311 if (min[0] >
'5')
goto invalid_utc_offset;
2312 n += num_from_2digits(min) * 60;
2314 if (s[0] !=
'+' && s[0] !=
'-')
goto invalid_utc_offset;
2315 if (!have_2digits(s+1))
goto invalid_utc_offset;
2316 n += num_from_2digits(s+1) * 3600;
2318 if (n == 0)
return UTC_ZONE;
2324 return num_exact(arg);
2337 validate_utc_offset(
off);
2340 TZMODE_SET_LOCALTIME(tobj);
2344extract_time(
VALUE time)
2347 const ID id_to_i = idTo_i;
2349#define EXTRACT_TIME() do { \
2350 t = NUM2WV(AREF(to_i)); \
2353 if (rb_typeddata_is_kind_of(time, &time_data_type)) {
2354 struct time_object *tobj = RTYPEDDATA_GET_DATA(time);
2357 t = rb_time_unmagnify(tobj->timew);
2362#define AREF(x) rb_struct_aref(time, ID2SYM(id_##x))
2367#define AREF(x) rb_funcallv(time, id_##x, 0, 0)
2380 const ID id_to_i = idTo_i;
2381 struct vtm *
vtm = &orig_tobj->vtm;
2383#define EXTRACT_VTM() do { \
2385 vtm->year = obj2vint(AREF(year)); \
2386 vtm->mon = month_arg(AREF(mon)); \
2387 vtm->mday = obj2ubits(AREF(mday), 5); \
2388 vtm->hour = obj2ubits(AREF(hour), 5); \
2389 vtm->min = obj2ubits(AREF(min), 6); \
2390 vtm->sec = obj2subsecx(AREF(sec), &subsecx); \
2391 vtm->isdst = RTEST(AREF(isdst)); \
2392 vtm->utc_offset = Qnil; \
2393 t = NUM2WV(AREF(to_i)); \
2396 if (rb_typeddata_is_kind_of(time, &time_data_type)) {
2397 struct time_object *tobj = RTYPEDDATA_GET_DATA(time);
2399 time_get_tm(time, tobj);
2400 time_set_vtm(orig_time, orig_tobj, tobj->vtm);
2401 t = rb_time_unmagnify(tobj->timew);
2402 if (TZMODE_FIXOFF_P(tobj) &&
vtm->utc_offset !=
INT2FIX(0))
2403 t = wadd(t, v2w(
vtm->utc_offset));
2408#define AREF(x) rb_struct_aref(time, ID2SYM(id_##x))
2414 struct vtm temp_vtm = *
vtm;
2415 GMTIMEW(rb_time_magnify(t), &temp_vtm);
2416 time_set_vtm(orig_time, orig_tobj, temp_vtm);
2419#define AREF(x) rb_funcallv(time, id_##x, 0, 0)
2425 RB_OBJ_WRITE_UNALIGNED(orig_time, &
vtm->subsecx, subsecx);
2438 tobj->vtm.isdst = (!UNDEF_P(dst) &&
RTEST(dst));
2445 struct time_object *tobj = RTYPEDDATA_GET_DATA(time);
2448 wdivmod(tobj->timew, WINT2FIXWV(TIME_SCALE), &t, &s);
2449 tm = tm_from_time(rb_cTimeTM, time);
2451 if (UNDEF_P(utc))
return 0;
2453 s = extract_time(utc);
2454 zone_set_offset(zone, tobj, t, s, time);
2455 s = rb_time_magnify(s);
2456 if (tobj->vtm.subsecx !=
INT2FIX(0)) {
2457 s = wadd(s, v2w(tobj->vtm.subsecx));
2459 time_set_timew(time, tobj, s);
2461 zone_set_dst(zone, tobj, tm);
2471 VALUE local, tm, subsecx;
2472 struct time_object *tobj = RTYPEDDATA_GET_DATA(time);
2475 split_second(tobj->timew, &t, &subsecx);
2476 tm = tm_from_time(rb_cTimeTM, time);
2479 if (UNDEF_P(local))
return 0;
2481 s = extract_vtm(local, time, tobj, subsecx);
2482 tobj->vtm.tm_got = 1;
2483 zone_set_offset(zone, tobj, s, t, time);
2484 zone_set_dst(zone, tobj, tm);
2496 return rb_check_funcall_default(klass, id_find_timezone, 1, &zone,
Qnil);
2502vtm_day_wraparound(
struct vtm *
vtm)
2504 if (
vtm->hour < 24)
return;
2509 vtm_add_day(
vtm, 1);
2524 vtm.wday = VTM_WDAY_INITVAL;
2526 vtm.zone = str_empty;
2528 vtm.year = obj2vint(year);
2530 vtm.mon =
NIL_P(mon) ? 1 : month_arg(mon);
2532 vtm.mday =
NIL_P(mday) ? 1 : obj2ubits(mday, 5);
2534 vtm.hour =
NIL_P(hour) ? 0 : obj2ubits(hour, 5);
2536 vtm.min =
NIL_P(min) ? 0 : obj2ubits(min, 6);
2544 vtm.sec = obj2subsecx(sec, &subsecx);
2545 vtm.subsecx = subsecx;
2548 return time_init_vtm(time,
vtm, zone);
2557 vtm.isdst = VTM_ISDST_INITVAL;
2559 const VALUE arg = zone;
2562 if (arg ==
ID2SYM(rb_intern(
"dst")))
2564 else if (arg ==
ID2SYM(rb_intern(
"std")))
2566 else if (maybe_tzobj_p(arg))
2568 else if (!
NIL_P(utc = utc_offset_arg(arg)))
2569 vtm.utc_offset = utc == UTC_ZONE ?
INT2FIX(0) : utc;
2570 else if (
NIL_P(zone = find_timezone(time, arg)))
2571 invalid_utc_offset(arg);
2577 GetNewTimeval(time, tobj);
2580 time_set_timew(time, tobj, timegmw(&
vtm));
2581 vtm_day_wraparound(&
vtm);
2582 time_set_vtm(time, tobj,
vtm);
2583 tobj->vtm.tm_got = 1;
2584 TZMODE_SET_LOCALTIME(tobj);
2585 if (zone_timelocal(zone, time)) {
2588 else if (
NIL_P(
vtm.utc_offset = utc_offset_arg(zone))) {
2589 if (
NIL_P(zone = find_timezone(time, zone)) || !zone_timelocal(zone, time))
2590 invalid_utc_offset(arg);
2594 if (utc == UTC_ZONE) {
2595 time_set_timew(time, tobj, timegmw(&
vtm));
2597 vtm_day_wraparound(&
vtm);
2598 time_set_vtm(time, tobj,
vtm);
2599 tobj->vtm.tm_got = 1;
2600 TZMODE_SET_UTC(tobj);
2604 TZMODE_SET_LOCALTIME(tobj);
2609 vtm_add_offset(&
vtm,
off, -1);
2611 time_set_timew(time, tobj, timegmw(&
vtm));
2613 return time_set_utc_offset(time,
off);
2616 time_set_timew(time, tobj, timelocalw(&
vtm));
2618 return time_localtime(time);
2623two_digits(
const char *ptr,
const char *end,
const char **endp,
const char *name)
2625 ssize_t
len = end - ptr;
2626 if (
len < 2 || !have_2digits(ptr) || ((
len > 2) &&
ISDIGIT(ptr[2]))) {
2627 VALUE mesg = rb_sprintf(
"two digits %s is expected", name);
2628 if (ptr[-1] ==
'-' || ptr[-1] ==
':') {
2629 rb_str_catf(mesg,
" after '%c'", ptr[-1]);
2631 rb_str_catf(mesg,
": %.*s", ((
len > 10) ? 10 : (int)(end - ptr)) + 1, ptr - 1);
2635 return num_from_2digits(ptr);
2639parse_int(
const char *ptr,
const char *end,
const char **endp,
size_t *ndigits,
bool sign)
2641 ssize_t
len = (end - ptr);
2643 return rb_int_parse_cstr(ptr,
len, (
char **)endp, ndigits, 10, flags);
2655 rb_raise(rb_eArgError,
"time string should have ASCII compatible encoding");
2658 size_t prec =
NIL_P(precision) ? SIZE_MAX :
NUM2SIZET(precision);
2660 const char *
const begin = RSTRING_PTR(str);
2662 const char *ptr = begin;
2664 int mon = -1, mday = -1, hour = -1, min = -1, sec = -1;
2668 rb_raise(rb_eArgError,
"can't parse: %+"PRIsVALUE, str);
2670 year = parse_int(ptr, end, &ptr, &ndigits,
true);
2672 rb_raise(rb_eArgError,
"can't parse: %+"PRIsVALUE, str);
2674 else if (ndigits < 4) {
2675 rb_raise(rb_eArgError,
"year must be 4 or more digits: %.*s", (
int)ndigits, ptr - ndigits);
2677 else if (ptr == end) {
2681#define peekable_p(n) ((ptrdiff_t)(n) < (end - ptr))
2682#define peek_n(c, n) (peekable_p(n) && ((unsigned char)ptr[n] == (c)))
2683#define peek(c) peek_n(c, 0)
2684#define peekc_n(n) (peekable_p(n) ? (int)(unsigned char)ptr[n] : -1)
2685#define peekc() peekc_n(0)
2686#define expect_two_digits(x, bits) \
2687 (((unsigned int)(x = two_digits(ptr + 1, end, &ptr, #x)) > (1U << bits) - 1) ? \
2688 rb_raise(rb_eArgError, #x" out of range") : (void)0)
2689 if (!peek(
'-'))
break;
2690 expect_two_digits(mon, 4);
2691 if (!peek(
'-'))
break;
2692 expect_two_digits(mday, 5);
2693 if (!peek(
' ') && !peek(
'T'))
break;
2694 const char *
const time_part = ptr + 1;
2695 if (!
ISDIGIT(peekc_n(1)))
break;
2696#define nofraction(x) \
2698 rb_raise(rb_eArgError, "fraction " #x " is not supported: %.*s", \
2699 (int)(ptr + 1 - time_part), time_part); \
2701#define need_colon(x) \
2703 rb_raise(rb_eArgError, "missing " #x " part: %.*s", \
2704 (int)(ptr + 1 - time_part), time_part); \
2706 expect_two_digits(hour, 5);
2709 expect_two_digits(min, 6);
2712 expect_two_digits(sec, 6);
2715 for (ndigits = 0; ndigits < prec &&
ISDIGIT(peekc_n(ndigits)); ++ndigits);
2717 int clen = rb_enc_precise_mbclen(ptr, end, rb_enc_get(str));
2718 if (clen < 0) clen = 0;
2719 rb_raise(rb_eArgError,
"subsecond expected after dot: %.*s",
2720 (
int)(ptr - time_part) + clen, time_part);
2722 subsec = parse_int(ptr, ptr + ndigits, &ptr, &ndigits,
false);
2723 if (
NIL_P(subsec))
break;
2724 while (ptr < end &&
ISDIGIT(*ptr)) ptr++;
2727 while (ptr < end &&
ISSPACE(*ptr)) ptr++;
2728 const char *
const zstr = ptr;
2729 while (ptr < end && !
ISSPACE(*ptr)) ptr++;
2730 const char *
const zend = ptr;
2731 while (ptr < end &&
ISSPACE(*ptr)) ptr++;
2740 else if (hour == -1) {
2741 rb_raise(rb_eArgError,
"no time information");
2743 if (!
NIL_P(subsec)) {
2745 if (ndigits < (
size_t)TIME_SCALE_NUMDIGITS) {
2747 subsec = rb_int_mul(subsec, mul);
2749 else if (ndigits > (
size_t)TIME_SCALE_NUMDIGITS) {
2759 .wday = VTM_WDAY_INITVAL,
2763 .mon = (mon < 0) ? 1 : mon,
2764 .mday = (mday < 0) ? 1 : mday,
2765 .hour = (hour < 0) ? 0 : hour,
2766 .min = (min < 0) ? 0 : min,
2767 .sec = (sec < 0) ? 0 : sec,
2770 return time_init_vtm(time,
vtm, zone);
2774subsec_normalize(wideint_t *secp,
long *subsecp,
const long maxsubsec)
2776 wideint_t sec = *secp;
2777 long subsec = *subsecp;
2780 if (UNLIKELY(subsec >= maxsubsec)) {
2781 sec2 = subsec / maxsubsec;
2782 if (WIDEINT_MAX - sec2 < sec) {
2785 subsec -= sec2 * maxsubsec;
2788 else if (UNLIKELY(subsec < 0)) {
2789 sec2 = NDIV(subsec, maxsubsec);
2790 if (sec < WIDEINT_MIN - sec2) {
2793 subsec -= sec2 * maxsubsec;
2800#define time_usec_normalize(secp, usecp) subsec_normalize(secp, usecp, 1000000)
2801#define time_nsec_normalize(secp, nsecp) subsec_normalize(secp, nsecp, 1000000000)
2806 VALUE time = time_s_alloc(klass);
2809 tobj = RTYPEDDATA_GET_DATA(time);
2810 TZMODE_SET_LOCALTIME(tobj);
2811 time_set_timew(time, tobj, timew);
2817TIMETtoWIDEINT(time_t t)
2819#if SIZEOF_TIME_T * CHAR_BIT - (SIGNEDNESS_OF_TIME_T < 0) > \
2820 SIZEOF_WIDEINT * CHAR_BIT - 1
2822 if (t > WIDEINT_MAX) rb_raise(rb_eArgError,
"out of Time range");
2824 return (wideint_t)t;
2830 wideint_t isec = TIMETtoWIDEINT(sec);
2831 time_usec_normalize(&isec, &usec);
2832 return time_new_timew(
rb_cTime, timenano2timew(isec, usec * 1000));
2839 wideint_t isec = TIMETtoWIDEINT(sec);
2840 time_nsec_normalize(&isec, &nsec);
2841 return time_new_timew(
rb_cTime, timenano2timew(isec, nsec));
2850 if (-86400 < offset && offset < 86400) {
2851 GetTimeval(time, tobj);
2852 TZMODE_SET_FIXOFF(time, tobj,
INT2FIX(offset));
2854 else if (offset == INT_MAX) {
2856 else if (offset == INT_MAX-1) {
2857 GetTimeval(time, tobj);
2858 TZMODE_SET_UTC(tobj);
2861 rb_raise(rb_eArgError,
"utc_offset out of range");
2870 VALUE time = time_new_timew(
rb_cTime, rb_time_magnify(v2w(timev)));
2875 if (maybe_tzobj_p(zone)) {
2877 if (zone_timelocal(zone, time))
return time;
2881 if (
NIL_P(zone = find_timezone(time,
off))) invalid_utc_offset(
off);
2883 if (!zone_timelocal(zone, time)) invalid_utc_offset(
off);
2886 else if (
off == UTC_ZONE) {
2887 return time_gmtime(time);
2890 validate_utc_offset(
off);
2891 time_set_utc_offset(time,
off);
2899time_timespec(
VALUE num, int interval)
2902 const char *
const tstr = interval ?
"time interval" :
"time";
2905#ifndef NEGATIVE_TIME_T
2906# define arg_range_check(v) \
2908 rb_raise(rb_eArgError, "%s must not be negative", tstr) : \
2911# define arg_range_check(v) \
2912 ((interval && (v) < 0) ? \
2913 rb_raise(rb_eArgError, "time interval must not be negative") : \
2918 t.tv_sec = NUM2TIMET(num);
2919 arg_range_check(t.tv_sec);
2930 t.tv_nsec = (int)(d*1e9+0.5);
2931 if (t.tv_nsec >= 1000000000) {
2932 t.tv_nsec -= 1000000000;
2936 else if ((t.tv_nsec = (
int)(-d*1e9+0.5)) > 0) {
2937 t.tv_nsec = 1000000000 - t.tv_nsec;
2940 t.tv_sec = (time_t)f;
2941 if (f != t.tv_sec) {
2946 else if (RB_BIGNUM_TYPE_P(num)) {
2947 t.tv_sec = NUM2TIMET(num);
2948 arg_range_check(t.tv_sec);
2957 t.tv_sec = NUM2TIMET(i);
2958 arg_range_check(t.tv_sec);
2963 rb_cant_convert(num, tstr);
2967#undef arg_range_check
2971time_timeval(
VALUE num, int interval)
2976 ts = time_timespec(num, interval);
2977 tv.tv_sec = (TYPEOF_TIMEVAL_TV_SEC)ts.tv_sec;
2978 tv.tv_usec = (TYPEOF_TIMEVAL_TV_USEC)(ts.tv_nsec / 1000);
2986 return time_timeval(num, TRUE);
2996 if (IsTimeval(time)) {
2997 GetTimeval(time, tobj);
2998 ts = timew2timespec(tobj->timew);
2999 t.tv_sec = (TYPEOF_TIMEVAL_TV_SEC)ts.tv_sec;
3000 t.tv_usec = (TYPEOF_TIMEVAL_TV_USEC)(ts.tv_nsec / 1000);
3003 return time_timeval(time, FALSE);
3012 if (IsTimeval(time)) {
3013 GetTimeval(time, tobj);
3014 t = timew2timespec(tobj->timew);
3017 return time_timespec(time, FALSE);
3023 return time_timespec(num, TRUE);
3027get_scale(
VALUE unit)
3029 if (unit ==
ID2SYM(id_nanosecond) || unit ==
ID2SYM(id_nsec)) {
3032 else if (unit ==
ID2SYM(id_microsecond) || unit ==
ID2SYM(id_usec)) {
3035 else if (unit ==
ID2SYM(id_millisecond)) {
3039 rb_raise(rb_eArgError,
"unexpected unit: %"PRIsVALUE, unit);
3050 int scale = get_scale(unit);
3051 time = num_exact(time);
3052 t = num_exact(subsec);
3053 timew = wadd(rb_time_magnify(v2w(time)), wmulquoll(v2w(t), TIME_SCALE, scale));
3054 t = time_new_timew(klass, timew);
3056 else if (IsTimeval(time)) {
3058 GetTimeval(time, tobj);
3059 t = time_new_timew(klass, tobj->timew);
3060 GetTimeval(t, tobj2);
3061 TZMODE_COPY(tobj2, tobj);
3064 timew = rb_time_magnify(v2w(num_exact(time)));
3065 t = time_new_timew(klass, timew);
3068 time_zonelocal(t, zone);
3080static const char months[][4] = {
3081 "jan",
"feb",
"mar",
"apr",
"may",
"jun",
3082 "jul",
"aug",
"sep",
"oct",
"nov",
"dec",
3089 obj = rb_str_to_inum(obj, 10, TRUE);
3097obj2ubits(
VALUE obj,
unsigned int bits)
3099 const unsigned int usable_mask = (1U << bits) - 1;
3100 unsigned int rv = (
unsigned int)obj2int(obj);
3102 if ((rv & usable_mask) != rv)
3103 rb_raise(rb_eArgError,
"argument out of range");
3104 return (uint32_t)rv;
3111 obj = rb_str_to_inum(obj, 10, TRUE);
3126 obj = rb_str_to_inum(obj, 10, TRUE);
3130 divmodv(num_exact(obj),
INT2FIX(1), &obj, &subsec);
3131 *subsecx = w2v(rb_time_magnify(v2w(subsec)));
3133 return obj2ubits(obj, 6);
3137usec2subsecx(
VALUE obj)
3140 obj = rb_str_to_inum(obj, 10, TRUE);
3143 return mulquov(num_exact(obj),
INT2FIX(TIME_SCALE),
INT2FIX(1000000));
3152 return obj2ubits(arg, 4);
3157 if (!
NIL_P(s) && RSTRING_LEN(s) > 0) {
3159 for (i=0; i<12; i++) {
3160 if (RSTRING_LEN(s) == 3 &&
3168 mon = obj2ubits(arg, 4);
3174validate_utc_offset(
VALUE utc_offset)
3176 if (le(utc_offset,
INT2FIX(-86400)) || ge(utc_offset,
INT2FIX(86400)))
3177 rb_raise(rb_eArgError,
"utc_offset out of range");
3182validate_zone_name(
VALUE zone_name)
3189validate_vtm(
struct vtm *
vtm)
3191#define validate_vtm_range(mem, b, e) \
3192 ((vtm->mem < b || vtm->mem > e) ? \
3193 rb_raise(rb_eArgError, #mem" out of range") : (void)0)
3194 validate_vtm_range(mon, 1, 12);
3195 validate_vtm_range(mday, 1, 31);
3196 validate_vtm_range(hour, 0, 24);
3197 validate_vtm_range(min, 0, (
vtm->hour == 24 ? 0 : 59));
3198 validate_vtm_range(sec, 0, (
vtm->hour == 24 ? 0 : 60));
3200 rb_raise(rb_eArgError,
"subsecx out of range");
3201 if (!
NIL_P(
vtm->utc_offset)) validate_utc_offset(
vtm->utc_offset);
3202#undef validate_vtm_range
3206time_arg(
int argc,
const VALUE *argv,
struct vtm *
vtm)
3222 vtm->zone = str_empty;
3232 vtm->isdst =
RTEST(argv[8]) ? 1 : 0;
3235 rb_scan_args(argc, argv,
"17", &v[0],&v[1],&v[2],&v[3],&v[4],&v[5],&v[6],&v[7]);
3238 vtm->wday = VTM_WDAY_INITVAL;
3239 vtm->isdst = VTM_ISDST_INITVAL;
3242 vtm->year = obj2vint(v[0]);
3248 vtm->mon = month_arg(v[1]);
3255 vtm->mday = obj2ubits(v[2], 5);
3263 unsigned int mday2 = leap_year_v_p(
vtm->year) ? 29 : 28;
3264 if (
vtm->mday > mday2) {
3274 if (
vtm->mday == 31) {
3281 vtm->hour =
NIL_P(v[3])?0:obj2ubits(v[3], 5);
3283 vtm->min =
NIL_P(v[4])?0:obj2ubits(v[4], 6);
3285 if (!
NIL_P(v[6]) && argc == 7) {
3286 vtm->sec =
NIL_P(v[5])?0:obj2ubits(v[5],6);
3287 subsecx = usec2subsecx(v[6]);
3295 vtm->sec = obj2subsecx(v[5], &subsecx);
3298 vtm->subsecx = subsecx;
3310 unsigned long uy = (
unsigned long)(LIKELY(y >= 0) ? y : -y);
3312 if (LIKELY(uy % 4 != 0))
return 0;
3314 unsigned long century = uy / 100;
3315 if (LIKELY(uy != century * 100))
return 1;
3316 return century % 4 == 0;
3320timegm_noleapsecond(
struct tm *tm)
3322 long tm_year = tm->tm_year;
3323 int tm_yday = calc_tm_yday(tm->tm_year, tm->tm_mon, tm->tm_mday);
3331 return tm->tm_sec + tm->tm_min*60 + tm->tm_hour*3600 +
3335 DIV(tm_year-1,100) +
3336 DIV(tm_year+299,400))*86400;
3340#define DEBUG_FIND_TIME_NUMGUESS
3341#define DEBUG_GUESSRANGE
3344static const bool debug_guessrange =
3345#ifdef DEBUG_GUESSRANGE
3351#define DEBUG_REPORT_GUESSRANGE \
3352 (debug_guessrange ? debug_report_guessrange(guess_lo, guess_hi) : (void)0)
3355debug_report_guessrange(time_t guess_lo, time_t guess_hi)
3357 time_t guess_diff = guess_hi - guess_lo;
3358 fprintf(stderr,
"find time guess range: %"PRI_TIMET_PREFIX
"d - "
3359 "%"PRI_TIMET_PREFIX
"d : %"PRI_TIMET_PREFIX
"u\n",
3360 guess_lo, guess_hi, guess_diff);
3363static const bool debug_find_time_numguess =
3364#ifdef DEBUG_FIND_TIME_NUMGUESS
3370#define DEBUG_FIND_TIME_NUMGUESS_INC \
3371 (void)(debug_find_time_numguess && find_time_numguess++),
3372static unsigned long long find_time_numguess;
3375find_time_numguess_getter(
ID name,
VALUE *data)
3377 unsigned long long *numguess = (
void *)data;
3382find_time_t(
struct tm *tptr,
int utc_p, time_t *tp)
3384 time_t guess, guess0, guess_lo, guess_hi;
3385 struct tm *tm, tm0, tm_lo, tm_hi;
3392#define GUESS(p) (DEBUG_FIND_TIME_NUMGUESS_INC (utc_p ? gmtime_with_leapsecond((p), &result) : LOCALTIME((p), result)))
3394 guess_lo = TIMET_MIN;
3395 guess_hi = TIMET_MAX;
3397 find_dst = 0 < tptr->tm_isdst;
3403 if (tm0.tm_mon < 0) {
3410 else if (11 < tm0.tm_mon) {
3417 else if (tm0.tm_mday < 1) {
3423 else if ((d = days_in_month_in(1900 + tm0.tm_year)[tm0.tm_mon]) < tm0.tm_mday) {
3429 else if (tm0.tm_hour < 0) {
3434 else if (23 < tm0.tm_hour) {
3439 else if (tm0.tm_min < 0) {
3443 else if (59 < tm0.tm_min) {
3447 else if (tm0.tm_sec < 0) {
3450 else if (60 < tm0.tm_sec) {
3454 DEBUG_REPORT_GUESSRANGE;
3455 guess0 = guess = timegm_noleapsecond(&tm0);
3458 d = tmcmp(tptr, tm);
3459 if (d == 0) {
goto found; }
3462 guess -= 24 * 60 * 60;
3466 guess += 24 * 60 * 60;
3468 DEBUG_REPORT_GUESSRANGE;
3469 if (guess_lo < guess && guess < guess_hi && (tm = GUESS(&guess)) != NULL) {
3470 d = tmcmp(tptr, tm);
3471 if (d == 0) {
goto found; }
3476 DEBUG_REPORT_GUESSRANGE;
3480 tm = GUESS(&guess_lo);
3481 if (!tm)
goto error;
3482 d = tmcmp(tptr, tm);
3483 if (d < 0)
goto out_of_range;
3484 if (d == 0) { guess = guess_lo;
goto found; }
3487 tm = GUESS(&guess_hi);
3488 if (!tm)
goto error;
3489 d = tmcmp(tptr, tm);
3490 if (d > 0)
goto out_of_range;
3491 if (d == 0) { guess = guess_hi;
goto found; }
3494 DEBUG_REPORT_GUESSRANGE;
3498 while (guess_lo + 1 < guess_hi) {
3501 guess = guess_lo / 2 + guess_hi / 2;
3502 if (guess <= guess_lo)
3503 guess = guess_lo + 1;
3504 else if (guess >= guess_hi)
3505 guess = guess_hi - 1;
3510 time_t guess0_hi = timegm_noleapsecond(&tm_hi);
3511 guess = guess_hi - (guess0_hi - guess0);
3512 if (guess == guess_hi)
3516 else if (status == 2) {
3517 time_t guess0_lo = timegm_noleapsecond(&tm_lo);
3518 guess = guess_lo + (guess0 - guess0_lo);
3519 if (guess == guess_lo)
3523 if (guess <= guess_lo || guess_hi <= guess) {
3525 if (debug_guessrange) {
3526 if (guess <= guess_lo) {
3527 fprintf(stderr,
"too small guess: %"PRI_TIMET_PREFIX
"d"\
3528 " <= %"PRI_TIMET_PREFIX
"d\n", guess, guess_lo);
3530 if (guess_hi <= guess) {
3531 fprintf(stderr,
"too big guess: %"PRI_TIMET_PREFIX
"d"\
3532 " <= %"PRI_TIMET_PREFIX
"d\n", guess_hi, guess);
3541 if (!tm)
goto error;
3543 d = tmcmp(tptr, tm);
3548 DEBUG_REPORT_GUESSRANGE;
3553 DEBUG_REPORT_GUESSRANGE;
3568 tptr_tm_yday = calc_tm_yday(tptr->tm_year, tptr->tm_mon, tptr->tm_mday);
3571 ((tptr->tm_year - tm_lo.tm_year) * 365 +
3572 DIV((tptr->tm_year-69), 4) -
3573 DIV((tptr->tm_year-1), 100) +
3574 DIV((tptr->tm_year+299), 400) -
3575 DIV((tm_lo.tm_year-69), 4) +
3576 DIV((tm_lo.tm_year-1), 100) -
3577 DIV((tm_lo.tm_year+299), 400) +
3579 tm_lo.tm_yday) * 86400 +
3580 (tptr->tm_hour - tm_lo.tm_hour) * 3600 +
3581 (tptr->tm_min - tm_lo.tm_min) * 60 +
3582 (tptr->tm_sec - (tm_lo.tm_sec == 60 ? 59 : tm_lo.tm_sec));
3591 guess2 = guess - 2 * 60 * 60;
3592 tm = LOCALTIME(&guess2, result);
3594 if (tptr->tm_hour != (tm->tm_hour + 2) % 24 ||
3595 tptr->tm_min != tm->tm_min ||
3596 tptr->tm_sec != tm->tm_sec) {
3597 guess2 -= (tm->tm_hour - tptr->tm_hour) * 60 * 60 +
3598 (tm->tm_min - tptr->tm_min) * 60 +
3599 (tm->tm_sec - tptr->tm_sec);
3600 if (tptr->tm_mday != tm->tm_mday)
3601 guess2 += 24 * 60 * 60;
3602 if (guess != guess2) {
3603 tm = LOCALTIME(&guess2, result);
3604 if (tm && tmcmp(tptr, tm) == 0) {
3616 guess2 = guess + 2 * 60 * 60;
3617 tm = LOCALTIME(&guess2, result);
3619 if ((tptr->tm_hour + 2) % 24 != tm->tm_hour ||
3620 tptr->tm_min != tm->tm_min ||
3621 tptr->tm_sec != tm->tm_sec) {
3622 guess2 -= (tm->tm_hour - tptr->tm_hour) * 60 * 60 +
3623 (tm->tm_min - tptr->tm_min) * 60 +
3624 (tm->tm_sec - tptr->tm_sec);
3625 if (tptr->tm_mday != tm->tm_mday)
3626 guess2 -= 24 * 60 * 60;
3627 if (guess != guess2) {
3628 tm = LOCALTIME(&guess2, result);
3629 if (tm && tmcmp(tptr, tm) == 0) {
3645 return "time out of range";
3648 return "gmtime/localtime error";
3652vtmcmp(
struct vtm *a,
struct vtm *b)
3654 if (ne(a->year, b->year))
3655 return lt(a->year, b->year) ? -1 : 1;
3656 else if (a->mon != b->mon)
3657 return a->mon < b->mon ? -1 : 1;
3658 else if (a->mday != b->mday)
3659 return a->mday < b->mday ? -1 : 1;
3660 else if (a->hour != b->hour)
3661 return a->hour < b->hour ? -1 : 1;
3662 else if (a->min != b->min)
3663 return a->min < b->min ? -1 : 1;
3664 else if (a->sec != b->sec)
3665 return a->sec < b->sec ? -1 : 1;
3666 else if (ne(a->subsecx, b->subsecx))
3667 return lt(a->subsecx, b->subsecx) ? -1 : 1;
3673tmcmp(
struct tm *a,
struct tm *b)
3675 if (a->tm_year != b->tm_year)
3676 return a->tm_year < b->tm_year ? -1 : 1;
3677 else if (a->tm_mon != b->tm_mon)
3678 return a->tm_mon < b->tm_mon ? -1 : 1;
3679 else if (a->tm_mday != b->tm_mday)
3680 return a->tm_mday < b->tm_mday ? -1 : 1;
3681 else if (a->tm_hour != b->tm_hour)
3682 return a->tm_hour < b->tm_hour ? -1 : 1;
3683 else if (a->tm_min != b->tm_min)
3684 return a->tm_min < b->tm_min ? -1 : 1;
3685 else if (a->tm_sec != b->tm_sec)
3686 return a->tm_sec < b->tm_sec ? -1 : 1;
3792time_s_mkutc(
int argc,
VALUE *argv,
VALUE klass)
3796 time_arg(argc, argv, &
vtm);
3797 return time_gmtime(time_new_timew(klass, timegmw(&
vtm)));
3818time_s_mktime(
int argc,
VALUE *argv,
VALUE klass)
3822 time_arg(argc, argv, &
vtm);
3823 return time_localtime(time_new_timew(klass, timelocalw(&
vtm)));
3843time_to_i(
VALUE time)
3847 GetTimeval(time, tobj);
3848 return w2v(wdiv(tobj->timew, WINT2FIXWV(TIME_SCALE)));
3871time_to_f(
VALUE time)
3875 GetTimeval(time, tobj);
3876 return rb_Float(rb_time_unmagnify_to_float(tobj->timew));
3892time_to_r(
VALUE time)
3897 GetTimeval(time, tobj);
3898 v = rb_time_unmagnify_to_rational(tobj->timew);
3920time_usec(
VALUE time)
3925 GetTimeval(time, tobj);
3927 w = wmod(tobj->timew, WINT2WV(TIME_SCALE));
3928 wmuldivmod(w, WINT2FIXWV(1000000), WINT2FIXWV(TIME_SCALE), &q, &r);
3947time_nsec(
VALUE time)
3951 GetTimeval(time, tobj);
3952 return rb_to_int(w2v(wmulquoll(wmod(tobj->timew, WINT2WV(TIME_SCALE)), 1000000000, TIME_SCALE)));
3973time_subsec(
VALUE time)
3977 GetTimeval(time, tobj);
3978 return quov(w2v(wmod(tobj->timew, WINT2FIXWV(TIME_SCALE))),
INT2FIX(TIME_SCALE));
4015 GetTimeval(time1, tobj1);
4016 if (IsTimeval(time2)) {
4017 GetTimeval(time2, tobj2);
4018 n = wcmp(tobj1->timew, tobj2->timew);
4021 return rb_invcmp(time1, time2);
4023 if (n == 0)
return INT2FIX(0);
4041 GetTimeval(time1, tobj1);
4042 if (IsTimeval(time2)) {
4043 GetTimeval(time2, tobj2);
4044 return rb_equal(w2v(tobj1->timew), w2v(tobj2->timew));
4077time_utc_p(
VALUE time)
4081 GetTimeval(time, tobj);
4082 return RBOOL(TZMODE_UTC_P(tobj));
4095time_hash(
VALUE time)
4099 GetTimeval(time, tobj);
4100 return rb_hash(w2v(tobj->timew));
4110 GetTimeval(time, tobj);
4111 GetNewTimeval(copy, tcopy);
4113 time_set_timew(copy, tcopy, tobj->timew);
4114 time_set_vtm(copy, tcopy, tobj->vtm);
4123 time_init_copy(dup, time);
4128time_localtime(
VALUE time)
4134 GetTimeval(time, tobj);
4135 if (TZMODE_LOCALTIME_P(tobj)) {
4136 if (tobj->vtm.tm_got)
4143 zone = tobj->vtm.zone;
4144 if (maybe_tzobj_p(zone) && zone_localtime(zone, time)) {
4148 if (!localtimew(tobj->timew, &
vtm))
4149 rb_raise(rb_eArgError,
"localtime error");
4150 time_set_vtm(time, tobj,
vtm);
4152 tobj->vtm.tm_got = 1;
4153 TZMODE_SET_LOCALTIME(tobj);
4161 if (maybe_tzobj_p(zone) && zone_localtime(zone, time))
return time;
4165 if (
NIL_P(zone = find_timezone(time,
off))) invalid_utc_offset(
off);
4166 if (!zone_localtime(zone, time)) invalid_utc_offset(
off);
4169 else if (
off == UTC_ZONE) {
4170 return time_gmtime(time);
4172 validate_utc_offset(
off);
4174 time_set_utc_offset(time,
off);
4175 return time_fixoff(time);
4203time_localtime_m(
int argc,
VALUE *argv,
VALUE time)
4208 return time_zonelocal(time,
off);
4211 return time_localtime(time);
4229time_gmtime(
VALUE time)
4234 GetTimeval(time, tobj);
4235 if (TZMODE_UTC_P(tobj)) {
4236 if (tobj->vtm.tm_got)
4244 GMTIMEW(tobj->timew, &
vtm);
4245 time_set_vtm(time, tobj,
vtm);
4247 tobj->vtm.tm_got = 1;
4248 TZMODE_SET_UTC(tobj);
4253time_fixoff(
VALUE time)
4259 GetTimeval(time, tobj);
4260 if (TZMODE_FIXOFF_P(tobj)) {
4261 if (tobj->vtm.tm_got)
4268 if (TZMODE_FIXOFF_P(tobj))
4269 off = tobj->vtm.utc_offset;
4273 GMTIMEW(tobj->timew, &
vtm);
4275 zone = tobj->vtm.zone;
4276 vtm_add_offset(&
vtm,
off, +1);
4278 time_set_vtm(time, tobj,
vtm);
4279 RB_OBJ_WRITE_UNALIGNED(time, &tobj->vtm.zone, zone);
4281 tobj->vtm.tm_got = 1;
4282 TZMODE_SET_FIXOFF(time, tobj,
off);
4304time_getlocaltime(
int argc,
VALUE *argv,
VALUE time)
4310 if (maybe_tzobj_p(zone)) {
4311 VALUE t = time_dup(time);
4312 if (zone_localtime(
off, t))
return t;
4317 if (
NIL_P(zone = find_timezone(time,
off))) invalid_utc_offset(
off);
4318 time = time_dup(time);
4319 if (!zone_localtime(zone, time)) invalid_utc_offset(
off);
4322 else if (
off == UTC_ZONE) {
4323 return time_gmtime(time_dup(time));
4325 validate_utc_offset(
off);
4327 time = time_dup(time);
4328 time_set_utc_offset(time,
off);
4329 return time_fixoff(time);
4332 return time_localtime(time_dup(time));
4351time_getgmtime(
VALUE time)
4353 return time_gmtime(time_dup(time));
4359 if (TZMODE_UTC_P(tobj))
return time_gmtime(time);
4360 if (TZMODE_FIXOFF_P(tobj))
return time_fixoff(time);
4361 return time_localtime(time);
4365#define strftimev(fmt, time, enc) strftime_cstr((fmt), rb_strlen_lit(fmt), (time), (enc))
4389time_asctime(
VALUE time)
4391 return strftimev(
"%a %b %e %T %Y", time, rb_usascii_encoding());
4411time_to_s(
VALUE time)
4415 GetTimeval(time, tobj);
4416 if (TZMODE_UTC_P(tobj))
4417 return strftimev(
"%Y-%m-%d %H:%M:%S UTC", time, rb_usascii_encoding());
4419 return strftimev(
"%Y-%m-%d %H:%M:%S %z", time, rb_usascii_encoding());
4439time_inspect(
VALUE time)
4444 GetTimeval(time, tobj);
4445 str = strftimev(
"%Y-%m-%d %H:%M:%S", time, rb_usascii_encoding());
4446 subsec = w2v(wmod(tobj->timew, WINT2FIXWV(TIME_SCALE)));
4451 rb_str_catf(str,
".%09ld",
FIX2LONG(subsec));
4452 for (
len=RSTRING_LEN(str); RSTRING_PTR(str)[
len-1] ==
'0' &&
len > 0;
len--)
4454 rb_str_resize(str,
len);
4458 subsec = quov(subsec,
INT2FIX(TIME_SCALE));
4461 if (TZMODE_UTC_P(tobj)) {
4467 char sign = (
off < 0) ? (
off = -
off,
'-') :
'+';
4469 int min = (
off /= 60) % 60;
4471 rb_str_catf(str,
" %c%.2d%.2d", sign, (
int)
off, min);
4472 if (sec) rb_str_catf(str,
"%.2d", sec);
4483 offset = num_exact(offset);
4485 result = time_new_timew(klass, wsub(tobj->timew, rb_time_magnify(v2w(offset))));
4487 result = time_new_timew(klass, wadd(tobj->timew, rb_time_magnify(v2w(offset))));
4488 GetTimeval(result, result_tobj);
4489 TZMODE_COPY(result_tobj, tobj);
4497 return time_add0(
rb_cTime, tobj, torig, offset, sign);
4518 GetTimeval(time1, tobj);
4520 if (IsTimeval(time2)) {
4523 return time_add(tobj, time1, time2, 1);
4553 GetTimeval(time1, tobj);
4554 if (IsTimeval(time2)) {
4557 GetTimeval(time2, tobj2);
4558 return rb_Float(rb_time_unmagnify_to_float(wsub(tobj->timew, tobj2->timew)));
4560 return time_add(tobj, time1, time2, -1);
4564ndigits_denominator(
VALUE ndigits)
4569 rb_raise(rb_eArgError,
"negative ndigits given");
4609 VALUE ndigits, v, den;
4615 den = ndigits_denominator(ndigits);
4617 GetTimeval(time, tobj);
4618 v = w2v(rb_time_unmagnify(tobj->timew));
4621 if (lt(v, quov(den,
INT2FIX(2))))
4622 return time_add(tobj, time, v, -1);
4624 return time_add(tobj, time, subv(den, v), 1);
4657 VALUE ndigits, v, den;
4663 den = ndigits_denominator(ndigits);
4665 GetTimeval(time, tobj);
4666 v = w2v(rb_time_unmagnify(tobj->timew));
4669 return time_add(tobj, time, v, -1);
4702 VALUE ndigits, v, den;
4708 den = ndigits_denominator(ndigits);
4710 GetTimeval(time, tobj);
4711 v = w2v(rb_time_unmagnify(tobj->timew));
4717 return time_add(tobj, time, v, 1);
4742 GetTimeval(time, tobj);
4743 MAKE_TM(time, tobj);
4744 return INT2FIX(tobj->vtm.sec);
4766 GetTimeval(time, tobj);
4767 MAKE_TM(time, tobj);
4768 return INT2FIX(tobj->vtm.min);
4786time_hour(
VALUE time)
4790 GetTimeval(time, tobj);
4791 MAKE_TM(time, tobj);
4792 return INT2FIX(tobj->vtm.hour);
4810time_mday(
VALUE time)
4814 GetTimeval(time, tobj);
4815 MAKE_TM(time, tobj);
4816 return INT2FIX(tobj->vtm.mday);
4838 GetTimeval(time, tobj);
4839 MAKE_TM(time, tobj);
4840 return INT2FIX(tobj->vtm.mon);
4857time_year(
VALUE time)
4861 GetTimeval(time, tobj);
4862 MAKE_TM(time, tobj);
4863 return tobj->vtm.year;
4882time_wday(
VALUE time)
4886 GetTimeval(time, tobj);
4887 MAKE_TM_ENSURE(time, tobj, tobj->vtm.wday != VTM_WDAY_INITVAL);
4888 return INT2FIX((
int)tobj->vtm.wday);
4892 return RBOOL(time_wday(time) == INT2FIX(n)); \
4908time_sunday(
VALUE time)
4926time_monday(
VALUE time)
4944time_tuesday(
VALUE time)
4962time_wednesday(
VALUE time)
4980time_thursday(
VALUE time)
4998time_friday(
VALUE time)
5016time_saturday(
VALUE time)
5032time_yday(
VALUE time)
5036 GetTimeval(time, tobj);
5037 MAKE_TM_ENSURE(time, tobj, tobj->vtm.yday != 0);
5038 return INT2FIX(tobj->vtm.yday);
5057time_isdst(
VALUE time)
5061 GetTimeval(time, tobj);
5062 MAKE_TM(time, tobj);
5063 if (tobj->vtm.isdst == VTM_ISDST_INITVAL) {
5066 return RBOOL(tobj->vtm.isdst);
5080time_zone(
VALUE time)
5085 GetTimeval(time, tobj);
5086 MAKE_TM(time, tobj);
5088 if (TZMODE_UTC_P(tobj)) {
5091 zone = tobj->vtm.zone;
5116 GetTimeval(time, tobj);
5118 if (TZMODE_UTC_P(tobj)) {
5122 MAKE_TM(time, tobj);
5123 return tobj->vtm.utc_offset;
5143time_to_a(
VALUE time)
5147 GetTimeval(time, tobj);
5148 MAKE_TM_ENSURE(time, tobj, tobj->vtm.yday != 0);
5158 RBOOL(tobj->vtm.isdst),
5204 GetTimeval(time, tobj);
5205 MAKE_TM_ENSURE(time, tobj, tobj->vtm.yday != 0);
5208 h = rb_hash_new_capa(11);
5210 rb_hash_aset(h, sym_year, tobj->vtm.year);
5211 rb_hash_aset(h, sym_month,
INT2FIX(tobj->vtm.mon));
5212 rb_hash_aset(h, sym_day,
INT2FIX(tobj->vtm.mday));
5213 rb_hash_aset(h, sym_yday,
INT2FIX(tobj->vtm.yday));
5214 rb_hash_aset(h, sym_wday,
INT2FIX(tobj->vtm.wday));
5215 rb_hash_aset(h, sym_hour,
INT2FIX(tobj->vtm.hour));
5216 rb_hash_aset(h, sym_min,
INT2FIX(tobj->vtm.min));
5217 rb_hash_aset(h, sym_sec,
INT2FIX(tobj->vtm.sec));
5218 rb_hash_aset(h, sym_subsec,
5219 quov(w2v(wmod(tobj->timew, WINT2FIXWV(TIME_SCALE))),
INT2FIX(TIME_SCALE)));
5220 rb_hash_aset(h, sym_dst, RBOOL(tobj->vtm.isdst));
5221 rb_hash_aset(h, sym_zone, time_zone(time));
5227 "wrong argument type %"PRIsVALUE
" (expected Array or nil)",
5237 if (sym_year == key) rb_hash_aset(h, key, tobj->vtm.year);
5238 if (sym_month == key) rb_hash_aset(h, key,
INT2FIX(tobj->vtm.mon));
5239 if (sym_day == key) rb_hash_aset(h, key,
INT2FIX(tobj->vtm.mday));
5240 if (sym_yday == key) rb_hash_aset(h, key,
INT2FIX(tobj->vtm.yday));
5241 if (sym_wday == key) rb_hash_aset(h, key,
INT2FIX(tobj->vtm.wday));
5242 if (sym_hour == key) rb_hash_aset(h, key,
INT2FIX(tobj->vtm.hour));
5243 if (sym_min == key) rb_hash_aset(h, key,
INT2FIX(tobj->vtm.min));
5244 if (sym_sec == key) rb_hash_aset(h, key,
INT2FIX(tobj->vtm.sec));
5245 if (sym_subsec == key) {
5246 rb_hash_aset(h, key, quov(w2v(wmod(tobj->timew, WINT2FIXWV(TIME_SCALE))),
INT2FIX(TIME_SCALE)));
5248 if (sym_dst == key) rb_hash_aset(h, key, RBOOL(tobj->vtm.isdst));
5249 if (sym_zone == key) rb_hash_aset(h, key, time_zone(time));
5255rb_strftime_alloc(
const char *format,
size_t format_len,
rb_encoding *enc,
5261 if (!timew2timespec_exact(timew, &ts))
5262 timev = w2v(rb_time_unmagnify(timew));
5265 return rb_strftime_timespec(format, format_len, enc, time,
vtm, &ts, gmt);
5268 return rb_strftime(format, format_len, enc, time,
vtm, timev, gmt);
5278 GetTimeval(time, tobj);
5279 MAKE_TM(time, tobj);
5280 str = rb_strftime_alloc(fmt,
len, enc, time, &tobj->vtm, tobj->timew, TZMODE_UTC_P(tobj));
5281 if (!str) rb_raise(rb_eArgError,
"invalid format: %s", fmt);
5303 GetTimeval(time, tobj);
5304 MAKE_TM_ENSURE(time, tobj, tobj->vtm.yday != 0);
5307 rb_raise(rb_eArgError,
"format should have ASCII compatible encoding");
5309 tmp = rb_str_tmp_frozen_acquire(format);
5310 fmt = RSTRING_PTR(tmp);
5311 len = RSTRING_LEN(tmp);
5312 enc = rb_enc_get(format);
5314 rb_warning(
"strftime called with empty format string");
5315 return rb_enc_str_new(0, 0, enc);
5318 VALUE str = rb_strftime_alloc(fmt,
len, enc, time, &tobj->vtm, tobj->timew,
5319 TZMODE_UTC_P(tobj));
5320 rb_str_tmp_frozen_release(format, tmp);
5321 if (!str) rb_raise(rb_eArgError,
"invalid format: %"PRIsVALUE, format);
5348time_xmlschema(
int argc,
VALUE *argv,
VALUE time)
5350 long fraction_digits = 0;
5353 fraction_digits =
NUM2LONG(argv[0]);
5354 if (fraction_digits < 0) {
5355 fraction_digits = 0;
5361 GetTimeval(time, tobj);
5362 MAKE_TM(time, tobj);
5364 const long size_after_year =
sizeof(
"-MM-DDTHH:MM:SS+ZH:ZM") + fraction_digits
5365 + (fraction_digits > 0);
5369# define fill_digits_long(len, prec, n) \
5370 for (int fill_it = 1, written = snprintf(ptr, len, "%0*ld", prec, n); \
5371 fill_it; ptr += written, fill_it = 0)
5374 long year =
FIX2LONG(tobj->vtm.year);
5376 int w = (year >= -9999 && year <= 9999 ? year_width : (year < 0) + (
int)
DECIMAL_SIZE_OF(year));
5378 ptr = RSTRING_PTR(str);
5379 fill_digits_long(w + 1, year_width, year) {
5380 if (year >= -9999 && year <= 9999) {
5390 str = rb_int2str(tobj->vtm.year, 10);
5395# define fill_2(c, n) (*ptr++ = c, *ptr++ = '0' + (n) / 10, *ptr++ = '0' + (n) % 10)
5396 fill_2(
'-', tobj->vtm.mon);
5397 fill_2(
'-', tobj->vtm.mday);
5398 fill_2(
'T', tobj->vtm.hour);
5399 fill_2(
':', tobj->vtm.min);
5400 fill_2(
':', tobj->vtm.sec);
5402 if (fraction_digits > 0) {
5403 VALUE subsecx = tobj->vtm.subsecx;
5407 if (fraction_digits <= TIME_SCALE_NUMDIGITS) {
5408 digits = TIME_SCALE_NUMDIGITS - (int)fraction_digits;
5411 long w = fraction_digits - TIME_SCALE_NUMDIGITS;
5418 if (digits >= 0 && fraction_digits < INT_MAX) {
5420 if (digits > 0) subsec /= (long)pow(10, digits);
5421 fill_digits_long(fraction_digits + 1, (
int)fraction_digits, subsec) {
5426 subsecx = rb_int2str(subsecx, 10);
5427 long len = RSTRING_LEN(subsecx);
5428 if (fraction_digits >
len) {
5429 memset(ptr,
'0', fraction_digits -
len);
5432 len = fraction_digits;
5434 ptr += fraction_digits;
5435 memcpy(ptr -
len, RSTRING_PTR(subsecx),
len);
5439 if (TZMODE_UTC_P(tobj)) {
5445 char sign = offset < 0 ?
'-' :
'+';
5446 if (offset < 0) offset = -offset;
5448 fill_2(sign, offset / 60);
5449 fill_2(
':', offset % 60);
5451 const char *
const start = RSTRING_PTR(str);
5456int ruby_marshal_write_long(
long x,
char *buf);
5458enum {base_dump_size = 8};
5462time_mdump(
VALUE time)
5466 char buf[base_dump_size +
sizeof(long) + 1];
5473 VALUE subsecx, nano, subnano, v, zone;
5476 const int max_year = 1900+0xffff;
5478 GetTimeval(time, tobj);
5480 gmtimew(tobj->timew, &
vtm);
5484 if (year > max_year) {
5485 year_extend =
INT2FIX(year - max_year);
5488 else if (year < 1900) {
5489 year_extend =
LONG2NUM(1900 - year);
5494 if (rb_int_positive_p(
vtm.year)) {
5495 year_extend = rb_int_minus(
vtm.year,
INT2FIX(max_year));
5499 year_extend = rb_int_minus(
INT2FIX(1900),
vtm.year);
5504 subsecx =
vtm.subsecx;
5506 nano = mulquov(subsecx,
INT2FIX(1000000000),
INT2FIX(TIME_SCALE));
5507 divmodv(nano,
INT2FIX(1), &v, &subnano);
5512 nano = addv(
LONG2FIX(nsec), subnano);
5515 TZMODE_UTC_P(tobj) << 30 |
5520 s = (
unsigned long)
vtm.min << 26 |
5524 for (i=0; i<4; i++) {
5525 buf[i] = (
unsigned char)p;
5528 for (i=4; i<8; i++) {
5529 buf[i] = (
unsigned char)s;
5533 if (!
NIL_P(year_extend)) {
5540 size_t ysize = rb_absint_size(year_extend, NULL);
5541 char *p, *
const buf_year_extend = buf + base_dump_size;
5542 if (ysize > LONG_MAX ||
5543 (i = ruby_marshal_write_long((
long)ysize, buf_year_extend)) < 0) {
5544 rb_raise(rb_eArgError,
"year too %s to marshal: %"PRIsVALUE
" UTC",
5545 (year == 1900 ?
"small" :
"big"),
vtm.year);
5547 i += base_dump_size;
5549 p = RSTRING_PTR(str);
5560 rb_ivar_set(str, id_nano_num, RRATIONAL(nano)->num);
5561 rb_ivar_set(str, id_nano_den, RRATIONAL(nano)->den);
5577 int len = (int)
sizeof(buf);
5578 buf[1] = (char)((nsec % 10) << 4);
5580 buf[0] = (char)(nsec % 10);
5582 buf[0] |= (char)((nsec % 10) << 4);
5587 if (!TZMODE_UTC_P(tobj)) {
5594 zone = tobj->vtm.zone;
5595 if (maybe_tzobj_p(zone)) {
5609 str = time_mdump(time);
5615mload_findzone(
VALUE arg)
5618 VALUE time = argp[0], zone = argp[1];
5619 return find_timezone(time, zone);
5629 if (
NIL_P(z))
return rb_fstring(zone);
5634long ruby_marshal_read_long(
const char **buf,
long len);
5648 VALUE submicro, nano_num, nano_den, offset, zone, year;
5653#define get_attr(attr, iffound) \
5654 attr = rb_attr_delete(str, id_##attr); \
5655 if (!NIL_P(attr)) { \
5659 get_attr(nano_num, {});
5660 get_attr(nano_den, {});
5661 get_attr(submicro, {});
5662 get_attr(offset, (offset =
rb_rescue(validate_utc_offset, offset, 0,
Qnil)));
5663 get_attr(zone, (zone =
rb_rescue(validate_zone_name, zone, 0,
Qnil)));
5671 buf = (
unsigned char *)RSTRING_PTR(str);
5672 if (RSTRING_LEN(str) < base_dump_size) {
5673 goto invalid_format;
5677 for (i=0; i<4; i++) {
5678 p |= (
unsigned long)buf[i]<<(8*i);
5680 for (i=4; i<8; i++) {
5681 s |= (
unsigned long)buf[i]<<(8*(i-4));
5684 if ((p & (1UL<<31)) == 0) {
5690 timew = wadd(rb_time_magnify(TIMET2WV(sec)), wmulquoll(WINT2FIXWV(usec), TIME_SCALE, 1000000));
5694 gmt = (int)((p >> 30) & 0x1);
5697 year =
INT2FIX(((
int)(p >> 14) & 0xffff) + 1900);
5699 if (RSTRING_LEN(str) > base_dump_size) {
5700 long len = RSTRING_LEN(str) - base_dump_size;
5703 const char *ybuf = (
const char *)(buf += base_dump_size);
5704 ysize = ruby_marshal_read_long(&ybuf,
len);
5705 len -= ybuf - (
const char *)buf;
5706 if (ysize < 0 || ysize >
len)
goto invalid_format;
5709 year = rb_int_minus(year, year_extend);
5712 year = rb_int_plus(year, year_extend);
5715 unsigned int mon = ((int)(p >> 10) & 0xf);
5722 vtm.mday = (int)(p >> 5) & 0x1f;
5723 vtm.hour = (int) p & 0x1f;
5724 vtm.min = (int)(s >> 26) & 0x3f;
5725 vtm.sec = (int)(s >> 20) & 0x3f;
5729 vtm.zone = str_empty;
5731 usec = (long)(s & 0xfffff);
5736 if (nano_num !=
Qnil) {
5737 VALUE nano = quov(num_exact(nano_num), num_exact(nano_den));
5740 else if (submicro !=
Qnil) {
5745 len = RSTRING_LEN(submicro);
5748 if (10 <= (digit = ptr[0] >> 4))
goto end_submicro;
5749 nsec += digit * 100;
5750 if (10 <= (digit = ptr[0] & 0xf))
goto end_submicro;
5754 if (10 <= (digit = ptr[1] >> 4))
goto end_submicro;
5760 timew = timegmw(&
vtm);
5763 GetNewTimeval(time, tobj);
5764 TZMODE_SET_LOCALTIME(tobj);
5765 tobj->vtm.tm_got = 0;
5766 time_set_timew(time, tobj, timew);
5769 TZMODE_SET_UTC(tobj);
5771 else if (!
NIL_P(offset)) {
5772 time_set_utc_offset(time, offset);
5776 zone = mload_zone(time, zone);
5778 zone_localtime(zone, time);
5792 VALUE time = time_s_alloc(klass);
5794 time_mload(time, str);
5819 GetTimeval(time, tobj);
5820 tm = time_s_alloc(klass);
5821 ttm = RTYPEDDATA_GET_DATA(tm);
5826 time_set_timew(tm, ttm, wsub(timew, v2w(v->subsecx)));
5829 time_set_vtm(tm, ttm, *v);
5831 ttm->vtm.tm_got = 1;
5832 TZMODE_SET_UTC(ttm);
5844tm_initialize(
int argc,
VALUE *argv,
VALUE time)
5850 time_arg(argc, argv, &
vtm);
5852 struct time_object *tobj = RTYPEDDATA_GET_DATA(time);
5853 TZMODE_SET_UTC(tobj);
5854 time_set_timew(time, tobj, t);
5855 time_set_vtm(time, tobj,
vtm);
5871 struct time_object *tobj = RTYPEDDATA_GET_DATA(dup);
5879 return time_add0(
rb_obj_class(tm), get_timeval(tm), tm, offset, +1);
5885 return time_add0(
rb_obj_class(tm), get_timeval(tm), tm, offset, -1);
5889Init_tm(
VALUE outer,
const char *name)
5893 tm = rb_define_class_under(outer, name,
rb_cObject);
5935rb_time_zone_abbreviation(
VALUE zone,
VALUE time)
5937 VALUE tm, abbr, strftime_args[2];
5940 if (!
NIL_P(abbr))
return abbr;
5942 tm = tm_from_time(rb_cTimeTM, time);
5944 if (!UNDEF_P(abbr)) {
5947#ifdef SUPPORT_TZINFO_ZONE_ABBREVIATION
5949 if (!UNDEF_P(abbr)) {
5950 abbr =
rb_funcallv(abbr, rb_intern(
"abbreviation"), 0, 0);
5954 strftime_args[0] = rb_fstring_lit(
"%Z");
5955 strftime_args[1] = tm;
5957 if (!UNDEF_P(abbr)) {
5960 abbr = rb_check_funcall_default(zone, idName, 0, 0,
Qnil);
5970 ruby_reset_timezone(getenv(
"TZ"));
6006 str_utc = rb_fstring_lit(
"UTC");
6007 rb_vm_register_global_object(str_utc);
6008 str_empty = rb_fstring_lit(
"");
6009 rb_vm_register_global_object(str_empty);
6093 if (debug_find_time_numguess) {
6095 find_time_numguess_getter, 0);
6098 rb_cTimeTM = Init_tm(
rb_cTime,
"tm");
6101#include "timev.rbinc"
#define RUBY_ASSERT(...)
Asserts that the given expression is truthy if and only if RUBY_DEBUG is truthy.
#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_singleton_class(VALUE obj)
Finds or creates the singleton class of the passed object.
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 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_exc_raise(VALUE mesg)
Raises an exception in the current thread.
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.
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_cObject
Object 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.
#define RB_OBJ_WRITTEN(old, oldv, young)
Identical to RB_OBJ_WRITE(), except it doesn't write any values, but only a WB declaration.
#define RB_OBJ_WRITE(old, slot, young)
Declaration of a "back" pointer.
static bool rb_enc_str_asciicompat_p(VALUE str)
Queries if the passed string is in an ASCII-compatible encoding.
VALUE rb_enc_interned_str(const char *ptr, long len, rb_encoding *enc)
Identical to rb_enc_str_new(), except it returns a "f"string.
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.
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.
#define INTEGER_PACK_NATIVE_BYTE_ORDER
Means either INTEGER_PACK_MSBYTE_FIRST or INTEGER_PACK_LSBYTE_FIRST, depending on the host processor'...
#define RB_INT_PARSE_SIGN
Allows a leading sign (+ or -).
#define INTEGER_PACK_LITTLE_ENDIAN
Little endian combination.
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.
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_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.
#define rb_utf8_str_new(str, len)
Identical to rb_str_new, except it generates a string of "UTF-8" encoding.
void rb_str_modify_expand(VALUE str, long capa)
Identical to rb_str_modify(), except it additionally expands the capacity of the receiver.
#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_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().
int off
Offset inside of ptr.
int len
Length of the buffer.
#define DECIMAL_SIZE_OF(expr)
An approximation of decimal representation size.
#define rb_long2int
Just another name of rb_long2int_inline.
#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 StringValue(v)
Ensures that the parameter object is a String.
#define StringValuePtr(v)
Identical to StringValue, except it returns a char*.
VALUE rb_str_export_locale(VALUE obj)
Identical to rb_str_export(), except it converts into the locale encoding instead.
static char * RSTRING_END(VALUE str)
Queries the end of 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.
const char * wrap_struct_name
Name of structs of this kind.
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.