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
65#define NDIV(x,y) (-(-((x)+1)/(y))-1)
66#define NMOD(x,y) ((y)-(-((x)+1)%(y))-1)
67#define DIV(n,d) ((n)<0 ? NDIV((n),(d)) : (n)/(d))
68#define MOD(n,d) ((n)<0 ? NMOD((n),(d)) : (n)%(d))
69#define VTM_WDAY_INITVAL (7)
70#define VTM_ISDST_INITVAL (3)
85 if ((
long)x < (
long)y)
87 if ((
long)x > (
long)y)
91 if (RB_BIGNUM_TYPE_P(x))
return FIX2INT(rb_big_cmp(x, y));
92 return rb_cmpint(
rb_funcall(x, idCmp, 1, y), x, y);
95#define ne(x,y) (!eq((x),(y)))
96#define lt(x,y) (cmp((x),(y)) < 0)
97#define gt(x,y) (cmp((x),(y)) > 0)
98#define le(x,y) (cmp((x),(y)) <= 0)
99#define ge(x,y) (cmp((x),(y)) >= 0)
107 if (RB_BIGNUM_TYPE_P(x))
return rb_big_plus(x, y);
117 if (RB_BIGNUM_TYPE_P(x))
return rb_big_minus(x, y);
125 return rb_fix_mul_fix(x, y);
127 if (RB_BIGNUM_TYPE_P(x))
128 return rb_big_mul(x, y);
136 return rb_fix_div_fix(x, y);
138 if (RB_BIGNUM_TYPE_P(x))
139 return rb_big_div(x, y);
148 if (
FIXNUM_P(x))
return rb_fix_mod_fix(x, y);
150 if (RB_BIGNUM_TYPE_P(x))
return rb_big_modulo(x, y);
154#define neg(x) (subv(INT2FIX(0), (x)))
170 return rb_numeric_quo(x, y);
176 VALUE ret = quor(x, y);
178 RRATIONAL(ret)->den ==
INT2FIX(1)) {
179 ret = RRATIONAL(ret)->num;
184#define mulquov(x,y,z) (((y) == (z)) ? (x) : quov(mulv((x),(y)),(z)))
193 rb_fix_divmod_fix(n, d, q, r);
198 ary = rb_check_array_type(tmp);
200 rb_raise(
rb_eTypeError,
"unexpected divmod result: into %"PRIsVALUE,
203 *q = rb_ary_entry(ary, 0);
204 *r = rb_ary_entry(ary, 1);
208# define INT64toNUM(x) LONG2NUM(x)
209#elif defined(HAVE_LONG_LONG) && SIZEOF_LONG_LONG == 8
210# define INT64toNUM(x) LL2NUM(x)
213#if defined(HAVE_UINT64_T) && SIZEOF_LONG*2 <= SIZEOF_UINT64_T
214 typedef uint64_t uwideint_t;
215 typedef int64_t wideint_t;
216 typedef uint64_t WIDEVALUE;
217 typedef int64_t SIGNED_WIDEVALUE;
218# define WIDEVALUE_IS_WIDER 1
219# define UWIDEINT_MAX UINT64_MAX
220# define WIDEINT_MAX INT64_MAX
221# define WIDEINT_MIN INT64_MIN
222# define FIXWINT_P(tv) ((tv) & 1)
223# define FIXWVtoINT64(tv) RSHIFT((SIGNED_WIDEVALUE)(tv), 1)
224# define INT64toFIXWV(wi) ((WIDEVALUE)((SIGNED_WIDEVALUE)(wi) << 1 | FIXNUM_FLAG))
225# define FIXWV_MAX (((int64_t)1 << 62) - 1)
226# define FIXWV_MIN (-((int64_t)1 << 62))
227# define FIXWVABLE(wi) (POSFIXWVABLE(wi) && NEGFIXWVABLE(wi))
228# define WINT2FIXWV(i) WIDEVAL_WRAP(INT64toFIXWV(i))
229# define FIXWV2WINT(w) FIXWVtoINT64(WIDEVAL_GET(w))
231 typedef unsigned long uwideint_t;
232 typedef long wideint_t;
233 typedef VALUE WIDEVALUE;
235# define WIDEVALUE_IS_WIDER 0
236# define UWIDEINT_MAX ULONG_MAX
237# define WIDEINT_MAX LONG_MAX
238# define WIDEINT_MIN LONG_MIN
239# define FIXWINT_P(v) FIXNUM_P(v)
240# define FIXWV_MAX FIXNUM_MAX
241# define FIXWV_MIN FIXNUM_MIN
242# define FIXWVABLE(i) FIXABLE(i)
243# define WINT2FIXWV(i) WIDEVAL_WRAP(LONG2FIX(i))
244# define FIXWV2WINT(w) FIX2LONG(WIDEVAL_GET(w))
247#define POSFIXWVABLE(wi) ((wi) < FIXWV_MAX+1)
248#define NEGFIXWVABLE(wi) ((wi) >= FIXWV_MIN)
249#define FIXWV_P(w) FIXWINT_P(WIDEVAL_GET(w))
250#define MUL_OVERFLOW_FIXWV_P(a, b) MUL_OVERFLOW_SIGNED_INTEGER_P(a, b, FIXWV_MIN, FIXWV_MAX)
258 static inline wideval_t WIDEVAL_WRAP(WIDEVALUE v) { wideval_t w = { v };
return w; }
259# define WIDEVAL_GET(w) ((w).value)
261 typedef WIDEVALUE wideval_t;
262# define WIDEVAL_WRAP(v) (v)
263# define WIDEVAL_GET(w) (w)
266#if WIDEVALUE_IS_WIDER
267 static inline wideval_t
268 wint2wv(wideint_t wi)
271 return WINT2FIXWV(wi);
273 return WIDEVAL_WRAP(INT64toNUM(wi));
275# define WINT2WV(wi) wint2wv(wi)
277# define WINT2WV(wi) WIDEVAL_WRAP(LONG2NUM(wi))
283#if WIDEVALUE_IS_WIDER
285 return INT64toNUM(FIXWV2WINT(w));
286 return (
VALUE)WIDEVAL_GET(w);
288 return WIDEVAL_GET(w);
292#if WIDEVALUE_IS_WIDER
298 sign = rb_integer_pack(v, &u, 1,
sizeof(u), 0,
301 return WINT2FIXWV(0);
302 else if (sign == -1) {
304 return WINT2FIXWV(-(wideint_t)u);
306 else if (sign == +1) {
308 return WINT2FIXWV((wideint_t)u);
310 return WIDEVAL_WRAP(v);
314static inline wideval_t
318 if (RRATIONAL(v)->den !=
LONG2FIX(1))
319 return WIDEVAL_WRAP(v);
320 v = RRATIONAL(v)->num;
322#if WIDEVALUE_IS_WIDER
324 return WIDEVAL_WRAP((WIDEVALUE)(SIGNED_WIDEVALUE)(
long)v);
326 else if (RB_BIGNUM_TYPE_P(v) &&
327 rb_absint_size(v, NULL) <=
sizeof(WIDEVALUE)) {
328 return v2w_bignum(v);
331 return WIDEVAL_WRAP(v);
335weq(wideval_t wx, wideval_t wy)
337#if WIDEVALUE_IS_WIDER
338 if (FIXWV_P(wx) && FIXWV_P(wy)) {
339 return WIDEVAL_GET(wx) == WIDEVAL_GET(wy);
343 return eq(WIDEVAL_GET(wx), WIDEVAL_GET(wy));
348wcmp(wideval_t wx, wideval_t wy)
351#if WIDEVALUE_IS_WIDER
352 if (FIXWV_P(wx) && FIXWV_P(wy)) {
368#define wne(x,y) (!weq((x),(y)))
369#define wlt(x,y) (wcmp((x),(y)) < 0)
370#define wgt(x,y) (wcmp((x),(y)) > 0)
371#define wle(x,y) (wcmp((x),(y)) <= 0)
372#define wge(x,y) (wcmp((x),(y)) >= 0)
375wadd(wideval_t wx, wideval_t wy)
377#if WIDEVALUE_IS_WIDER
378 if (FIXWV_P(wx) && FIXWV_P(wy)) {
379 wideint_t r = FIXWV2WINT(wx) + FIXWV2WINT(wy);
383 return v2w(addv(w2v(wx), w2v(wy)));
387wsub(wideval_t wx, wideval_t wy)
389#if WIDEVALUE_IS_WIDER
390 if (FIXWV_P(wx) && FIXWV_P(wy)) {
391 wideint_t r = FIXWV2WINT(wx) - FIXWV2WINT(wy);
395 return v2w(subv(w2v(wx), w2v(wy)));
399wmul(wideval_t wx, wideval_t wy)
401#if WIDEVALUE_IS_WIDER
402 if (FIXWV_P(wx) && FIXWV_P(wy)) {
403 if (!MUL_OVERFLOW_FIXWV_P(FIXWV2WINT(wx), FIXWV2WINT(wy)))
404 return WINT2WV(FIXWV2WINT(wx) * FIXWV2WINT(wy));
407 return v2w(mulv(w2v(wx), w2v(wy)));
411wquo(wideval_t wx, wideval_t wy)
413#if WIDEVALUE_IS_WIDER
414 if (FIXWV_P(wx) && FIXWV_P(wy)) {
425 return v2w(quov(w2v(wx), w2v(wy)));
428#define wmulquo(x,y,z) ((WIDEVAL_GET(y) == WIDEVAL_GET(z)) ? (x) : wquo(wmul((x),(y)),(z)))
429#define wmulquoll(x,y,z) (((y) == (z)) ? (x) : wquo(wmul((x),WINT2WV(y)),WINT2WV(z)))
431#if WIDEVALUE_IS_WIDER
433wdivmod0(wideval_t wn, wideval_t wd, wideval_t *wq, wideval_t *wr)
435 if (FIXWV_P(wn) && FIXWV_P(wd)) {
436 wideint_t n, d, q, r;
445 wideint_t xneg = -FIXWV2WINT(wn);
458 if (d > 0 ? r < 0 : r > 0) {
471wdivmod(wideval_t wn, wideval_t wd, wideval_t *wq, wideval_t *wr)
474#if WIDEVALUE_IS_WIDER
475 if (wdivmod0(wn, wd, wq, wr))
return;
477 divmodv(w2v(wn), w2v(wd), &vq, &vr);
483wmuldivmod(wideval_t wx, wideval_t wy, wideval_t wz, wideval_t *wq, wideval_t *wr)
485 if (WIDEVAL_GET(wy) == WIDEVAL_GET(wz)) {
490 wdivmod(wmul(wx,wy), wz, wq, wr);
494wdiv(wideval_t wx, wideval_t wy)
496#if WIDEVALUE_IS_WIDER
498 if (wdivmod0(wx, wy, &q, &dmy))
return q;
500 return v2w(divv(w2v(wx), w2v(wy)));
504wmod(wideval_t wx, wideval_t wy)
506#if WIDEVALUE_IS_WIDER
508 if (wdivmod0(wx, wy, &dmy, &r))
return r;
510 return v2w(modv(w2v(wx), w2v(wy)));
514num_exact_check(
VALUE v)
525 tmp = rb_rational_canonicalize(v);
539 tmp = rb_rational_canonicalize(tmp);
555NORETURN(
static void num_exact_fail(
VALUE v));
557num_exact_fail(
VALUE v)
559 rb_raise(
rb_eTypeError,
"can't convert %"PRIsVALUE
" into an exact number",
566 VALUE num = num_exact_check(v);
567 if (
NIL_P(num)) num_exact_fail(v);
574rb_time_magnify(wideval_t w)
576 return wmul(w, WINT2FIXWV(TIME_SCALE));
580rb_time_unmagnify_to_rational(wideval_t w)
582 return quor(w2v(w),
INT2FIX(TIME_SCALE));
586rb_time_unmagnify(wideval_t w)
588 return v2w(rb_time_unmagnify_to_rational(w));
592rb_time_unmagnify_to_float(wideval_t w)
595#if WIDEVALUE_IS_WIDER
604 v =
DBL2NUM((
double)FIXWV2WINT(w));
605 return quov(v,
DBL2NUM(TIME_SCALE));
612 return quov(v,
DBL2NUM(TIME_SCALE));
616split_second(wideval_t timew, wideval_t *timew_p,
VALUE *subsecx_p)
619 wdivmod(timew, WINT2FIXWV(TIME_SCALE), &q, &r);
627#if WIDEVALUE_IS_WIDER
628 if (TIMET_MIN == 0) {
629 uwideint_t wi = (uwideint_t)t;
630 if (wi <= FIXWV_MAX) {
631 return WINT2FIXWV(wi);
635 wideint_t wi = (wideint_t)t;
636 if (FIXWV_MIN <= wi && wi <= FIXWV_MAX) {
637 return WINT2FIXWV(wi);
641 return v2w(TIMET2NUM(t));
643#define TIMET2WV(t) timet2wv(t)
648#if WIDEVALUE_IS_WIDER
650 wideint_t wi = FIXWV2WINT(w);
651 if (TIMET_MIN == 0) {
653 rb_raise(
rb_eRangeError,
"negative value to convert into `time_t'");
654 if (TIMET_MAX < (uwideint_t)wi)
658 if (wi < TIMET_MIN || TIMET_MAX < wi)
664 return NUM2TIMET(w2v(w));
666#define WV2TIMET(t) wv2timet(t)
669static VALUE rb_cTimeTM;
671static int obj2int(
VALUE obj);
672static uint32_t obj2ubits(
VALUE obj,
unsigned int bits);
674static uint32_t month_arg(
VALUE arg);
675static VALUE validate_utc_offset(
VALUE utc_offset);
676static VALUE validate_zone_name(
VALUE zone_name);
677static void validate_vtm(
struct vtm *
vtm);
678static void vtm_add_day(
struct vtm *
vtm,
int day);
679static uint32_t obj2subsecx(
VALUE obj,
VALUE *subsecx);
686static time_t timegm_noleapsecond(
struct tm *tm);
687static int tmcmp(
struct tm *a,
struct tm *b);
688static int vtmcmp(
struct vtm *a,
struct vtm *b);
689static const char *find_time_t(
struct tm *tptr,
int utc_p, time_t *tp);
691static struct vtm *localtimew(wideval_t timew,
struct vtm *result);
693static int leap_year_p(
long y);
694#define leap_year_v_p(y) leap_year_p(NUM2LONG(modv((y), INT2FIX(400))))
698bool ruby_tz_uptodate_p;
701ruby_reset_timezone(
void)
703 ruby_tz_uptodate_p =
false;
704 ruby_reset_leap_second_info();
710 if (ruby_tz_uptodate_p)
return;
711 ruby_tz_uptodate_p =
true;
716rb_localtime_r(
const time_t *t,
struct tm *result)
718#if defined __APPLE__ && defined __LP64__
719 if (*t != (time_t)(
int)*t)
return NULL;
723 result = localtime_r(t, result);
726 struct tm *tmp = localtime(t);
727 if (tmp) *result = *tmp;
730#if defined(HAVE_MKTIME) && defined(LOCALTIME_OVERFLOW_PROBLEM)
734 struct tm tmp = *result;
737# if defined(HAVE_STRUCT_TM_TM_GMTOFF)
738 gmtoff1 = result->tm_gmtoff;
739 gmtoff2 = tmp.tm_gmtoff;
741 if (*t + gmtoff1 != t2 + gmtoff2)
747#define LOCALTIME(tm, result) rb_localtime_r((tm), &(result))
749#ifndef HAVE_STRUCT_TM_TM_GMTOFF
751rb_gmtime_r(
const time_t *t,
struct tm *result)
754 result = gmtime_r(t, result);
756 struct tm *tmp = gmtime(t);
757 if (tmp) *result = *tmp;
759#if defined(HAVE_TIMEGM) && defined(LOCALTIME_OVERFLOW_PROBLEM)
760 if (result && *t != timegm(result)) {
766# define GMTIME(tm, result) rb_gmtime_r((tm), &(result))
769static const int16_t common_year_yday_offset[] = {
774 -1 + 31 + 28 + 31 + 30,
775 -1 + 31 + 28 + 31 + 30 + 31,
776 -1 + 31 + 28 + 31 + 30 + 31 + 30,
777 -1 + 31 + 28 + 31 + 30 + 31 + 30 + 31,
778 -1 + 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31,
779 -1 + 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30,
780 -1 + 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31,
781 -1 + 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30
784static const int16_t leap_year_yday_offset[] = {
789 -1 + 31 + 29 + 31 + 30,
790 -1 + 31 + 29 + 31 + 30 + 31,
791 -1 + 31 + 29 + 31 + 30 + 31 + 30,
792 -1 + 31 + 29 + 31 + 30 + 31 + 30 + 31,
793 -1 + 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31,
794 -1 + 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30,
795 -1 + 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31,
796 -1 + 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30
800static const int8_t common_year_days_in_month[] = {
801 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
803static const int8_t leap_year_days_in_month[] = {
804 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
807#define days_in_month_of(leap) ((leap) ? leap_year_days_in_month : common_year_days_in_month)
808#define days_in_month_in(y) days_in_month_of(leap_year_p(y))
809#define days_in_month_in_v(y) days_in_month_of(leap_year_v_p(y))
812 (m),(m),(m),(m),(m),(m),(m),(m),(m),(m), \
813 (m),(m),(m),(m),(m),(m),(m),(m),(m),(m), \
814 (m),(m),(m),(m),(m),(m),(m),(m)
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),(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),(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), (m)
828static const uint8_t common_year_mon_of_yday[] = {
829 M31(1), M28(2), M31(3), M30(4), M31(5), M30(6),
830 M31(7), M31(8), M30(9), M31(10), M30(11), M31(12)
832static const uint8_t leap_year_mon_of_yday[] = {
833 M31(1), M29(2), M31(3), M30(4), M31(5), M30(6),
834 M31(7), M31(8), M30(9), M31(10), M30(11), M31(12)
844 10,11,12,13,14,15,16,17,18,19, \
845 20,21,22,23,24,25,26,27,28
848 10,11,12,13,14,15,16,17,18,19, \
849 20,21,22,23,24,25,26,27,28,29
852 10,11,12,13,14,15,16,17,18,19, \
853 20,21,22,23,24,25,26,27,28,29,30
856 10,11,12,13,14,15,16,17,18,19, \
857 20,21,22,23,24,25,26,27,28,29,30,31
859static const uint8_t common_year_mday_of_yday[] = {
861 D31, D28, D31, D30, D31, D30, D31, D31, D30, D31, D30, D31
863static const uint8_t leap_year_mday_of_yday[] = {
864 D31, D29, D31, D30, D31, D30, D31, D31, D30, D31, D30, D31
873calc_tm_yday(
long tm_year,
int tm_mon,
int tm_mday)
875 int tm_year_mod400 = (int)MOD(tm_year, 400);
876 int tm_yday = tm_mday;
878 if (leap_year_p(tm_year_mod400 + 1900))
879 tm_yday += leap_year_yday_offset[tm_mon];
881 tm_yday += common_year_yday_offset[tm_mon];
887timegmw_noleapsecond(
struct vtm *
vtm)
899 divmodv(year1900,
INT2FIX(400), &q400, &r400);
902 yday = calc_tm_yday(year_mod400,
vtm->mon-1,
vtm->mday);
915 + DIV(year_mod400 - 69, 4)
916 - DIV(year_mod400 - 1, 100)
917 + (year_mod400 + 299) / 400;
919 vdays = addv(vdays, mulv(q400,
INT2FIX(97)));
920 vdays = addv(vdays, mulv(year1900,
INT2FIX(365)));
921 wret = wadd(rb_time_magnify(v2w(ret)), wmul(rb_time_magnify(v2w(vdays)), WINT2FIXWV(86400)));
922 wret = wadd(wret, v2w(
vtm->subsecx));
928zone_str(
const char *zone)
936 return rb_fstring_lit(
"(NO-TIMEZONE-ABBREVIATION)");
939 for (p = zone; *p; p++)
944 len = p - zone + strlen(p);
949 str = rb_enc_str_new(zone,
len, rb_locale_encoding());
951 return rb_fstring(str);
955gmtimew_noleapsecond(wideval_t timew,
struct vtm *
vtm)
961 wideval_t timew2, w, w2;
966 split_second(timew, &timew2, &subsecx);
967 vtm->subsecx = subsecx;
969 wdivmod(timew2, WINT2FIXWV(86400), &w2, &w);
974 vtm->wday = (wday + 4) % 7;
977 vtm->sec = n % 60; n = n / 60;
978 vtm->min = n % 60; n = n / 60;
982 divmodv(timev,
INT2FIX(400*365 + 97), &timev, &v);
995 if (30*365+7+31+29-1 <= n) {
1009 x = n / (365*100 + 24);
1010 n = n % (365*100 + 24);
1012 if (30*365+7+31+29-1 <= n) {
1022 x = n / (365*4 + 1);
1023 n = n % (365*4 + 1);
1025 if (365*2+31+29-1 <= n) {
1026 if (n < 365*2+366) {
1043 if (leap_year_p(y)) {
1044 vtm->mon = leap_year_mon_of_yday[n];
1045 vtm->mday = leap_year_mday_of_yday[n];
1048 vtm->mon = common_year_mon_of_yday[n];
1049 vtm->mday = common_year_mday_of_yday[n];
1053 vtm->zone = str_utc;
1057gmtime_with_leapsecond(
const time_t *timep,
struct tm *result)
1059#if defined(HAVE_STRUCT_TM_TM_GMTOFF)
1063 int gmtoff_sec, gmtoff_min, gmtoff_hour, gmtoff_day;
1065 t = LOCALTIME(timep, *result);
1070 if (t->tm_gmtoff < 0) {
1072 gmtoff = -t->tm_gmtoff;
1076 gmtoff = t->tm_gmtoff;
1078 gmtoff_sec = (int)(gmtoff % 60);
1079 gmtoff = gmtoff / 60;
1080 gmtoff_min = (int)(gmtoff % 60);
1081 gmtoff = gmtoff / 60;
1082 gmtoff_hour = (int)gmtoff;
1086 gmtoff_hour *= sign;
1093 result->tm_sec += gmtoff_sec;
1094 if (result->tm_sec < 0) {
1095 result->tm_sec += 60;
1098 if (60 <= result->tm_sec) {
1099 result->tm_sec -= 60;
1104 result->tm_min += gmtoff_min;
1105 if (result->tm_min < 0) {
1106 result->tm_min += 60;
1109 if (60 <= result->tm_min) {
1110 result->tm_min -= 60;
1115 result->tm_hour += gmtoff_hour;
1116 if (result->tm_hour < 0) {
1117 result->tm_hour += 24;
1120 if (24 <= result->tm_hour) {
1121 result->tm_hour -= 24;
1127 if (gmtoff_day < 0) {
1128 if (result->tm_yday == 0) {
1129 result->tm_mday = 31;
1130 result->tm_mon = 11;
1132 result->tm_yday = leap_year_p(result->tm_year + 1900) ? 365 : 364;
1134 else if (result->tm_mday == 1) {
1135 const int8_t *days_in_month = days_in_month_in(result->tm_year + 1900);
1137 result->tm_mday = days_in_month[result->tm_mon];
1144 result->tm_wday = (result->tm_wday + 6) % 7;
1147 int leap = leap_year_p(result->tm_year + 1900);
1148 if (result->tm_yday == (leap ? 365 : 364)) {
1151 result->tm_mday = 1;
1152 result->tm_yday = 0;
1154 else if (result->tm_mday == days_in_month_of(leap)[result->tm_mon]) {
1156 result->tm_mday = 1;
1163 result->tm_wday = (result->tm_wday + 1) % 7;
1166 result->tm_isdst = 0;
1167 result->tm_gmtoff = 0;
1168#if defined(HAVE_TM_ZONE)
1169 result->tm_zone = (
char *)
"UTC";
1173 return GMTIME(timep, *result);
1177static long this_year = 0;
1178static time_t known_leap_seconds_limit;
1179static int number_of_leap_seconds_known;
1182init_leap_second_info(
void)
1189 if (this_year == 0) {
1191 struct tm *tm, result;
1196 gmtime_r(&now, &result);
1200 tm = gmtime_with_leapsecond(&now, &result);
1202 this_year = tm->tm_year;
1204 if (TIMET_MAX - now < (time_t)(366*86400))
1205 known_leap_seconds_limit = TIMET_MAX;
1207 known_leap_seconds_limit = now + (time_t)(366*86400);
1209 if (!gmtime_with_leapsecond(&known_leap_seconds_limit, &result))
1213 vtm.mon = result.tm_mon + 1;
1214 vtm.mday = result.tm_mday;
1215 vtm.hour = result.tm_hour;
1216 vtm.min = result.tm_min;
1217 vtm.sec = result.tm_sec;
1221 timew = timegmw_noleapsecond(&
vtm);
1223 number_of_leap_seconds_known =
NUM2INT(w2v(wsub(TIMET2WV(known_leap_seconds_limit), rb_time_unmagnify(timew))));
1229ruby_reset_leap_second_info(
void)
1245 return timegmw_noleapsecond(
vtm);
1247 init_leap_second_info();
1249 timew = timegmw_noleapsecond(
vtm);
1252 if (number_of_leap_seconds_known == 0) {
1258 else if (wlt(rb_time_magnify(TIMET2WV(known_leap_seconds_limit)), timew)) {
1259 return wadd(timew, rb_time_magnify(WINT2WV(number_of_leap_seconds_known)));
1263 tm.tm_mon =
vtm->mon - 1;
1264 tm.tm_mday =
vtm->mday;
1265 tm.tm_hour =
vtm->hour;
1266 tm.tm_min =
vtm->min;
1267 tm.tm_sec =
vtm->sec;
1270 errmsg = find_time_t(&tm, 1, &t);
1272 rb_raise(rb_eArgError,
"%s", errmsg);
1273 return wadd(rb_time_magnify(TIMET2WV(t)), v2w(
vtm->subsecx));
1277gmtimew(wideval_t timew,
struct vtm *result)
1284 if (wlt(timew, WINT2FIXWV(0))) {
1285 gmtimew_noleapsecond(timew, result);
1289 init_leap_second_info();
1291 if (number_of_leap_seconds_known == 0) {
1295 gmtimew_noleapsecond(timew, result);
1298 else if (wlt(rb_time_magnify(TIMET2WV(known_leap_seconds_limit)), timew)) {
1299 timew = wsub(timew, rb_time_magnify(WINT2WV(number_of_leap_seconds_known)));
1300 gmtimew_noleapsecond(timew, result);
1304 split_second(timew, &timew2, &subsecx);
1306 t = WV2TIMET(timew2);
1307 if (!gmtime_with_leapsecond(&t, &tm))
1310 result->year =
LONG2NUM((
long)tm.tm_year + 1900);
1311 result->mon = tm.tm_mon + 1;
1312 result->mday = tm.tm_mday;
1313 result->hour = tm.tm_hour;
1314 result->min = tm.tm_min;
1315 result->sec = tm.tm_sec;
1316 result->subsecx = subsecx;
1317 result->utc_offset =
INT2FIX(0);
1318 result->wday = tm.tm_wday;
1319 result->yday = tm.tm_yday+1;
1320 result->isdst = tm.tm_isdst;
1325#define GMTIMEW(w, v) \
1326 (gmtimew(w, v) ? (void)0 : rb_raise(rb_eArgError, "gmtime error"))
1328static struct tm *localtime_with_gmtoff_zone(
const time_t *t,
struct tm *result,
long *gmtoff,
VALUE *zone);
1363static const int compat_common_month_table[12][7] = {
1365 { 2034, 2035, 2036, 2031, 2032, 2027, 2033 },
1366 { 2026, 2027, 2033, 2034, 2035, 2030, 2031 },
1367 { 2026, 2032, 2033, 2034, 2035, 2030, 2036 },
1368 { 2035, 2030, 2036, 2026, 2032, 2033, 2034 },
1369 { 2033, 2034, 2035, 2030, 2036, 2026, 2032 },
1370 { 2036, 2026, 2032, 2033, 2034, 2035, 2030 },
1371 { 2035, 2030, 2036, 2026, 2032, 2033, 2034 },
1372 { 2032, 2033, 2034, 2035, 2030, 2036, 2026 },
1373 { 2030, 2036, 2026, 2032, 2033, 2034, 2035 },
1374 { 2034, 2035, 2030, 2036, 2026, 2032, 2033 },
1375 { 2026, 2032, 2033, 2034, 2035, 2030, 2036 },
1376 { 2030, 2036, 2026, 2032, 2033, 2034, 2035 },
1404static const int compat_leap_month_table[7] = {
1406 2032, 2016, 2028, 2012, 2024, 2036, 2020,
1410calc_wday(
int year_mod400,
int month,
int day)
1415 a = (14 - month) / 12;
1416 y = year_mod400 + 4800 - a;
1417 m = month + 12 * a - 3;
1418 wday = day + (153*m+2)/5 + 365*y + y/4 - y/100 + y/400 + 2;
1424guess_local_offset(
struct vtm *vtm_utc,
int *isdst_ret,
VALUE *zone_ret)
1432 int year_mod400, wday;
1436 if (lt(vtm_utc->year,
INT2FIX(1916))) {
1439 zone = rb_fstring_lit(
"UTC");
1441# if defined(NEGATIVE_TIME_T)
1442# if SIZEOF_TIME_T <= 4
1444# define THE_TIME_OLD_ENOUGH ((time_t)0x80000000)
1448# define THE_TIME_OLD_ENOUGH ((time_t)(1600-1970)*366*24*60*60)
1450 if (localtime_with_gmtoff_zone((t = THE_TIME_OLD_ENOUGH, &t), &tm, &gmtoff, &zone)) {
1452 isdst = tm.tm_isdst;
1457 if (localtime_with_gmtoff_zone((t = 0, &t), &tm, &gmtoff, &zone)) {
1459 isdst = tm.tm_isdst;
1475 wday = calc_wday(year_mod400, vtm_utc->mon, 1);
1476 if (vtm_utc->mon == 2 && leap_year_p(year_mod400))
1477 vtm2.year =
INT2FIX(compat_leap_month_table[wday]);
1479 vtm2.year =
INT2FIX(compat_common_month_table[vtm_utc->mon-1][wday]);
1481 timev = w2v(rb_time_unmagnify(timegmw(&vtm2)));
1482 t = NUM2TIMET(timev);
1484 if (localtime_with_gmtoff_zone(&t, &tm, &gmtoff, &zone)) {
1486 *isdst_ret = tm.tm_isdst;
1494 static time_t now = 0;
1495 static long now_gmtoff = 0;
1496 static int now_isdst = 0;
1497 static VALUE now_zone;
1501 localtime_with_gmtoff_zone(&now, &tm, &now_gmtoff, &zone);
1502 now_isdst = tm.tm_isdst;
1503 zone = rb_fstring(zone);
1504 rb_gc_register_mark_object(zone);
1508 *isdst_ret = now_isdst;
1510 *zone_ret = now_zone;
1516small_vtm_sub(
struct vtm *vtm1,
struct vtm *vtm2)
1520 off = vtm1->sec - vtm2->sec;
1521 off += (vtm1->min - vtm2->min) * 60;
1522 off += (vtm1->hour - vtm2->hour) * 3600;
1523 if (ne(vtm1->year, vtm2->year))
1524 off += lt(vtm1->year, vtm2->year) ? -24*3600 : 24*3600;
1525 else if (vtm1->mon != vtm2->mon)
1526 off += vtm1->mon < vtm2->mon ? -24*3600 : 24*3600;
1527 else if (vtm1->mday != vtm2->mday)
1528 off += vtm1->mday < vtm2->mday ? -24*3600 : 24*3600;
1534timelocalw(
struct vtm *
vtm)
1539 wideval_t timew1, timew2;
1540 struct vtm vtm1, vtm2;
1545 if (l < INT_MIN || INT_MAX < l)
1547 tm.tm_year = (int)l;
1556 tm.tm_mon =
vtm->mon-1;
1557 tm.tm_mday =
vtm->mday;
1558 tm.tm_hour =
vtm->hour;
1559 tm.tm_min =
vtm->min;
1560 tm.tm_sec =
vtm->sec;
1561 tm.tm_isdst =
vtm->isdst == VTM_ISDST_INITVAL ? -1 :
vtm->isdst;
1563 if (find_time_t(&tm, 0, &t))
1565 return wadd(rb_time_magnify(TIMET2WV(t)), v2w(
vtm->subsecx));
1568 timew1 = timegmw(
vtm);
1570 if (!localtimew(timew1, &vtm1))
1571 rb_raise(rb_eArgError,
"localtimew error");
1573 n = vtmcmp(
vtm, &vtm1);
1575 timew1 = wsub(timew1, rb_time_magnify(WINT2FIXWV(12*3600)));
1576 if (!localtimew(timew1, &vtm1))
1577 rb_raise(rb_eArgError,
"localtimew error");
1584 timew1 = wsub(timew1, rb_time_magnify(WINT2FIXWV(24*3600)));
1585 if (!localtimew(timew1, &vtm1))
1586 rb_raise(rb_eArgError,
"localtimew error");
1589 timew2 = wadd(timew1, rb_time_magnify(WINT2FIXWV(24*3600)));
1590 if (!localtimew(timew2, &vtm2))
1591 rb_raise(rb_eArgError,
"localtimew error");
1593 timew1 = wadd(timew1, rb_time_magnify(v2w(small_vtm_sub(
vtm, &vtm1))));
1594 timew2 = wadd(timew2, rb_time_magnify(v2w(small_vtm_sub(
vtm, &vtm2))));
1596 if (weq(timew1, timew2))
1599 if (!localtimew(timew1, &vtm1))
1600 rb_raise(rb_eArgError,
"localtimew error");
1601 if (
vtm->hour != vtm1.hour ||
vtm->min != vtm1.min ||
vtm->sec != vtm1.sec)
1604 if (!localtimew(timew2, &vtm2))
1605 rb_raise(rb_eArgError,
"localtimew error");
1606 if (
vtm->hour != vtm2.hour ||
vtm->min != vtm2.min ||
vtm->sec != vtm2.sec)
1610 return lt(vtm1.utc_offset, vtm2.utc_offset) ? timew2 : timew1;
1612 return lt(vtm1.utc_offset, vtm2.utc_offset) ? timew1 : timew2;
1616localtime_with_gmtoff_zone(
const time_t *t,
struct tm *result,
long *gmtoff,
VALUE *zone)
1620 if (LOCALTIME(t, tm)) {
1621#if defined(HAVE_STRUCT_TM_TM_GMTOFF)
1622 *gmtoff = tm.tm_gmtoff;
1628 u = GMTIME(t, tmbuf);
1631 if (l->tm_year != u->tm_year)
1632 off = l->tm_year < u->tm_year ? -1 : 1;
1633 else if (l->tm_mon != u->tm_mon)
1634 off = l->tm_mon < u->tm_mon ? -1 : 1;
1635 else if (l->tm_mday != u->tm_mday)
1636 off = l->tm_mday < u->tm_mday ? -1 : 1;
1639 off =
off * 24 + l->tm_hour - u->tm_hour;
1640 off =
off * 60 + l->tm_min - u->tm_min;
1641 off =
off * 60 + l->tm_sec - u->tm_sec;
1646#if defined(HAVE_TM_ZONE)
1647 *zone = zone_str(tm.tm_zone);
1648#elif defined(HAVE_TZNAME) && defined(HAVE_DAYLIGHT)
1649# if defined(RUBY_MSVCRT_VERSION) && RUBY_MSVCRT_VERSION >= 140
1650# define tzname _tzname
1651# define daylight _daylight
1654 *zone = zone_str(tzname[daylight && tm.tm_isdst]);
1658 strftime(buf,
sizeof(buf),
"%Z", &tm);
1659 *zone = zone_str(buf);
1671timew_out_of_timet_range(wideval_t timew)
1674#if WIDEVALUE_IS_WIDER && SIZEOF_TIME_T < SIZEOF_INT64_T
1675 if (FIXWV_P(timew)) {
1676 wideint_t t = FIXWV2WINT(timew);
1677 if (t < TIME_SCALE * (wideint_t)TIMET_MIN ||
1678 TIME_SCALE * (1 + (wideint_t)TIMET_MAX) <= t)
1683#if SIZEOF_TIME_T == SIZEOF_INT64_T
1684 if (FIXWV_P(timew)) {
1685 wideint_t t = FIXWV2WINT(timew);
1686 if (~(time_t)0 <= 0) {
1696 timexv = w2v(timew);
1697 if (lt(timexv, mulv(
INT2FIX(TIME_SCALE), TIMET2NUM(TIMET_MIN))) ||
1698 le(mulv(
INT2FIX(TIME_SCALE), addv(TIMET2NUM(TIMET_MAX),
INT2FIX(1))), timexv))
1704localtimew(wideval_t timew,
struct vtm *result)
1706 VALUE subsecx, offset;
1710 if (!timew_out_of_timet_range(timew)) {
1716 split_second(timew, &timew2, &subsecx);
1718 t = WV2TIMET(timew2);
1720 if (localtime_with_gmtoff_zone(&t, &tm, &gmtoff, &zone)) {
1721 result->year =
LONG2NUM((
long)tm.tm_year + 1900);
1722 result->mon = tm.tm_mon + 1;
1723 result->mday = tm.tm_mday;
1724 result->hour = tm.tm_hour;
1725 result->min = tm.tm_min;
1726 result->sec = tm.tm_sec;
1727 result->subsecx = subsecx;
1728 result->wday = tm.tm_wday;
1729 result->yday = tm.tm_yday+1;
1730 result->isdst = tm.tm_isdst;
1731 result->utc_offset =
LONG2NUM(gmtoff);
1732 result->zone = zone;
1737 if (!gmtimew(timew, result))
1740 offset = guess_local_offset(result, &isdst, &zone);
1742 if (!gmtimew(wadd(timew, rb_time_magnify(v2w(offset))), result))
1745 result->utc_offset = offset;
1746 result->isdst = isdst;
1747 result->zone = zone;
1752#define TIME_TZMODE_LOCALTIME 0
1753#define TIME_TZMODE_UTC 1
1754#define TIME_TZMODE_FIXOFF 2
1755#define TIME_TZMODE_UNINITIALIZED 3
1762#define GetTimeval(obj, tobj) ((tobj) = get_timeval(obj))
1763#define GetNewTimeval(obj, tobj) ((tobj) = get_new_timeval(obj))
1765#define IsTimeval(obj) rb_typeddata_is_kind_of((obj), &time_data_type)
1766#define TIME_INIT_P(tobj) ((tobj)->vtm.tzmode != TIME_TZMODE_UNINITIALIZED)
1768#define TZMODE_UTC_P(tobj) ((tobj)->vtm.tzmode == TIME_TZMODE_UTC)
1769#define TZMODE_SET_UTC(tobj) ((tobj)->vtm.tzmode = TIME_TZMODE_UTC)
1771#define TZMODE_LOCALTIME_P(tobj) ((tobj)->vtm.tzmode == TIME_TZMODE_LOCALTIME)
1772#define TZMODE_SET_LOCALTIME(tobj) ((tobj)->vtm.tzmode = TIME_TZMODE_LOCALTIME)
1774#define TZMODE_FIXOFF_P(tobj) ((tobj)->vtm.tzmode == TIME_TZMODE_FIXOFF)
1775#define TZMODE_SET_FIXOFF(time, tobj, off) do { \
1776 (tobj)->vtm.tzmode = TIME_TZMODE_FIXOFF; \
1777 RB_OBJ_WRITE_UNALIGNED(time, &(tobj)->vtm.utc_offset, off); \
1780#define TZMODE_COPY(tobj1, tobj2) \
1781 ((tobj1)->vtm.tzmode = (tobj2)->vtm.tzmode, \
1782 (tobj1)->vtm.utc_offset = (tobj2)->vtm.utc_offset, \
1783 (tobj1)->vtm.zone = (tobj2)->vtm.zone)
1785static int zone_localtime(
VALUE zone,
VALUE time);
1787#define MAKE_TM(time, tobj) \
1789 if ((tobj)->vtm.tm_got == 0) { \
1790 time_get_tm((time), (tobj)); \
1793#define MAKE_TM_ENSURE(time, tobj, cond) \
1795 MAKE_TM(time, tobj); \
1797 force_make_tm(time, tobj); \
1804 tobj->timew = timew;
1805 if (!FIXWV_P(timew)) {
1824 VALUE zone = tobj->vtm.zone;
1825 if (!
NIL_P(zone) && zone != str_empty && zone != str_utc) {
1826 if (zone_localtime(zone, time))
return;
1828 tobj->vtm.tm_got = 0;
1829 time_get_tm(time, tobj);
1836 if (!FIXWV_P(tobj->timew))
1837 rb_gc_mark(w2v(tobj->timew));
1838 rb_gc_mark(tobj->vtm.year);
1839 rb_gc_mark(tobj->vtm.subsecx);
1840 rb_gc_mark(tobj->vtm.utc_offset);
1841 rb_gc_mark(tobj->vtm.zone);
1852 (RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_FROZEN_SHAREABLE | RUBY_TYPED_WB_PROTECTED | RUBY_TYPED_EMBEDDABLE),
1856time_s_alloc(
VALUE klass)
1862 tobj->vtm.tzmode = TIME_TZMODE_UNINITIALIZED;
1863 tobj->vtm.tm_got = 0;
1864 time_set_timew(obj, tobj, WINT2FIXWV(0));
1865 tobj->vtm.zone =
Qnil;
1871get_timeval(
VALUE obj)
1875 if (!TIME_INIT_P(tobj)) {
1882get_new_timeval(
VALUE obj)
1886 if (TIME_INIT_P(tobj)) {
1893time_modify(
VALUE time)
1899timenano2timew(time_t sec,
long nsec)
1903 timew = rb_time_magnify(TIMET2WV(sec));
1905 timew = wadd(timew, wmulquoll(WINT2WV(nsec), TIME_SCALE, 1000000000));
1910timew2timespec(wideval_t timew)
1916 if (timew_out_of_timet_range(timew))
1917 rb_raise(rb_eArgError,
"time out of system range");
1918 split_second(timew, &timew2, &subsecx);
1919 ts.tv_sec = WV2TIMET(timew2);
1925timew2timespec_exact(wideval_t timew,
struct timespec *ts)
1931 if (timew_out_of_timet_range(timew))
1933 split_second(timew, &timew2, &subsecx);
1934 ts->tv_sec = WV2TIMET(timew2);
1935 nsecv = mulquov(subsecx,
INT2FIX(1000000000),
INT2FIX(TIME_SCALE));
1945#ifdef HAVE_CLOCK_GETTIME
1946 if (clock_gettime(CLOCK_REALTIME, ts) == -1) {
1947 rb_sys_fail(
"clock_gettime");
1952 if (gettimeofday(&tv, 0) < 0) {
1953 rb_sys_fail(
"gettimeofday");
1955 ts->tv_sec = tv.tv_sec;
1956 ts->tv_nsec = tv.tv_usec * 1000;
1968 GetNewTimeval(time, tobj);
1969 TZMODE_SET_LOCALTIME(tobj);
1972 time_set_timew(time, tobj, timenano2timew(ts.tv_sec, ts.tv_nsec));
1975 time_zonelocal(time, zone);
1983 VALUE t = time_s_alloc(klass);
1984 return time_init_now(ec, t, zone);
1994 GetTimeval(time, tobj);
1996 tobj->vtm.tm_got = 0;
1997 tobj->vtm.zone =
Qnil;
1998 TZMODE_SET_FIXOFF(time, tobj,
off);
2023 subsec = neg(subsec);
2032 vtm->subsecx = addv(
vtm->subsecx, w2v(rb_time_magnify(v2w(subsec))));
2081 vtm_add_day(
vtm, day);
2085vtm_add_day(
struct vtm *
vtm,
int day)
2089 if (
vtm->mon == 1 &&
vtm->mday == 1) {
2094 vtm->yday = leap_year_v_p(
vtm->year) ? 366 : 365;
2096 else if (
vtm->mday == 1) {
2097 const int8_t *days_in_month = days_in_month_in_v(
vtm->year);
2099 vtm->mday = days_in_month[
vtm->mon-1];
2100 if (
vtm->yday != 0)
vtm->yday--;
2104 if (
vtm->yday != 0)
vtm->yday--;
2106 if (
vtm->wday != VTM_WDAY_INITVAL)
vtm->wday = (
vtm->wday + 6) % 7;
2109 int leap = leap_year_v_p(
vtm->year);
2110 if (
vtm->mon == 12 &&
vtm->mday == 31) {
2116 else if (
vtm->mday == days_in_month_of(leap)[
vtm->mon-1]) {
2119 if (
vtm->yday != 0)
vtm->yday++;
2123 if (
vtm->yday != 0)
vtm->yday++;
2125 if (
vtm->wday != VTM_WDAY_INITVAL)
vtm->wday = (
vtm->wday + 1) % 7;
2131maybe_tzobj_p(
VALUE obj)
2133 if (
NIL_P(obj))
return FALSE;
2135 if (RB_TYPE_P(obj,
T_STRING))
return FALSE;
2139NORETURN(
static void invalid_utc_offset(
VALUE));
2141invalid_utc_offset(
VALUE zone)
2143 rb_raise(rb_eArgError,
"\"+HH:MM\", \"-HH:MM\", \"UTC\" or "
2144 "\"A\"..\"I\",\"K\"..\"Z\" expected for utc_offset: %"PRIsVALUE,
2149utc_offset_arg(
VALUE arg)
2154 const char *s = RSTRING_PTR(tmp), *min = NULL, *sec = NULL;
2156 goto invalid_utc_offset;
2158 switch (RSTRING_LEN(tmp)) {
2164 if (s[0] >=
'A' && s[0] <=
'I') {
2165 n = (int)s[0] -
'A' + 1;
2168 else if (s[0] >=
'K' && s[0] <=
'M') {
2169 n = (int)s[0] -
'A';
2171 else if (s[0] >=
'N' && s[0] <=
'Y') {
2172 n =
'M' - (int)s[0];
2175 goto invalid_utc_offset;
2191 if (s[6] !=
':')
goto invalid_utc_offset;
2195 if (s[3] !=
':')
goto invalid_utc_offset;
2199 goto invalid_utc_offset;
2202 if (!
ISDIGIT(sec[0]) || !
ISDIGIT(sec[1]))
goto invalid_utc_offset;
2203 n += (sec[0] * 10 + sec[1] -
'0' * 11);
2207 if (!
ISDIGIT(min[0]) || !
ISDIGIT(min[1]))
goto invalid_utc_offset;
2208 if (min[0] >
'5')
goto invalid_utc_offset;
2209 n += (min[0] * 10 + min[1] -
'0' * 11) * 60;
2211 if (s[0] !=
'+' && s[0] !=
'-')
goto invalid_utc_offset;
2213 n += (s[1] * 10 + s[2] -
'0' * 11) * 3600;
2215 if (n == 0)
return UTC_ZONE;
2221 return num_exact(arg);
2229 wideval_t tlocal, wideval_t tutc)
2232 wideval_t w = wsub(tlocal, tutc);
2234 validate_utc_offset(
off);
2235 tobj->vtm.utc_offset =
off;
2236 tobj->vtm.zone = zone;
2237 TZMODE_SET_LOCALTIME(tobj);
2241extract_time(
VALUE time)
2244 const ID id_to_i = idTo_i;
2246#define EXTRACT_TIME() do { \
2247 t = v2w(rb_Integer(AREF(to_i))); \
2251 struct time_object *tobj = RTYPEDDATA_GET_DATA(time);
2254 t = rb_time_unmagnify(tobj->timew);
2258 else if (RB_TYPE_P(time,
T_STRUCT)) {
2259#define AREF(x) rb_struct_aref(time, ID2SYM(id_##x))
2264#define AREF(x) rb_funcallv(time, id_##x, 0, 0)
2277 const ID id_to_i = idTo_i;
2278 struct vtm *
vtm = &orig_tobj->vtm;
2280#define EXTRACT_VTM() do { \
2282 vtm->year = obj2vint(AREF(year)); \
2283 vtm->mon = month_arg(AREF(mon)); \
2284 vtm->mday = obj2ubits(AREF(mday), 5); \
2285 vtm->hour = obj2ubits(AREF(hour), 5); \
2286 vtm->min = obj2ubits(AREF(min), 6); \
2287 vtm->sec = obj2subsecx(AREF(sec), &subsecx); \
2288 vtm->isdst = RTEST(AREF(isdst)); \
2289 vtm->utc_offset = Qnil; \
2290 t = v2w(rb_Integer(AREF(to_i))); \
2294 struct time_object *tobj = RTYPEDDATA_GET_DATA(time);
2296 time_get_tm(time, tobj);
2297 time_set_vtm(orig_time, orig_tobj, tobj->vtm);
2298 t = rb_time_unmagnify(tobj->timew);
2299 if (TZMODE_FIXOFF_P(tobj) &&
vtm->utc_offset !=
INT2FIX(0))
2300 t = wadd(t, v2w(
vtm->utc_offset));
2304 else if (RB_TYPE_P(time,
T_STRUCT)) {
2305#define AREF(x) rb_struct_aref(time, ID2SYM(id_##x))
2311 struct vtm temp_vtm = *
vtm;
2312 GMTIMEW(rb_time_magnify(t), &temp_vtm);
2313 time_set_vtm(orig_time, orig_tobj, temp_vtm);
2316#define AREF(x) rb_funcallv(time, id_##x, 0, 0)
2322 RB_OBJ_WRITE_UNALIGNED(orig_time, &
vtm->subsecx, subsecx);
2335 tobj->vtm.isdst = (!UNDEF_P(dst) &&
RTEST(dst));
2342 struct time_object *tobj = RTYPEDDATA_GET_DATA(time);
2345 split_second(tobj->timew, &t, &s);
2346 tm = tm_from_time(rb_cTimeTM, time);
2348 if (UNDEF_P(utc))
return 0;
2350 s = extract_time(utc);
2351 zone_set_offset(zone, tobj, t, s);
2352 s = rb_time_magnify(s);
2353 if (tobj->vtm.subsecx !=
INT2FIX(0)) {
2354 s = wadd(s, v2w(tobj->vtm.subsecx));
2356 time_set_timew(time, tobj, s);
2358 zone_set_dst(zone, tobj, tm);
2368 VALUE local, tm, subsecx;
2369 struct time_object *tobj = RTYPEDDATA_GET_DATA(time);
2372 split_second(tobj->timew, &t, &subsecx);
2373 tm = tm_from_time(rb_cTimeTM, time);
2376 if (UNDEF_P(local))
return 0;
2378 s = extract_vtm(local, time, tobj, subsecx);
2379 tobj->vtm.tm_got = 1;
2380 zone_set_offset(zone, tobj, s, t);
2381 zone_set_dst(zone, tobj, tm);
2393 return rb_check_funcall_default(klass, id_find_timezone, 1, &zone,
Qnil);
2399vtm_day_wraparound(
struct vtm *
vtm)
2401 if (
vtm->hour < 24)
return;
2406 vtm_add_day(
vtm, 1);
2417 vtm.wday = VTM_WDAY_INITVAL;
2419 vtm.zone = str_empty;
2421 vtm.year = obj2vint(year);
2423 vtm.mon =
NIL_P(mon) ? 1 : month_arg(mon);
2425 vtm.mday =
NIL_P(mday) ? 1 : obj2ubits(mday, 5);
2427 vtm.hour =
NIL_P(hour) ? 0 : obj2ubits(hour, 5);
2429 vtm.min =
NIL_P(min) ? 0 : obj2ubits(min, 6);
2437 vtm.sec = obj2subsecx(sec, &subsecx);
2438 vtm.subsecx = subsecx;
2441 return time_init_vtm(time,
vtm, zone);
2450 vtm.isdst = VTM_ISDST_INITVAL;
2452 const VALUE arg = zone;
2455 if (arg ==
ID2SYM(rb_intern(
"dst")))
2457 else if (arg ==
ID2SYM(rb_intern(
"std")))
2459 else if (maybe_tzobj_p(arg))
2461 else if (!
NIL_P(utc = utc_offset_arg(arg)))
2462 vtm.utc_offset = utc == UTC_ZONE ?
INT2FIX(0) : utc;
2463 else if (
NIL_P(zone = find_timezone(time, arg)))
2464 invalid_utc_offset(arg);
2470 GetNewTimeval(time, tobj);
2473 time_set_timew(time, tobj, timegmw(&
vtm));
2474 vtm_day_wraparound(&
vtm);
2475 time_set_vtm(time, tobj,
vtm);
2476 tobj->vtm.tm_got = 1;
2477 TZMODE_SET_LOCALTIME(tobj);
2478 if (zone_timelocal(zone, time)) {
2481 else if (
NIL_P(
vtm.utc_offset = utc_offset_arg(zone))) {
2482 if (
NIL_P(zone = find_timezone(time, zone)) || !zone_timelocal(zone, time))
2483 invalid_utc_offset(arg);
2487 if (utc == UTC_ZONE) {
2488 time_set_timew(time, tobj, timegmw(&
vtm));
2490 vtm_day_wraparound(&
vtm);
2491 time_set_vtm(time, tobj,
vtm);
2492 tobj->vtm.tm_got = 1;
2493 TZMODE_SET_UTC(tobj);
2497 TZMODE_SET_LOCALTIME(tobj);
2502 vtm_add_offset(&
vtm,
off, -1);
2504 time_set_timew(time, tobj, timegmw(&
vtm));
2506 return time_set_utc_offset(time,
off);
2509 time_set_timew(time, tobj, timelocalw(&
vtm));
2511 return time_localtime(time);
2516two_digits(
const char *ptr,
const char *end,
const char **endp,
const char *name)
2518 ssize_t
len = end - ptr;
2521 VALUE mesg = rb_sprintf(
"two digits %s is expected", name);
2522 if (ptr[-1] ==
'-' || ptr[-1] ==
':') {
2523 rb_str_catf(mesg,
" after `%c'", ptr[-1]);
2525 rb_str_catf(mesg,
": %.*s", ((
len > 10) ? 10 : (int)(end - ptr)) + 1, ptr - 1);
2529 return (ptr[0] -
'0') * 10 + (ptr[1] -
'0');
2533parse_int(
const char *ptr,
const char *end,
const char **endp,
size_t *ndigits,
bool sign)
2535 ssize_t
len = (end - ptr);
2536 int flags = sign ? RB_INT_PARSE_SIGN : 0;
2537 return rb_int_parse_cstr(ptr,
len, (
char **)endp, ndigits, 10, flags);
2545 rb_raise(rb_eArgError,
"time string should have ASCII compatible encoding");
2548 const char *
const begin = RSTRING_PTR(str);
2549 const char *
const end = RSTRING_END(str);
2550 const char *ptr = begin;
2552 int mon = -1, mday = -1, hour = -1, min = -1, sec = -1;
2554 size_t prec =
NIL_P(precision) ? SIZE_MAX :
NUM2SIZET(precision);
2557 rb_raise(rb_eArgError,
"can't parse: %+"PRIsVALUE, str);
2559 year = parse_int(ptr, end, &ptr, &ndigits,
true);
2561 rb_raise(rb_eArgError,
"can't parse: %+"PRIsVALUE, str);
2563 else if (ndigits < 4) {
2564 rb_raise(rb_eArgError,
"year must be 4 or more digits: %.*s", (
int)ndigits, ptr - ndigits);
2566 else if (ptr == end) {
2570#define peekable_p(n) ((ptrdiff_t)(n) < (end - ptr))
2571#define peek_n(c, n) (peekable_p(n) && ((unsigned char)ptr[n] == (c)))
2572#define peek(c) peek_n(c, 0)
2573#define peekc_n(n) (peekable_p(n) ? (int)(unsigned char)ptr[n] : -1)
2574#define peekc() peekc_n(0)
2575#define expect_two_digits(x, bits) \
2576 (((unsigned int)(x = two_digits(ptr + 1, end, &ptr, #x)) > (1U << bits) - 1) ? \
2577 rb_raise(rb_eArgError, #x" out of range") : (void)0)
2578 if (!peek(
'-'))
break;
2579 expect_two_digits(mon, 4);
2580 if (!peek(
'-'))
break;
2581 expect_two_digits(mday, 5);
2582 if (!peek(
' ') && !peek(
'T'))
break;
2583 const char *
const time_part = ptr + 1;
2584 if (!
ISDIGIT(peekc_n(1)))
break;
2585#define nofraction(x) \
2587 rb_raise(rb_eArgError, "fraction " #x " is not supported: %.*s", \
2588 (int)(ptr + 1 - time_part), time_part); \
2590#define need_colon(x) \
2592 rb_raise(rb_eArgError, "missing " #x " part: %.*s", \
2593 (int)(ptr + 1 - time_part), time_part); \
2595 expect_two_digits(hour, 5);
2598 expect_two_digits(min, 6);
2601 expect_two_digits(sec, 6);
2604 for (ndigits = 0; ndigits < prec &&
ISDIGIT(peekc_n(ndigits)); ++ndigits);
2606 int clen = rb_enc_precise_mbclen(ptr, end, rb_enc_get(str));
2607 if (clen < 0) clen = 0;
2608 rb_raise(rb_eArgError,
"subsecond expected after dot: %.*s",
2609 (
int)(ptr - time_part) + clen, time_part);
2611 subsec = parse_int(ptr, ptr + ndigits, &ptr, &ndigits,
false);
2612 if (
NIL_P(subsec))
break;
2613 while (ptr < end &&
ISDIGIT(*ptr)) ptr++;
2616 while (ptr < end &&
ISSPACE(*ptr)) ptr++;
2617 const char *
const zstr = ptr;
2618 while (ptr < end && !
ISSPACE(*ptr)) ptr++;
2619 const char *
const zend = ptr;
2620 while (ptr < end &&
ISSPACE(*ptr)) ptr++;
2623 rb_str_cat(mesg, ptr, end - ptr);
2627 zone = rb_str_subseq(str, zstr - begin, zend - zstr);
2629 else if (hour == -1) {
2630 rb_raise(rb_eArgError,
"no time information");
2632 if (!
NIL_P(subsec)) {
2634 static const size_t TIME_SCALE_NUMDIGITS =
2638 if (ndigits < TIME_SCALE_NUMDIGITS) {
2639 VALUE mul = rb_int_positive_pow(10, TIME_SCALE_NUMDIGITS - ndigits);
2640 subsec = rb_int_mul(subsec, mul);
2642 else if (ndigits > TIME_SCALE_NUMDIGITS) {
2643 VALUE num = rb_int_positive_pow(10, ndigits - TIME_SCALE_NUMDIGITS);
2644 subsec = rb_rational_new(subsec, num);
2652 .wday = VTM_WDAY_INITVAL,
2656 .mon = (mon < 0) ? 1 : mon,
2657 .mday = (mday < 0) ? 1 : mday,
2658 .hour = (hour < 0) ? 0 : hour,
2659 .min = (min < 0) ? 0 : min,
2660 .sec = (sec < 0) ? 0 : sec,
2663 return time_init_vtm(klass,
vtm, zone);
2667subsec_normalize(time_t *secp,
long *subsecp,
const long maxsubsec)
2670 long subsec = *subsecp;
2673 if (UNLIKELY(subsec >= maxsubsec)) {
2674 sec2 = subsec / maxsubsec;
2675 if (TIMET_MAX - sec2 < sec) {
2678 subsec -= sec2 * maxsubsec;
2681 else if (UNLIKELY(subsec < 0)) {
2682 sec2 = NDIV(subsec, maxsubsec);
2683 if (sec < TIMET_MIN - sec2) {
2686 subsec -= sec2 * maxsubsec;
2689#ifndef NEGATIVE_TIME_T
2691 rb_raise(rb_eArgError,
"time must be positive");
2697#define time_usec_normalize(secp, usecp) subsec_normalize(secp, usecp, 1000000)
2698#define time_nsec_normalize(secp, nsecp) subsec_normalize(secp, nsecp, 1000000000)
2701nsec2timew(time_t sec,
long nsec)
2703 time_nsec_normalize(&sec, &nsec);
2704 return timenano2timew(sec, nsec);
2708time_new_timew(
VALUE klass, wideval_t timew)
2710 VALUE time = time_s_alloc(klass);
2713 tobj = RTYPEDDATA_GET_DATA(time);
2714 TZMODE_SET_LOCALTIME(tobj);
2715 time_set_timew(time, tobj, timew);
2723 time_usec_normalize(&sec, &usec);
2724 return time_new_timew(
rb_cTime, timenano2timew(sec, usec * 1000));
2731 return time_new_timew(
rb_cTime, nsec2timew(sec, nsec));
2738 VALUE time = time_new_timew(
rb_cTime, nsec2timew(ts->tv_sec, ts->tv_nsec));
2740 if (-86400 < offset && offset < 86400) {
2741 GetTimeval(time, tobj);
2742 TZMODE_SET_FIXOFF(time, tobj,
INT2FIX(offset));
2744 else if (offset == INT_MAX) {
2746 else if (offset == INT_MAX-1) {
2747 GetTimeval(time, tobj);
2748 TZMODE_SET_UTC(tobj);
2751 rb_raise(rb_eArgError,
"utc_offset out of range");
2760 VALUE time = time_new_timew(
rb_cTime, rb_time_magnify(v2w(timev)));
2765 if (maybe_tzobj_p(zone)) {
2767 if (zone_timelocal(zone, time))
return time;
2771 if (
NIL_P(zone = find_timezone(time,
off))) invalid_utc_offset(
off);
2773 if (!zone_timelocal(zone, time)) invalid_utc_offset(
off);
2776 else if (
off == UTC_ZONE) {
2777 return time_gmtime(time);
2780 validate_utc_offset(
off);
2781 time_set_utc_offset(time,
off);
2789time_timespec(
VALUE num, int interval)
2792 const char *
const tstr = interval ?
"time interval" :
"time";
2795#ifndef NEGATIVE_TIME_T
2796# define arg_range_check(v) \
2798 rb_raise(rb_eArgError, "%s must not be negative", tstr) : \
2801# define arg_range_check(v) \
2802 ((interval && (v) < 0) ? \
2803 rb_raise(rb_eArgError, "time interval must not be negative") : \
2808 t.tv_sec = NUM2TIMET(num);
2809 arg_range_check(t.tv_sec);
2820 t.tv_nsec = (int)(d*1e9+0.5);
2821 if (t.tv_nsec >= 1000000000) {
2822 t.tv_nsec -= 1000000000;
2826 else if ((t.tv_nsec = (
int)(-d*1e9+0.5)) > 0) {
2827 t.tv_nsec = 1000000000 - t.tv_nsec;
2830 t.tv_sec = (time_t)f;
2831 if (f != t.tv_sec) {
2836 else if (RB_BIGNUM_TYPE_P(num)) {
2837 t.tv_sec = NUM2TIMET(num);
2838 arg_range_check(t.tv_sec);
2844 if (!UNDEF_P(ary) && !
NIL_P(ary = rb_check_array_type(ary))) {
2845 i = rb_ary_entry(ary, 0);
2846 f = rb_ary_entry(ary, 1);
2847 t.tv_sec = NUM2TIMET(i);
2848 arg_range_check(t.tv_sec);
2853 rb_raise(
rb_eTypeError,
"can't convert %"PRIsVALUE
" into %s",
2858#undef arg_range_check
2862time_timeval(
VALUE num, int interval)
2867 ts = time_timespec(num, interval);
2868 tv.tv_sec = (TYPEOF_TIMEVAL_TV_SEC)ts.tv_sec;
2869 tv.tv_usec = (TYPEOF_TIMEVAL_TV_USEC)(ts.tv_nsec / 1000);
2877 return time_timeval(num, TRUE);
2887 if (IsTimeval(time)) {
2888 GetTimeval(time, tobj);
2889 ts = timew2timespec(tobj->timew);
2890 t.tv_sec = (TYPEOF_TIMEVAL_TV_SEC)ts.tv_sec;
2891 t.tv_usec = (TYPEOF_TIMEVAL_TV_USEC)(ts.tv_nsec / 1000);
2894 return time_timeval(time, FALSE);
2903 if (IsTimeval(time)) {
2904 GetTimeval(time, tobj);
2905 t = timew2timespec(tobj->timew);
2908 return time_timespec(time, FALSE);
2914 return time_timespec(num, TRUE);
2918get_scale(
VALUE unit)
2920 if (unit ==
ID2SYM(id_nanosecond) || unit ==
ID2SYM(id_nsec)) {
2923 else if (unit ==
ID2SYM(id_microsecond) || unit ==
ID2SYM(id_usec)) {
2926 else if (unit ==
ID2SYM(id_millisecond)) {
2930 rb_raise(rb_eArgError,
"unexpected unit: %"PRIsVALUE, unit);
2941 int scale = get_scale(unit);
2942 time = num_exact(time);
2943 t = num_exact(subsec);
2944 timew = wadd(rb_time_magnify(v2w(time)), wmulquoll(v2w(t), TIME_SCALE, scale));
2945 t = time_new_timew(klass, timew);
2947 else if (IsTimeval(time)) {
2949 GetTimeval(time, tobj);
2950 t = time_new_timew(klass, tobj->timew);
2951 GetTimeval(t, tobj2);
2952 TZMODE_COPY(tobj2, tobj);
2955 timew = rb_time_magnify(v2w(num_exact(time)));
2956 t = time_new_timew(klass, timew);
2959 time_zonelocal(t, zone);
2971static const char months[][4] = {
2972 "jan",
"feb",
"mar",
"apr",
"may",
"jun",
2973 "jul",
"aug",
"sep",
"oct",
"nov",
"dec",
2980 obj = rb_str_to_inum(obj, 10, TRUE);
2988obj2ubits(
VALUE obj,
unsigned int bits)
2990 const unsigned int usable_mask = (1U << bits) - 1;
2991 unsigned int rv = (
unsigned int)obj2int(obj);
2993 if ((rv & usable_mask) != rv)
2994 rb_raise(rb_eArgError,
"argument out of range");
2995 return (uint32_t)rv;
3002 obj = rb_str_to_inum(obj, 10, TRUE);
3017 obj = rb_str_to_inum(obj, 10, TRUE);
3021 divmodv(num_exact(obj),
INT2FIX(1), &obj, &subsec);
3022 *subsecx = w2v(rb_time_magnify(v2w(subsec)));
3024 return obj2ubits(obj, 6);
3028usec2subsecx(
VALUE obj)
3031 obj = rb_str_to_inum(obj, 10, TRUE);
3034 return mulquov(num_exact(obj),
INT2FIX(TIME_SCALE),
INT2FIX(1000000));
3043 return obj2ubits(arg, 4);
3048 if (!
NIL_P(s) && RSTRING_LEN(s) > 0) {
3050 for (i=0; i<12; i++) {
3051 if (RSTRING_LEN(s) == 3 &&
3059 mon = obj2ubits(arg, 4);
3065validate_utc_offset(
VALUE utc_offset)
3067 if (le(utc_offset,
INT2FIX(-86400)) || ge(utc_offset,
INT2FIX(86400)))
3068 rb_raise(rb_eArgError,
"utc_offset out of range");
3073validate_zone_name(
VALUE zone_name)
3080validate_vtm(
struct vtm *
vtm)
3082#define validate_vtm_range(mem, b, e) \
3083 ((vtm->mem < b || vtm->mem > e) ? \
3084 rb_raise(rb_eArgError, #mem" out of range") : (void)0)
3085 validate_vtm_range(mon, 1, 12);
3086 validate_vtm_range(mday, 1, 31);
3087 validate_vtm_range(hour, 0, 24);
3088 validate_vtm_range(min, 0, (
vtm->hour == 24 ? 0 : 59));
3089 validate_vtm_range(sec, 0, (
vtm->hour == 24 ? 0 : 60));
3091 rb_raise(rb_eArgError,
"subsecx out of range");
3092 if (!
NIL_P(
vtm->utc_offset)) validate_utc_offset(
vtm->utc_offset);
3093#undef validate_vtm_range
3097time_arg(
int argc,
const VALUE *argv,
struct vtm *
vtm)
3113 vtm->zone = str_empty;
3123 vtm->isdst =
RTEST(argv[8]) ? 1 : 0;
3126 rb_scan_args(argc, argv,
"17", &v[0],&v[1],&v[2],&v[3],&v[4],&v[5],&v[6],&v[7]);
3129 vtm->wday = VTM_WDAY_INITVAL;
3130 vtm->isdst = VTM_ISDST_INITVAL;
3133 vtm->year = obj2vint(v[0]);
3139 vtm->mon = month_arg(v[1]);
3146 vtm->mday = obj2ubits(v[2], 5);
3154 unsigned int mday2 = leap_year_v_p(
vtm->year) ? 29 : 28;
3155 if (
vtm->mday > mday2) {
3165 if (
vtm->mday == 31) {
3172 vtm->hour =
NIL_P(v[3])?0:obj2ubits(v[3], 5);
3174 vtm->min =
NIL_P(v[4])?0:obj2ubits(v[4], 6);
3176 if (!
NIL_P(v[6]) && argc == 7) {
3177 vtm->sec =
NIL_P(v[5])?0:obj2ubits(v[5],6);
3178 subsecx = usec2subsecx(v[6]);
3186 vtm->sec = obj2subsecx(v[5], &subsecx);
3189 vtm->subsecx = subsecx;
3201 unsigned long uy = (
unsigned long)(LIKELY(y >= 0) ? y : -y);
3203 if (LIKELY(uy % 4 != 0))
return 0;
3205 unsigned long century = uy / 100;
3206 if (LIKELY(uy != century * 100))
return 1;
3207 return century % 4 == 0;
3211timegm_noleapsecond(
struct tm *tm)
3213 long tm_year = tm->tm_year;
3214 int tm_yday = calc_tm_yday(tm->tm_year, tm->tm_mon, tm->tm_mday);
3222 return tm->tm_sec + tm->tm_min*60 + tm->tm_hour*3600 +
3226 DIV(tm_year-1,100) +
3227 DIV(tm_year+299,400))*86400;
3231#define DEBUG_FIND_TIME_NUMGUESS
3232#define DEBUG_GUESSRANGE
3235static const bool debug_guessrange =
3236#ifdef DEBUG_GUESSRANGE
3242#define DEBUG_REPORT_GUESSRANGE \
3243 (debug_guessrange ? debug_report_guessrange(guess_lo, guess_hi) : (void)0)
3246debug_report_guessrange(time_t guess_lo, time_t guess_hi)
3248 time_t guess_diff = guess_hi - guess_lo;
3249 fprintf(stderr,
"find time guess range: %"PRI_TIMET_PREFIX
"d - "
3250 "%"PRI_TIMET_PREFIX
"d : %"PRI_TIMET_PREFIX
"u\n",
3251 guess_lo, guess_hi, guess_diff);
3254static const bool debug_find_time_numguess =
3255#ifdef DEBUG_FIND_TIME_NUMGUESS
3261#define DEBUG_FIND_TIME_NUMGUESS_INC \
3262 (void)(debug_find_time_numguess && find_time_numguess++),
3263static unsigned long long find_time_numguess;
3266find_time_numguess_getter(
ID name,
VALUE *data)
3268 unsigned long long *numguess = (
void *)data;
3273find_time_t(
struct tm *tptr,
int utc_p, time_t *tp)
3275 time_t guess, guess0, guess_lo, guess_hi;
3276 struct tm *tm, tm0, tm_lo, tm_hi;
3283#define GUESS(p) (DEBUG_FIND_TIME_NUMGUESS_INC (utc_p ? gmtime_with_leapsecond((p), &result) : LOCALTIME((p), result)))
3285 guess_lo = TIMET_MIN;
3286 guess_hi = TIMET_MAX;
3288 find_dst = 0 < tptr->tm_isdst;
3294 if (tm0.tm_mon < 0) {
3301 else if (11 < tm0.tm_mon) {
3308 else if (tm0.tm_mday < 1) {
3314 else if ((d = days_in_month_in(1900 + tm0.tm_year)[tm0.tm_mon]) < tm0.tm_mday) {
3320 else if (tm0.tm_hour < 0) {
3325 else if (23 < tm0.tm_hour) {
3330 else if (tm0.tm_min < 0) {
3334 else if (59 < tm0.tm_min) {
3338 else if (tm0.tm_sec < 0) {
3341 else if (60 < tm0.tm_sec) {
3345 DEBUG_REPORT_GUESSRANGE;
3346 guess0 = guess = timegm_noleapsecond(&tm0);
3349 d = tmcmp(tptr, tm);
3350 if (d == 0) {
goto found; }
3353 guess -= 24 * 60 * 60;
3357 guess += 24 * 60 * 60;
3359 DEBUG_REPORT_GUESSRANGE;
3360 if (guess_lo < guess && guess < guess_hi && (tm = GUESS(&guess)) != NULL) {
3361 d = tmcmp(tptr, tm);
3362 if (d == 0) {
goto found; }
3367 DEBUG_REPORT_GUESSRANGE;
3371 tm = GUESS(&guess_lo);
3372 if (!tm)
goto error;
3373 d = tmcmp(tptr, tm);
3374 if (d < 0)
goto out_of_range;
3375 if (d == 0) { guess = guess_lo;
goto found; }
3378 tm = GUESS(&guess_hi);
3379 if (!tm)
goto error;
3380 d = tmcmp(tptr, tm);
3381 if (d > 0)
goto out_of_range;
3382 if (d == 0) { guess = guess_hi;
goto found; }
3385 DEBUG_REPORT_GUESSRANGE;
3389 while (guess_lo + 1 < guess_hi) {
3392 guess = guess_lo / 2 + guess_hi / 2;
3393 if (guess <= guess_lo)
3394 guess = guess_lo + 1;
3395 else if (guess >= guess_hi)
3396 guess = guess_hi - 1;
3401 time_t guess0_hi = timegm_noleapsecond(&tm_hi);
3402 guess = guess_hi - (guess0_hi - guess0);
3403 if (guess == guess_hi)
3407 else if (status == 2) {
3408 time_t guess0_lo = timegm_noleapsecond(&tm_lo);
3409 guess = guess_lo + (guess0 - guess0_lo);
3410 if (guess == guess_lo)
3414 if (guess <= guess_lo || guess_hi <= guess) {
3416 if (debug_guessrange) {
3417 if (guess <= guess_lo) {
3418 fprintf(stderr,
"too small guess: %"PRI_TIMET_PREFIX
"d"\
3419 " <= %"PRI_TIMET_PREFIX
"d\n", guess, guess_lo);
3421 if (guess_hi <= guess) {
3422 fprintf(stderr,
"too big guess: %"PRI_TIMET_PREFIX
"d"\
3423 " <= %"PRI_TIMET_PREFIX
"d\n", guess_hi, guess);
3432 if (!tm)
goto error;
3434 d = tmcmp(tptr, tm);
3439 DEBUG_REPORT_GUESSRANGE;
3444 DEBUG_REPORT_GUESSRANGE;
3459 tptr_tm_yday = calc_tm_yday(tptr->tm_year, tptr->tm_mon, tptr->tm_mday);
3462 ((tptr->tm_year - tm_lo.tm_year) * 365 +
3463 DIV((tptr->tm_year-69), 4) -
3464 DIV((tptr->tm_year-1), 100) +
3465 DIV((tptr->tm_year+299), 400) -
3466 DIV((tm_lo.tm_year-69), 4) +
3467 DIV((tm_lo.tm_year-1), 100) -
3468 DIV((tm_lo.tm_year+299), 400) +
3470 tm_lo.tm_yday) * 86400 +
3471 (tptr->tm_hour - tm_lo.tm_hour) * 3600 +
3472 (tptr->tm_min - tm_lo.tm_min) * 60 +
3473 (tptr->tm_sec - (tm_lo.tm_sec == 60 ? 59 : tm_lo.tm_sec));
3482 guess2 = guess - 2 * 60 * 60;
3483 tm = LOCALTIME(&guess2, result);
3485 if (tptr->tm_hour != (tm->tm_hour + 2) % 24 ||
3486 tptr->tm_min != tm->tm_min ||
3487 tptr->tm_sec != tm->tm_sec) {
3488 guess2 -= (tm->tm_hour - tptr->tm_hour) * 60 * 60 +
3489 (tm->tm_min - tptr->tm_min) * 60 +
3490 (tm->tm_sec - tptr->tm_sec);
3491 if (tptr->tm_mday != tm->tm_mday)
3492 guess2 += 24 * 60 * 60;
3493 if (guess != guess2) {
3494 tm = LOCALTIME(&guess2, result);
3495 if (tm && tmcmp(tptr, tm) == 0) {
3507 guess2 = guess + 2 * 60 * 60;
3508 tm = LOCALTIME(&guess2, result);
3510 if ((tptr->tm_hour + 2) % 24 != tm->tm_hour ||
3511 tptr->tm_min != tm->tm_min ||
3512 tptr->tm_sec != tm->tm_sec) {
3513 guess2 -= (tm->tm_hour - tptr->tm_hour) * 60 * 60 +
3514 (tm->tm_min - tptr->tm_min) * 60 +
3515 (tm->tm_sec - tptr->tm_sec);
3516 if (tptr->tm_mday != tm->tm_mday)
3517 guess2 -= 24 * 60 * 60;
3518 if (guess != guess2) {
3519 tm = LOCALTIME(&guess2, result);
3520 if (tm && tmcmp(tptr, tm) == 0) {
3536 return "time out of range";
3539 return "gmtime/localtime error";
3543vtmcmp(
struct vtm *a,
struct vtm *b)
3545 if (ne(a->year, b->year))
3546 return lt(a->year, b->year) ? -1 : 1;
3547 else if (a->mon != b->mon)
3548 return a->mon < b->mon ? -1 : 1;
3549 else if (a->mday != b->mday)
3550 return a->mday < b->mday ? -1 : 1;
3551 else if (a->hour != b->hour)
3552 return a->hour < b->hour ? -1 : 1;
3553 else if (a->min != b->min)
3554 return a->min < b->min ? -1 : 1;
3555 else if (a->sec != b->sec)
3556 return a->sec < b->sec ? -1 : 1;
3557 else if (ne(a->subsecx, b->subsecx))
3558 return lt(a->subsecx, b->subsecx) ? -1 : 1;
3564tmcmp(
struct tm *a,
struct tm *b)
3566 if (a->tm_year != b->tm_year)
3567 return a->tm_year < b->tm_year ? -1 : 1;
3568 else if (a->tm_mon != b->tm_mon)
3569 return a->tm_mon < b->tm_mon ? -1 : 1;
3570 else if (a->tm_mday != b->tm_mday)
3571 return a->tm_mday < b->tm_mday ? -1 : 1;
3572 else if (a->tm_hour != b->tm_hour)
3573 return a->tm_hour < b->tm_hour ? -1 : 1;
3574 else if (a->tm_min != b->tm_min)
3575 return a->tm_min < b->tm_min ? -1 : 1;
3576 else if (a->tm_sec != b->tm_sec)
3577 return a->tm_sec < b->tm_sec ? -1 : 1;
3683time_s_mkutc(
int argc,
VALUE *argv,
VALUE klass)
3687 time_arg(argc, argv, &
vtm);
3688 return time_gmtime(time_new_timew(klass, timegmw(&
vtm)));
3709time_s_mktime(
int argc,
VALUE *argv,
VALUE klass)
3713 time_arg(argc, argv, &
vtm);
3714 return time_localtime(time_new_timew(klass, timelocalw(&
vtm)));
3734time_to_i(
VALUE time)
3738 GetTimeval(time, tobj);
3739 return w2v(wdiv(tobj->timew, WINT2FIXWV(TIME_SCALE)));
3763time_to_f(
VALUE time)
3767 GetTimeval(time, tobj);
3768 return rb_Float(rb_time_unmagnify_to_float(tobj->timew));
3784time_to_r(
VALUE time)
3789 GetTimeval(time, tobj);
3790 v = rb_time_unmagnify_to_rational(tobj->timew);
3812time_usec(
VALUE time)
3817 GetTimeval(time, tobj);
3819 w = wmod(tobj->timew, WINT2WV(TIME_SCALE));
3820 wmuldivmod(w, WINT2FIXWV(1000000), WINT2FIXWV(TIME_SCALE), &q, &r);
3839time_nsec(
VALUE time)
3843 GetTimeval(time, tobj);
3844 return rb_to_int(w2v(wmulquoll(wmod(tobj->timew, WINT2WV(TIME_SCALE)), 1000000000, TIME_SCALE)));
3865time_subsec(
VALUE time)
3869 GetTimeval(time, tobj);
3870 return quov(w2v(wmod(tobj->timew, WINT2FIXWV(TIME_SCALE))),
INT2FIX(TIME_SCALE));
3907 GetTimeval(time1, tobj1);
3908 if (IsTimeval(time2)) {
3909 GetTimeval(time2, tobj2);
3910 n = wcmp(tobj1->timew, tobj2->timew);
3913 return rb_invcmp(time1, time2);
3915 if (n == 0)
return INT2FIX(0);
3933 GetTimeval(time1, tobj1);
3934 if (IsTimeval(time2)) {
3935 GetTimeval(time2, tobj2);
3936 return rb_equal(w2v(tobj1->timew), w2v(tobj2->timew));
3958time_utc_p(
VALUE time)
3962 GetTimeval(time, tobj);
3963 return RBOOL(TZMODE_UTC_P(tobj));
3976time_hash(
VALUE time)
3980 GetTimeval(time, tobj);
3981 return rb_hash(w2v(tobj->timew));
3991 GetTimeval(time, tobj);
3992 GetNewTimeval(copy, tcopy);
4002 time_init_copy(dup, time);
4007time_localtime(
VALUE time)
4013 GetTimeval(time, tobj);
4014 if (TZMODE_LOCALTIME_P(tobj)) {
4015 if (tobj->vtm.tm_got)
4022 zone = tobj->vtm.zone;
4023 if (maybe_tzobj_p(zone) && zone_localtime(zone, time)) {
4027 if (!localtimew(tobj->timew, &
vtm))
4028 rb_raise(rb_eArgError,
"localtime error");
4029 time_set_vtm(time, tobj,
vtm);
4031 tobj->vtm.tm_got = 1;
4032 TZMODE_SET_LOCALTIME(tobj);
4040 if (zone_localtime(zone, time))
return time;
4044 if (
NIL_P(zone = find_timezone(time,
off))) invalid_utc_offset(
off);
4045 if (!zone_localtime(zone, time)) invalid_utc_offset(
off);
4048 else if (
off == UTC_ZONE) {
4049 return time_gmtime(time);
4051 validate_utc_offset(
off);
4053 time_set_utc_offset(time,
off);
4054 return time_fixoff(time);
4083time_localtime_m(
int argc,
VALUE *argv,
VALUE time)
4088 return time_zonelocal(time,
off);
4091 return time_localtime(time);
4109time_gmtime(
VALUE time)
4114 GetTimeval(time, tobj);
4115 if (TZMODE_UTC_P(tobj)) {
4116 if (tobj->vtm.tm_got)
4124 GMTIMEW(tobj->timew, &
vtm);
4125 time_set_vtm(time, tobj,
vtm);
4127 tobj->vtm.tm_got = 1;
4128 TZMODE_SET_UTC(tobj);
4133time_fixoff(
VALUE time)
4139 GetTimeval(time, tobj);
4140 if (TZMODE_FIXOFF_P(tobj)) {
4141 if (tobj->vtm.tm_got)
4148 if (TZMODE_FIXOFF_P(tobj))
4149 off = tobj->vtm.utc_offset;
4153 GMTIMEW(tobj->timew, &
vtm);
4155 zone = tobj->vtm.zone;
4156 vtm_add_offset(&
vtm,
off, +1);
4158 time_set_vtm(time, tobj,
vtm);
4159 RB_OBJ_WRITE_UNALIGNED(time, &tobj->vtm.zone, zone);
4161 tobj->vtm.tm_got = 1;
4162 TZMODE_SET_FIXOFF(time, tobj,
off);
4184time_getlocaltime(
int argc,
VALUE *argv,
VALUE time)
4190 if (maybe_tzobj_p(zone)) {
4191 VALUE t = time_dup(time);
4192 if (zone_localtime(
off, t))
return t;
4197 if (
NIL_P(zone = find_timezone(time,
off))) invalid_utc_offset(
off);
4198 time = time_dup(time);
4199 if (!zone_localtime(zone, time)) invalid_utc_offset(
off);
4202 else if (
off == UTC_ZONE) {
4203 return time_gmtime(time_dup(time));
4205 validate_utc_offset(
off);
4207 time = time_dup(time);
4208 time_set_utc_offset(time,
off);
4209 return time_fixoff(time);
4212 return time_localtime(time_dup(time));
4231time_getgmtime(
VALUE time)
4233 return time_gmtime(time_dup(time));
4239 if (TZMODE_UTC_P(tobj))
return time_gmtime(time);
4240 if (TZMODE_FIXOFF_P(tobj))
return time_fixoff(time);
4241 return time_localtime(time);
4245#define strftimev(fmt, time, enc) strftime_cstr((fmt), rb_strlen_lit(fmt), (time), (enc))
4269time_asctime(
VALUE time)
4271 return strftimev(
"%a %b %e %T %Y", time, rb_usascii_encoding());
4291time_to_s(
VALUE time)
4295 GetTimeval(time, tobj);
4296 if (TZMODE_UTC_P(tobj))
4297 return strftimev(
"%Y-%m-%d %H:%M:%S UTC", time, rb_usascii_encoding());
4299 return strftimev(
"%Y-%m-%d %H:%M:%S %z", time, rb_usascii_encoding());
4319time_inspect(
VALUE time)
4324 GetTimeval(time, tobj);
4325 str = strftimev(
"%Y-%m-%d %H:%M:%S", time, rb_usascii_encoding());
4326 subsec = w2v(wmod(tobj->timew, WINT2FIXWV(TIME_SCALE)));
4331 rb_str_catf(str,
".%09ld",
FIX2LONG(subsec));
4332 for (
len=RSTRING_LEN(str); RSTRING_PTR(str)[
len-1] ==
'0' &&
len > 0;
len--)
4334 rb_str_resize(str,
len);
4338 subsec = quov(subsec,
INT2FIX(TIME_SCALE));
4341 if (TZMODE_UTC_P(tobj)) {
4347 char sign = (
off < 0) ? (
off = -
off,
'-') :
'+';
4349 int min = (
off /= 60) % 60;
4351 rb_str_catf(str,
" %c%.2d%.2d", sign, (
int)
off, min);
4352 if (sec) rb_str_catf(str,
"%.2d", sec);
4363 offset = num_exact(offset);
4365 result = time_new_timew(klass, wsub(tobj->timew, rb_time_magnify(v2w(offset))));
4367 result = time_new_timew(klass, wadd(tobj->timew, rb_time_magnify(v2w(offset))));
4368 GetTimeval(result, result_tobj);
4369 TZMODE_COPY(result_tobj, tobj);
4377 return time_add0(
rb_cTime, tobj, torig, offset, sign);
4398 GetTimeval(time1, tobj);
4400 if (IsTimeval(time2)) {
4403 return time_add(tobj, time1, time2, 1);
4433 GetTimeval(time1, tobj);
4434 if (IsTimeval(time2)) {
4437 GetTimeval(time2, tobj2);
4438 return rb_Float(rb_time_unmagnify_to_float(wsub(tobj->timew, tobj2->timew)));
4440 return time_add(tobj, time1, time2, -1);
4444ndigits_denominator(
VALUE ndigits)
4449 rb_raise(rb_eArgError,
"negative ndigits given");
4454 return rb_rational_new(
INT2FIX(1),
4455 rb_int_positive_pow(10, (
unsigned long)nd));
4489 VALUE ndigits, v, den;
4495 den = ndigits_denominator(ndigits);
4497 GetTimeval(time, tobj);
4498 v = w2v(rb_time_unmagnify(tobj->timew));
4501 if (lt(v, quov(den,
INT2FIX(2))))
4502 return time_add(tobj, time, v, -1);
4504 return time_add(tobj, time, subv(den, v), 1);
4537 VALUE ndigits, v, den;
4543 den = ndigits_denominator(ndigits);
4545 GetTimeval(time, tobj);
4546 v = w2v(rb_time_unmagnify(tobj->timew));
4549 return time_add(tobj, time, v, -1);
4582 VALUE ndigits, v, den;
4588 den = ndigits_denominator(ndigits);
4590 GetTimeval(time, tobj);
4591 v = w2v(rb_time_unmagnify(tobj->timew));
4597 return time_add(tobj, time, v, 1);
4622 GetTimeval(time, tobj);
4623 MAKE_TM(time, tobj);
4624 return INT2FIX(tobj->vtm.sec);
4646 GetTimeval(time, tobj);
4647 MAKE_TM(time, tobj);
4648 return INT2FIX(tobj->vtm.min);
4666time_hour(
VALUE time)
4670 GetTimeval(time, tobj);
4671 MAKE_TM(time, tobj);
4672 return INT2FIX(tobj->vtm.hour);
4690time_mday(
VALUE time)
4694 GetTimeval(time, tobj);
4695 MAKE_TM(time, tobj);
4696 return INT2FIX(tobj->vtm.mday);
4718 GetTimeval(time, tobj);
4719 MAKE_TM(time, tobj);
4720 return INT2FIX(tobj->vtm.mon);
4737time_year(
VALUE time)
4741 GetTimeval(time, tobj);
4742 MAKE_TM(time, tobj);
4743 return tobj->vtm.year;
4762time_wday(
VALUE time)
4766 GetTimeval(time, tobj);
4767 MAKE_TM_ENSURE(time, tobj, tobj->vtm.wday != VTM_WDAY_INITVAL);
4768 return INT2FIX((
int)tobj->vtm.wday);
4772 return RBOOL(time_wday(time) == INT2FIX(n)); \
4788time_sunday(
VALUE time)
4806time_monday(
VALUE time)
4824time_tuesday(
VALUE time)
4842time_wednesday(
VALUE time)
4860time_thursday(
VALUE time)
4878time_friday(
VALUE time)
4896time_saturday(
VALUE time)
4912time_yday(
VALUE time)
4916 GetTimeval(time, tobj);
4917 MAKE_TM_ENSURE(time, tobj, tobj->vtm.yday != 0);
4918 return INT2FIX(tobj->vtm.yday);
4937time_isdst(
VALUE time)
4941 GetTimeval(time, tobj);
4942 MAKE_TM(time, tobj);
4943 if (tobj->vtm.isdst == VTM_ISDST_INITVAL) {
4946 return RBOOL(tobj->vtm.isdst);
4960time_zone(
VALUE time)
4965 GetTimeval(time, tobj);
4966 MAKE_TM(time, tobj);
4968 if (TZMODE_UTC_P(tobj)) {
4971 zone = tobj->vtm.zone;
4976 zone = rb_str_dup(zone);
4996 GetTimeval(time, tobj);
4998 if (TZMODE_UTC_P(tobj)) {
5002 MAKE_TM(time, tobj);
5003 return tobj->vtm.utc_offset;
5023time_to_a(
VALUE time)
5027 GetTimeval(time, tobj);
5028 MAKE_TM_ENSURE(time, tobj, tobj->vtm.yday != 0);
5038 RBOOL(tobj->vtm.isdst),
5084 GetTimeval(time, tobj);
5085 MAKE_TM_ENSURE(time, tobj, tobj->vtm.yday != 0);
5088 h = rb_hash_new_with_size(11);
5090 rb_hash_aset(h, sym_year, tobj->vtm.year);
5091 rb_hash_aset(h, sym_month,
INT2FIX(tobj->vtm.mon));
5092 rb_hash_aset(h, sym_day,
INT2FIX(tobj->vtm.mday));
5093 rb_hash_aset(h, sym_yday,
INT2FIX(tobj->vtm.yday));
5094 rb_hash_aset(h, sym_wday,
INT2FIX(tobj->vtm.wday));
5095 rb_hash_aset(h, sym_hour,
INT2FIX(tobj->vtm.hour));
5096 rb_hash_aset(h, sym_min,
INT2FIX(tobj->vtm.min));
5097 rb_hash_aset(h, sym_sec,
INT2FIX(tobj->vtm.sec));
5098 rb_hash_aset(h, sym_subsec,
5099 quov(w2v(wmod(tobj->timew, WINT2FIXWV(TIME_SCALE))),
INT2FIX(TIME_SCALE)));
5100 rb_hash_aset(h, sym_dst, RBOOL(tobj->vtm.isdst));
5101 rb_hash_aset(h, sym_zone, time_zone(time));
5105 if (UNLIKELY(!RB_TYPE_P(keys,
T_ARRAY))) {
5107 "wrong argument type %"PRIsVALUE
" (expected Array or nil)",
5117 if (sym_year == key) rb_hash_aset(h, key, tobj->vtm.year);
5118 if (sym_month == key) rb_hash_aset(h, key,
INT2FIX(tobj->vtm.mon));
5119 if (sym_day == key) rb_hash_aset(h, key,
INT2FIX(tobj->vtm.mday));
5120 if (sym_yday == key) rb_hash_aset(h, key,
INT2FIX(tobj->vtm.yday));
5121 if (sym_wday == key) rb_hash_aset(h, key,
INT2FIX(tobj->vtm.wday));
5122 if (sym_hour == key) rb_hash_aset(h, key,
INT2FIX(tobj->vtm.hour));
5123 if (sym_min == key) rb_hash_aset(h, key,
INT2FIX(tobj->vtm.min));
5124 if (sym_sec == key) rb_hash_aset(h, key,
INT2FIX(tobj->vtm.sec));
5125 if (sym_subsec == key) {
5126 rb_hash_aset(h, key, quov(w2v(wmod(tobj->timew, WINT2FIXWV(TIME_SCALE))),
INT2FIX(TIME_SCALE)));
5128 if (sym_dst == key) rb_hash_aset(h, key, RBOOL(tobj->vtm.isdst));
5129 if (sym_zone == key) rb_hash_aset(h, key, time_zone(time));
5135rb_strftime_alloc(
const char *format,
size_t format_len,
rb_encoding *enc,
5136 VALUE time,
struct vtm *
vtm, wideval_t timew,
int gmt)
5141 if (!timew2timespec_exact(timew, &ts))
5142 timev = w2v(rb_time_unmagnify(timew));
5145 return rb_strftime_timespec(format, format_len, enc, time,
vtm, &ts, gmt);
5148 return rb_strftime(format, format_len, enc, time,
vtm, timev, gmt);
5158 GetTimeval(time, tobj);
5159 MAKE_TM(time, tobj);
5160 str = rb_strftime_alloc(fmt,
len, enc, time, &tobj->vtm, tobj->timew, TZMODE_UTC_P(tobj));
5161 if (!str) rb_raise(rb_eArgError,
"invalid format: %s", fmt);
5183 GetTimeval(time, tobj);
5184 MAKE_TM_ENSURE(time, tobj, tobj->vtm.yday != 0);
5187 rb_raise(rb_eArgError,
"format should have ASCII compatible encoding");
5189 tmp = rb_str_tmp_frozen_acquire(format);
5190 fmt = RSTRING_PTR(tmp);
5191 len = RSTRING_LEN(tmp);
5192 enc = rb_enc_get(format);
5194 rb_warning(
"strftime called with empty format string");
5195 return rb_enc_str_new(0, 0, enc);
5198 VALUE str = rb_strftime_alloc(fmt,
len, enc, time, &tobj->vtm, tobj->timew,
5199 TZMODE_UTC_P(tobj));
5200 rb_str_tmp_frozen_release(format, tmp);
5201 if (!str) rb_raise(rb_eArgError,
"invalid format: %"PRIsVALUE, format);
5206int ruby_marshal_write_long(
long x,
char *buf);
5208enum {base_dump_size = 8};
5212time_mdump(
VALUE time)
5216 char buf[base_dump_size +
sizeof(long) + 1];
5223 VALUE subsecx, nano, subnano, v, zone;
5226 const int max_year = 1900+0xffff;
5228 GetTimeval(time, tobj);
5230 gmtimew(tobj->timew, &
vtm);
5234 if (year > max_year) {
5235 year_extend =
INT2FIX(year - max_year);
5238 else if (year < 1900) {
5239 year_extend =
LONG2NUM(1900 - year);
5244 if (rb_int_positive_p(
vtm.year)) {
5245 year_extend = rb_int_minus(
vtm.year,
INT2FIX(max_year));
5249 year_extend = rb_int_minus(
INT2FIX(1900),
vtm.year);
5254 subsecx =
vtm.subsecx;
5256 nano = mulquov(subsecx,
INT2FIX(1000000000),
INT2FIX(TIME_SCALE));
5257 divmodv(nano,
INT2FIX(1), &v, &subnano);
5262 nano = addv(
LONG2FIX(nsec), subnano);
5265 TZMODE_UTC_P(tobj) << 30 |
5270 s = (
unsigned long)
vtm.min << 26 |
5274 for (i=0; i<4; i++) {
5275 buf[i] = (
unsigned char)p;
5278 for (i=4; i<8; i++) {
5279 buf[i] = (
unsigned char)s;
5283 if (!
NIL_P(year_extend)) {
5290 size_t ysize = rb_absint_size(year_extend, NULL);
5291 char *p, *
const buf_year_extend = buf + base_dump_size;
5292 if (ysize > LONG_MAX ||
5293 (i = ruby_marshal_write_long((
long)ysize, buf_year_extend)) < 0) {
5294 rb_raise(rb_eArgError,
"year too %s to marshal: %"PRIsVALUE
" UTC",
5295 (year == 1900 ?
"small" :
"big"),
vtm.year);
5297 i += base_dump_size;
5299 p = RSTRING_PTR(str);
5310 rb_ivar_set(str, id_nano_num, RRATIONAL(nano)->num);
5311 rb_ivar_set(str, id_nano_den, RRATIONAL(nano)->den);
5327 int len = (int)
sizeof(buf);
5328 buf[1] = (char)((nsec % 10) << 4);
5330 buf[0] = (char)(nsec % 10);
5332 buf[0] |= (char)((nsec % 10) << 4);
5337 if (!TZMODE_UTC_P(tobj)) {
5344 zone = tobj->vtm.zone;
5345 if (maybe_tzobj_p(zone)) {
5346 zone = rb_funcallv(zone, id_name, 0, 0);
5359 str = time_mdump(time);
5365mload_findzone(
VALUE arg)
5368 VALUE time = argp[0], zone = argp[1];
5369 return find_timezone(time, zone);
5379 if (
NIL_P(z))
return rb_fstring(zone);
5380 if (RB_TYPE_P(z,
T_STRING))
return rb_fstring(z);
5384long ruby_marshal_read_long(
const char **buf,
long len);
5398 VALUE submicro, nano_num, nano_den, offset, zone, year;
5403#define get_attr(attr, iffound) \
5404 attr = rb_attr_delete(str, id_##attr); \
5405 if (!NIL_P(attr)) { \
5409 get_attr(nano_num, {});
5410 get_attr(nano_den, {});
5411 get_attr(submicro, {});
5412 get_attr(offset, (offset =
rb_rescue(validate_utc_offset, offset, 0,
Qnil)));
5413 get_attr(zone, (zone =
rb_rescue(validate_zone_name, zone, 0,
Qnil)));
5421 buf = (
unsigned char *)RSTRING_PTR(str);
5422 if (RSTRING_LEN(str) < base_dump_size) {
5423 goto invalid_format;
5427 for (i=0; i<4; i++) {
5428 p |= (
unsigned long)buf[i]<<(8*i);
5430 for (i=4; i<8; i++) {
5431 s |= (
unsigned long)buf[i]<<(8*(i-4));
5434 if ((p & (1UL<<31)) == 0) {
5440 timew = wadd(rb_time_magnify(TIMET2WV(sec)), wmulquoll(WINT2FIXWV(usec), TIME_SCALE, 1000000));
5444 gmt = (int)((p >> 30) & 0x1);
5447 year =
INT2FIX(((
int)(p >> 14) & 0xffff) + 1900);
5449 if (RSTRING_LEN(str) > base_dump_size) {
5450 long len = RSTRING_LEN(str) - base_dump_size;
5453 const char *ybuf = (
const char *)(buf += base_dump_size);
5454 ysize = ruby_marshal_read_long(&ybuf,
len);
5455 len -= ybuf - (
const char *)buf;
5456 if (ysize < 0 || ysize >
len)
goto invalid_format;
5459 year = rb_int_minus(year, year_extend);
5462 year = rb_int_plus(year, year_extend);
5465 unsigned int mon = ((int)(p >> 10) & 0xf);
5472 vtm.mday = (int)(p >> 5) & 0x1f;
5473 vtm.hour = (int) p & 0x1f;
5474 vtm.min = (int)(s >> 26) & 0x3f;
5475 vtm.sec = (int)(s >> 20) & 0x3f;
5479 vtm.zone = str_empty;
5481 usec = (long)(s & 0xfffff);
5486 if (nano_num !=
Qnil) {
5487 VALUE nano = quov(num_exact(nano_num), num_exact(nano_den));
5490 else if (submicro !=
Qnil) {
5495 len = RSTRING_LEN(submicro);
5498 if (10 <= (digit = ptr[0] >> 4))
goto end_submicro;
5499 nsec += digit * 100;
5500 if (10 <= (digit = ptr[0] & 0xf))
goto end_submicro;
5504 if (10 <= (digit = ptr[1] >> 4))
goto end_submicro;
5510 timew = timegmw(&
vtm);
5513 GetNewTimeval(time, tobj);
5514 TZMODE_SET_LOCALTIME(tobj);
5515 tobj->vtm.tm_got = 0;
5516 time_set_timew(time, tobj, timew);
5519 TZMODE_SET_UTC(tobj);
5521 else if (!
NIL_P(offset)) {
5522 time_set_utc_offset(time, offset);
5526 zone = mload_zone(time, zone);
5527 tobj->vtm.zone = zone;
5528 zone_localtime(zone, time);
5542 VALUE time = time_s_alloc(klass);
5544 time_mload(time, str);
5570 GetTimeval(time, tobj);
5571 tm = time_s_alloc(klass);
5572 ttm = RTYPEDDATA_GET_DATA(tm);
5574 GMTIMEW(ttm->timew = tobj->timew, v);
5575 ttm->timew = wsub(ttm->timew, v->subsecx);
5578 time_set_vtm(tm, ttm, *v);
5580 ttm->vtm.tm_got = 1;
5581 TZMODE_SET_UTC(ttm);
5594tm_initialize(
int argc,
VALUE *argv,
VALUE time)
5600 time_arg(argc, argv, &
vtm);
5602 struct time_object *tobj = RTYPEDDATA_GET_DATA(time);
5603 TZMODE_SET_UTC(tobj);
5604 time_set_timew(time, tobj, t);
5605 time_set_vtm(time, tobj,
vtm);
5622 struct time_object *tobj = RTYPEDDATA_GET_DATA(dup);
5630 return time_add0(
rb_obj_class(tm), get_timeval(tm), tm, offset, +1);
5636 return time_add0(
rb_obj_class(tm), get_timeval(tm), tm, offset, -1);
5640Init_tm(
VALUE outer,
const char *name)
5686rb_time_zone_abbreviation(
VALUE zone,
VALUE time)
5688 VALUE tm, abbr, strftime_args[2];
5691 if (!
NIL_P(abbr))
return abbr;
5693 tm = tm_from_time(rb_cTimeTM, time);
5695 if (!UNDEF_P(abbr)) {
5698#ifdef SUPPORT_TZINFO_ZONE_ABBREVIATION
5700 if (!UNDEF_P(abbr)) {
5701 abbr = rb_funcallv(abbr, rb_intern(
"abbreviation"), 0, 0);
5705 strftime_args[0] = rb_fstring_lit(
"%Z");
5706 strftime_args[1] = tm;
5708 if (!UNDEF_P(abbr)) {
5711 abbr = rb_check_funcall_default(zone, idName, 0, 0,
Qnil);
5713 return rb_obj_as_string(abbr);
5763 str_utc = rb_fstring_lit(
"UTC");
5764 rb_gc_register_mark_object(str_utc);
5765 str_empty = rb_fstring_lit(
"");
5766 rb_gc_register_mark_object(str_empty);
5848 if (debug_find_time_numguess) {
5850 find_time_numguess_getter, 0);
5853 rb_cTimeTM = Init_tm(
rb_cTime,
"tm");
5856#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 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.
int rb_typeddata_is_kind_of(VALUE obj, const rb_data_type_t *data_type)
Checks if the given object is of given kind.
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_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.
static bool rb_enc_str_asciicompat_p(VALUE str)
Queries if the passed string is in an ASCII-compatible 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.
#define rb_Rational1(x)
Shorthand of (x/1)r.
#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.
#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.
#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_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 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 StringValue(v)
Ensures that the parameter object is a String.
#define StringValuePtr(v)
Identical to StringValue, except it returns a char*.
#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.