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"
50static ID id_submicro, id_nano_num, id_nano_den, id_offset, id_zone;
51static ID id_nanosecond, id_microsecond, id_millisecond, id_nsec, id_usec;
52static ID id_local_to_utc, id_utc_to_local, id_find_timezone;
53static ID id_year, id_mon, id_mday, id_hour, id_min, id_sec, id_isdst;
54static VALUE str_utc, str_empty;
57static VALUE sym_year, sym_month, sym_day, sym_yday, sym_wday;
58static VALUE sym_hour, sym_min, sym_sec, sym_subsec, sym_dst, sym_zone;
62#define id_divmod idDivmod
64#define UTC_ZONE Qundef
66#define NDIV(x,y) (-(-((x)+1)/(y))-1)
67#define NMOD(x,y) ((y)-(-((x)+1)%(y))-1)
68#define DIV(n,d) ((n)<0 ? NDIV((n),(d)) : (n)/(d))
69#define MOD(n,d) ((n)<0 ? NMOD((n),(d)) : (n)%(d))
70#define VTM_WDAY_INITVAL (7)
71#define VTM_ISDST_INITVAL (3)
86 if ((
long)x < (
long)y)
88 if ((
long)x > (
long)y)
92 if (RB_BIGNUM_TYPE_P(x))
return FIX2INT(rb_big_cmp(x, y));
93 return rb_cmpint(
rb_funcall(x, idCmp, 1, y), x, y);
96#define ne(x,y) (!eq((x),(y)))
97#define lt(x,y) (cmp((x),(y)) < 0)
98#define gt(x,y) (cmp((x),(y)) > 0)
99#define le(x,y) (cmp((x),(y)) <= 0)
100#define ge(x,y) (cmp((x),(y)) >= 0)
108 if (RB_BIGNUM_TYPE_P(x))
return rb_big_plus(x, y);
118 if (RB_BIGNUM_TYPE_P(x))
return rb_big_minus(x, y);
126 return rb_fix_mul_fix(x, y);
128 if (RB_BIGNUM_TYPE_P(x))
129 return rb_big_mul(x, y);
137 return rb_fix_div_fix(x, y);
139 if (RB_BIGNUM_TYPE_P(x))
140 return rb_big_div(x, y);
149 if (
FIXNUM_P(x))
return rb_fix_mod_fix(x, y);
151 if (RB_BIGNUM_TYPE_P(x))
return rb_big_modulo(x, y);
155#define neg(x) (subv(INT2FIX(0), (x)))
171 return rb_numeric_quo(x, y);
177 VALUE ret = quor(x, y);
179 RRATIONAL(ret)->den ==
INT2FIX(1)) {
180 ret = RRATIONAL(ret)->num;
185#define mulquov(x,y,z) (((y) == (z)) ? (x) : quov(mulv((x),(y)),(z)))
194 rb_fix_divmod_fix(n, d, q, r);
199 ary = rb_check_array_type(tmp);
201 rb_raise(
rb_eTypeError,
"unexpected divmod result: into %"PRIsVALUE,
204 *q = rb_ary_entry(ary, 0);
205 *r = rb_ary_entry(ary, 1);
209# define INT64toNUM(x) LONG2NUM(x)
210#elif defined(HAVE_LONG_LONG) && SIZEOF_LONG_LONG == 8
211# define INT64toNUM(x) LL2NUM(x)
214#if defined(HAVE_UINT64_T) && SIZEOF_LONG*2 <= SIZEOF_UINT64_T
215 typedef uint64_t uwideint_t;
216 typedef int64_t wideint_t;
217 typedef uint64_t WIDEVALUE;
218 typedef int64_t SIGNED_WIDEVALUE;
219# define WIDEVALUE_IS_WIDER 1
220# define UWIDEINT_MAX UINT64_MAX
221# define WIDEINT_MAX INT64_MAX
222# define WIDEINT_MIN INT64_MIN
223# define FIXWINT_P(tv) ((tv) & 1)
224# define FIXWVtoINT64(tv) RSHIFT((SIGNED_WIDEVALUE)(tv), 1)
225# define INT64toFIXWV(wi) ((WIDEVALUE)((SIGNED_WIDEVALUE)(wi) << 1 | FIXNUM_FLAG))
226# define FIXWV_MAX (((int64_t)1 << 62) - 1)
227# define FIXWV_MIN (-((int64_t)1 << 62))
228# define FIXWVABLE(wi) (POSFIXWVABLE(wi) && NEGFIXWVABLE(wi))
229# define WINT2FIXWV(i) WIDEVAL_WRAP(INT64toFIXWV(i))
230# define FIXWV2WINT(w) FIXWVtoINT64(WIDEVAL_GET(w))
232 typedef unsigned long uwideint_t;
233 typedef long wideint_t;
234 typedef VALUE WIDEVALUE;
236# define WIDEVALUE_IS_WIDER 0
237# define UWIDEINT_MAX ULONG_MAX
238# define WIDEINT_MAX LONG_MAX
239# define WIDEINT_MIN LONG_MIN
240# define FIXWINT_P(v) FIXNUM_P(v)
241# define FIXWV_MAX FIXNUM_MAX
242# define FIXWV_MIN FIXNUM_MIN
243# define FIXWVABLE(i) FIXABLE(i)
244# define WINT2FIXWV(i) WIDEVAL_WRAP(LONG2FIX(i))
245# define FIXWV2WINT(w) FIX2LONG(WIDEVAL_GET(w))
248#define POSFIXWVABLE(wi) ((wi) < FIXWV_MAX+1)
249#define NEGFIXWVABLE(wi) ((wi) >= FIXWV_MIN)
250#define FIXWV_P(w) FIXWINT_P(WIDEVAL_GET(w))
251#define MUL_OVERFLOW_FIXWV_P(a, b) MUL_OVERFLOW_SIGNED_INTEGER_P(a, b, FIXWV_MIN, FIXWV_MAX)
259 static inline wideval_t WIDEVAL_WRAP(WIDEVALUE v) { wideval_t w = { v };
return w; }
260# define WIDEVAL_GET(w) ((w).value)
262 typedef WIDEVALUE wideval_t;
263# define WIDEVAL_WRAP(v) (v)
264# define WIDEVAL_GET(w) (w)
267#if WIDEVALUE_IS_WIDER
268 static inline wideval_t
269 wint2wv(wideint_t wi)
272 return WINT2FIXWV(wi);
274 return WIDEVAL_WRAP(INT64toNUM(wi));
276# define WINT2WV(wi) wint2wv(wi)
278# define WINT2WV(wi) WIDEVAL_WRAP(LONG2NUM(wi))
284#if WIDEVALUE_IS_WIDER
286 return INT64toNUM(FIXWV2WINT(w));
287 return (
VALUE)WIDEVAL_GET(w);
289 return WIDEVAL_GET(w);
293#if WIDEVALUE_IS_WIDER
299 sign = rb_integer_pack(v, &u, 1,
sizeof(u), 0,
302 return WINT2FIXWV(0);
303 else if (sign == -1) {
305 return WINT2FIXWV(-(wideint_t)u);
307 else if (sign == +1) {
309 return WINT2FIXWV((wideint_t)u);
311 return WIDEVAL_WRAP(v);
315static inline wideval_t
319 if (RRATIONAL(v)->den !=
LONG2FIX(1))
320 return WIDEVAL_WRAP(v);
321 v = RRATIONAL(v)->num;
323#if WIDEVALUE_IS_WIDER
325 return WIDEVAL_WRAP((WIDEVALUE)(SIGNED_WIDEVALUE)(
long)v);
327 else if (RB_BIGNUM_TYPE_P(v) &&
328 rb_absint_size(v, NULL) <=
sizeof(WIDEVALUE)) {
329 return v2w_bignum(v);
332 return WIDEVAL_WRAP(v);
335#define NUM2WV(v) v2w(rb_Integer(v))
338weq(wideval_t wx, wideval_t wy)
340#if WIDEVALUE_IS_WIDER
341 if (FIXWV_P(wx) && FIXWV_P(wy)) {
342 return WIDEVAL_GET(wx) == WIDEVAL_GET(wy);
346 return eq(WIDEVAL_GET(wx), WIDEVAL_GET(wy));
351wcmp(wideval_t wx, wideval_t wy)
354#if WIDEVALUE_IS_WIDER
355 if (FIXWV_P(wx) && FIXWV_P(wy)) {
371#define wne(x,y) (!weq((x),(y)))
372#define wlt(x,y) (wcmp((x),(y)) < 0)
373#define wgt(x,y) (wcmp((x),(y)) > 0)
374#define wle(x,y) (wcmp((x),(y)) <= 0)
375#define wge(x,y) (wcmp((x),(y)) >= 0)
378wadd(wideval_t wx, wideval_t wy)
380#if WIDEVALUE_IS_WIDER
381 if (FIXWV_P(wx) && FIXWV_P(wy)) {
382 wideint_t r = FIXWV2WINT(wx) + FIXWV2WINT(wy);
386 return v2w(addv(w2v(wx), w2v(wy)));
390wsub(wideval_t wx, wideval_t wy)
392#if WIDEVALUE_IS_WIDER
393 if (FIXWV_P(wx) && FIXWV_P(wy)) {
394 wideint_t r = FIXWV2WINT(wx) - FIXWV2WINT(wy);
398 return v2w(subv(w2v(wx), w2v(wy)));
402wmul(wideval_t wx, wideval_t wy)
404#if WIDEVALUE_IS_WIDER
405 if (FIXWV_P(wx) && FIXWV_P(wy)) {
406 if (!MUL_OVERFLOW_FIXWV_P(FIXWV2WINT(wx), FIXWV2WINT(wy)))
407 return WINT2WV(FIXWV2WINT(wx) * FIXWV2WINT(wy));
410 return v2w(mulv(w2v(wx), w2v(wy)));
414wquo(wideval_t wx, wideval_t wy)
416#if WIDEVALUE_IS_WIDER
417 if (FIXWV_P(wx) && FIXWV_P(wy)) {
428 return v2w(quov(w2v(wx), w2v(wy)));
431#define wmulquo(x,y,z) ((WIDEVAL_GET(y) == WIDEVAL_GET(z)) ? (x) : wquo(wmul((x),(y)),(z)))
432#define wmulquoll(x,y,z) (((y) == (z)) ? (x) : wquo(wmul((x),WINT2WV(y)),WINT2WV(z)))
434#if WIDEVALUE_IS_WIDER
436wdivmod0(wideval_t wn, wideval_t wd, wideval_t *wq, wideval_t *wr)
438 if (FIXWV_P(wn) && FIXWV_P(wd)) {
439 wideint_t n, d, q, r;
448 wideint_t xneg = -FIXWV2WINT(wn);
461 if (d > 0 ? r < 0 : r > 0) {
474wdivmod(wideval_t wn, wideval_t wd, wideval_t *wq, wideval_t *wr)
477#if WIDEVALUE_IS_WIDER
478 if (wdivmod0(wn, wd, wq, wr))
return;
480 divmodv(w2v(wn), w2v(wd), &vq, &vr);
486wmuldivmod(wideval_t wx, wideval_t wy, wideval_t wz, wideval_t *wq, wideval_t *wr)
488 if (WIDEVAL_GET(wy) == WIDEVAL_GET(wz)) {
493 wdivmod(wmul(wx,wy), wz, wq, wr);
497wdiv(wideval_t wx, wideval_t wy)
499#if WIDEVALUE_IS_WIDER
501 if (wdivmod0(wx, wy, &q, &dmy))
return q;
503 return v2w(divv(w2v(wx), w2v(wy)));
507wmod(wideval_t wx, wideval_t wy)
509#if WIDEVALUE_IS_WIDER
511 if (wdivmod0(wx, wy, &dmy, &r))
return r;
513 return v2w(modv(w2v(wx), w2v(wy)));
517num_exact_check(
VALUE v)
528 tmp = rb_rational_canonicalize(v);
542 tmp = rb_rational_canonicalize(tmp);
558NORETURN(
static void num_exact_fail(
VALUE v));
560num_exact_fail(
VALUE v)
562 rb_raise(
rb_eTypeError,
"can't convert %"PRIsVALUE
" into an exact number",
569 VALUE num = num_exact_check(v);
570 if (
NIL_P(num)) num_exact_fail(v);
577static const int TIME_SCALE_NUMDIGITS =
rb_strlen_lit(STRINGIZE(TIME_SCALE)) - 1;
580rb_time_magnify(wideval_t w)
582 return wmul(w, WINT2FIXWV(TIME_SCALE));
586rb_time_unmagnify_to_rational(wideval_t w)
588 return quor(w2v(w),
INT2FIX(TIME_SCALE));
592rb_time_unmagnify(wideval_t w)
594 return v2w(rb_time_unmagnify_to_rational(w));
598rb_time_unmagnify_to_float(wideval_t w)
601#if WIDEVALUE_IS_WIDER
610 v =
DBL2NUM((
double)FIXWV2WINT(w));
611 return quov(v,
DBL2NUM(TIME_SCALE));
618 return quov(v,
DBL2NUM(TIME_SCALE));
622split_second(wideval_t timew, wideval_t *timew_p,
VALUE *subsecx_p)
625 wdivmod(timew, WINT2FIXWV(TIME_SCALE), &q, &r);
633#if WIDEVALUE_IS_WIDER
634 if (TIMET_MIN == 0) {
635 uwideint_t wi = (uwideint_t)t;
636 if (wi <= FIXWV_MAX) {
637 return WINT2FIXWV(wi);
641 wideint_t wi = (wideint_t)t;
642 if (FIXWV_MIN <= wi && wi <= FIXWV_MAX) {
643 return WINT2FIXWV(wi);
647 return v2w(TIMET2NUM(t));
649#define TIMET2WV(t) timet2wv(t)
654#if WIDEVALUE_IS_WIDER
656 wideint_t wi = FIXWV2WINT(w);
657 if (TIMET_MIN == 0) {
659 rb_raise(
rb_eRangeError,
"negative value to convert into 'time_t'");
660 if (TIMET_MAX < (uwideint_t)wi)
664 if (wi < TIMET_MIN || TIMET_MAX < wi)
670 return NUM2TIMET(w2v(w));
672#define WV2TIMET(t) wv2timet(t)
675static VALUE rb_cTimeTM;
677static int obj2int(
VALUE obj);
678static uint32_t obj2ubits(
VALUE obj,
unsigned int bits);
680static uint32_t month_arg(
VALUE arg);
681static VALUE validate_utc_offset(
VALUE utc_offset);
682static VALUE validate_zone_name(
VALUE zone_name);
683static void validate_vtm(
struct vtm *
vtm);
684static void vtm_add_day(
struct vtm *
vtm,
int day);
685static uint32_t obj2subsecx(
VALUE obj,
VALUE *subsecx);
692static time_t timegm_noleapsecond(
struct tm *tm);
693static int tmcmp(
struct tm *a,
struct tm *b);
694static int vtmcmp(
struct vtm *a,
struct vtm *b);
695static const char *find_time_t(
struct tm *tptr,
int utc_p, time_t *tp);
697static struct vtm *localtimew(wideval_t timew,
struct vtm *result);
699static int leap_year_p(
long y);
700#define leap_year_v_p(y) leap_year_p(NUM2LONG(modv((y), INT2FIX(400))))
704bool ruby_tz_uptodate_p;
707ruby_reset_timezone(
void)
709 ruby_tz_uptodate_p =
false;
710 ruby_reset_leap_second_info();
716 if (ruby_tz_uptodate_p)
return;
717 ruby_tz_uptodate_p =
true;
722rb_localtime_r(
const time_t *t,
struct tm *result)
724#if defined __APPLE__ && defined __LP64__
725 if (*t != (time_t)(
int)*t)
return NULL;
729 result = localtime_r(t, result);
732 struct tm *tmp = localtime(t);
733 if (tmp) *result = *tmp;
736#if defined(HAVE_MKTIME) && defined(LOCALTIME_OVERFLOW_PROBLEM)
740 struct tm tmp = *result;
743# if defined(HAVE_STRUCT_TM_TM_GMTOFF)
744 gmtoff1 = result->tm_gmtoff;
745 gmtoff2 = tmp.tm_gmtoff;
747 if (*t + gmtoff1 != t2 + gmtoff2)
753#define LOCALTIME(tm, result) rb_localtime_r((tm), &(result))
755#ifndef HAVE_STRUCT_TM_TM_GMTOFF
757rb_gmtime_r(
const time_t *t,
struct tm *result)
760 result = gmtime_r(t, result);
762 struct tm *tmp = gmtime(t);
763 if (tmp) *result = *tmp;
765#if defined(HAVE_TIMEGM) && defined(LOCALTIME_OVERFLOW_PROBLEM)
766 if (result && *t != timegm(result)) {
772# define GMTIME(tm, result) rb_gmtime_r((tm), &(result))
775static const int16_t common_year_yday_offset[] = {
780 -1 + 31 + 28 + 31 + 30,
781 -1 + 31 + 28 + 31 + 30 + 31,
782 -1 + 31 + 28 + 31 + 30 + 31 + 30,
783 -1 + 31 + 28 + 31 + 30 + 31 + 30 + 31,
784 -1 + 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31,
785 -1 + 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30,
786 -1 + 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31,
787 -1 + 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30
790static const int16_t leap_year_yday_offset[] = {
795 -1 + 31 + 29 + 31 + 30,
796 -1 + 31 + 29 + 31 + 30 + 31,
797 -1 + 31 + 29 + 31 + 30 + 31 + 30,
798 -1 + 31 + 29 + 31 + 30 + 31 + 30 + 31,
799 -1 + 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31,
800 -1 + 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30,
801 -1 + 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31,
802 -1 + 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30
806static const int8_t common_year_days_in_month[] = {
807 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
809static const int8_t leap_year_days_in_month[] = {
810 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
813#define days_in_month_of(leap) ((leap) ? leap_year_days_in_month : common_year_days_in_month)
814#define days_in_month_in(y) days_in_month_of(leap_year_p(y))
815#define days_in_month_in_v(y) days_in_month_of(leap_year_v_p(y))
818 (m),(m),(m),(m),(m),(m),(m),(m),(m),(m), \
819 (m),(m),(m),(m),(m),(m),(m),(m),(m),(m), \
820 (m),(m),(m),(m),(m),(m),(m),(m)
822 (m),(m),(m),(m),(m),(m),(m),(m),(m),(m), \
823 (m),(m),(m),(m),(m),(m),(m),(m),(m),(m), \
824 (m),(m),(m),(m),(m),(m),(m),(m),(m)
826 (m),(m),(m),(m),(m),(m),(m),(m),(m),(m), \
827 (m),(m),(m),(m),(m),(m),(m),(m),(m),(m), \
828 (m),(m),(m),(m),(m),(m),(m),(m),(m),(m)
830 (m),(m),(m),(m),(m),(m),(m),(m),(m),(m), \
831 (m),(m),(m),(m),(m),(m),(m),(m),(m),(m), \
832 (m),(m),(m),(m),(m),(m),(m),(m),(m),(m), (m)
834static const uint8_t common_year_mon_of_yday[] = {
835 M31(1), M28(2), M31(3), M30(4), M31(5), M30(6),
836 M31(7), M31(8), M30(9), M31(10), M30(11), M31(12)
838static const uint8_t leap_year_mon_of_yday[] = {
839 M31(1), M29(2), M31(3), M30(4), M31(5), M30(6),
840 M31(7), M31(8), M30(9), M31(10), M30(11), M31(12)
850 10,11,12,13,14,15,16,17,18,19, \
851 20,21,22,23,24,25,26,27,28
854 10,11,12,13,14,15,16,17,18,19, \
855 20,21,22,23,24,25,26,27,28,29
858 10,11,12,13,14,15,16,17,18,19, \
859 20,21,22,23,24,25,26,27,28,29,30
862 10,11,12,13,14,15,16,17,18,19, \
863 20,21,22,23,24,25,26,27,28,29,30,31
865static const uint8_t common_year_mday_of_yday[] = {
867 D31, D28, D31, D30, D31, D30, D31, D31, D30, D31, D30, D31
869static const uint8_t leap_year_mday_of_yday[] = {
870 D31, D29, D31, D30, D31, D30, D31, D31, D30, D31, D30, D31
879calc_tm_yday(
long tm_year,
int tm_mon,
int tm_mday)
881 int tm_year_mod400 = (int)MOD(tm_year, 400);
882 int tm_yday = tm_mday;
884 if (leap_year_p(tm_year_mod400 + 1900))
885 tm_yday += leap_year_yday_offset[tm_mon];
887 tm_yday += common_year_yday_offset[tm_mon];
893timegmw_noleapsecond(
struct vtm *
vtm)
905 divmodv(year1900,
INT2FIX(400), &q400, &r400);
908 yday = calc_tm_yday(year_mod400,
vtm->mon-1,
vtm->mday);
921 + DIV(year_mod400 - 69, 4)
922 - DIV(year_mod400 - 1, 100)
923 + (year_mod400 + 299) / 400;
925 vdays = addv(vdays, mulv(q400,
INT2FIX(97)));
926 vdays = addv(vdays, mulv(year1900,
INT2FIX(365)));
927 wret = wadd(rb_time_magnify(v2w(ret)), wmul(rb_time_magnify(v2w(vdays)), WINT2FIXWV(86400)));
928 wret = wadd(wret, v2w(
vtm->subsecx));
934zone_str(
const char *zone)
942 return rb_fstring_lit(
"(NO-TIMEZONE-ABBREVIATION)");
945 for (p = zone; *p; p++)
950 len = p - zone + strlen(p);
955 str = rb_enc_str_new(zone,
len, rb_locale_encoding());
957 return rb_fstring(str);
961gmtimew_noleapsecond(wideval_t timew,
struct vtm *
vtm)
967 wideval_t timew2, w, w2;
972 split_second(timew, &timew2, &subsecx);
973 vtm->subsecx = subsecx;
975 wdivmod(timew2, WINT2FIXWV(86400), &w2, &w);
980 vtm->wday = (wday + 4) % 7;
983 vtm->sec = n % 60; n = n / 60;
984 vtm->min = n % 60; n = n / 60;
988 divmodv(timev,
INT2FIX(400*365 + 97), &timev, &v);
1001 if (30*365+7+31+29-1 <= n) {
1015 x = n / (365*100 + 24);
1016 n = n % (365*100 + 24);
1018 if (30*365+7+31+29-1 <= n) {
1028 x = n / (365*4 + 1);
1029 n = n % (365*4 + 1);
1031 if (365*2+31+29-1 <= n) {
1032 if (n < 365*2+366) {
1049 if (leap_year_p(y)) {
1050 vtm->mon = leap_year_mon_of_yday[n];
1051 vtm->mday = leap_year_mday_of_yday[n];
1054 vtm->mon = common_year_mon_of_yday[n];
1055 vtm->mday = common_year_mday_of_yday[n];
1059 vtm->zone = str_utc;
1063gmtime_with_leapsecond(
const time_t *timep,
struct tm *result)
1065#if defined(HAVE_STRUCT_TM_TM_GMTOFF)
1069 int gmtoff_sec, gmtoff_min, gmtoff_hour, gmtoff_day;
1071 t = LOCALTIME(timep, *result);
1076 if (t->tm_gmtoff < 0) {
1078 gmtoff = -t->tm_gmtoff;
1082 gmtoff = t->tm_gmtoff;
1084 gmtoff_sec = (int)(gmtoff % 60);
1085 gmtoff = gmtoff / 60;
1086 gmtoff_min = (int)(gmtoff % 60);
1087 gmtoff = gmtoff / 60;
1088 gmtoff_hour = (int)gmtoff;
1092 gmtoff_hour *= sign;
1099 result->tm_sec += gmtoff_sec;
1100 if (result->tm_sec < 0) {
1101 result->tm_sec += 60;
1104 if (60 <= result->tm_sec) {
1105 result->tm_sec -= 60;
1110 result->tm_min += gmtoff_min;
1111 if (result->tm_min < 0) {
1112 result->tm_min += 60;
1115 if (60 <= result->tm_min) {
1116 result->tm_min -= 60;
1121 result->tm_hour += gmtoff_hour;
1122 if (result->tm_hour < 0) {
1123 result->tm_hour += 24;
1126 if (24 <= result->tm_hour) {
1127 result->tm_hour -= 24;
1133 if (gmtoff_day < 0) {
1134 if (result->tm_yday == 0) {
1135 result->tm_mday = 31;
1136 result->tm_mon = 11;
1138 result->tm_yday = leap_year_p(result->tm_year + 1900) ? 365 : 364;
1140 else if (result->tm_mday == 1) {
1141 const int8_t *days_in_month = days_in_month_in(result->tm_year + 1900);
1143 result->tm_mday = days_in_month[result->tm_mon];
1150 result->tm_wday = (result->tm_wday + 6) % 7;
1153 int leap = leap_year_p(result->tm_year + 1900);
1154 if (result->tm_yday == (leap ? 365 : 364)) {
1157 result->tm_mday = 1;
1158 result->tm_yday = 0;
1160 else if (result->tm_mday == days_in_month_of(leap)[result->tm_mon]) {
1162 result->tm_mday = 1;
1169 result->tm_wday = (result->tm_wday + 1) % 7;
1172 result->tm_isdst = 0;
1173 result->tm_gmtoff = 0;
1174#if defined(HAVE_TM_ZONE)
1175 result->tm_zone = (
char *)
"UTC";
1179 return GMTIME(timep, *result);
1183static long this_year = 0;
1184static time_t known_leap_seconds_limit;
1185static int number_of_leap_seconds_known;
1188init_leap_second_info(
void)
1195 if (this_year == 0) {
1197 struct tm *tm, result;
1202 gmtime_r(&now, &result);
1206 tm = gmtime_with_leapsecond(&now, &result);
1208 this_year = tm->tm_year;
1210 if (TIMET_MAX - now < (time_t)(366*86400))
1211 known_leap_seconds_limit = TIMET_MAX;
1213 known_leap_seconds_limit = now + (time_t)(366*86400);
1215 if (!gmtime_with_leapsecond(&known_leap_seconds_limit, &result))
1219 vtm.mon = result.tm_mon + 1;
1220 vtm.mday = result.tm_mday;
1221 vtm.hour = result.tm_hour;
1222 vtm.min = result.tm_min;
1223 vtm.sec = result.tm_sec;
1227 timew = timegmw_noleapsecond(&
vtm);
1229 number_of_leap_seconds_known =
NUM2INT(w2v(wsub(TIMET2WV(known_leap_seconds_limit), rb_time_unmagnify(timew))));
1235ruby_reset_leap_second_info(
void)
1251 return timegmw_noleapsecond(
vtm);
1253 init_leap_second_info();
1255 timew = timegmw_noleapsecond(
vtm);
1258 if (number_of_leap_seconds_known == 0) {
1264 else if (wlt(rb_time_magnify(TIMET2WV(known_leap_seconds_limit)), timew)) {
1265 return wadd(timew, rb_time_magnify(WINT2WV(number_of_leap_seconds_known)));
1269 tm.tm_mon =
vtm->mon - 1;
1270 tm.tm_mday =
vtm->mday;
1271 tm.tm_hour =
vtm->hour;
1272 tm.tm_min =
vtm->min;
1273 tm.tm_sec =
vtm->sec;
1276 errmsg = find_time_t(&tm, 1, &t);
1278 rb_raise(rb_eArgError,
"%s", errmsg);
1279 return wadd(rb_time_magnify(TIMET2WV(t)), v2w(
vtm->subsecx));
1283gmtimew(wideval_t timew,
struct vtm *result)
1290 if (wlt(timew, WINT2FIXWV(0))) {
1291 gmtimew_noleapsecond(timew, result);
1295 init_leap_second_info();
1297 if (number_of_leap_seconds_known == 0) {
1301 gmtimew_noleapsecond(timew, result);
1304 else if (wlt(rb_time_magnify(TIMET2WV(known_leap_seconds_limit)), timew)) {
1305 timew = wsub(timew, rb_time_magnify(WINT2WV(number_of_leap_seconds_known)));
1306 gmtimew_noleapsecond(timew, result);
1310 split_second(timew, &timew2, &subsecx);
1312 t = WV2TIMET(timew2);
1313 if (!gmtime_with_leapsecond(&t, &tm))
1316 result->year =
LONG2NUM((
long)tm.tm_year + 1900);
1317 result->mon = tm.tm_mon + 1;
1318 result->mday = tm.tm_mday;
1319 result->hour = tm.tm_hour;
1320 result->min = tm.tm_min;
1321 result->sec = tm.tm_sec;
1322 result->subsecx = subsecx;
1323 result->utc_offset =
INT2FIX(0);
1324 result->wday = tm.tm_wday;
1325 result->yday = tm.tm_yday+1;
1326 result->isdst = tm.tm_isdst;
1331#define GMTIMEW(w, v) \
1332 (gmtimew(w, v) ? (void)0 : rb_raise(rb_eArgError, "gmtime error"))
1334static struct tm *localtime_with_gmtoff_zone(
const time_t *t,
struct tm *result,
long *gmtoff,
VALUE *zone);
1369static const int compat_common_month_table[12][7] = {
1371 { 2034, 2035, 2036, 2031, 2032, 2027, 2033 },
1372 { 2026, 2027, 2033, 2034, 2035, 2030, 2031 },
1373 { 2026, 2032, 2033, 2034, 2035, 2030, 2036 },
1374 { 2035, 2030, 2036, 2026, 2032, 2033, 2034 },
1375 { 2033, 2034, 2035, 2030, 2036, 2026, 2032 },
1376 { 2036, 2026, 2032, 2033, 2034, 2035, 2030 },
1377 { 2035, 2030, 2036, 2026, 2032, 2033, 2034 },
1378 { 2032, 2033, 2034, 2035, 2030, 2036, 2026 },
1379 { 2030, 2036, 2026, 2032, 2033, 2034, 2035 },
1380 { 2034, 2035, 2030, 2036, 2026, 2032, 2033 },
1381 { 2026, 2032, 2033, 2034, 2035, 2030, 2036 },
1382 { 2030, 2036, 2026, 2032, 2033, 2034, 2035 },
1410static const int compat_leap_month_table[7] = {
1412 2032, 2016, 2028, 2012, 2024, 2036, 2020,
1416calc_wday(
int year_mod400,
int month,
int day)
1421 a = (14 - month) / 12;
1422 y = year_mod400 + 4800 - a;
1423 m = month + 12 * a - 3;
1424 wday = day + (153*m+2)/5 + 365*y + y/4 - y/100 + y/400 + 2;
1430guess_local_offset(
struct vtm *vtm_utc,
int *isdst_ret,
VALUE *zone_ret)
1438 int year_mod400, wday;
1442 if (lt(vtm_utc->year,
INT2FIX(1916))) {
1445 zone = rb_fstring_lit(
"UTC");
1447# if defined(NEGATIVE_TIME_T)
1448# if SIZEOF_TIME_T <= 4
1450# define THE_TIME_OLD_ENOUGH ((time_t)0x80000000)
1454# define THE_TIME_OLD_ENOUGH ((time_t)(1600-1970)*366*24*60*60)
1456 if (localtime_with_gmtoff_zone((t = THE_TIME_OLD_ENOUGH, &t), &tm, &gmtoff, &zone)) {
1458 isdst = tm.tm_isdst;
1463 if (localtime_with_gmtoff_zone((t = 0, &t), &tm, &gmtoff, &zone)) {
1465 isdst = tm.tm_isdst;
1481 wday = calc_wday(year_mod400, vtm_utc->mon, 1);
1482 if (vtm_utc->mon == 2 && leap_year_p(year_mod400))
1483 vtm2.year =
INT2FIX(compat_leap_month_table[wday]);
1485 vtm2.year =
INT2FIX(compat_common_month_table[vtm_utc->mon-1][wday]);
1487 timev = w2v(rb_time_unmagnify(timegmw(&vtm2)));
1488 t = NUM2TIMET(timev);
1490 if (localtime_with_gmtoff_zone(&t, &tm, &gmtoff, &zone)) {
1492 *isdst_ret = tm.tm_isdst;
1500 static time_t now = 0;
1501 static long now_gmtoff = 0;
1502 static int now_isdst = 0;
1503 static VALUE now_zone;
1507 localtime_with_gmtoff_zone(&now, &tm, &now_gmtoff, &zone);
1508 now_isdst = tm.tm_isdst;
1509 zone = rb_fstring(zone);
1510 rb_vm_register_global_object(zone);
1514 *isdst_ret = now_isdst;
1516 *zone_ret = now_zone;
1522small_vtm_sub(
struct vtm *vtm1,
struct vtm *vtm2)
1526 off = vtm1->sec - vtm2->sec;
1527 off += (vtm1->min - vtm2->min) * 60;
1528 off += (vtm1->hour - vtm2->hour) * 3600;
1529 if (ne(vtm1->year, vtm2->year))
1530 off += lt(vtm1->year, vtm2->year) ? -24*3600 : 24*3600;
1531 else if (vtm1->mon != vtm2->mon)
1532 off += vtm1->mon < vtm2->mon ? -24*3600 : 24*3600;
1533 else if (vtm1->mday != vtm2->mday)
1534 off += vtm1->mday < vtm2->mday ? -24*3600 : 24*3600;
1540timelocalw(
struct vtm *
vtm)
1545 wideval_t timew1, timew2;
1546 struct vtm vtm1, vtm2;
1551 if (l < INT_MIN || INT_MAX < l)
1553 tm.tm_year = (int)l;
1562 tm.tm_mon =
vtm->mon-1;
1563 tm.tm_mday =
vtm->mday;
1564 tm.tm_hour =
vtm->hour;
1565 tm.tm_min =
vtm->min;
1566 tm.tm_sec =
vtm->sec;
1567 tm.tm_isdst =
vtm->isdst == VTM_ISDST_INITVAL ? -1 :
vtm->isdst;
1569 if (find_time_t(&tm, 0, &t))
1571 return wadd(rb_time_magnify(TIMET2WV(t)), v2w(
vtm->subsecx));
1574 timew1 = timegmw(
vtm);
1576 if (!localtimew(timew1, &vtm1))
1577 rb_raise(rb_eArgError,
"localtimew error");
1579 n = vtmcmp(
vtm, &vtm1);
1581 timew1 = wsub(timew1, rb_time_magnify(WINT2FIXWV(12*3600)));
1582 if (!localtimew(timew1, &vtm1))
1583 rb_raise(rb_eArgError,
"localtimew error");
1590 timew1 = wsub(timew1, rb_time_magnify(WINT2FIXWV(24*3600)));
1591 if (!localtimew(timew1, &vtm1))
1592 rb_raise(rb_eArgError,
"localtimew error");
1595 timew2 = wadd(timew1, rb_time_magnify(WINT2FIXWV(24*3600)));
1596 if (!localtimew(timew2, &vtm2))
1597 rb_raise(rb_eArgError,
"localtimew error");
1599 timew1 = wadd(timew1, rb_time_magnify(v2w(small_vtm_sub(
vtm, &vtm1))));
1600 timew2 = wadd(timew2, rb_time_magnify(v2w(small_vtm_sub(
vtm, &vtm2))));
1602 if (weq(timew1, timew2))
1605 if (!localtimew(timew1, &vtm1))
1606 rb_raise(rb_eArgError,
"localtimew error");
1607 if (
vtm->hour != vtm1.hour ||
vtm->min != vtm1.min ||
vtm->sec != vtm1.sec)
1610 if (!localtimew(timew2, &vtm2))
1611 rb_raise(rb_eArgError,
"localtimew error");
1612 if (
vtm->hour != vtm2.hour ||
vtm->min != vtm2.min ||
vtm->sec != vtm2.sec)
1616 return lt(vtm1.utc_offset, vtm2.utc_offset) ? timew2 : timew1;
1618 return lt(vtm1.utc_offset, vtm2.utc_offset) ? timew1 : timew2;
1622localtime_with_gmtoff_zone(
const time_t *t,
struct tm *result,
long *gmtoff,
VALUE *zone)
1626 if (LOCALTIME(t, tm)) {
1627#if defined(HAVE_STRUCT_TM_TM_GMTOFF)
1628 *gmtoff = tm.tm_gmtoff;
1634 u = GMTIME(t, tmbuf);
1637 if (l->tm_year != u->tm_year)
1638 off = l->tm_year < u->tm_year ? -1 : 1;
1639 else if (l->tm_mon != u->tm_mon)
1640 off = l->tm_mon < u->tm_mon ? -1 : 1;
1641 else if (l->tm_mday != u->tm_mday)
1642 off = l->tm_mday < u->tm_mday ? -1 : 1;
1645 off =
off * 24 + l->tm_hour - u->tm_hour;
1646 off =
off * 60 + l->tm_min - u->tm_min;
1647 off =
off * 60 + l->tm_sec - u->tm_sec;
1652#if defined(HAVE_TM_ZONE)
1653 *zone = zone_str(tm.tm_zone);
1654#elif defined(HAVE_TZNAME) && defined(HAVE_DAYLIGHT)
1655# if defined(RUBY_MSVCRT_VERSION) && RUBY_MSVCRT_VERSION >= 140
1656# define tzname _tzname
1657# define daylight _daylight
1660 *zone = zone_str(tzname[daylight && tm.tm_isdst]);
1664 strftime(buf,
sizeof(buf),
"%Z", &tm);
1665 *zone = zone_str(buf);
1677timew_out_of_timet_range(wideval_t timew)
1680#if WIDEVALUE_IS_WIDER && SIZEOF_TIME_T < SIZEOF_INT64_T
1681 if (FIXWV_P(timew)) {
1682 wideint_t t = FIXWV2WINT(timew);
1683 if (t < TIME_SCALE * (wideint_t)TIMET_MIN ||
1684 TIME_SCALE * (1 + (wideint_t)TIMET_MAX) <= t)
1689#if SIZEOF_TIME_T == SIZEOF_INT64_T
1690 if (FIXWV_P(timew)) {
1691 wideint_t t = FIXWV2WINT(timew);
1692 if (~(time_t)0 <= 0) {
1702 timexv = w2v(timew);
1703 if (lt(timexv, mulv(
INT2FIX(TIME_SCALE), TIMET2NUM(TIMET_MIN))) ||
1704 le(mulv(
INT2FIX(TIME_SCALE), addv(TIMET2NUM(TIMET_MAX),
INT2FIX(1))), timexv))
1710localtimew(wideval_t timew,
struct vtm *result)
1712 VALUE subsecx, offset;
1716 if (!timew_out_of_timet_range(timew)) {
1722 split_second(timew, &timew2, &subsecx);
1724 t = WV2TIMET(timew2);
1726 if (localtime_with_gmtoff_zone(&t, &tm, &gmtoff, &zone)) {
1727 result->year =
LONG2NUM((
long)tm.tm_year + 1900);
1728 result->mon = tm.tm_mon + 1;
1729 result->mday = tm.tm_mday;
1730 result->hour = tm.tm_hour;
1731 result->min = tm.tm_min;
1732 result->sec = tm.tm_sec;
1733 result->subsecx = subsecx;
1734 result->wday = tm.tm_wday;
1735 result->yday = tm.tm_yday+1;
1736 result->isdst = tm.tm_isdst;
1737 result->utc_offset =
LONG2NUM(gmtoff);
1738 result->zone = zone;
1743 if (!gmtimew(timew, result))
1746 offset = guess_local_offset(result, &isdst, &zone);
1748 if (!gmtimew(wadd(timew, rb_time_magnify(v2w(offset))), result))
1751 result->utc_offset = offset;
1752 result->isdst = isdst;
1753 result->zone = zone;
1758#define TIME_TZMODE_LOCALTIME 0
1759#define TIME_TZMODE_UTC 1
1760#define TIME_TZMODE_FIXOFF 2
1761#define TIME_TZMODE_UNINITIALIZED 3
1768#define GetTimeval(obj, tobj) ((tobj) = get_timeval(obj))
1769#define GetNewTimeval(obj, tobj) ((tobj) = get_new_timeval(obj))
1771#define IsTimeval(obj) rb_typeddata_is_kind_of((obj), &time_data_type)
1772#define TIME_INIT_P(tobj) ((tobj)->vtm.tzmode != TIME_TZMODE_UNINITIALIZED)
1774#define TZMODE_UTC_P(tobj) ((tobj)->vtm.tzmode == TIME_TZMODE_UTC)
1775#define TZMODE_SET_UTC(tobj) ((tobj)->vtm.tzmode = TIME_TZMODE_UTC)
1777#define TZMODE_LOCALTIME_P(tobj) ((tobj)->vtm.tzmode == TIME_TZMODE_LOCALTIME)
1778#define TZMODE_SET_LOCALTIME(tobj) ((tobj)->vtm.tzmode = TIME_TZMODE_LOCALTIME)
1780#define TZMODE_FIXOFF_P(tobj) ((tobj)->vtm.tzmode == TIME_TZMODE_FIXOFF)
1781#define TZMODE_SET_FIXOFF(time, tobj, off) do { \
1782 (tobj)->vtm.tzmode = TIME_TZMODE_FIXOFF; \
1783 RB_OBJ_WRITE_UNALIGNED(time, &(tobj)->vtm.utc_offset, off); \
1786#define TZMODE_COPY(tobj1, tobj2) \
1787 ((tobj1)->vtm.tzmode = (tobj2)->vtm.tzmode, \
1788 (tobj1)->vtm.utc_offset = (tobj2)->vtm.utc_offset, \
1789 (tobj1)->vtm.zone = (tobj2)->vtm.zone)
1791static int zone_localtime(
VALUE zone,
VALUE time);
1793#define MAKE_TM(time, tobj) \
1795 if ((tobj)->vtm.tm_got == 0) { \
1796 time_get_tm((time), (tobj)); \
1799#define MAKE_TM_ENSURE(time, tobj, cond) \
1801 MAKE_TM(time, tobj); \
1803 force_make_tm(time, tobj); \
1810 tobj->timew = timew;
1811 if (!FIXWV_P(timew)) {
1830 VALUE zone = tobj->vtm.zone;
1831 if (!
NIL_P(zone) && zone != str_empty && zone != str_utc) {
1832 if (zone_localtime(zone, time))
return;
1834 tobj->vtm.tm_got = 0;
1835 time_get_tm(time, tobj);
1842 if (!FIXWV_P(tobj->timew))
1843 rb_gc_mark(w2v(tobj->timew));
1844 rb_gc_mark(tobj->vtm.year);
1845 rb_gc_mark(tobj->vtm.subsecx);
1846 rb_gc_mark(tobj->vtm.utc_offset);
1847 rb_gc_mark(tobj->vtm.zone);
1858 (RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_FROZEN_SHAREABLE | RUBY_TYPED_WB_PROTECTED | RUBY_TYPED_EMBEDDABLE),
1862time_s_alloc(
VALUE klass)
1868 tobj->vtm.tzmode = TIME_TZMODE_UNINITIALIZED;
1869 tobj->vtm.tm_got = 0;
1870 time_set_timew(obj, tobj, WINT2FIXWV(0));
1871 tobj->vtm.zone =
Qnil;
1877get_timeval(
VALUE obj)
1881 if (!TIME_INIT_P(tobj)) {
1888get_new_timeval(
VALUE obj)
1892 if (TIME_INIT_P(tobj)) {
1899time_modify(
VALUE time)
1901 rb_check_frozen(time);
1905timenano2timew(time_t sec,
long nsec)
1909 timew = rb_time_magnify(TIMET2WV(sec));
1911 timew = wadd(timew, wmulquoll(WINT2WV(nsec), TIME_SCALE, 1000000000));
1916timew2timespec(wideval_t timew)
1922 if (timew_out_of_timet_range(timew))
1923 rb_raise(rb_eArgError,
"time out of system range");
1924 split_second(timew, &timew2, &subsecx);
1925 ts.tv_sec = WV2TIMET(timew2);
1931timew2timespec_exact(wideval_t timew,
struct timespec *ts)
1937 if (timew_out_of_timet_range(timew))
1939 split_second(timew, &timew2, &subsecx);
1940 ts->tv_sec = WV2TIMET(timew2);
1941 nsecv = mulquov(subsecx,
INT2FIX(1000000000),
INT2FIX(TIME_SCALE));
1951#ifdef HAVE_CLOCK_GETTIME
1952 if (clock_gettime(CLOCK_REALTIME, ts) == -1) {
1953 rb_sys_fail(
"clock_gettime");
1958 if (gettimeofday(&tv, 0) < 0) {
1959 rb_sys_fail(
"gettimeofday");
1961 ts->tv_sec = tv.tv_sec;
1962 ts->tv_nsec = tv.tv_usec * 1000;
1972time_init_now(rb_execution_context_t *ec,
VALUE time,
VALUE zone)
1978 GetNewTimeval(time, tobj);
1979 TZMODE_SET_LOCALTIME(tobj);
1982 time_set_timew(time, tobj, timenano2timew(ts.tv_sec, ts.tv_nsec));
1985 time_zonelocal(time, zone);
1991time_s_now(rb_execution_context_t *ec,
VALUE klass,
VALUE zone)
1993 VALUE t = time_s_alloc(klass);
1994 return time_init_now(ec, t, zone);
2004 GetTimeval(time, tobj);
2006 tobj->vtm.tm_got = 0;
2007 tobj->vtm.zone =
Qnil;
2008 TZMODE_SET_FIXOFF(time, tobj,
off);
2033 subsec = neg(subsec);
2042 vtm->subsecx = addv(
vtm->subsecx, w2v(rb_time_magnify(v2w(subsec))));
2091 vtm_add_day(
vtm, day);
2095vtm_add_day(
struct vtm *
vtm,
int day)
2099 if (
vtm->mon == 1 &&
vtm->mday == 1) {
2104 vtm->yday = leap_year_v_p(
vtm->year) ? 366 : 365;
2106 else if (
vtm->mday == 1) {
2107 const int8_t *days_in_month = days_in_month_in_v(
vtm->year);
2109 vtm->mday = days_in_month[
vtm->mon-1];
2110 if (
vtm->yday != 0)
vtm->yday--;
2114 if (
vtm->yday != 0)
vtm->yday--;
2116 if (
vtm->wday != VTM_WDAY_INITVAL)
vtm->wday = (
vtm->wday + 6) % 7;
2119 int leap = leap_year_v_p(
vtm->year);
2120 if (
vtm->mon == 12 &&
vtm->mday == 31) {
2126 else if (
vtm->mday == days_in_month_of(leap)[
vtm->mon-1]) {
2129 if (
vtm->yday != 0)
vtm->yday++;
2133 if (
vtm->yday != 0)
vtm->yday++;
2135 if (
vtm->wday != VTM_WDAY_INITVAL)
vtm->wday = (
vtm->wday + 1) % 7;
2141maybe_tzobj_p(
VALUE obj)
2143 if (
NIL_P(obj))
return FALSE;
2149NORETURN(
static void invalid_utc_offset(
VALUE));
2151invalid_utc_offset(
VALUE zone)
2153 rb_raise(rb_eArgError,
"\"+HH:MM\", \"-HH:MM\", \"UTC\" or "
2154 "\"A\"..\"I\",\"K\"..\"Z\" expected for utc_offset: %"PRIsVALUE,
2158#define have_2digits(ptr) (ISDIGIT((ptr)[0]) && ISDIGIT((ptr)[1]))
2159#define num_from_2digits(ptr) ((ptr)[0] * 10 + (ptr)[1] - '0' * 11)
2162utc_offset_arg(
VALUE arg)
2167 const char *s = RSTRING_PTR(tmp), *min = NULL, *sec = NULL;
2169 goto invalid_utc_offset;
2171 switch (RSTRING_LEN(tmp)) {
2177 if (s[0] >=
'A' && s[0] <=
'I') {
2178 n = (int)s[0] -
'A' + 1;
2181 else if (s[0] >=
'K' && s[0] <=
'M') {
2182 n = (int)s[0] -
'A';
2184 else if (s[0] >=
'N' && s[0] <=
'Y') {
2185 n =
'M' - (int)s[0];
2188 goto invalid_utc_offset;
2204 if (s[6] !=
':')
goto invalid_utc_offset;
2208 if (s[3] !=
':')
goto invalid_utc_offset;
2212 goto invalid_utc_offset;
2215 if (!have_2digits(sec))
goto invalid_utc_offset;
2216 if (sec[0] >
'5')
goto invalid_utc_offset;
2217 n += num_from_2digits(sec);
2221 if (!have_2digits(min))
goto invalid_utc_offset;
2222 if (min[0] >
'5')
goto invalid_utc_offset;
2223 n += num_from_2digits(min) * 60;
2225 if (s[0] !=
'+' && s[0] !=
'-')
goto invalid_utc_offset;
2226 if (!have_2digits(s+1))
goto invalid_utc_offset;
2227 n += num_from_2digits(s+1) * 3600;
2229 if (n == 0)
return UTC_ZONE;
2235 return num_exact(arg);
2243 wideval_t tlocal, wideval_t tutc)
2246 wideval_t w = wsub(tlocal, tutc);
2248 validate_utc_offset(
off);
2249 tobj->vtm.utc_offset =
off;
2250 tobj->vtm.zone = zone;
2251 TZMODE_SET_LOCALTIME(tobj);
2255extract_time(
VALUE time)
2258 const ID id_to_i = idTo_i;
2260#define EXTRACT_TIME() do { \
2261 t = NUM2WV(AREF(to_i)); \
2265 struct time_object *tobj = RTYPEDDATA_GET_DATA(time);
2268 t = rb_time_unmagnify(tobj->timew);
2273#define AREF(x) rb_struct_aref(time, ID2SYM(id_##x))
2278#define AREF(x) rb_funcallv(time, id_##x, 0, 0)
2291 const ID id_to_i = idTo_i;
2292 struct vtm *
vtm = &orig_tobj->vtm;
2294#define EXTRACT_VTM() do { \
2296 vtm->year = obj2vint(AREF(year)); \
2297 vtm->mon = month_arg(AREF(mon)); \
2298 vtm->mday = obj2ubits(AREF(mday), 5); \
2299 vtm->hour = obj2ubits(AREF(hour), 5); \
2300 vtm->min = obj2ubits(AREF(min), 6); \
2301 vtm->sec = obj2subsecx(AREF(sec), &subsecx); \
2302 vtm->isdst = RTEST(AREF(isdst)); \
2303 vtm->utc_offset = Qnil; \
2304 t = NUM2WV(AREF(to_i)); \
2308 struct time_object *tobj = RTYPEDDATA_GET_DATA(time);
2310 time_get_tm(time, tobj);
2311 time_set_vtm(orig_time, orig_tobj, tobj->vtm);
2312 t = rb_time_unmagnify(tobj->timew);
2313 if (TZMODE_FIXOFF_P(tobj) &&
vtm->utc_offset !=
INT2FIX(0))
2314 t = wadd(t, v2w(
vtm->utc_offset));
2319#define AREF(x) rb_struct_aref(time, ID2SYM(id_##x))
2325 struct vtm temp_vtm = *
vtm;
2326 GMTIMEW(rb_time_magnify(t), &temp_vtm);
2327 time_set_vtm(orig_time, orig_tobj, temp_vtm);
2330#define AREF(x) rb_funcallv(time, id_##x, 0, 0)
2336 RB_OBJ_WRITE_UNALIGNED(orig_time, &
vtm->subsecx, subsecx);
2349 tobj->vtm.isdst = (!UNDEF_P(dst) &&
RTEST(dst));
2356 struct time_object *tobj = RTYPEDDATA_GET_DATA(time);
2359 wdivmod(tobj->timew, WINT2FIXWV(TIME_SCALE), &t, &s);
2360 tm = tm_from_time(rb_cTimeTM, time);
2362 if (UNDEF_P(utc))
return 0;
2364 s = extract_time(utc);
2365 zone_set_offset(zone, tobj, t, s);
2366 s = rb_time_magnify(s);
2367 if (tobj->vtm.subsecx !=
INT2FIX(0)) {
2368 s = wadd(s, v2w(tobj->vtm.subsecx));
2370 time_set_timew(time, tobj, s);
2372 zone_set_dst(zone, tobj, tm);
2382 VALUE local, tm, subsecx;
2383 struct time_object *tobj = RTYPEDDATA_GET_DATA(time);
2386 split_second(tobj->timew, &t, &subsecx);
2387 tm = tm_from_time(rb_cTimeTM, time);
2390 if (UNDEF_P(local))
return 0;
2392 s = extract_vtm(local, time, tobj, subsecx);
2393 tobj->vtm.tm_got = 1;
2394 zone_set_offset(zone, tobj, s, t);
2395 zone_set_dst(zone, tobj, tm);
2407 return rb_check_funcall_default(klass, id_find_timezone, 1, &zone,
Qnil);
2413vtm_day_wraparound(
struct vtm *
vtm)
2415 if (
vtm->hour < 24)
return;
2420 vtm_add_day(
vtm, 1);
2435 vtm.wday = VTM_WDAY_INITVAL;
2437 vtm.zone = str_empty;
2439 vtm.year = obj2vint(year);
2441 vtm.mon =
NIL_P(mon) ? 1 : month_arg(mon);
2443 vtm.mday =
NIL_P(mday) ? 1 : obj2ubits(mday, 5);
2445 vtm.hour =
NIL_P(hour) ? 0 : obj2ubits(hour, 5);
2447 vtm.min =
NIL_P(min) ? 0 : obj2ubits(min, 6);
2455 vtm.sec = obj2subsecx(sec, &subsecx);
2456 vtm.subsecx = subsecx;
2459 return time_init_vtm(time,
vtm, zone);
2468 vtm.isdst = VTM_ISDST_INITVAL;
2470 const VALUE arg = zone;
2473 if (arg ==
ID2SYM(rb_intern(
"dst")))
2475 else if (arg ==
ID2SYM(rb_intern(
"std")))
2477 else if (maybe_tzobj_p(arg))
2479 else if (!
NIL_P(utc = utc_offset_arg(arg)))
2480 vtm.utc_offset = utc == UTC_ZONE ?
INT2FIX(0) : utc;
2481 else if (
NIL_P(zone = find_timezone(time, arg)))
2482 invalid_utc_offset(arg);
2488 GetNewTimeval(time, tobj);
2491 time_set_timew(time, tobj, timegmw(&
vtm));
2492 vtm_day_wraparound(&
vtm);
2493 time_set_vtm(time, tobj,
vtm);
2494 tobj->vtm.tm_got = 1;
2495 TZMODE_SET_LOCALTIME(tobj);
2496 if (zone_timelocal(zone, time)) {
2499 else if (
NIL_P(
vtm.utc_offset = utc_offset_arg(zone))) {
2500 if (
NIL_P(zone = find_timezone(time, zone)) || !zone_timelocal(zone, time))
2501 invalid_utc_offset(arg);
2505 if (utc == UTC_ZONE) {
2506 time_set_timew(time, tobj, timegmw(&
vtm));
2508 vtm_day_wraparound(&
vtm);
2509 time_set_vtm(time, tobj,
vtm);
2510 tobj->vtm.tm_got = 1;
2511 TZMODE_SET_UTC(tobj);
2515 TZMODE_SET_LOCALTIME(tobj);
2520 vtm_add_offset(&
vtm,
off, -1);
2522 time_set_timew(time, tobj, timegmw(&
vtm));
2524 return time_set_utc_offset(time,
off);
2527 time_set_timew(time, tobj, timelocalw(&
vtm));
2529 return time_localtime(time);
2534two_digits(
const char *ptr,
const char *end,
const char **endp,
const char *name)
2536 ssize_t
len = end - ptr;
2537 if (
len < 2 || !have_2digits(ptr) || ((
len > 2) &&
ISDIGIT(ptr[2]))) {
2538 VALUE mesg = rb_sprintf(
"two digits %s is expected", name);
2539 if (ptr[-1] ==
'-' || ptr[-1] ==
':') {
2540 rb_str_catf(mesg,
" after '%c'", ptr[-1]);
2542 rb_str_catf(mesg,
": %.*s", ((
len > 10) ? 10 : (
int)(end - ptr)) + 1, ptr - 1);
2546 return num_from_2digits(ptr);
2550parse_int(
const char *ptr,
const char *end,
const char **endp,
size_t *ndigits,
bool sign)
2552 ssize_t
len = (end - ptr);
2553 int flags = sign ? RB_INT_PARSE_SIGN : 0;
2554 return rb_int_parse_cstr(ptr,
len, (
char **)endp, ndigits, 10, flags);
2566 rb_raise(rb_eArgError,
"time string should have ASCII compatible encoding");
2569 const char *
const begin = RSTRING_PTR(str);
2571 const char *ptr = begin;
2573 int mon = -1, mday = -1, hour = -1, min = -1, sec = -1;
2575 size_t prec =
NIL_P(precision) ? SIZE_MAX :
NUM2SIZET(precision);
2578 rb_raise(rb_eArgError,
"can't parse: %+"PRIsVALUE, str);
2580 year = parse_int(ptr, end, &ptr, &ndigits,
true);
2582 rb_raise(rb_eArgError,
"can't parse: %+"PRIsVALUE, str);
2584 else if (ndigits < 4) {
2585 rb_raise(rb_eArgError,
"year must be 4 or more digits: %.*s", (
int)ndigits, ptr - ndigits);
2587 else if (ptr == end) {
2591#define peekable_p(n) ((ptrdiff_t)(n) < (end - ptr))
2592#define peek_n(c, n) (peekable_p(n) && ((unsigned char)ptr[n] == (c)))
2593#define peek(c) peek_n(c, 0)
2594#define peekc_n(n) (peekable_p(n) ? (int)(unsigned char)ptr[n] : -1)
2595#define peekc() peekc_n(0)
2596#define expect_two_digits(x, bits) \
2597 (((unsigned int)(x = two_digits(ptr + 1, end, &ptr, #x)) > (1U << bits) - 1) ? \
2598 rb_raise(rb_eArgError, #x" out of range") : (void)0)
2599 if (!peek(
'-'))
break;
2600 expect_two_digits(mon, 4);
2601 if (!peek(
'-'))
break;
2602 expect_two_digits(mday, 5);
2603 if (!peek(
' ') && !peek(
'T'))
break;
2604 const char *
const time_part = ptr + 1;
2605 if (!
ISDIGIT(peekc_n(1)))
break;
2606#define nofraction(x) \
2608 rb_raise(rb_eArgError, "fraction " #x " is not supported: %.*s", \
2609 (int)(ptr + 1 - time_part), time_part); \
2611#define need_colon(x) \
2613 rb_raise(rb_eArgError, "missing " #x " part: %.*s", \
2614 (int)(ptr + 1 - time_part), time_part); \
2616 expect_two_digits(hour, 5);
2619 expect_two_digits(min, 6);
2622 expect_two_digits(sec, 6);
2625 for (ndigits = 0; ndigits < prec &&
ISDIGIT(peekc_n(ndigits)); ++ndigits);
2627 int clen = rb_enc_precise_mbclen(ptr, end, rb_enc_get(str));
2628 if (clen < 0) clen = 0;
2629 rb_raise(rb_eArgError,
"subsecond expected after dot: %.*s",
2630 (
int)(ptr - time_part) + clen, time_part);
2632 subsec = parse_int(ptr, ptr + ndigits, &ptr, &ndigits,
false);
2633 if (
NIL_P(subsec))
break;
2634 while (ptr < end &&
ISDIGIT(*ptr)) ptr++;
2637 while (ptr < end &&
ISSPACE(*ptr)) ptr++;
2638 const char *
const zstr = ptr;
2639 while (ptr < end && !
ISSPACE(*ptr)) ptr++;
2640 const char *
const zend = ptr;
2641 while (ptr < end &&
ISSPACE(*ptr)) ptr++;
2650 else if (hour == -1) {
2651 rb_raise(rb_eArgError,
"no time information");
2653 if (!
NIL_P(subsec)) {
2655 if (ndigits < (
size_t)TIME_SCALE_NUMDIGITS) {
2657 subsec = rb_int_mul(subsec, mul);
2659 else if (ndigits > (
size_t)TIME_SCALE_NUMDIGITS) {
2669 .wday = VTM_WDAY_INITVAL,
2673 .mon = (mon < 0) ? 1 : mon,
2674 .mday = (mday < 0) ? 1 : mday,
2675 .hour = (hour < 0) ? 0 : hour,
2676 .min = (min < 0) ? 0 : min,
2677 .sec = (sec < 0) ? 0 : sec,
2680 return time_init_vtm(time,
vtm, zone);
2684subsec_normalize(time_t *secp,
long *subsecp,
const long maxsubsec)
2687 long subsec = *subsecp;
2690 if (UNLIKELY(subsec >= maxsubsec)) {
2691 sec2 = subsec / maxsubsec;
2692 if (TIMET_MAX - sec2 < sec) {
2695 subsec -= sec2 * maxsubsec;
2698 else if (UNLIKELY(subsec < 0)) {
2699 sec2 = NDIV(subsec, maxsubsec);
2700 if (sec < TIMET_MIN - sec2) {
2703 subsec -= sec2 * maxsubsec;
2706#ifndef NEGATIVE_TIME_T
2708 rb_raise(rb_eArgError,
"time must be positive");
2714#define time_usec_normalize(secp, usecp) subsec_normalize(secp, usecp, 1000000)
2715#define time_nsec_normalize(secp, nsecp) subsec_normalize(secp, nsecp, 1000000000)
2718nsec2timew(time_t sec,
long nsec)
2720 time_nsec_normalize(&sec, &nsec);
2721 return timenano2timew(sec, nsec);
2725time_new_timew(
VALUE klass, wideval_t timew)
2727 VALUE time = time_s_alloc(klass);
2730 tobj = RTYPEDDATA_GET_DATA(time);
2731 TZMODE_SET_LOCALTIME(tobj);
2732 time_set_timew(time, tobj, timew);
2740 time_usec_normalize(&sec, &usec);
2741 return time_new_timew(
rb_cTime, timenano2timew(sec, usec * 1000));
2748 return time_new_timew(
rb_cTime, nsec2timew(sec, nsec));
2755 VALUE time = time_new_timew(
rb_cTime, nsec2timew(ts->tv_sec, ts->tv_nsec));
2757 if (-86400 < offset && offset < 86400) {
2758 GetTimeval(time, tobj);
2759 TZMODE_SET_FIXOFF(time, tobj,
INT2FIX(offset));
2761 else if (offset == INT_MAX) {
2763 else if (offset == INT_MAX-1) {
2764 GetTimeval(time, tobj);
2765 TZMODE_SET_UTC(tobj);
2768 rb_raise(rb_eArgError,
"utc_offset out of range");
2777 VALUE time = time_new_timew(
rb_cTime, rb_time_magnify(v2w(timev)));
2782 if (maybe_tzobj_p(zone)) {
2784 if (zone_timelocal(zone, time))
return time;
2788 if (
NIL_P(zone = find_timezone(time,
off))) invalid_utc_offset(
off);
2790 if (!zone_timelocal(zone, time)) invalid_utc_offset(
off);
2793 else if (
off == UTC_ZONE) {
2794 return time_gmtime(time);
2797 validate_utc_offset(
off);
2798 time_set_utc_offset(time,
off);
2806time_timespec(
VALUE num, int interval)
2809 const char *
const tstr = interval ?
"time interval" :
"time";
2812#ifndef NEGATIVE_TIME_T
2813# define arg_range_check(v) \
2815 rb_raise(rb_eArgError, "%s must not be negative", tstr) : \
2818# define arg_range_check(v) \
2819 ((interval && (v) < 0) ? \
2820 rb_raise(rb_eArgError, "time interval must not be negative") : \
2825 t.tv_sec = NUM2TIMET(num);
2826 arg_range_check(t.tv_sec);
2837 t.tv_nsec = (int)(d*1e9+0.5);
2838 if (t.tv_nsec >= 1000000000) {
2839 t.tv_nsec -= 1000000000;
2843 else if ((t.tv_nsec = (
int)(-d*1e9+0.5)) > 0) {
2844 t.tv_nsec = 1000000000 - t.tv_nsec;
2847 t.tv_sec = (time_t)f;
2848 if (f != t.tv_sec) {
2853 else if (RB_BIGNUM_TYPE_P(num)) {
2854 t.tv_sec = NUM2TIMET(num);
2855 arg_range_check(t.tv_sec);
2861 if (!UNDEF_P(ary) && !
NIL_P(ary = rb_check_array_type(ary))) {
2862 i = rb_ary_entry(ary, 0);
2863 f = rb_ary_entry(ary, 1);
2864 t.tv_sec = NUM2TIMET(i);
2865 arg_range_check(t.tv_sec);
2870 rb_raise(
rb_eTypeError,
"can't convert %"PRIsVALUE
" into %s",
2875#undef arg_range_check
2879time_timeval(
VALUE num, int interval)
2884 ts = time_timespec(num, interval);
2885 tv.tv_sec = (TYPEOF_TIMEVAL_TV_SEC)ts.tv_sec;
2886 tv.tv_usec = (TYPEOF_TIMEVAL_TV_USEC)(ts.tv_nsec / 1000);
2894 return time_timeval(num, TRUE);
2904 if (IsTimeval(time)) {
2905 GetTimeval(time, tobj);
2906 ts = timew2timespec(tobj->timew);
2907 t.tv_sec = (TYPEOF_TIMEVAL_TV_SEC)ts.tv_sec;
2908 t.tv_usec = (TYPEOF_TIMEVAL_TV_USEC)(ts.tv_nsec / 1000);
2911 return time_timeval(time, FALSE);
2920 if (IsTimeval(time)) {
2921 GetTimeval(time, tobj);
2922 t = timew2timespec(tobj->timew);
2925 return time_timespec(time, FALSE);
2931 return time_timespec(num, TRUE);
2935get_scale(
VALUE unit)
2937 if (unit ==
ID2SYM(id_nanosecond) || unit ==
ID2SYM(id_nsec)) {
2940 else if (unit ==
ID2SYM(id_microsecond) || unit ==
ID2SYM(id_usec)) {
2943 else if (unit ==
ID2SYM(id_millisecond)) {
2947 rb_raise(rb_eArgError,
"unexpected unit: %"PRIsVALUE, unit);
2958 int scale = get_scale(unit);
2959 time = num_exact(time);
2960 t = num_exact(subsec);
2961 timew = wadd(rb_time_magnify(v2w(time)), wmulquoll(v2w(t), TIME_SCALE, scale));
2962 t = time_new_timew(klass, timew);
2964 else if (IsTimeval(time)) {
2966 GetTimeval(time, tobj);
2967 t = time_new_timew(klass, tobj->timew);
2968 GetTimeval(t, tobj2);
2969 TZMODE_COPY(tobj2, tobj);
2972 timew = rb_time_magnify(v2w(num_exact(time)));
2973 t = time_new_timew(klass, timew);
2976 time_zonelocal(t, zone);
2983time_s_at1(rb_execution_context_t *ec,
VALUE klass,
VALUE time)
2988static const char months[][4] = {
2989 "jan",
"feb",
"mar",
"apr",
"may",
"jun",
2990 "jul",
"aug",
"sep",
"oct",
"nov",
"dec",
2997 obj = rb_str_to_inum(obj, 10, TRUE);
3005obj2ubits(
VALUE obj,
unsigned int bits)
3007 const unsigned int usable_mask = (1U << bits) - 1;
3008 unsigned int rv = (
unsigned int)obj2int(obj);
3010 if ((rv & usable_mask) != rv)
3011 rb_raise(rb_eArgError,
"argument out of range");
3012 return (uint32_t)rv;
3019 obj = rb_str_to_inum(obj, 10, TRUE);
3034 obj = rb_str_to_inum(obj, 10, TRUE);
3038 divmodv(num_exact(obj),
INT2FIX(1), &obj, &subsec);
3039 *subsecx = w2v(rb_time_magnify(v2w(subsec)));
3041 return obj2ubits(obj, 6);
3045usec2subsecx(
VALUE obj)
3048 obj = rb_str_to_inum(obj, 10, TRUE);
3051 return mulquov(num_exact(obj),
INT2FIX(TIME_SCALE),
INT2FIX(1000000));
3060 return obj2ubits(arg, 4);
3065 if (!
NIL_P(s) && RSTRING_LEN(s) > 0) {
3067 for (i=0; i<12; i++) {
3068 if (RSTRING_LEN(s) == 3 &&
3076 mon = obj2ubits(arg, 4);
3082validate_utc_offset(
VALUE utc_offset)
3084 if (le(utc_offset,
INT2FIX(-86400)) || ge(utc_offset,
INT2FIX(86400)))
3085 rb_raise(rb_eArgError,
"utc_offset out of range");
3090validate_zone_name(
VALUE zone_name)
3097validate_vtm(
struct vtm *
vtm)
3099#define validate_vtm_range(mem, b, e) \
3100 ((vtm->mem < b || vtm->mem > e) ? \
3101 rb_raise(rb_eArgError, #mem" out of range") : (void)0)
3102 validate_vtm_range(mon, 1, 12);
3103 validate_vtm_range(mday, 1, 31);
3104 validate_vtm_range(hour, 0, 24);
3105 validate_vtm_range(min, 0, (
vtm->hour == 24 ? 0 : 59));
3106 validate_vtm_range(sec, 0, (
vtm->hour == 24 ? 0 : 60));
3108 rb_raise(rb_eArgError,
"subsecx out of range");
3109 if (!
NIL_P(
vtm->utc_offset)) validate_utc_offset(
vtm->utc_offset);
3110#undef validate_vtm_range
3114time_arg(
int argc,
const VALUE *argv,
struct vtm *
vtm)
3130 vtm->zone = str_empty;
3140 vtm->isdst =
RTEST(argv[8]) ? 1 : 0;
3143 rb_scan_args(argc, argv,
"17", &v[0],&v[1],&v[2],&v[3],&v[4],&v[5],&v[6],&v[7]);
3146 vtm->wday = VTM_WDAY_INITVAL;
3147 vtm->isdst = VTM_ISDST_INITVAL;
3150 vtm->year = obj2vint(v[0]);
3156 vtm->mon = month_arg(v[1]);
3163 vtm->mday = obj2ubits(v[2], 5);
3171 unsigned int mday2 = leap_year_v_p(
vtm->year) ? 29 : 28;
3172 if (
vtm->mday > mday2) {
3182 if (
vtm->mday == 31) {
3189 vtm->hour =
NIL_P(v[3])?0:obj2ubits(v[3], 5);
3191 vtm->min =
NIL_P(v[4])?0:obj2ubits(v[4], 6);
3193 if (!
NIL_P(v[6]) && argc == 7) {
3194 vtm->sec =
NIL_P(v[5])?0:obj2ubits(v[5],6);
3195 subsecx = usec2subsecx(v[6]);
3203 vtm->sec = obj2subsecx(v[5], &subsecx);
3206 vtm->subsecx = subsecx;
3218 unsigned long uy = (
unsigned long)(LIKELY(y >= 0) ? y : -y);
3220 if (LIKELY(uy % 4 != 0))
return 0;
3222 unsigned long century = uy / 100;
3223 if (LIKELY(uy != century * 100))
return 1;
3224 return century % 4 == 0;
3228timegm_noleapsecond(
struct tm *tm)
3230 long tm_year = tm->tm_year;
3231 int tm_yday = calc_tm_yday(tm->tm_year, tm->tm_mon, tm->tm_mday);
3239 return tm->tm_sec + tm->tm_min*60 + tm->tm_hour*3600 +
3243 DIV(tm_year-1,100) +
3244 DIV(tm_year+299,400))*86400;
3248#define DEBUG_FIND_TIME_NUMGUESS
3249#define DEBUG_GUESSRANGE
3252static const bool debug_guessrange =
3253#ifdef DEBUG_GUESSRANGE
3259#define DEBUG_REPORT_GUESSRANGE \
3260 (debug_guessrange ? debug_report_guessrange(guess_lo, guess_hi) : (void)0)
3263debug_report_guessrange(time_t guess_lo, time_t guess_hi)
3265 time_t guess_diff = guess_hi - guess_lo;
3266 fprintf(stderr,
"find time guess range: %"PRI_TIMET_PREFIX
"d - "
3267 "%"PRI_TIMET_PREFIX
"d : %"PRI_TIMET_PREFIX
"u\n",
3268 guess_lo, guess_hi, guess_diff);
3271static const bool debug_find_time_numguess =
3272#ifdef DEBUG_FIND_TIME_NUMGUESS
3278#define DEBUG_FIND_TIME_NUMGUESS_INC \
3279 (void)(debug_find_time_numguess && find_time_numguess++),
3280static unsigned long long find_time_numguess;
3283find_time_numguess_getter(
ID name,
VALUE *data)
3285 unsigned long long *numguess = (
void *)data;
3290find_time_t(
struct tm *tptr,
int utc_p, time_t *tp)
3292 time_t guess, guess0, guess_lo, guess_hi;
3293 struct tm *tm, tm0, tm_lo, tm_hi;
3300#define GUESS(p) (DEBUG_FIND_TIME_NUMGUESS_INC (utc_p ? gmtime_with_leapsecond((p), &result) : LOCALTIME((p), result)))
3302 guess_lo = TIMET_MIN;
3303 guess_hi = TIMET_MAX;
3305 find_dst = 0 < tptr->tm_isdst;
3311 if (tm0.tm_mon < 0) {
3318 else if (11 < tm0.tm_mon) {
3325 else if (tm0.tm_mday < 1) {
3331 else if ((d = days_in_month_in(1900 + tm0.tm_year)[tm0.tm_mon]) < tm0.tm_mday) {
3337 else if (tm0.tm_hour < 0) {
3342 else if (23 < tm0.tm_hour) {
3347 else if (tm0.tm_min < 0) {
3351 else if (59 < tm0.tm_min) {
3355 else if (tm0.tm_sec < 0) {
3358 else if (60 < tm0.tm_sec) {
3362 DEBUG_REPORT_GUESSRANGE;
3363 guess0 = guess = timegm_noleapsecond(&tm0);
3366 d = tmcmp(tptr, tm);
3367 if (d == 0) {
goto found; }
3370 guess -= 24 * 60 * 60;
3374 guess += 24 * 60 * 60;
3376 DEBUG_REPORT_GUESSRANGE;
3377 if (guess_lo < guess && guess < guess_hi && (tm = GUESS(&guess)) != NULL) {
3378 d = tmcmp(tptr, tm);
3379 if (d == 0) {
goto found; }
3384 DEBUG_REPORT_GUESSRANGE;
3388 tm = GUESS(&guess_lo);
3389 if (!tm)
goto error;
3390 d = tmcmp(tptr, tm);
3391 if (d < 0)
goto out_of_range;
3392 if (d == 0) { guess = guess_lo;
goto found; }
3395 tm = GUESS(&guess_hi);
3396 if (!tm)
goto error;
3397 d = tmcmp(tptr, tm);
3398 if (d > 0)
goto out_of_range;
3399 if (d == 0) { guess = guess_hi;
goto found; }
3402 DEBUG_REPORT_GUESSRANGE;
3406 while (guess_lo + 1 < guess_hi) {
3409 guess = guess_lo / 2 + guess_hi / 2;
3410 if (guess <= guess_lo)
3411 guess = guess_lo + 1;
3412 else if (guess >= guess_hi)
3413 guess = guess_hi - 1;
3418 time_t guess0_hi = timegm_noleapsecond(&tm_hi);
3419 guess = guess_hi - (guess0_hi - guess0);
3420 if (guess == guess_hi)
3424 else if (status == 2) {
3425 time_t guess0_lo = timegm_noleapsecond(&tm_lo);
3426 guess = guess_lo + (guess0 - guess0_lo);
3427 if (guess == guess_lo)
3431 if (guess <= guess_lo || guess_hi <= guess) {
3433 if (debug_guessrange) {
3434 if (guess <= guess_lo) {
3435 fprintf(stderr,
"too small guess: %"PRI_TIMET_PREFIX
"d"\
3436 " <= %"PRI_TIMET_PREFIX
"d\n", guess, guess_lo);
3438 if (guess_hi <= guess) {
3439 fprintf(stderr,
"too big guess: %"PRI_TIMET_PREFIX
"d"\
3440 " <= %"PRI_TIMET_PREFIX
"d\n", guess_hi, guess);
3449 if (!tm)
goto error;
3451 d = tmcmp(tptr, tm);
3456 DEBUG_REPORT_GUESSRANGE;
3461 DEBUG_REPORT_GUESSRANGE;
3476 tptr_tm_yday = calc_tm_yday(tptr->tm_year, tptr->tm_mon, tptr->tm_mday);
3479 ((tptr->tm_year - tm_lo.tm_year) * 365 +
3480 DIV((tptr->tm_year-69), 4) -
3481 DIV((tptr->tm_year-1), 100) +
3482 DIV((tptr->tm_year+299), 400) -
3483 DIV((tm_lo.tm_year-69), 4) +
3484 DIV((tm_lo.tm_year-1), 100) -
3485 DIV((tm_lo.tm_year+299), 400) +
3487 tm_lo.tm_yday) * 86400 +
3488 (tptr->tm_hour - tm_lo.tm_hour) * 3600 +
3489 (tptr->tm_min - tm_lo.tm_min) * 60 +
3490 (tptr->tm_sec - (tm_lo.tm_sec == 60 ? 59 : tm_lo.tm_sec));
3499 guess2 = guess - 2 * 60 * 60;
3500 tm = LOCALTIME(&guess2, result);
3502 if (tptr->tm_hour != (tm->tm_hour + 2) % 24 ||
3503 tptr->tm_min != tm->tm_min ||
3504 tptr->tm_sec != tm->tm_sec) {
3505 guess2 -= (tm->tm_hour - tptr->tm_hour) * 60 * 60 +
3506 (tm->tm_min - tptr->tm_min) * 60 +
3507 (tm->tm_sec - tptr->tm_sec);
3508 if (tptr->tm_mday != tm->tm_mday)
3509 guess2 += 24 * 60 * 60;
3510 if (guess != guess2) {
3511 tm = LOCALTIME(&guess2, result);
3512 if (tm && tmcmp(tptr, tm) == 0) {
3524 guess2 = guess + 2 * 60 * 60;
3525 tm = LOCALTIME(&guess2, result);
3527 if ((tptr->tm_hour + 2) % 24 != tm->tm_hour ||
3528 tptr->tm_min != tm->tm_min ||
3529 tptr->tm_sec != tm->tm_sec) {
3530 guess2 -= (tm->tm_hour - tptr->tm_hour) * 60 * 60 +
3531 (tm->tm_min - tptr->tm_min) * 60 +
3532 (tm->tm_sec - tptr->tm_sec);
3533 if (tptr->tm_mday != tm->tm_mday)
3534 guess2 -= 24 * 60 * 60;
3535 if (guess != guess2) {
3536 tm = LOCALTIME(&guess2, result);
3537 if (tm && tmcmp(tptr, tm) == 0) {
3553 return "time out of range";
3556 return "gmtime/localtime error";
3560vtmcmp(
struct vtm *a,
struct vtm *b)
3562 if (ne(a->year, b->year))
3563 return lt(a->year, b->year) ? -1 : 1;
3564 else if (a->mon != b->mon)
3565 return a->mon < b->mon ? -1 : 1;
3566 else if (a->mday != b->mday)
3567 return a->mday < b->mday ? -1 : 1;
3568 else if (a->hour != b->hour)
3569 return a->hour < b->hour ? -1 : 1;
3570 else if (a->min != b->min)
3571 return a->min < b->min ? -1 : 1;
3572 else if (a->sec != b->sec)
3573 return a->sec < b->sec ? -1 : 1;
3574 else if (ne(a->subsecx, b->subsecx))
3575 return lt(a->subsecx, b->subsecx) ? -1 : 1;
3581tmcmp(
struct tm *a,
struct tm *b)
3583 if (a->tm_year != b->tm_year)
3584 return a->tm_year < b->tm_year ? -1 : 1;
3585 else if (a->tm_mon != b->tm_mon)
3586 return a->tm_mon < b->tm_mon ? -1 : 1;
3587 else if (a->tm_mday != b->tm_mday)
3588 return a->tm_mday < b->tm_mday ? -1 : 1;
3589 else if (a->tm_hour != b->tm_hour)
3590 return a->tm_hour < b->tm_hour ? -1 : 1;
3591 else if (a->tm_min != b->tm_min)
3592 return a->tm_min < b->tm_min ? -1 : 1;
3593 else if (a->tm_sec != b->tm_sec)
3594 return a->tm_sec < b->tm_sec ? -1 : 1;
3700time_s_mkutc(
int argc,
VALUE *argv,
VALUE klass)
3704 time_arg(argc, argv, &
vtm);
3705 return time_gmtime(time_new_timew(klass, timegmw(&
vtm)));
3726time_s_mktime(
int argc,
VALUE *argv,
VALUE klass)
3730 time_arg(argc, argv, &
vtm);
3731 return time_localtime(time_new_timew(klass, timelocalw(&
vtm)));
3751time_to_i(
VALUE time)
3755 GetTimeval(time, tobj);
3756 return w2v(wdiv(tobj->timew, WINT2FIXWV(TIME_SCALE)));
3780time_to_f(
VALUE time)
3784 GetTimeval(time, tobj);
3785 return rb_Float(rb_time_unmagnify_to_float(tobj->timew));
3801time_to_r(
VALUE time)
3806 GetTimeval(time, tobj);
3807 v = rb_time_unmagnify_to_rational(tobj->timew);
3829time_usec(
VALUE time)
3834 GetTimeval(time, tobj);
3836 w = wmod(tobj->timew, WINT2WV(TIME_SCALE));
3837 wmuldivmod(w, WINT2FIXWV(1000000), WINT2FIXWV(TIME_SCALE), &q, &r);
3856time_nsec(
VALUE time)
3860 GetTimeval(time, tobj);
3861 return rb_to_int(w2v(wmulquoll(wmod(tobj->timew, WINT2WV(TIME_SCALE)), 1000000000, TIME_SCALE)));
3882time_subsec(
VALUE time)
3886 GetTimeval(time, tobj);
3887 return quov(w2v(wmod(tobj->timew, WINT2FIXWV(TIME_SCALE))),
INT2FIX(TIME_SCALE));
3924 GetTimeval(time1, tobj1);
3925 if (IsTimeval(time2)) {
3926 GetTimeval(time2, tobj2);
3927 n = wcmp(tobj1->timew, tobj2->timew);
3930 return rb_invcmp(time1, time2);
3932 if (n == 0)
return INT2FIX(0);
3950 GetTimeval(time1, tobj1);
3951 if (IsTimeval(time2)) {
3952 GetTimeval(time2, tobj2);
3953 return rb_equal(w2v(tobj1->timew), w2v(tobj2->timew));
3975time_utc_p(
VALUE time)
3979 GetTimeval(time, tobj);
3980 return RBOOL(TZMODE_UTC_P(tobj));
3993time_hash(
VALUE time)
3997 GetTimeval(time, tobj);
3998 return rb_hash(w2v(tobj->timew));
4008 GetTimeval(time, tobj);
4009 GetNewTimeval(copy, tcopy);
4019 time_init_copy(dup, time);
4024time_localtime(
VALUE time)
4030 GetTimeval(time, tobj);
4031 if (TZMODE_LOCALTIME_P(tobj)) {
4032 if (tobj->vtm.tm_got)
4039 zone = tobj->vtm.zone;
4040 if (maybe_tzobj_p(zone) && zone_localtime(zone, time)) {
4044 if (!localtimew(tobj->timew, &
vtm))
4045 rb_raise(rb_eArgError,
"localtime error");
4046 time_set_vtm(time, tobj,
vtm);
4048 tobj->vtm.tm_got = 1;
4049 TZMODE_SET_LOCALTIME(tobj);
4057 if (zone_localtime(zone, time))
return time;
4061 if (
NIL_P(zone = find_timezone(time,
off))) invalid_utc_offset(
off);
4062 if (!zone_localtime(zone, time)) invalid_utc_offset(
off);
4065 else if (
off == UTC_ZONE) {
4066 return time_gmtime(time);
4068 validate_utc_offset(
off);
4070 time_set_utc_offset(time,
off);
4071 return time_fixoff(time);
4100time_localtime_m(
int argc,
VALUE *argv,
VALUE time)
4105 return time_zonelocal(time,
off);
4108 return time_localtime(time);
4126time_gmtime(
VALUE time)
4131 GetTimeval(time, tobj);
4132 if (TZMODE_UTC_P(tobj)) {
4133 if (tobj->vtm.tm_got)
4141 GMTIMEW(tobj->timew, &
vtm);
4142 time_set_vtm(time, tobj,
vtm);
4144 tobj->vtm.tm_got = 1;
4145 TZMODE_SET_UTC(tobj);
4150time_fixoff(
VALUE time)
4156 GetTimeval(time, tobj);
4157 if (TZMODE_FIXOFF_P(tobj)) {
4158 if (tobj->vtm.tm_got)
4165 if (TZMODE_FIXOFF_P(tobj))
4166 off = tobj->vtm.utc_offset;
4170 GMTIMEW(tobj->timew, &
vtm);
4172 zone = tobj->vtm.zone;
4173 vtm_add_offset(&
vtm,
off, +1);
4175 time_set_vtm(time, tobj,
vtm);
4176 RB_OBJ_WRITE_UNALIGNED(time, &tobj->vtm.zone, zone);
4178 tobj->vtm.tm_got = 1;
4179 TZMODE_SET_FIXOFF(time, tobj,
off);
4201time_getlocaltime(
int argc,
VALUE *argv,
VALUE time)
4207 if (maybe_tzobj_p(zone)) {
4208 VALUE t = time_dup(time);
4209 if (zone_localtime(
off, t))
return t;
4214 if (
NIL_P(zone = find_timezone(time,
off))) invalid_utc_offset(
off);
4215 time = time_dup(time);
4216 if (!zone_localtime(zone, time)) invalid_utc_offset(
off);
4219 else if (
off == UTC_ZONE) {
4220 return time_gmtime(time_dup(time));
4222 validate_utc_offset(
off);
4224 time = time_dup(time);
4225 time_set_utc_offset(time,
off);
4226 return time_fixoff(time);
4229 return time_localtime(time_dup(time));
4248time_getgmtime(
VALUE time)
4250 return time_gmtime(time_dup(time));
4256 if (TZMODE_UTC_P(tobj))
return time_gmtime(time);
4257 if (TZMODE_FIXOFF_P(tobj))
return time_fixoff(time);
4258 return time_localtime(time);
4261static VALUE strftime_cstr(
const char *fmt,
size_t len,
VALUE time, rb_encoding *enc);
4262#define strftimev(fmt, time, enc) strftime_cstr((fmt), rb_strlen_lit(fmt), (time), (enc))
4286time_asctime(
VALUE time)
4288 return strftimev(
"%a %b %e %T %Y", time, rb_usascii_encoding());
4308time_to_s(
VALUE time)
4312 GetTimeval(time, tobj);
4313 if (TZMODE_UTC_P(tobj))
4314 return strftimev(
"%Y-%m-%d %H:%M:%S UTC", time, rb_usascii_encoding());
4316 return strftimev(
"%Y-%m-%d %H:%M:%S %z", time, rb_usascii_encoding());
4336time_inspect(
VALUE time)
4341 GetTimeval(time, tobj);
4342 str = strftimev(
"%Y-%m-%d %H:%M:%S", time, rb_usascii_encoding());
4343 subsec = w2v(wmod(tobj->timew, WINT2FIXWV(TIME_SCALE)));
4348 rb_str_catf(str,
".%09ld",
FIX2LONG(subsec));
4349 for (
len=RSTRING_LEN(str); RSTRING_PTR(str)[
len-1] ==
'0' &&
len > 0;
len--)
4351 rb_str_resize(str,
len);
4355 subsec = quov(subsec,
INT2FIX(TIME_SCALE));
4358 if (TZMODE_UTC_P(tobj)) {
4364 char sign = (
off < 0) ? (
off = -
off,
'-') :
'+';
4366 int min = (
off /= 60) % 60;
4368 rb_str_catf(str,
" %c%.2d%.2d", sign, (
int)
off, min);
4369 if (sec) rb_str_catf(str,
"%.2d", sec);
4380 offset = num_exact(offset);
4382 result = time_new_timew(klass, wsub(tobj->timew, rb_time_magnify(v2w(offset))));
4384 result = time_new_timew(klass, wadd(tobj->timew, rb_time_magnify(v2w(offset))));
4385 GetTimeval(result, result_tobj);
4386 TZMODE_COPY(result_tobj, tobj);
4394 return time_add0(
rb_cTime, tobj, torig, offset, sign);
4415 GetTimeval(time1, tobj);
4417 if (IsTimeval(time2)) {
4420 return time_add(tobj, time1, time2, 1);
4450 GetTimeval(time1, tobj);
4451 if (IsTimeval(time2)) {
4454 GetTimeval(time2, tobj2);
4455 return rb_Float(rb_time_unmagnify_to_float(wsub(tobj->timew, tobj2->timew)));
4457 return time_add(tobj, time1, time2, -1);
4461ndigits_denominator(
VALUE ndigits)
4466 rb_raise(rb_eArgError,
"negative ndigits given");
4506 VALUE ndigits, v, den;
4512 den = ndigits_denominator(ndigits);
4514 GetTimeval(time, tobj);
4515 v = w2v(rb_time_unmagnify(tobj->timew));
4518 if (lt(v, quov(den,
INT2FIX(2))))
4519 return time_add(tobj, time, v, -1);
4521 return time_add(tobj, time, subv(den, v), 1);
4554 VALUE ndigits, v, den;
4560 den = ndigits_denominator(ndigits);
4562 GetTimeval(time, tobj);
4563 v = w2v(rb_time_unmagnify(tobj->timew));
4566 return time_add(tobj, time, v, -1);
4599 VALUE ndigits, v, den;
4605 den = ndigits_denominator(ndigits);
4607 GetTimeval(time, tobj);
4608 v = w2v(rb_time_unmagnify(tobj->timew));
4614 return time_add(tobj, time, v, 1);
4639 GetTimeval(time, tobj);
4640 MAKE_TM(time, tobj);
4641 return INT2FIX(tobj->vtm.sec);
4663 GetTimeval(time, tobj);
4664 MAKE_TM(time, tobj);
4665 return INT2FIX(tobj->vtm.min);
4683time_hour(
VALUE time)
4687 GetTimeval(time, tobj);
4688 MAKE_TM(time, tobj);
4689 return INT2FIX(tobj->vtm.hour);
4707time_mday(
VALUE time)
4711 GetTimeval(time, tobj);
4712 MAKE_TM(time, tobj);
4713 return INT2FIX(tobj->vtm.mday);
4735 GetTimeval(time, tobj);
4736 MAKE_TM(time, tobj);
4737 return INT2FIX(tobj->vtm.mon);
4754time_year(
VALUE time)
4758 GetTimeval(time, tobj);
4759 MAKE_TM(time, tobj);
4760 return tobj->vtm.year;
4779time_wday(
VALUE time)
4783 GetTimeval(time, tobj);
4784 MAKE_TM_ENSURE(time, tobj, tobj->vtm.wday != VTM_WDAY_INITVAL);
4785 return INT2FIX((
int)tobj->vtm.wday);
4789 return RBOOL(time_wday(time) == INT2FIX(n)); \
4805time_sunday(
VALUE time)
4823time_monday(
VALUE time)
4841time_tuesday(
VALUE time)
4859time_wednesday(
VALUE time)
4877time_thursday(
VALUE time)
4895time_friday(
VALUE time)
4913time_saturday(
VALUE time)
4929time_yday(
VALUE time)
4933 GetTimeval(time, tobj);
4934 MAKE_TM_ENSURE(time, tobj, tobj->vtm.yday != 0);
4935 return INT2FIX(tobj->vtm.yday);
4954time_isdst(
VALUE time)
4958 GetTimeval(time, tobj);
4959 MAKE_TM(time, tobj);
4960 if (tobj->vtm.isdst == VTM_ISDST_INITVAL) {
4963 return RBOOL(tobj->vtm.isdst);
4977time_zone(
VALUE time)
4982 GetTimeval(time, tobj);
4983 MAKE_TM(time, tobj);
4985 if (TZMODE_UTC_P(tobj)) {
4988 zone = tobj->vtm.zone;
5013 GetTimeval(time, tobj);
5015 if (TZMODE_UTC_P(tobj)) {
5019 MAKE_TM(time, tobj);
5020 return tobj->vtm.utc_offset;
5040time_to_a(
VALUE time)
5044 GetTimeval(time, tobj);
5045 MAKE_TM_ENSURE(time, tobj, tobj->vtm.yday != 0);
5055 RBOOL(tobj->vtm.isdst),
5101 GetTimeval(time, tobj);
5102 MAKE_TM_ENSURE(time, tobj, tobj->vtm.yday != 0);
5105 h = rb_hash_new_with_size(11);
5107 rb_hash_aset(h, sym_year, tobj->vtm.year);
5108 rb_hash_aset(h, sym_month,
INT2FIX(tobj->vtm.mon));
5109 rb_hash_aset(h, sym_day,
INT2FIX(tobj->vtm.mday));
5110 rb_hash_aset(h, sym_yday,
INT2FIX(tobj->vtm.yday));
5111 rb_hash_aset(h, sym_wday,
INT2FIX(tobj->vtm.wday));
5112 rb_hash_aset(h, sym_hour,
INT2FIX(tobj->vtm.hour));
5113 rb_hash_aset(h, sym_min,
INT2FIX(tobj->vtm.min));
5114 rb_hash_aset(h, sym_sec,
INT2FIX(tobj->vtm.sec));
5115 rb_hash_aset(h, sym_subsec,
5116 quov(w2v(wmod(tobj->timew, WINT2FIXWV(TIME_SCALE))),
INT2FIX(TIME_SCALE)));
5117 rb_hash_aset(h, sym_dst, RBOOL(tobj->vtm.isdst));
5118 rb_hash_aset(h, sym_zone, time_zone(time));
5124 "wrong argument type %"PRIsVALUE
" (expected Array or nil)",
5134 if (sym_year == key) rb_hash_aset(h, key, tobj->vtm.year);
5135 if (sym_month == key) rb_hash_aset(h, key,
INT2FIX(tobj->vtm.mon));
5136 if (sym_day == key) rb_hash_aset(h, key,
INT2FIX(tobj->vtm.mday));
5137 if (sym_yday == key) rb_hash_aset(h, key,
INT2FIX(tobj->vtm.yday));
5138 if (sym_wday == key) rb_hash_aset(h, key,
INT2FIX(tobj->vtm.wday));
5139 if (sym_hour == key) rb_hash_aset(h, key,
INT2FIX(tobj->vtm.hour));
5140 if (sym_min == key) rb_hash_aset(h, key,
INT2FIX(tobj->vtm.min));
5141 if (sym_sec == key) rb_hash_aset(h, key,
INT2FIX(tobj->vtm.sec));
5142 if (sym_subsec == key) {
5143 rb_hash_aset(h, key, quov(w2v(wmod(tobj->timew, WINT2FIXWV(TIME_SCALE))),
INT2FIX(TIME_SCALE)));
5145 if (sym_dst == key) rb_hash_aset(h, key, RBOOL(tobj->vtm.isdst));
5146 if (sym_zone == key) rb_hash_aset(h, key, time_zone(time));
5152rb_strftime_alloc(
const char *format,
size_t format_len, rb_encoding *enc,
5153 VALUE time,
struct vtm *
vtm, wideval_t timew,
int gmt)
5158 if (!timew2timespec_exact(timew, &ts))
5159 timev = w2v(rb_time_unmagnify(timew));
5162 return rb_strftime_timespec(format, format_len, enc, time,
vtm, &ts, gmt);
5165 return rb_strftime(format, format_len, enc, time,
vtm, timev, gmt);
5170strftime_cstr(
const char *fmt,
size_t len,
VALUE time, rb_encoding *enc)
5175 GetTimeval(time, tobj);
5176 MAKE_TM(time, tobj);
5177 str = rb_strftime_alloc(fmt,
len, enc, time, &tobj->vtm, tobj->timew, TZMODE_UTC_P(tobj));
5178 if (!str) rb_raise(rb_eArgError,
"invalid format: %s", fmt);
5200 GetTimeval(time, tobj);
5201 MAKE_TM_ENSURE(time, tobj, tobj->vtm.yday != 0);
5204 rb_raise(rb_eArgError,
"format should have ASCII compatible encoding");
5206 tmp = rb_str_tmp_frozen_acquire(format);
5207 fmt = RSTRING_PTR(tmp);
5208 len = RSTRING_LEN(tmp);
5209 enc = rb_enc_get(format);
5211 rb_warning(
"strftime called with empty format string");
5212 return rb_enc_str_new(0, 0, enc);
5215 VALUE str = rb_strftime_alloc(fmt,
len, enc, time, &tobj->vtm, tobj->timew,
5216 TZMODE_UTC_P(tobj));
5217 rb_str_tmp_frozen_release(format, tmp);
5218 if (!str) rb_raise(rb_eArgError,
"invalid format: %"PRIsVALUE, format);
5245time_xmlschema(
int argc,
VALUE *argv,
VALUE time)
5247 long fraction_digits = 0;
5250 fraction_digits =
NUM2LONG(argv[0]);
5251 if (fraction_digits < 0) {
5252 fraction_digits = 0;
5258 GetTimeval(time, tobj);
5259 MAKE_TM(time, tobj);
5261 const long size_after_year =
sizeof(
"-MM-DDTHH:MM:SS+ZH:ZM") + fraction_digits
5262 + (fraction_digits > 0);
5266# define fill_digits_long(len, prec, n) \
5267 for (int fill_it = 1, written = snprintf(ptr, len, "%0*ld", prec, n); \
5268 fill_it; ptr += written, fill_it = 0)
5271 long year =
FIX2LONG(tobj->vtm.year);
5273 int w = (year >= -9999 && year <= 9999 ? year_width : (year < 0) + (int)
DECIMAL_SIZE_OF(year));
5275 ptr = RSTRING_PTR(str);
5276 fill_digits_long(w + 1, year_width, year) {
5277 if (year >= -9999 && year <= 9999) {
5287 str = rb_int2str(tobj->vtm.year, 10);
5292# define fill_2(c, n) (*ptr++ = c, *ptr++ = '0' + (n) / 10, *ptr++ = '0' + (n) % 10)
5293 fill_2(
'-', tobj->vtm.mon);
5294 fill_2(
'-', tobj->vtm.mday);
5295 fill_2(
'T', tobj->vtm.hour);
5296 fill_2(
':', tobj->vtm.min);
5297 fill_2(
':', tobj->vtm.sec);
5299 if (fraction_digits > 0) {
5300 VALUE subsecx = tobj->vtm.subsecx;
5304 if (fraction_digits <= TIME_SCALE_NUMDIGITS) {
5305 digits = TIME_SCALE_NUMDIGITS - (int)fraction_digits;
5308 long w = fraction_digits - TIME_SCALE_NUMDIGITS;
5315 if (digits >= 0 && fraction_digits < INT_MAX) {
5317 if (digits > 0) subsec /= (long)pow(10, digits);
5318 fill_digits_long(fraction_digits + 1, (
int)fraction_digits, subsec) {
5323 subsecx = rb_int2str(subsecx, 10);
5324 long len = RSTRING_LEN(subsecx);
5325 if (fraction_digits >
len) {
5326 memset(ptr,
'0', fraction_digits -
len);
5329 len = fraction_digits;
5331 ptr += fraction_digits;
5332 memcpy(ptr -
len, RSTRING_PTR(subsecx),
len);
5336 if (TZMODE_UTC_P(tobj)) {
5342 char sign = offset < 0 ?
'-' :
'+';
5343 if (offset < 0) offset = -offset;
5345 fill_2(sign, offset / 60);
5346 fill_2(
':', offset % 60);
5348 const char *
const start = RSTRING_PTR(str);
5353int ruby_marshal_write_long(
long x,
char *buf);
5355enum {base_dump_size = 8};
5359time_mdump(
VALUE time)
5363 char buf[base_dump_size +
sizeof(long) + 1];
5370 VALUE subsecx, nano, subnano, v, zone;
5373 const int max_year = 1900+0xffff;
5375 GetTimeval(time, tobj);
5377 gmtimew(tobj->timew, &
vtm);
5381 if (year > max_year) {
5382 year_extend =
INT2FIX(year - max_year);
5385 else if (year < 1900) {
5386 year_extend =
LONG2NUM(1900 - year);
5391 if (rb_int_positive_p(
vtm.year)) {
5392 year_extend = rb_int_minus(
vtm.year,
INT2FIX(max_year));
5396 year_extend = rb_int_minus(
INT2FIX(1900),
vtm.year);
5401 subsecx =
vtm.subsecx;
5403 nano = mulquov(subsecx,
INT2FIX(1000000000),
INT2FIX(TIME_SCALE));
5404 divmodv(nano,
INT2FIX(1), &v, &subnano);
5409 nano = addv(
LONG2FIX(nsec), subnano);
5412 TZMODE_UTC_P(tobj) << 30 |
5417 s = (
unsigned long)
vtm.min << 26 |
5421 for (i=0; i<4; i++) {
5422 buf[i] = (
unsigned char)p;
5425 for (i=4; i<8; i++) {
5426 buf[i] = (
unsigned char)s;
5430 if (!
NIL_P(year_extend)) {
5437 size_t ysize = rb_absint_size(year_extend, NULL);
5438 char *p, *
const buf_year_extend = buf + base_dump_size;
5439 if (ysize > LONG_MAX ||
5440 (i = ruby_marshal_write_long((
long)ysize, buf_year_extend)) < 0) {
5441 rb_raise(rb_eArgError,
"year too %s to marshal: %"PRIsVALUE
" UTC",
5442 (year == 1900 ?
"small" :
"big"),
vtm.year);
5444 i += base_dump_size;
5446 p = RSTRING_PTR(str);
5457 rb_ivar_set(str, id_nano_num, RRATIONAL(nano)->num);
5458 rb_ivar_set(str, id_nano_den, RRATIONAL(nano)->den);
5474 int len = (int)
sizeof(buf);
5475 buf[1] = (char)((nsec % 10) << 4);
5477 buf[0] = (char)(nsec % 10);
5479 buf[0] |= (char)((nsec % 10) << 4);
5484 if (!TZMODE_UTC_P(tobj)) {
5491 zone = tobj->vtm.zone;
5492 if (maybe_tzobj_p(zone)) {
5493 zone = rb_funcallv(zone, id_name, 0, 0);
5506 str = time_mdump(time);
5512mload_findzone(
VALUE arg)
5515 VALUE time = argp[0], zone = argp[1];
5516 return find_timezone(time, zone);
5526 if (
NIL_P(z))
return rb_fstring(zone);
5531long ruby_marshal_read_long(
const char **buf,
long len);
5545 VALUE submicro, nano_num, nano_den, offset, zone, year;
5550#define get_attr(attr, iffound) \
5551 attr = rb_attr_delete(str, id_##attr); \
5552 if (!NIL_P(attr)) { \
5556 get_attr(nano_num, {});
5557 get_attr(nano_den, {});
5558 get_attr(submicro, {});
5559 get_attr(offset, (offset =
rb_rescue(validate_utc_offset, offset, 0,
Qnil)));
5560 get_attr(zone, (zone =
rb_rescue(validate_zone_name, zone, 0,
Qnil)));
5568 buf = (
unsigned char *)RSTRING_PTR(str);
5569 if (RSTRING_LEN(str) < base_dump_size) {
5570 goto invalid_format;
5574 for (i=0; i<4; i++) {
5575 p |= (
unsigned long)buf[i]<<(8*i);
5577 for (i=4; i<8; i++) {
5578 s |= (
unsigned long)buf[i]<<(8*(i-4));
5581 if ((p & (1UL<<31)) == 0) {
5587 timew = wadd(rb_time_magnify(TIMET2WV(sec)), wmulquoll(WINT2FIXWV(usec), TIME_SCALE, 1000000));
5591 gmt = (int)((p >> 30) & 0x1);
5594 year =
INT2FIX(((
int)(p >> 14) & 0xffff) + 1900);
5596 if (RSTRING_LEN(str) > base_dump_size) {
5597 long len = RSTRING_LEN(str) - base_dump_size;
5600 const char *ybuf = (
const char *)(buf += base_dump_size);
5601 ysize = ruby_marshal_read_long(&ybuf,
len);
5602 len -= ybuf - (
const char *)buf;
5603 if (ysize < 0 || ysize >
len)
goto invalid_format;
5606 year = rb_int_minus(year, year_extend);
5609 year = rb_int_plus(year, year_extend);
5612 unsigned int mon = ((int)(p >> 10) & 0xf);
5619 vtm.mday = (int)(p >> 5) & 0x1f;
5620 vtm.hour = (int) p & 0x1f;
5621 vtm.min = (int)(s >> 26) & 0x3f;
5622 vtm.sec = (int)(s >> 20) & 0x3f;
5626 vtm.zone = str_empty;
5628 usec = (long)(s & 0xfffff);
5633 if (nano_num !=
Qnil) {
5634 VALUE nano = quov(num_exact(nano_num), num_exact(nano_den));
5637 else if (submicro !=
Qnil) {
5642 len = RSTRING_LEN(submicro);
5645 if (10 <= (digit = ptr[0] >> 4))
goto end_submicro;
5646 nsec += digit * 100;
5647 if (10 <= (digit = ptr[0] & 0xf))
goto end_submicro;
5651 if (10 <= (digit = ptr[1] >> 4))
goto end_submicro;
5657 timew = timegmw(&
vtm);
5660 GetNewTimeval(time, tobj);
5661 TZMODE_SET_LOCALTIME(tobj);
5662 tobj->vtm.tm_got = 0;
5663 time_set_timew(time, tobj, timew);
5666 TZMODE_SET_UTC(tobj);
5668 else if (!
NIL_P(offset)) {
5669 time_set_utc_offset(time, offset);
5673 zone = mload_zone(time, zone);
5674 tobj->vtm.zone = zone;
5675 zone_localtime(zone, time);
5689 VALUE time = time_s_alloc(klass);
5691 time_mload(time, str);
5717 GetTimeval(time, tobj);
5718 tm = time_s_alloc(klass);
5719 ttm = RTYPEDDATA_GET_DATA(tm);
5721 GMTIMEW(ttm->timew = tobj->timew, v);
5722 ttm->timew = wsub(ttm->timew, v->subsecx);
5725 time_set_vtm(tm, ttm, *v);
5727 ttm->vtm.tm_got = 1;
5728 TZMODE_SET_UTC(ttm);
5741tm_initialize(
int argc,
VALUE *argv,
VALUE time)
5747 time_arg(argc, argv, &
vtm);
5749 struct time_object *tobj = RTYPEDDATA_GET_DATA(time);
5750 TZMODE_SET_UTC(tobj);
5751 time_set_timew(time, tobj, t);
5752 time_set_vtm(time, tobj,
vtm);
5769 struct time_object *tobj = RTYPEDDATA_GET_DATA(dup);
5777 return time_add0(
rb_obj_class(tm), get_timeval(tm), tm, offset, +1);
5783 return time_add0(
rb_obj_class(tm), get_timeval(tm), tm, offset, -1);
5787Init_tm(
VALUE outer,
const char *name)
5833rb_time_zone_abbreviation(
VALUE zone,
VALUE time)
5835 VALUE tm, abbr, strftime_args[2];
5838 if (!
NIL_P(abbr))
return abbr;
5840 tm = tm_from_time(rb_cTimeTM, time);
5842 if (!UNDEF_P(abbr)) {
5845#ifdef SUPPORT_TZINFO_ZONE_ABBREVIATION
5847 if (!UNDEF_P(abbr)) {
5848 abbr = rb_funcallv(abbr, rb_intern(
"abbreviation"), 0, 0);
5852 strftime_args[0] = rb_fstring_lit(
"%Z");
5853 strftime_args[1] = tm;
5855 if (!UNDEF_P(abbr)) {
5858 abbr = rb_check_funcall_default(zone, idName, 0, 0,
Qnil);
5860 return rb_obj_as_string(abbr);
5900 str_utc = rb_fstring_lit(
"UTC");
5901 rb_vm_register_global_object(str_utc);
5902 str_empty = rb_fstring_lit(
"");
5903 rb_vm_register_global_object(str_empty);
5987 if (debug_find_time_numguess) {
5989 find_time_numguess_getter, 0);
5992 rb_cTimeTM = Init_tm(
rb_cTime,
"tm");
5995#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_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.
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.
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_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 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*.
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.
struct rb_data_type_struct rb_data_type_t
This is the struct that holds necessary info for a struct.
#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.
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.