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"
49# include "timezoneapi.h"
54static ID id_submicro, id_nano_num, id_nano_den, id_offset, id_zone;
55static ID id_nanosecond, id_microsecond, id_millisecond, id_nsec, id_usec;
56static ID id_local_to_utc, id_utc_to_local, id_find_timezone;
57static ID id_year, id_mon, id_mday, id_hour, id_min, id_sec, id_isdst;
58static VALUE str_utc, str_empty;
61static VALUE sym_year, sym_month, sym_day, sym_yday, sym_wday;
62static VALUE sym_hour, sym_min, sym_sec, sym_subsec, sym_dst, sym_zone;
66#define id_divmod idDivmod
68#define UTC_ZONE Qundef
70#define NDIV(x,y) (-(-((x)+1)/(y))-1)
71#define NMOD(x,y) ((y)-(-((x)+1)%(y))-1)
72#define DIV(n,d) ((n)<0 ? NDIV((n),(d)) : (n)/(d))
73#define MOD(n,d) ((n)<0 ? NMOD((n),(d)) : (n)%(d))
74#define VTM_WDAY_INITVAL (7)
75#define VTM_ISDST_INITVAL (3)
90 if ((
long)x < (
long)y)
92 if ((
long)x > (
long)y)
96 if (RB_BIGNUM_TYPE_P(x))
return FIX2INT(rb_big_cmp(x, y));
97 return rb_cmpint(
rb_funcall(x, idCmp, 1, y), x, y);
100#define ne(x,y) (!eq((x),(y)))
101#define lt(x,y) (cmp((x),(y)) < 0)
102#define gt(x,y) (cmp((x),(y)) > 0)
103#define le(x,y) (cmp((x),(y)) <= 0)
104#define ge(x,y) (cmp((x),(y)) >= 0)
112 if (RB_BIGNUM_TYPE_P(x))
return rb_big_plus(x, y);
122 if (RB_BIGNUM_TYPE_P(x))
return rb_big_minus(x, y);
130 return rb_fix_mul_fix(x, y);
132 if (RB_BIGNUM_TYPE_P(x))
133 return rb_big_mul(x, y);
141 return rb_fix_div_fix(x, y);
143 if (RB_BIGNUM_TYPE_P(x))
144 return rb_big_div(x, y);
153 if (
FIXNUM_P(x))
return rb_fix_mod_fix(x, y);
155 if (RB_BIGNUM_TYPE_P(x))
return rb_big_modulo(x, y);
159#define neg(x) (subv(INT2FIX(0), (x)))
175 return rb_numeric_quo(x, y);
181 VALUE ret = quor(x, y);
183 RRATIONAL(ret)->den ==
INT2FIX(1)) {
184 ret = RRATIONAL(ret)->num;
189#define mulquov(x,y,z) (((y) == (z)) ? (x) : quov(mulv((x),(y)),(z)))
198 rb_fix_divmod_fix(n, d, q, r);
203 ary = rb_check_array_type(tmp);
205 rb_raise(
rb_eTypeError,
"unexpected divmod result: into %"PRIsVALUE,
208 *q = rb_ary_entry(ary, 0);
209 *r = rb_ary_entry(ary, 1);
213# define INT64toNUM(x) LONG2NUM(x)
214#elif defined(HAVE_LONG_LONG) && SIZEOF_LONG_LONG == 8
215# define INT64toNUM(x) LL2NUM(x)
218#if defined(HAVE_UINT64_T) && SIZEOF_LONG*2 <= SIZEOF_UINT64_T
219 typedef uint64_t uwideint_t;
220 typedef int64_t wideint_t;
221 typedef uint64_t WIDEVALUE;
222 typedef int64_t SIGNED_WIDEVALUE;
223# define WIDEVALUE_IS_WIDER 1
224# define UWIDEINT_MAX UINT64_MAX
225# define WIDEINT_MAX INT64_MAX
226# define WIDEINT_MIN INT64_MIN
227# define FIXWINT_P(tv) ((tv) & 1)
228# define FIXWVtoINT64(tv) RSHIFT((SIGNED_WIDEVALUE)(tv), 1)
229# define INT64toFIXWV(wi) ((WIDEVALUE)((SIGNED_WIDEVALUE)(wi) << 1 | FIXNUM_FLAG))
230# define FIXWV_MAX (((int64_t)1 << 62) - 1)
231# define FIXWV_MIN (-((int64_t)1 << 62))
232# define FIXWVABLE(wi) (POSFIXWVABLE(wi) && NEGFIXWVABLE(wi))
233# define WINT2FIXWV(i) WIDEVAL_WRAP(INT64toFIXWV(i))
234# define FIXWV2WINT(w) FIXWVtoINT64(WIDEVAL_GET(w))
236 typedef unsigned long uwideint_t;
237 typedef long wideint_t;
238 typedef VALUE WIDEVALUE;
240# define WIDEVALUE_IS_WIDER 0
241# define UWIDEINT_MAX ULONG_MAX
242# define WIDEINT_MAX LONG_MAX
243# define WIDEINT_MIN LONG_MIN
244# define FIXWINT_P(v) FIXNUM_P(v)
245# define FIXWV_MAX FIXNUM_MAX
246# define FIXWV_MIN FIXNUM_MIN
247# define FIXWVABLE(i) FIXABLE(i)
248# define WINT2FIXWV(i) WIDEVAL_WRAP(LONG2FIX(i))
249# define FIXWV2WINT(w) FIX2LONG(WIDEVAL_GET(w))
252#define POSFIXWVABLE(wi) ((wi) < FIXWV_MAX+1)
253#define NEGFIXWVABLE(wi) ((wi) >= FIXWV_MIN)
254#define FIXWV_P(w) FIXWINT_P(WIDEVAL_GET(w))
255#define MUL_OVERFLOW_FIXWV_P(a, b) MUL_OVERFLOW_SIGNED_INTEGER_P(a, b, FIXWV_MIN, FIXWV_MAX)
263 static inline wideval_t WIDEVAL_WRAP(WIDEVALUE v) { wideval_t w = { v };
return w; }
264# define WIDEVAL_GET(w) ((w).value)
266 typedef WIDEVALUE wideval_t;
267# define WIDEVAL_WRAP(v) (v)
268# define WIDEVAL_GET(w) (w)
271#if WIDEVALUE_IS_WIDER
272 static inline wideval_t
273 wint2wv(wideint_t wi)
276 return WINT2FIXWV(wi);
278 return WIDEVAL_WRAP(INT64toNUM(wi));
280# define WINT2WV(wi) wint2wv(wi)
282# define WINT2WV(wi) WIDEVAL_WRAP(LONG2NUM(wi))
288#if WIDEVALUE_IS_WIDER
290 return INT64toNUM(FIXWV2WINT(w));
291 return (
VALUE)WIDEVAL_GET(w);
293 return WIDEVAL_GET(w);
297#if WIDEVALUE_IS_WIDER
303 sign = rb_integer_pack(v, &u, 1,
sizeof(u), 0,
306 return WINT2FIXWV(0);
307 else if (sign == -1) {
309 return WINT2FIXWV(-(wideint_t)u);
311 else if (sign == +1) {
313 return WINT2FIXWV((wideint_t)u);
315 return WIDEVAL_WRAP(v);
319static inline wideval_t
323 if (RRATIONAL(v)->den !=
LONG2FIX(1))
324 return WIDEVAL_WRAP(v);
325 v = RRATIONAL(v)->num;
327#if WIDEVALUE_IS_WIDER
329 return WIDEVAL_WRAP((WIDEVALUE)(SIGNED_WIDEVALUE)(
long)v);
331 else if (RB_BIGNUM_TYPE_P(v) &&
332 rb_absint_size(v, NULL) <=
sizeof(WIDEVALUE)) {
333 return v2w_bignum(v);
336 return WIDEVAL_WRAP(v);
339#define NUM2WV(v) v2w(rb_Integer(v))
342weq(wideval_t wx, wideval_t wy)
344#if WIDEVALUE_IS_WIDER
345 if (FIXWV_P(wx) && FIXWV_P(wy)) {
346 return WIDEVAL_GET(wx) == WIDEVAL_GET(wy);
350 return eq(WIDEVAL_GET(wx), WIDEVAL_GET(wy));
355wcmp(wideval_t wx, wideval_t wy)
358#if WIDEVALUE_IS_WIDER
359 if (FIXWV_P(wx) && FIXWV_P(wy)) {
375#define wne(x,y) (!weq((x),(y)))
376#define wlt(x,y) (wcmp((x),(y)) < 0)
377#define wgt(x,y) (wcmp((x),(y)) > 0)
378#define wle(x,y) (wcmp((x),(y)) <= 0)
379#define wge(x,y) (wcmp((x),(y)) >= 0)
382wadd(wideval_t wx, wideval_t wy)
384#if WIDEVALUE_IS_WIDER
385 if (FIXWV_P(wx) && FIXWV_P(wy)) {
386 wideint_t r = FIXWV2WINT(wx) + FIXWV2WINT(wy);
390 return v2w(addv(w2v(wx), w2v(wy)));
394wsub(wideval_t wx, wideval_t wy)
396#if WIDEVALUE_IS_WIDER
397 if (FIXWV_P(wx) && FIXWV_P(wy)) {
398 wideint_t r = FIXWV2WINT(wx) - FIXWV2WINT(wy);
402 return v2w(subv(w2v(wx), w2v(wy)));
406wmul(wideval_t wx, wideval_t wy)
408#if WIDEVALUE_IS_WIDER
409 if (FIXWV_P(wx) && FIXWV_P(wy)) {
410 if (!MUL_OVERFLOW_FIXWV_P(FIXWV2WINT(wx), FIXWV2WINT(wy)))
411 return WINT2WV(FIXWV2WINT(wx) * FIXWV2WINT(wy));
414 return v2w(mulv(w2v(wx), w2v(wy)));
418wquo(wideval_t wx, wideval_t wy)
420#if WIDEVALUE_IS_WIDER
421 if (FIXWV_P(wx) && FIXWV_P(wy)) {
432 return v2w(quov(w2v(wx), w2v(wy)));
435#define wmulquo(x,y,z) ((WIDEVAL_GET(y) == WIDEVAL_GET(z)) ? (x) : wquo(wmul((x),(y)),(z)))
436#define wmulquoll(x,y,z) (((y) == (z)) ? (x) : wquo(wmul((x),WINT2WV(y)),WINT2WV(z)))
438#if WIDEVALUE_IS_WIDER
440wdivmod0(wideval_t wn, wideval_t wd, wideval_t *wq, wideval_t *wr)
442 if (FIXWV_P(wn) && FIXWV_P(wd)) {
443 wideint_t n, d, q, r;
452 wideint_t xneg = -FIXWV2WINT(wn);
465 if (d > 0 ? r < 0 : r > 0) {
478wdivmod(wideval_t wn, wideval_t wd, wideval_t *wq, wideval_t *wr)
481#if WIDEVALUE_IS_WIDER
482 if (wdivmod0(wn, wd, wq, wr))
return;
484 divmodv(w2v(wn), w2v(wd), &vq, &vr);
490wmuldivmod(wideval_t wx, wideval_t wy, wideval_t wz, wideval_t *wq, wideval_t *wr)
492 if (WIDEVAL_GET(wy) == WIDEVAL_GET(wz)) {
497 wdivmod(wmul(wx,wy), wz, wq, wr);
501wdiv(wideval_t wx, wideval_t wy)
503#if WIDEVALUE_IS_WIDER
505 if (wdivmod0(wx, wy, &q, &dmy))
return q;
507 return v2w(divv(w2v(wx), w2v(wy)));
511wmod(wideval_t wx, wideval_t wy)
513#if WIDEVALUE_IS_WIDER
515 if (wdivmod0(wx, wy, &dmy, &r))
return r;
517 return v2w(modv(w2v(wx), w2v(wy)));
521num_exact_check(
VALUE v)
532 tmp = rb_rational_canonicalize(v);
546 tmp = rb_rational_canonicalize(tmp);
562NORETURN(
static void num_exact_fail(
VALUE v));
564num_exact_fail(
VALUE v)
566 rb_raise(
rb_eTypeError,
"can't convert %"PRIsVALUE
" into an exact number",
573 VALUE num = num_exact_check(v);
574 if (
NIL_P(num)) num_exact_fail(v);
581static const int TIME_SCALE_NUMDIGITS =
rb_strlen_lit(STRINGIZE(TIME_SCALE)) - 1;
584rb_time_magnify(wideval_t w)
586 return wmul(w, WINT2FIXWV(TIME_SCALE));
590rb_time_unmagnify_to_rational(wideval_t w)
592 return quor(w2v(w),
INT2FIX(TIME_SCALE));
596rb_time_unmagnify(wideval_t w)
598 return v2w(rb_time_unmagnify_to_rational(w));
602rb_time_unmagnify_to_float(wideval_t w)
605#if WIDEVALUE_IS_WIDER
614 v =
DBL2NUM((
double)FIXWV2WINT(w));
615 return quov(v,
DBL2NUM(TIME_SCALE));
622 return quov(v,
DBL2NUM(TIME_SCALE));
626split_second(wideval_t timew, wideval_t *timew_p,
VALUE *subsecx_p)
629 wdivmod(timew, WINT2FIXWV(TIME_SCALE), &q, &r);
637#if WIDEVALUE_IS_WIDER
638 if (TIMET_MIN == 0) {
639 uwideint_t wi = (uwideint_t)t;
640 if (wi <= FIXWV_MAX) {
641 return WINT2FIXWV(wi);
645 wideint_t wi = (wideint_t)t;
646 if (FIXWV_MIN <= wi && wi <= FIXWV_MAX) {
647 return WINT2FIXWV(wi);
651 return v2w(TIMET2NUM(t));
653#define TIMET2WV(t) timet2wv(t)
658#if WIDEVALUE_IS_WIDER
660 wideint_t wi = FIXWV2WINT(w);
661 if (TIMET_MIN == 0) {
663 rb_raise(
rb_eRangeError,
"negative value to convert into 'time_t'");
664 if (TIMET_MAX < (uwideint_t)wi)
668 if (wi < TIMET_MIN || TIMET_MAX < wi)
674 return NUM2TIMET(w2v(w));
676#define WV2TIMET(t) wv2timet(t)
679static VALUE rb_cTimeTM;
681static int obj2int(
VALUE obj);
682static uint32_t obj2ubits(
VALUE obj,
unsigned int bits);
684static uint32_t month_arg(
VALUE arg);
685static VALUE validate_utc_offset(
VALUE utc_offset);
686static VALUE validate_zone_name(
VALUE zone_name);
687static void validate_vtm(
struct vtm *
vtm);
688static void vtm_add_day(
struct vtm *
vtm,
int day);
689static uint32_t obj2subsecx(
VALUE obj,
VALUE *subsecx);
696static time_t timegm_noleapsecond(
struct tm *tm);
697static int tmcmp(
struct tm *a,
struct tm *b);
698static int vtmcmp(
struct vtm *a,
struct vtm *b);
699static const char *find_time_t(
struct tm *tptr,
int utc_p, time_t *tp);
701static struct vtm *localtimew(wideval_t timew,
struct vtm *result);
703static int leap_year_p(
long y);
704#define leap_year_v_p(y) leap_year_p(NUM2LONG(modv((y), INT2FIX(400))))
708bool ruby_tz_uptodate_p;
711enum {tzkey_max = numberof(((DYNAMIC_TIME_ZONE_INFORMATION *)NULL)->TimeZoneKeyName)};
714 char name[tzkey_max * 4 + 1];
720 if (w32_tz.use_tzkey) {
721 if (w32_tz.name[0]) {
731 DYNAMIC_TIME_ZONE_INFORMATION tzi;
732 WCHAR *
const wtzkey = tzi.TimeZoneKeyName;
733 DWORD tzret = GetDynamicTimeZoneInformation(&tzi);
734 if (tzret != TIME_ZONE_ID_INVALID && *wtzkey) {
735 int wlen = (int)wcsnlen(wtzkey, tzkey_max);
736 int clen = WideCharToMultiByte(CP_UTF8, 0, wtzkey, wlen,
737 w32_tz.name,
sizeof(w32_tz.name) - 1,
739 w32_tz.name[clen] =
'\0';
744 return _tzname[_daylight && dst];
749ruby_reset_timezone(
const char *val)
751 ruby_tz_uptodate_p =
false;
753 w32_tz.use_tzkey = !val || !*val;
755 ruby_reset_leap_second_info();
761 if (ruby_tz_uptodate_p)
return;
762 ruby_tz_uptodate_p =
true;
767rb_localtime_r(
const time_t *t,
struct tm *result)
769#if defined __APPLE__ && defined __LP64__
770 if (*t != (time_t)(
int)*t)
return NULL;
774 result = localtime_r(t, result);
777 struct tm *tmp = localtime(t);
778 if (tmp) *result = *tmp;
781#if defined(HAVE_MKTIME) && defined(LOCALTIME_OVERFLOW_PROBLEM)
785 struct tm tmp = *result;
788# if defined(HAVE_STRUCT_TM_TM_GMTOFF)
789 gmtoff1 = result->tm_gmtoff;
790 gmtoff2 = tmp.tm_gmtoff;
792 if (*t + gmtoff1 != t2 + gmtoff2)
798#define LOCALTIME(tm, result) rb_localtime_r((tm), &(result))
800#ifndef HAVE_STRUCT_TM_TM_GMTOFF
802rb_gmtime_r(
const time_t *t,
struct tm *result)
805 result = gmtime_r(t, result);
807 struct tm *tmp = gmtime(t);
808 if (tmp) *result = *tmp;
810#if defined(HAVE_TIMEGM) && defined(LOCALTIME_OVERFLOW_PROBLEM)
811 if (result && *t != timegm(result)) {
817# define GMTIME(tm, result) rb_gmtime_r((tm), &(result))
820static const int16_t common_year_yday_offset[] = {
825 -1 + 31 + 28 + 31 + 30,
826 -1 + 31 + 28 + 31 + 30 + 31,
827 -1 + 31 + 28 + 31 + 30 + 31 + 30,
828 -1 + 31 + 28 + 31 + 30 + 31 + 30 + 31,
829 -1 + 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31,
830 -1 + 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30,
831 -1 + 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31,
832 -1 + 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30
835static const int16_t leap_year_yday_offset[] = {
840 -1 + 31 + 29 + 31 + 30,
841 -1 + 31 + 29 + 31 + 30 + 31,
842 -1 + 31 + 29 + 31 + 30 + 31 + 30,
843 -1 + 31 + 29 + 31 + 30 + 31 + 30 + 31,
844 -1 + 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31,
845 -1 + 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30,
846 -1 + 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31,
847 -1 + 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30
851static const int8_t common_year_days_in_month[] = {
852 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
854static const int8_t leap_year_days_in_month[] = {
855 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
858#define days_in_month_of(leap) ((leap) ? leap_year_days_in_month : common_year_days_in_month)
859#define days_in_month_in(y) days_in_month_of(leap_year_p(y))
860#define days_in_month_in_v(y) days_in_month_of(leap_year_v_p(y))
863 (m),(m),(m),(m),(m),(m),(m),(m),(m),(m), \
864 (m),(m),(m),(m),(m),(m),(m),(m),(m),(m), \
865 (m),(m),(m),(m),(m),(m),(m),(m)
867 (m),(m),(m),(m),(m),(m),(m),(m),(m),(m), \
868 (m),(m),(m),(m),(m),(m),(m),(m),(m),(m), \
869 (m),(m),(m),(m),(m),(m),(m),(m),(m)
871 (m),(m),(m),(m),(m),(m),(m),(m),(m),(m), \
872 (m),(m),(m),(m),(m),(m),(m),(m),(m),(m), \
873 (m),(m),(m),(m),(m),(m),(m),(m),(m),(m)
875 (m),(m),(m),(m),(m),(m),(m),(m),(m),(m), \
876 (m),(m),(m),(m),(m),(m),(m),(m),(m),(m), \
877 (m),(m),(m),(m),(m),(m),(m),(m),(m),(m), (m)
879static const uint8_t common_year_mon_of_yday[] = {
880 M31(1), M28(2), M31(3), M30(4), M31(5), M30(6),
881 M31(7), M31(8), M30(9), M31(10), M30(11), M31(12)
883static const uint8_t leap_year_mon_of_yday[] = {
884 M31(1), M29(2), M31(3), M30(4), M31(5), M30(6),
885 M31(7), M31(8), M30(9), M31(10), M30(11), M31(12)
895 10,11,12,13,14,15,16,17,18,19, \
896 20,21,22,23,24,25,26,27,28
899 10,11,12,13,14,15,16,17,18,19, \
900 20,21,22,23,24,25,26,27,28,29
903 10,11,12,13,14,15,16,17,18,19, \
904 20,21,22,23,24,25,26,27,28,29,30
907 10,11,12,13,14,15,16,17,18,19, \
908 20,21,22,23,24,25,26,27,28,29,30,31
910static const uint8_t common_year_mday_of_yday[] = {
912 D31, D28, D31, D30, D31, D30, D31, D31, D30, D31, D30, D31
914static const uint8_t leap_year_mday_of_yday[] = {
915 D31, D29, D31, D30, D31, D30, D31, D31, D30, D31, D30, D31
924calc_tm_yday(
long tm_year,
int tm_mon,
int tm_mday)
926 int tm_year_mod400 = (int)MOD(tm_year, 400);
927 int tm_yday = tm_mday;
929 if (leap_year_p(tm_year_mod400 + 1900))
930 tm_yday += leap_year_yday_offset[tm_mon];
932 tm_yday += common_year_yday_offset[tm_mon];
938timegmw_noleapsecond(
struct vtm *
vtm)
950 divmodv(year1900,
INT2FIX(400), &q400, &r400);
953 yday = calc_tm_yday(year_mod400,
vtm->mon-1,
vtm->mday);
966 + DIV(year_mod400 - 69, 4)
967 - DIV(year_mod400 - 1, 100)
968 + (year_mod400 + 299) / 400;
970 vdays = addv(vdays, mulv(q400,
INT2FIX(97)));
971 vdays = addv(vdays, mulv(year1900,
INT2FIX(365)));
972 wret = wadd(rb_time_magnify(v2w(ret)), wmul(rb_time_magnify(v2w(vdays)), WINT2FIXWV(86400)));
973 wret = wadd(wret, v2w(
vtm->subsecx));
979zone_str(
const char *zone)
987 return rb_fstring_lit(
"(NO-TIMEZONE-ABBREVIATION)");
990 for (p = zone; *p; p++)
995 len = p - zone + strlen(p);
1000 str = rb_enc_str_new(zone,
len, rb_locale_encoding());
1002 return rb_fstring(str);
1006gmtimew_noleapsecond(wideval_t timew,
struct vtm *
vtm)
1012 wideval_t timew2, w, w2;
1017 split_second(timew, &timew2, &subsecx);
1018 vtm->subsecx = subsecx;
1020 wdivmod(timew2, WINT2FIXWV(86400), &w2, &w);
1025 vtm->wday = (wday + 4) % 7;
1028 vtm->sec = n % 60; n = n / 60;
1029 vtm->min = n % 60; n = n / 60;
1033 divmodv(timev,
INT2FIX(400*365 + 97), &timev, &v);
1046 if (30*365+7+31+29-1 <= n) {
1060 x = n / (365*100 + 24);
1061 n = n % (365*100 + 24);
1063 if (30*365+7+31+29-1 <= n) {
1073 x = n / (365*4 + 1);
1074 n = n % (365*4 + 1);
1076 if (365*2+31+29-1 <= n) {
1077 if (n < 365*2+366) {
1094 if (leap_year_p(y)) {
1095 vtm->mon = leap_year_mon_of_yday[n];
1096 vtm->mday = leap_year_mday_of_yday[n];
1099 vtm->mon = common_year_mon_of_yday[n];
1100 vtm->mday = common_year_mday_of_yday[n];
1104 vtm->zone = str_utc;
1108gmtime_with_leapsecond(
const time_t *timep,
struct tm *result)
1110#if defined(HAVE_STRUCT_TM_TM_GMTOFF)
1114 int gmtoff_sec, gmtoff_min, gmtoff_hour, gmtoff_day;
1116 t = LOCALTIME(timep, *result);
1121 if (t->tm_gmtoff < 0) {
1123 gmtoff = -t->tm_gmtoff;
1127 gmtoff = t->tm_gmtoff;
1129 gmtoff_sec = (int)(gmtoff % 60);
1130 gmtoff = gmtoff / 60;
1131 gmtoff_min = (int)(gmtoff % 60);
1132 gmtoff = gmtoff / 60;
1133 gmtoff_hour = (int)gmtoff;
1137 gmtoff_hour *= sign;
1144 result->tm_sec += gmtoff_sec;
1145 if (result->tm_sec < 0) {
1146 result->tm_sec += 60;
1149 if (60 <= result->tm_sec) {
1150 result->tm_sec -= 60;
1155 result->tm_min += gmtoff_min;
1156 if (result->tm_min < 0) {
1157 result->tm_min += 60;
1160 if (60 <= result->tm_min) {
1161 result->tm_min -= 60;
1166 result->tm_hour += gmtoff_hour;
1167 if (result->tm_hour < 0) {
1168 result->tm_hour += 24;
1171 if (24 <= result->tm_hour) {
1172 result->tm_hour -= 24;
1178 if (gmtoff_day < 0) {
1179 if (result->tm_yday == 0) {
1180 result->tm_mday = 31;
1181 result->tm_mon = 11;
1183 result->tm_yday = leap_year_p(result->tm_year + 1900) ? 365 : 364;
1185 else if (result->tm_mday == 1) {
1186 const int8_t *days_in_month = days_in_month_in(result->tm_year + 1900);
1188 result->tm_mday = days_in_month[result->tm_mon];
1195 result->tm_wday = (result->tm_wday + 6) % 7;
1198 int leap = leap_year_p(result->tm_year + 1900);
1199 if (result->tm_yday == (leap ? 365 : 364)) {
1202 result->tm_mday = 1;
1203 result->tm_yday = 0;
1205 else if (result->tm_mday == days_in_month_of(leap)[result->tm_mon]) {
1207 result->tm_mday = 1;
1214 result->tm_wday = (result->tm_wday + 1) % 7;
1217 result->tm_isdst = 0;
1218 result->tm_gmtoff = 0;
1219#if defined(HAVE_TM_ZONE)
1220 result->tm_zone = (
char *)
"UTC";
1224 return GMTIME(timep, *result);
1228static long this_year = 0;
1229static time_t known_leap_seconds_limit;
1230static int number_of_leap_seconds_known;
1233init_leap_second_info(
void)
1240 if (this_year == 0) {
1242 struct tm *tm, result;
1247 gmtime_r(&now, &result);
1251 tm = gmtime_with_leapsecond(&now, &result);
1253 this_year = tm->tm_year;
1255 if (TIMET_MAX - now < (time_t)(366*86400))
1256 known_leap_seconds_limit = TIMET_MAX;
1258 known_leap_seconds_limit = now + (time_t)(366*86400);
1260 if (!gmtime_with_leapsecond(&known_leap_seconds_limit, &result))
1264 vtm.mon = result.tm_mon + 1;
1265 vtm.mday = result.tm_mday;
1266 vtm.hour = result.tm_hour;
1267 vtm.min = result.tm_min;
1268 vtm.sec = result.tm_sec;
1272 timew = timegmw_noleapsecond(&
vtm);
1274 number_of_leap_seconds_known =
NUM2INT(w2v(wsub(TIMET2WV(known_leap_seconds_limit), rb_time_unmagnify(timew))));
1280ruby_reset_leap_second_info(
void)
1296 return timegmw_noleapsecond(
vtm);
1298 init_leap_second_info();
1300 timew = timegmw_noleapsecond(
vtm);
1303 if (number_of_leap_seconds_known == 0) {
1309 else if (wlt(rb_time_magnify(TIMET2WV(known_leap_seconds_limit)), timew)) {
1310 return wadd(timew, rb_time_magnify(WINT2WV(number_of_leap_seconds_known)));
1314 tm.tm_mon =
vtm->mon - 1;
1315 tm.tm_mday =
vtm->mday;
1316 tm.tm_hour =
vtm->hour;
1317 tm.tm_min =
vtm->min;
1318 tm.tm_sec =
vtm->sec;
1321 errmsg = find_time_t(&tm, 1, &t);
1323 rb_raise(rb_eArgError,
"%s", errmsg);
1324 return wadd(rb_time_magnify(TIMET2WV(t)), v2w(
vtm->subsecx));
1328gmtimew(wideval_t timew,
struct vtm *result)
1335 if (wlt(timew, WINT2FIXWV(0))) {
1336 gmtimew_noleapsecond(timew, result);
1340 init_leap_second_info();
1342 if (number_of_leap_seconds_known == 0) {
1346 gmtimew_noleapsecond(timew, result);
1349 else if (wlt(rb_time_magnify(TIMET2WV(known_leap_seconds_limit)), timew)) {
1350 timew = wsub(timew, rb_time_magnify(WINT2WV(number_of_leap_seconds_known)));
1351 gmtimew_noleapsecond(timew, result);
1355 split_second(timew, &timew2, &subsecx);
1357 t = WV2TIMET(timew2);
1358 if (!gmtime_with_leapsecond(&t, &tm))
1361 result->year =
LONG2NUM((
long)tm.tm_year + 1900);
1362 result->mon = tm.tm_mon + 1;
1363 result->mday = tm.tm_mday;
1364 result->hour = tm.tm_hour;
1365 result->min = tm.tm_min;
1366 result->sec = tm.tm_sec;
1367 result->subsecx = subsecx;
1368 result->utc_offset =
INT2FIX(0);
1369 result->wday = tm.tm_wday;
1370 result->yday = tm.tm_yday+1;
1371 result->isdst = tm.tm_isdst;
1376#define GMTIMEW(w, v) \
1377 (gmtimew(w, v) ? (void)0 : rb_raise(rb_eArgError, "gmtime error"))
1379static struct tm *localtime_with_gmtoff_zone(
const time_t *t,
struct tm *result,
long *gmtoff,
VALUE *zone);
1414static const int compat_common_month_table[12][7] = {
1416 { 2034, 2035, 2036, 2031, 2032, 2027, 2033 },
1417 { 2026, 2027, 2033, 2034, 2035, 2030, 2031 },
1418 { 2026, 2032, 2033, 2034, 2035, 2030, 2036 },
1419 { 2035, 2030, 2036, 2026, 2032, 2033, 2034 },
1420 { 2033, 2034, 2035, 2030, 2036, 2026, 2032 },
1421 { 2036, 2026, 2032, 2033, 2034, 2035, 2030 },
1422 { 2035, 2030, 2036, 2026, 2032, 2033, 2034 },
1423 { 2032, 2033, 2034, 2035, 2030, 2036, 2026 },
1424 { 2030, 2036, 2026, 2032, 2033, 2034, 2035 },
1425 { 2034, 2035, 2030, 2036, 2026, 2032, 2033 },
1426 { 2026, 2032, 2033, 2034, 2035, 2030, 2036 },
1427 { 2030, 2036, 2026, 2032, 2033, 2034, 2035 },
1455static const int compat_leap_month_table[7] = {
1457 2032, 2016, 2028, 2012, 2024, 2036, 2020,
1461calc_wday(
int year_mod400,
int month,
int day)
1466 a = (14 - month) / 12;
1467 y = year_mod400 + 4800 - a;
1468 m = month + 12 * a - 3;
1469 wday = day + (153*m+2)/5 + 365*y + y/4 - y/100 + y/400 + 2;
1475guess_local_offset(
struct vtm *vtm_utc,
int *isdst_ret,
VALUE *zone_ret)
1483 int year_mod400, wday;
1487 if (lt(vtm_utc->year,
INT2FIX(1916))) {
1490 zone = rb_fstring_lit(
"UTC");
1492# if defined(NEGATIVE_TIME_T)
1493# if SIZEOF_TIME_T <= 4
1495# define THE_TIME_OLD_ENOUGH ((time_t)0x80000000)
1499# define THE_TIME_OLD_ENOUGH ((time_t)(1600-1970)*366*24*60*60)
1501 if (localtime_with_gmtoff_zone((t = THE_TIME_OLD_ENOUGH, &t), &tm, &gmtoff, &zone)) {
1503 isdst = tm.tm_isdst;
1508 if (localtime_with_gmtoff_zone((t = 0, &t), &tm, &gmtoff, &zone)) {
1510 isdst = tm.tm_isdst;
1526 wday = calc_wday(year_mod400, vtm_utc->mon, 1);
1527 if (vtm_utc->mon == 2 && leap_year_p(year_mod400))
1528 vtm2.year =
INT2FIX(compat_leap_month_table[wday]);
1530 vtm2.year =
INT2FIX(compat_common_month_table[vtm_utc->mon-1][wday]);
1532 timev = w2v(rb_time_unmagnify(timegmw(&vtm2)));
1533 t = NUM2TIMET(timev);
1535 if (localtime_with_gmtoff_zone(&t, &tm, &gmtoff, &zone)) {
1537 *isdst_ret = tm.tm_isdst;
1545 static time_t now = 0;
1546 static long now_gmtoff = 0;
1547 static int now_isdst = 0;
1548 static VALUE now_zone;
1552 localtime_with_gmtoff_zone(&now, &tm, &now_gmtoff, &zone);
1553 now_isdst = tm.tm_isdst;
1554 zone = rb_fstring(zone);
1555 rb_vm_register_global_object(zone);
1559 *isdst_ret = now_isdst;
1561 *zone_ret = now_zone;
1567small_vtm_sub(
struct vtm *vtm1,
struct vtm *vtm2)
1571 off = vtm1->sec - vtm2->sec;
1572 off += (vtm1->min - vtm2->min) * 60;
1573 off += (vtm1->hour - vtm2->hour) * 3600;
1574 if (ne(vtm1->year, vtm2->year))
1575 off += lt(vtm1->year, vtm2->year) ? -24*3600 : 24*3600;
1576 else if (vtm1->mon != vtm2->mon)
1577 off += vtm1->mon < vtm2->mon ? -24*3600 : 24*3600;
1578 else if (vtm1->mday != vtm2->mday)
1579 off += vtm1->mday < vtm2->mday ? -24*3600 : 24*3600;
1585timelocalw(
struct vtm *
vtm)
1590 wideval_t timew1, timew2;
1591 struct vtm vtm1, vtm2;
1596 if (l < INT_MIN || INT_MAX < l)
1598 tm.tm_year = (int)l;
1607 tm.tm_mon =
vtm->mon-1;
1608 tm.tm_mday =
vtm->mday;
1609 tm.tm_hour =
vtm->hour;
1610 tm.tm_min =
vtm->min;
1611 tm.tm_sec =
vtm->sec;
1612 tm.tm_isdst =
vtm->isdst == VTM_ISDST_INITVAL ? -1 :
vtm->isdst;
1614 if (find_time_t(&tm, 0, &t))
1616 return wadd(rb_time_magnify(TIMET2WV(t)), v2w(
vtm->subsecx));
1619 timew1 = timegmw(
vtm);
1621 if (!localtimew(timew1, &vtm1))
1622 rb_raise(rb_eArgError,
"localtimew error");
1624 n = vtmcmp(
vtm, &vtm1);
1626 timew1 = wsub(timew1, rb_time_magnify(WINT2FIXWV(12*3600)));
1627 if (!localtimew(timew1, &vtm1))
1628 rb_raise(rb_eArgError,
"localtimew error");
1635 timew1 = wsub(timew1, rb_time_magnify(WINT2FIXWV(24*3600)));
1636 if (!localtimew(timew1, &vtm1))
1637 rb_raise(rb_eArgError,
"localtimew error");
1640 timew2 = wadd(timew1, rb_time_magnify(WINT2FIXWV(24*3600)));
1641 if (!localtimew(timew2, &vtm2))
1642 rb_raise(rb_eArgError,
"localtimew error");
1644 timew1 = wadd(timew1, rb_time_magnify(v2w(small_vtm_sub(
vtm, &vtm1))));
1645 timew2 = wadd(timew2, rb_time_magnify(v2w(small_vtm_sub(
vtm, &vtm2))));
1647 if (weq(timew1, timew2))
1650 if (!localtimew(timew1, &vtm1))
1651 rb_raise(rb_eArgError,
"localtimew error");
1652 if (
vtm->hour != vtm1.hour ||
vtm->min != vtm1.min ||
vtm->sec != vtm1.sec)
1655 if (!localtimew(timew2, &vtm2))
1656 rb_raise(rb_eArgError,
"localtimew error");
1657 if (
vtm->hour != vtm2.hour ||
vtm->min != vtm2.min ||
vtm->sec != vtm2.sec)
1661 return lt(vtm1.utc_offset, vtm2.utc_offset) ? timew2 : timew1;
1663 return lt(vtm1.utc_offset, vtm2.utc_offset) ? timew1 : timew2;
1667localtime_with_gmtoff_zone(
const time_t *t,
struct tm *result,
long *gmtoff,
VALUE *zone)
1671 if (LOCALTIME(t, tm)) {
1672#if defined(HAVE_STRUCT_TM_TM_GMTOFF)
1673 *gmtoff = tm.tm_gmtoff;
1679 u = GMTIME(t, tmbuf);
1682 if (l->tm_year != u->tm_year)
1683 off = l->tm_year < u->tm_year ? -1 : 1;
1684 else if (l->tm_mon != u->tm_mon)
1685 off = l->tm_mon < u->tm_mon ? -1 : 1;
1686 else if (l->tm_mday != u->tm_mday)
1687 off = l->tm_mday < u->tm_mday ? -1 : 1;
1690 off =
off * 24 + l->tm_hour - u->tm_hour;
1691 off =
off * 60 + l->tm_min - u->tm_min;
1692 off =
off * 60 + l->tm_sec - u->tm_sec;
1697#if defined(HAVE_TM_ZONE)
1698 *zone = zone_str(tm.tm_zone);
1699#elif defined(_WIN32)
1700 *zone = zone_str(get_tzname(tm.tm_isdst));
1701#elif defined(HAVE_TZNAME) && defined(HAVE_DAYLIGHT)
1703 *zone = zone_str(tzname[daylight && tm.tm_isdst]);
1707 strftime(buf,
sizeof(buf),
"%Z", &tm);
1708 *zone = zone_str(buf);
1720timew_out_of_timet_range(wideval_t timew)
1723#if WIDEVALUE_IS_WIDER && SIZEOF_TIME_T < SIZEOF_INT64_T
1724 if (FIXWV_P(timew)) {
1725 wideint_t t = FIXWV2WINT(timew);
1726 if (t < TIME_SCALE * (wideint_t)TIMET_MIN ||
1727 TIME_SCALE * (1 + (wideint_t)TIMET_MAX) <= t)
1732#if SIZEOF_TIME_T == SIZEOF_INT64_T
1733 if (FIXWV_P(timew)) {
1734 wideint_t t = FIXWV2WINT(timew);
1735 if (~(time_t)0 <= 0) {
1745 timexv = w2v(timew);
1746 if (lt(timexv, mulv(
INT2FIX(TIME_SCALE), TIMET2NUM(TIMET_MIN))) ||
1747 le(mulv(
INT2FIX(TIME_SCALE), addv(TIMET2NUM(TIMET_MAX),
INT2FIX(1))), timexv))
1753localtimew(wideval_t timew,
struct vtm *result)
1755 VALUE subsecx, offset;
1759 if (!timew_out_of_timet_range(timew)) {
1765 split_second(timew, &timew2, &subsecx);
1767 t = WV2TIMET(timew2);
1769 if (localtime_with_gmtoff_zone(&t, &tm, &gmtoff, &zone)) {
1770 result->year =
LONG2NUM((
long)tm.tm_year + 1900);
1771 result->mon = tm.tm_mon + 1;
1772 result->mday = tm.tm_mday;
1773 result->hour = tm.tm_hour;
1774 result->min = tm.tm_min;
1775 result->sec = tm.tm_sec;
1776 result->subsecx = subsecx;
1777 result->wday = tm.tm_wday;
1778 result->yday = tm.tm_yday+1;
1779 result->isdst = tm.tm_isdst;
1780 result->utc_offset =
LONG2NUM(gmtoff);
1781 result->zone = zone;
1786 if (!gmtimew(timew, result))
1789 offset = guess_local_offset(result, &isdst, &zone);
1791 if (!gmtimew(wadd(timew, rb_time_magnify(v2w(offset))), result))
1794 result->utc_offset = offset;
1795 result->isdst = isdst;
1796 result->zone = zone;
1801#define TIME_TZMODE_LOCALTIME 0
1802#define TIME_TZMODE_UTC 1
1803#define TIME_TZMODE_FIXOFF 2
1804#define TIME_TZMODE_UNINITIALIZED 3
1811#define GetTimeval(obj, tobj) ((tobj) = get_timeval(obj))
1812#define GetNewTimeval(obj, tobj) ((tobj) = get_new_timeval(obj))
1814#define IsTimeval(obj) rb_typeddata_is_kind_of((obj), &time_data_type)
1815#define TIME_INIT_P(tobj) ((tobj)->vtm.tzmode != TIME_TZMODE_UNINITIALIZED)
1817#define TZMODE_UTC_P(tobj) ((tobj)->vtm.tzmode == TIME_TZMODE_UTC)
1818#define TZMODE_SET_UTC(tobj) ((tobj)->vtm.tzmode = TIME_TZMODE_UTC)
1820#define TZMODE_LOCALTIME_P(tobj) ((tobj)->vtm.tzmode == TIME_TZMODE_LOCALTIME)
1821#define TZMODE_SET_LOCALTIME(tobj) ((tobj)->vtm.tzmode = TIME_TZMODE_LOCALTIME)
1823#define TZMODE_FIXOFF_P(tobj) ((tobj)->vtm.tzmode == TIME_TZMODE_FIXOFF)
1824#define TZMODE_SET_FIXOFF(time, tobj, off) do { \
1825 (tobj)->vtm.tzmode = TIME_TZMODE_FIXOFF; \
1826 RB_OBJ_WRITE_UNALIGNED(time, &(tobj)->vtm.utc_offset, off); \
1829#define TZMODE_COPY(tobj1, tobj2) \
1830 ((tobj1)->vtm.tzmode = (tobj2)->vtm.tzmode, \
1831 (tobj1)->vtm.utc_offset = (tobj2)->vtm.utc_offset, \
1832 (tobj1)->vtm.zone = (tobj2)->vtm.zone)
1834static int zone_localtime(
VALUE zone,
VALUE time);
1836#define MAKE_TM(time, tobj) \
1838 if ((tobj)->vtm.tm_got == 0) { \
1839 time_get_tm((time), (tobj)); \
1842#define MAKE_TM_ENSURE(time, tobj, cond) \
1844 MAKE_TM(time, tobj); \
1846 force_make_tm(time, tobj); \
1853 tobj->timew = timew;
1854 if (!FIXWV_P(timew)) {
1873 VALUE zone = tobj->vtm.zone;
1874 if (!
NIL_P(zone) && zone != str_empty && zone != str_utc) {
1875 if (zone_localtime(zone, time))
return;
1877 tobj->vtm.tm_got = 0;
1878 time_get_tm(time, tobj);
1885 if (!FIXWV_P(tobj->timew))
1886 rb_gc_mark(w2v(tobj->timew));
1887 rb_gc_mark(tobj->vtm.year);
1888 rb_gc_mark(tobj->vtm.subsecx);
1889 rb_gc_mark(tobj->vtm.utc_offset);
1890 rb_gc_mark(tobj->vtm.zone);
1901 (RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_FROZEN_SHAREABLE | RUBY_TYPED_WB_PROTECTED | RUBY_TYPED_EMBEDDABLE),
1905time_s_alloc(
VALUE klass)
1911 tobj->vtm.tzmode = TIME_TZMODE_UNINITIALIZED;
1912 tobj->vtm.tm_got = 0;
1913 time_set_timew(obj, tobj, WINT2FIXWV(0));
1914 tobj->vtm.zone =
Qnil;
1920get_timeval(
VALUE obj)
1924 if (!TIME_INIT_P(tobj)) {
1931get_new_timeval(
VALUE obj)
1935 if (TIME_INIT_P(tobj)) {
1942time_modify(
VALUE time)
1944 rb_check_frozen(time);
1948timenano2timew(time_t sec,
long nsec)
1952 timew = rb_time_magnify(TIMET2WV(sec));
1954 timew = wadd(timew, wmulquoll(WINT2WV(nsec), TIME_SCALE, 1000000000));
1959timew2timespec(wideval_t timew)
1965 if (timew_out_of_timet_range(timew))
1966 rb_raise(rb_eArgError,
"time out of system range");
1967 split_second(timew, &timew2, &subsecx);
1968 ts.tv_sec = WV2TIMET(timew2);
1974timew2timespec_exact(wideval_t timew,
struct timespec *ts)
1980 if (timew_out_of_timet_range(timew))
1982 split_second(timew, &timew2, &subsecx);
1983 ts->tv_sec = WV2TIMET(timew2);
1984 nsecv = mulquov(subsecx,
INT2FIX(1000000000),
INT2FIX(TIME_SCALE));
1994#ifdef HAVE_CLOCK_GETTIME
1995 if (clock_gettime(CLOCK_REALTIME, ts) == -1) {
1996 rb_sys_fail(
"clock_gettime");
2001 if (gettimeofday(&tv, 0) < 0) {
2002 rb_sys_fail(
"gettimeofday");
2004 ts->tv_sec = tv.tv_sec;
2005 ts->tv_nsec = tv.tv_usec * 1000;
2015time_init_now(rb_execution_context_t *ec,
VALUE time,
VALUE zone)
2021 GetNewTimeval(time, tobj);
2022 TZMODE_SET_LOCALTIME(tobj);
2025 time_set_timew(time, tobj, timenano2timew(ts.tv_sec, ts.tv_nsec));
2028 time_zonelocal(time, zone);
2034time_s_now(rb_execution_context_t *ec,
VALUE klass,
VALUE zone)
2036 VALUE t = time_s_alloc(klass);
2037 return time_init_now(ec, t, zone);
2047 GetTimeval(time, tobj);
2049 tobj->vtm.tm_got = 0;
2050 tobj->vtm.zone =
Qnil;
2051 TZMODE_SET_FIXOFF(time, tobj,
off);
2076 subsec = neg(subsec);
2085 vtm->subsecx = addv(
vtm->subsecx, w2v(rb_time_magnify(v2w(subsec))));
2134 vtm_add_day(
vtm, day);
2138vtm_add_day(
struct vtm *
vtm,
int day)
2142 if (
vtm->mon == 1 &&
vtm->mday == 1) {
2147 vtm->yday = leap_year_v_p(
vtm->year) ? 366 : 365;
2149 else if (
vtm->mday == 1) {
2150 const int8_t *days_in_month = days_in_month_in_v(
vtm->year);
2152 vtm->mday = days_in_month[
vtm->mon-1];
2153 if (
vtm->yday != 0)
vtm->yday--;
2157 if (
vtm->yday != 0)
vtm->yday--;
2159 if (
vtm->wday != VTM_WDAY_INITVAL)
vtm->wday = (
vtm->wday + 6) % 7;
2162 int leap = leap_year_v_p(
vtm->year);
2163 if (
vtm->mon == 12 &&
vtm->mday == 31) {
2169 else if (
vtm->mday == days_in_month_of(leap)[
vtm->mon-1]) {
2172 if (
vtm->yday != 0)
vtm->yday++;
2176 if (
vtm->yday != 0)
vtm->yday++;
2178 if (
vtm->wday != VTM_WDAY_INITVAL)
vtm->wday = (
vtm->wday + 1) % 7;
2184maybe_tzobj_p(
VALUE obj)
2186 if (
NIL_P(obj))
return FALSE;
2192NORETURN(
static void invalid_utc_offset(
VALUE));
2194invalid_utc_offset(
VALUE zone)
2196 rb_raise(rb_eArgError,
"\"+HH:MM\", \"-HH:MM\", \"UTC\" or "
2197 "\"A\"..\"I\",\"K\"..\"Z\" expected for utc_offset: %"PRIsVALUE,
2201#define have_2digits(ptr) (ISDIGIT((ptr)[0]) && ISDIGIT((ptr)[1]))
2202#define num_from_2digits(ptr) ((ptr)[0] * 10 + (ptr)[1] - '0' * 11)
2205utc_offset_arg(
VALUE arg)
2210 const char *s = RSTRING_PTR(tmp), *min = NULL, *sec = NULL;
2212 goto invalid_utc_offset;
2214 switch (RSTRING_LEN(tmp)) {
2220 if (s[0] >=
'A' && s[0] <=
'I') {
2221 n = (int)s[0] -
'A' + 1;
2224 else if (s[0] >=
'K' && s[0] <=
'M') {
2225 n = (int)s[0] -
'A';
2227 else if (s[0] >=
'N' && s[0] <=
'Y') {
2228 n =
'M' - (int)s[0];
2231 goto invalid_utc_offset;
2247 if (s[6] !=
':')
goto invalid_utc_offset;
2251 if (s[3] !=
':')
goto invalid_utc_offset;
2255 goto invalid_utc_offset;
2258 if (!have_2digits(sec))
goto invalid_utc_offset;
2259 if (sec[0] >
'5')
goto invalid_utc_offset;
2260 n += num_from_2digits(sec);
2264 if (!have_2digits(min))
goto invalid_utc_offset;
2265 if (min[0] >
'5')
goto invalid_utc_offset;
2266 n += num_from_2digits(min) * 60;
2268 if (s[0] !=
'+' && s[0] !=
'-')
goto invalid_utc_offset;
2269 if (!have_2digits(s+1))
goto invalid_utc_offset;
2270 n += num_from_2digits(s+1) * 3600;
2272 if (n == 0)
return UTC_ZONE;
2278 return num_exact(arg);
2286 wideval_t tlocal, wideval_t tutc)
2289 wideval_t w = wsub(tlocal, tutc);
2291 validate_utc_offset(
off);
2292 tobj->vtm.utc_offset =
off;
2293 tobj->vtm.zone = zone;
2294 TZMODE_SET_LOCALTIME(tobj);
2298extract_time(
VALUE time)
2301 const ID id_to_i = idTo_i;
2303#define EXTRACT_TIME() do { \
2304 t = NUM2WV(AREF(to_i)); \
2308 struct time_object *tobj = RTYPEDDATA_GET_DATA(time);
2311 t = rb_time_unmagnify(tobj->timew);
2316#define AREF(x) rb_struct_aref(time, ID2SYM(id_##x))
2321#define AREF(x) rb_funcallv(time, id_##x, 0, 0)
2334 const ID id_to_i = idTo_i;
2335 struct vtm *
vtm = &orig_tobj->vtm;
2337#define EXTRACT_VTM() do { \
2339 vtm->year = obj2vint(AREF(year)); \
2340 vtm->mon = month_arg(AREF(mon)); \
2341 vtm->mday = obj2ubits(AREF(mday), 5); \
2342 vtm->hour = obj2ubits(AREF(hour), 5); \
2343 vtm->min = obj2ubits(AREF(min), 6); \
2344 vtm->sec = obj2subsecx(AREF(sec), &subsecx); \
2345 vtm->isdst = RTEST(AREF(isdst)); \
2346 vtm->utc_offset = Qnil; \
2347 t = NUM2WV(AREF(to_i)); \
2351 struct time_object *tobj = RTYPEDDATA_GET_DATA(time);
2353 time_get_tm(time, tobj);
2354 time_set_vtm(orig_time, orig_tobj, tobj->vtm);
2355 t = rb_time_unmagnify(tobj->timew);
2356 if (TZMODE_FIXOFF_P(tobj) &&
vtm->utc_offset !=
INT2FIX(0))
2357 t = wadd(t, v2w(
vtm->utc_offset));
2362#define AREF(x) rb_struct_aref(time, ID2SYM(id_##x))
2368 struct vtm temp_vtm = *
vtm;
2369 GMTIMEW(rb_time_magnify(t), &temp_vtm);
2370 time_set_vtm(orig_time, orig_tobj, temp_vtm);
2373#define AREF(x) rb_funcallv(time, id_##x, 0, 0)
2379 RB_OBJ_WRITE_UNALIGNED(orig_time, &
vtm->subsecx, subsecx);
2392 tobj->vtm.isdst = (!UNDEF_P(dst) &&
RTEST(dst));
2399 struct time_object *tobj = RTYPEDDATA_GET_DATA(time);
2402 wdivmod(tobj->timew, WINT2FIXWV(TIME_SCALE), &t, &s);
2403 tm = tm_from_time(rb_cTimeTM, time);
2405 if (UNDEF_P(utc))
return 0;
2407 s = extract_time(utc);
2408 zone_set_offset(zone, tobj, t, s);
2409 s = rb_time_magnify(s);
2410 if (tobj->vtm.subsecx !=
INT2FIX(0)) {
2411 s = wadd(s, v2w(tobj->vtm.subsecx));
2413 time_set_timew(time, tobj, s);
2415 zone_set_dst(zone, tobj, tm);
2425 VALUE local, tm, subsecx;
2426 struct time_object *tobj = RTYPEDDATA_GET_DATA(time);
2429 split_second(tobj->timew, &t, &subsecx);
2430 tm = tm_from_time(rb_cTimeTM, time);
2433 if (UNDEF_P(local))
return 0;
2435 s = extract_vtm(local, time, tobj, subsecx);
2436 tobj->vtm.tm_got = 1;
2437 zone_set_offset(zone, tobj, s, t);
2438 zone_set_dst(zone, tobj, tm);
2450 return rb_check_funcall_default(klass, id_find_timezone, 1, &zone,
Qnil);
2456vtm_day_wraparound(
struct vtm *
vtm)
2458 if (
vtm->hour < 24)
return;
2463 vtm_add_day(
vtm, 1);
2478 vtm.wday = VTM_WDAY_INITVAL;
2480 vtm.zone = str_empty;
2482 vtm.year = obj2vint(year);
2484 vtm.mon =
NIL_P(mon) ? 1 : month_arg(mon);
2486 vtm.mday =
NIL_P(mday) ? 1 : obj2ubits(mday, 5);
2488 vtm.hour =
NIL_P(hour) ? 0 : obj2ubits(hour, 5);
2490 vtm.min =
NIL_P(min) ? 0 : obj2ubits(min, 6);
2498 vtm.sec = obj2subsecx(sec, &subsecx);
2499 vtm.subsecx = subsecx;
2502 return time_init_vtm(time,
vtm, zone);
2511 vtm.isdst = VTM_ISDST_INITVAL;
2513 const VALUE arg = zone;
2516 if (arg ==
ID2SYM(rb_intern(
"dst")))
2518 else if (arg ==
ID2SYM(rb_intern(
"std")))
2520 else if (maybe_tzobj_p(arg))
2522 else if (!
NIL_P(utc = utc_offset_arg(arg)))
2523 vtm.utc_offset = utc == UTC_ZONE ?
INT2FIX(0) : utc;
2524 else if (
NIL_P(zone = find_timezone(time, arg)))
2525 invalid_utc_offset(arg);
2531 GetNewTimeval(time, tobj);
2534 time_set_timew(time, tobj, timegmw(&
vtm));
2535 vtm_day_wraparound(&
vtm);
2536 time_set_vtm(time, tobj,
vtm);
2537 tobj->vtm.tm_got = 1;
2538 TZMODE_SET_LOCALTIME(tobj);
2539 if (zone_timelocal(zone, time)) {
2542 else if (
NIL_P(
vtm.utc_offset = utc_offset_arg(zone))) {
2543 if (
NIL_P(zone = find_timezone(time, zone)) || !zone_timelocal(zone, time))
2544 invalid_utc_offset(arg);
2548 if (utc == UTC_ZONE) {
2549 time_set_timew(time, tobj, timegmw(&
vtm));
2551 vtm_day_wraparound(&
vtm);
2552 time_set_vtm(time, tobj,
vtm);
2553 tobj->vtm.tm_got = 1;
2554 TZMODE_SET_UTC(tobj);
2558 TZMODE_SET_LOCALTIME(tobj);
2563 vtm_add_offset(&
vtm,
off, -1);
2565 time_set_timew(time, tobj, timegmw(&
vtm));
2567 return time_set_utc_offset(time,
off);
2570 time_set_timew(time, tobj, timelocalw(&
vtm));
2572 return time_localtime(time);
2577two_digits(
const char *ptr,
const char *end,
const char **endp,
const char *name)
2579 ssize_t
len = end - ptr;
2580 if (
len < 2 || !have_2digits(ptr) || ((
len > 2) &&
ISDIGIT(ptr[2]))) {
2581 VALUE mesg = rb_sprintf(
"two digits %s is expected", name);
2582 if (ptr[-1] ==
'-' || ptr[-1] ==
':') {
2583 rb_str_catf(mesg,
" after '%c'", ptr[-1]);
2585 rb_str_catf(mesg,
": %.*s", ((
len > 10) ? 10 : (
int)(end - ptr)) + 1, ptr - 1);
2589 return num_from_2digits(ptr);
2593parse_int(
const char *ptr,
const char *end,
const char **endp,
size_t *ndigits,
bool sign)
2595 ssize_t
len = (end - ptr);
2596 int flags = sign ? RB_INT_PARSE_SIGN : 0;
2597 return rb_int_parse_cstr(ptr,
len, (
char **)endp, ndigits, 10, flags);
2609 rb_raise(rb_eArgError,
"time string should have ASCII compatible encoding");
2612 const char *
const begin = RSTRING_PTR(str);
2614 const char *ptr = begin;
2616 int mon = -1, mday = -1, hour = -1, min = -1, sec = -1;
2618 size_t prec =
NIL_P(precision) ? SIZE_MAX :
NUM2SIZET(precision);
2621 rb_raise(rb_eArgError,
"can't parse: %+"PRIsVALUE, str);
2623 year = parse_int(ptr, end, &ptr, &ndigits,
true);
2625 rb_raise(rb_eArgError,
"can't parse: %+"PRIsVALUE, str);
2627 else if (ndigits < 4) {
2628 rb_raise(rb_eArgError,
"year must be 4 or more digits: %.*s", (
int)ndigits, ptr - ndigits);
2630 else if (ptr == end) {
2634#define peekable_p(n) ((ptrdiff_t)(n) < (end - ptr))
2635#define peek_n(c, n) (peekable_p(n) && ((unsigned char)ptr[n] == (c)))
2636#define peek(c) peek_n(c, 0)
2637#define peekc_n(n) (peekable_p(n) ? (int)(unsigned char)ptr[n] : -1)
2638#define peekc() peekc_n(0)
2639#define expect_two_digits(x, bits) \
2640 (((unsigned int)(x = two_digits(ptr + 1, end, &ptr, #x)) > (1U << bits) - 1) ? \
2641 rb_raise(rb_eArgError, #x" out of range") : (void)0)
2642 if (!peek(
'-'))
break;
2643 expect_two_digits(mon, 4);
2644 if (!peek(
'-'))
break;
2645 expect_two_digits(mday, 5);
2646 if (!peek(
' ') && !peek(
'T'))
break;
2647 const char *
const time_part = ptr + 1;
2648 if (!
ISDIGIT(peekc_n(1)))
break;
2649#define nofraction(x) \
2651 rb_raise(rb_eArgError, "fraction " #x " is not supported: %.*s", \
2652 (int)(ptr + 1 - time_part), time_part); \
2654#define need_colon(x) \
2656 rb_raise(rb_eArgError, "missing " #x " part: %.*s", \
2657 (int)(ptr + 1 - time_part), time_part); \
2659 expect_two_digits(hour, 5);
2662 expect_two_digits(min, 6);
2665 expect_two_digits(sec, 6);
2668 for (ndigits = 0; ndigits < prec &&
ISDIGIT(peekc_n(ndigits)); ++ndigits);
2670 int clen = rb_enc_precise_mbclen(ptr, end, rb_enc_get(str));
2671 if (clen < 0) clen = 0;
2672 rb_raise(rb_eArgError,
"subsecond expected after dot: %.*s",
2673 (
int)(ptr - time_part) + clen, time_part);
2675 subsec = parse_int(ptr, ptr + ndigits, &ptr, &ndigits,
false);
2676 if (
NIL_P(subsec))
break;
2677 while (ptr < end &&
ISDIGIT(*ptr)) ptr++;
2680 while (ptr < end &&
ISSPACE(*ptr)) ptr++;
2681 const char *
const zstr = ptr;
2682 while (ptr < end && !
ISSPACE(*ptr)) ptr++;
2683 const char *
const zend = ptr;
2684 while (ptr < end &&
ISSPACE(*ptr)) ptr++;
2693 else if (hour == -1) {
2694 rb_raise(rb_eArgError,
"no time information");
2696 if (!
NIL_P(subsec)) {
2698 if (ndigits < (
size_t)TIME_SCALE_NUMDIGITS) {
2700 subsec = rb_int_mul(subsec, mul);
2702 else if (ndigits > (
size_t)TIME_SCALE_NUMDIGITS) {
2712 .wday = VTM_WDAY_INITVAL,
2716 .mon = (mon < 0) ? 1 : mon,
2717 .mday = (mday < 0) ? 1 : mday,
2718 .hour = (hour < 0) ? 0 : hour,
2719 .min = (min < 0) ? 0 : min,
2720 .sec = (sec < 0) ? 0 : sec,
2723 return time_init_vtm(time,
vtm, zone);
2727subsec_normalize(time_t *secp,
long *subsecp,
const long maxsubsec)
2730 long subsec = *subsecp;
2733 if (UNLIKELY(subsec >= maxsubsec)) {
2734 sec2 = subsec / maxsubsec;
2735 if (TIMET_MAX - sec2 < sec) {
2738 subsec -= sec2 * maxsubsec;
2741 else if (UNLIKELY(subsec < 0)) {
2742 sec2 = NDIV(subsec, maxsubsec);
2743 if (sec < TIMET_MIN - sec2) {
2746 subsec -= sec2 * maxsubsec;
2749#ifndef NEGATIVE_TIME_T
2751 rb_raise(rb_eArgError,
"time must be positive");
2757#define time_usec_normalize(secp, usecp) subsec_normalize(secp, usecp, 1000000)
2758#define time_nsec_normalize(secp, nsecp) subsec_normalize(secp, nsecp, 1000000000)
2761nsec2timew(time_t sec,
long nsec)
2763 time_nsec_normalize(&sec, &nsec);
2764 return timenano2timew(sec, nsec);
2768time_new_timew(
VALUE klass, wideval_t timew)
2770 VALUE time = time_s_alloc(klass);
2773 tobj = RTYPEDDATA_GET_DATA(time);
2774 TZMODE_SET_LOCALTIME(tobj);
2775 time_set_timew(time, tobj, timew);
2783 time_usec_normalize(&sec, &usec);
2784 return time_new_timew(
rb_cTime, timenano2timew(sec, usec * 1000));
2791 return time_new_timew(
rb_cTime, nsec2timew(sec, nsec));
2798 VALUE time = time_new_timew(
rb_cTime, nsec2timew(ts->tv_sec, ts->tv_nsec));
2800 if (-86400 < offset && offset < 86400) {
2801 GetTimeval(time, tobj);
2802 TZMODE_SET_FIXOFF(time, tobj,
INT2FIX(offset));
2804 else if (offset == INT_MAX) {
2806 else if (offset == INT_MAX-1) {
2807 GetTimeval(time, tobj);
2808 TZMODE_SET_UTC(tobj);
2811 rb_raise(rb_eArgError,
"utc_offset out of range");
2820 VALUE time = time_new_timew(
rb_cTime, rb_time_magnify(v2w(timev)));
2825 if (maybe_tzobj_p(zone)) {
2827 if (zone_timelocal(zone, time))
return time;
2831 if (
NIL_P(zone = find_timezone(time,
off))) invalid_utc_offset(
off);
2833 if (!zone_timelocal(zone, time)) invalid_utc_offset(
off);
2836 else if (
off == UTC_ZONE) {
2837 return time_gmtime(time);
2840 validate_utc_offset(
off);
2841 time_set_utc_offset(time,
off);
2849time_timespec(
VALUE num, int interval)
2852 const char *
const tstr = interval ?
"time interval" :
"time";
2855#ifndef NEGATIVE_TIME_T
2856# define arg_range_check(v) \
2858 rb_raise(rb_eArgError, "%s must not be negative", tstr) : \
2861# define arg_range_check(v) \
2862 ((interval && (v) < 0) ? \
2863 rb_raise(rb_eArgError, "time interval must not be negative") : \
2868 t.tv_sec = NUM2TIMET(num);
2869 arg_range_check(t.tv_sec);
2880 t.tv_nsec = (int)(d*1e9+0.5);
2881 if (t.tv_nsec >= 1000000000) {
2882 t.tv_nsec -= 1000000000;
2886 else if ((t.tv_nsec = (
int)(-d*1e9+0.5)) > 0) {
2887 t.tv_nsec = 1000000000 - t.tv_nsec;
2890 t.tv_sec = (time_t)f;
2891 if (f != t.tv_sec) {
2896 else if (RB_BIGNUM_TYPE_P(num)) {
2897 t.tv_sec = NUM2TIMET(num);
2898 arg_range_check(t.tv_sec);
2904 if (!UNDEF_P(ary) && !
NIL_P(ary = rb_check_array_type(ary))) {
2905 i = rb_ary_entry(ary, 0);
2906 f = rb_ary_entry(ary, 1);
2907 t.tv_sec = NUM2TIMET(i);
2908 arg_range_check(t.tv_sec);
2913 rb_raise(
rb_eTypeError,
"can't convert %"PRIsVALUE
" into %s",
2918#undef arg_range_check
2922time_timeval(
VALUE num, int interval)
2927 ts = time_timespec(num, interval);
2928 tv.tv_sec = (TYPEOF_TIMEVAL_TV_SEC)ts.tv_sec;
2929 tv.tv_usec = (TYPEOF_TIMEVAL_TV_USEC)(ts.tv_nsec / 1000);
2937 return time_timeval(num, TRUE);
2947 if (IsTimeval(time)) {
2948 GetTimeval(time, tobj);
2949 ts = timew2timespec(tobj->timew);
2950 t.tv_sec = (TYPEOF_TIMEVAL_TV_SEC)ts.tv_sec;
2951 t.tv_usec = (TYPEOF_TIMEVAL_TV_USEC)(ts.tv_nsec / 1000);
2954 return time_timeval(time, FALSE);
2963 if (IsTimeval(time)) {
2964 GetTimeval(time, tobj);
2965 t = timew2timespec(tobj->timew);
2968 return time_timespec(time, FALSE);
2974 return time_timespec(num, TRUE);
2978get_scale(
VALUE unit)
2980 if (unit ==
ID2SYM(id_nanosecond) || unit ==
ID2SYM(id_nsec)) {
2983 else if (unit ==
ID2SYM(id_microsecond) || unit ==
ID2SYM(id_usec)) {
2986 else if (unit ==
ID2SYM(id_millisecond)) {
2990 rb_raise(rb_eArgError,
"unexpected unit: %"PRIsVALUE, unit);
3001 int scale = get_scale(unit);
3002 time = num_exact(time);
3003 t = num_exact(subsec);
3004 timew = wadd(rb_time_magnify(v2w(time)), wmulquoll(v2w(t), TIME_SCALE, scale));
3005 t = time_new_timew(klass, timew);
3007 else if (IsTimeval(time)) {
3009 GetTimeval(time, tobj);
3010 t = time_new_timew(klass, tobj->timew);
3011 GetTimeval(t, tobj2);
3012 TZMODE_COPY(tobj2, tobj);
3015 timew = rb_time_magnify(v2w(num_exact(time)));
3016 t = time_new_timew(klass, timew);
3019 time_zonelocal(t, zone);
3026time_s_at1(rb_execution_context_t *ec,
VALUE klass,
VALUE time)
3031static const char months[][4] = {
3032 "jan",
"feb",
"mar",
"apr",
"may",
"jun",
3033 "jul",
"aug",
"sep",
"oct",
"nov",
"dec",
3040 obj = rb_str_to_inum(obj, 10, TRUE);
3048obj2ubits(
VALUE obj,
unsigned int bits)
3050 const unsigned int usable_mask = (1U << bits) - 1;
3051 unsigned int rv = (
unsigned int)obj2int(obj);
3053 if ((rv & usable_mask) != rv)
3054 rb_raise(rb_eArgError,
"argument out of range");
3055 return (uint32_t)rv;
3062 obj = rb_str_to_inum(obj, 10, TRUE);
3077 obj = rb_str_to_inum(obj, 10, TRUE);
3081 divmodv(num_exact(obj),
INT2FIX(1), &obj, &subsec);
3082 *subsecx = w2v(rb_time_magnify(v2w(subsec)));
3084 return obj2ubits(obj, 6);
3088usec2subsecx(
VALUE obj)
3091 obj = rb_str_to_inum(obj, 10, TRUE);
3094 return mulquov(num_exact(obj),
INT2FIX(TIME_SCALE),
INT2FIX(1000000));
3103 return obj2ubits(arg, 4);
3108 if (!
NIL_P(s) && RSTRING_LEN(s) > 0) {
3110 for (i=0; i<12; i++) {
3111 if (RSTRING_LEN(s) == 3 &&
3119 mon = obj2ubits(arg, 4);
3125validate_utc_offset(
VALUE utc_offset)
3127 if (le(utc_offset,
INT2FIX(-86400)) || ge(utc_offset,
INT2FIX(86400)))
3128 rb_raise(rb_eArgError,
"utc_offset out of range");
3133validate_zone_name(
VALUE zone_name)
3140validate_vtm(
struct vtm *
vtm)
3142#define validate_vtm_range(mem, b, e) \
3143 ((vtm->mem < b || vtm->mem > e) ? \
3144 rb_raise(rb_eArgError, #mem" out of range") : (void)0)
3145 validate_vtm_range(mon, 1, 12);
3146 validate_vtm_range(mday, 1, 31);
3147 validate_vtm_range(hour, 0, 24);
3148 validate_vtm_range(min, 0, (
vtm->hour == 24 ? 0 : 59));
3149 validate_vtm_range(sec, 0, (
vtm->hour == 24 ? 0 : 60));
3151 rb_raise(rb_eArgError,
"subsecx out of range");
3152 if (!
NIL_P(
vtm->utc_offset)) validate_utc_offset(
vtm->utc_offset);
3153#undef validate_vtm_range
3157time_arg(
int argc,
const VALUE *argv,
struct vtm *
vtm)
3173 vtm->zone = str_empty;
3183 vtm->isdst =
RTEST(argv[8]) ? 1 : 0;
3186 rb_scan_args(argc, argv,
"17", &v[0],&v[1],&v[2],&v[3],&v[4],&v[5],&v[6],&v[7]);
3189 vtm->wday = VTM_WDAY_INITVAL;
3190 vtm->isdst = VTM_ISDST_INITVAL;
3193 vtm->year = obj2vint(v[0]);
3199 vtm->mon = month_arg(v[1]);
3206 vtm->mday = obj2ubits(v[2], 5);
3214 unsigned int mday2 = leap_year_v_p(
vtm->year) ? 29 : 28;
3215 if (
vtm->mday > mday2) {
3225 if (
vtm->mday == 31) {
3232 vtm->hour =
NIL_P(v[3])?0:obj2ubits(v[3], 5);
3234 vtm->min =
NIL_P(v[4])?0:obj2ubits(v[4], 6);
3236 if (!
NIL_P(v[6]) && argc == 7) {
3237 vtm->sec =
NIL_P(v[5])?0:obj2ubits(v[5],6);
3238 subsecx = usec2subsecx(v[6]);
3246 vtm->sec = obj2subsecx(v[5], &subsecx);
3249 vtm->subsecx = subsecx;
3261 unsigned long uy = (
unsigned long)(LIKELY(y >= 0) ? y : -y);
3263 if (LIKELY(uy % 4 != 0))
return 0;
3265 unsigned long century = uy / 100;
3266 if (LIKELY(uy != century * 100))
return 1;
3267 return century % 4 == 0;
3271timegm_noleapsecond(
struct tm *tm)
3273 long tm_year = tm->tm_year;
3274 int tm_yday = calc_tm_yday(tm->tm_year, tm->tm_mon, tm->tm_mday);
3282 return tm->tm_sec + tm->tm_min*60 + tm->tm_hour*3600 +
3286 DIV(tm_year-1,100) +
3287 DIV(tm_year+299,400))*86400;
3291#define DEBUG_FIND_TIME_NUMGUESS
3292#define DEBUG_GUESSRANGE
3295static const bool debug_guessrange =
3296#ifdef DEBUG_GUESSRANGE
3302#define DEBUG_REPORT_GUESSRANGE \
3303 (debug_guessrange ? debug_report_guessrange(guess_lo, guess_hi) : (void)0)
3306debug_report_guessrange(time_t guess_lo, time_t guess_hi)
3308 time_t guess_diff = guess_hi - guess_lo;
3309 fprintf(stderr,
"find time guess range: %"PRI_TIMET_PREFIX
"d - "
3310 "%"PRI_TIMET_PREFIX
"d : %"PRI_TIMET_PREFIX
"u\n",
3311 guess_lo, guess_hi, guess_diff);
3314static const bool debug_find_time_numguess =
3315#ifdef DEBUG_FIND_TIME_NUMGUESS
3321#define DEBUG_FIND_TIME_NUMGUESS_INC \
3322 (void)(debug_find_time_numguess && find_time_numguess++),
3323static unsigned long long find_time_numguess;
3326find_time_numguess_getter(
ID name,
VALUE *data)
3328 unsigned long long *numguess = (
void *)data;
3333find_time_t(
struct tm *tptr,
int utc_p, time_t *tp)
3335 time_t guess, guess0, guess_lo, guess_hi;
3336 struct tm *tm, tm0, tm_lo, tm_hi;
3343#define GUESS(p) (DEBUG_FIND_TIME_NUMGUESS_INC (utc_p ? gmtime_with_leapsecond((p), &result) : LOCALTIME((p), result)))
3345 guess_lo = TIMET_MIN;
3346 guess_hi = TIMET_MAX;
3348 find_dst = 0 < tptr->tm_isdst;
3354 if (tm0.tm_mon < 0) {
3361 else if (11 < tm0.tm_mon) {
3368 else if (tm0.tm_mday < 1) {
3374 else if ((d = days_in_month_in(1900 + tm0.tm_year)[tm0.tm_mon]) < tm0.tm_mday) {
3380 else if (tm0.tm_hour < 0) {
3385 else if (23 < tm0.tm_hour) {
3390 else if (tm0.tm_min < 0) {
3394 else if (59 < tm0.tm_min) {
3398 else if (tm0.tm_sec < 0) {
3401 else if (60 < tm0.tm_sec) {
3405 DEBUG_REPORT_GUESSRANGE;
3406 guess0 = guess = timegm_noleapsecond(&tm0);
3409 d = tmcmp(tptr, tm);
3410 if (d == 0) {
goto found; }
3413 guess -= 24 * 60 * 60;
3417 guess += 24 * 60 * 60;
3419 DEBUG_REPORT_GUESSRANGE;
3420 if (guess_lo < guess && guess < guess_hi && (tm = GUESS(&guess)) != NULL) {
3421 d = tmcmp(tptr, tm);
3422 if (d == 0) {
goto found; }
3427 DEBUG_REPORT_GUESSRANGE;
3431 tm = GUESS(&guess_lo);
3432 if (!tm)
goto error;
3433 d = tmcmp(tptr, tm);
3434 if (d < 0)
goto out_of_range;
3435 if (d == 0) { guess = guess_lo;
goto found; }
3438 tm = GUESS(&guess_hi);
3439 if (!tm)
goto error;
3440 d = tmcmp(tptr, tm);
3441 if (d > 0)
goto out_of_range;
3442 if (d == 0) { guess = guess_hi;
goto found; }
3445 DEBUG_REPORT_GUESSRANGE;
3449 while (guess_lo + 1 < guess_hi) {
3452 guess = guess_lo / 2 + guess_hi / 2;
3453 if (guess <= guess_lo)
3454 guess = guess_lo + 1;
3455 else if (guess >= guess_hi)
3456 guess = guess_hi - 1;
3461 time_t guess0_hi = timegm_noleapsecond(&tm_hi);
3462 guess = guess_hi - (guess0_hi - guess0);
3463 if (guess == guess_hi)
3467 else if (status == 2) {
3468 time_t guess0_lo = timegm_noleapsecond(&tm_lo);
3469 guess = guess_lo + (guess0 - guess0_lo);
3470 if (guess == guess_lo)
3474 if (guess <= guess_lo || guess_hi <= guess) {
3476 if (debug_guessrange) {
3477 if (guess <= guess_lo) {
3478 fprintf(stderr,
"too small guess: %"PRI_TIMET_PREFIX
"d"\
3479 " <= %"PRI_TIMET_PREFIX
"d\n", guess, guess_lo);
3481 if (guess_hi <= guess) {
3482 fprintf(stderr,
"too big guess: %"PRI_TIMET_PREFIX
"d"\
3483 " <= %"PRI_TIMET_PREFIX
"d\n", guess_hi, guess);
3492 if (!tm)
goto error;
3494 d = tmcmp(tptr, tm);
3499 DEBUG_REPORT_GUESSRANGE;
3504 DEBUG_REPORT_GUESSRANGE;
3519 tptr_tm_yday = calc_tm_yday(tptr->tm_year, tptr->tm_mon, tptr->tm_mday);
3522 ((tptr->tm_year - tm_lo.tm_year) * 365 +
3523 DIV((tptr->tm_year-69), 4) -
3524 DIV((tptr->tm_year-1), 100) +
3525 DIV((tptr->tm_year+299), 400) -
3526 DIV((tm_lo.tm_year-69), 4) +
3527 DIV((tm_lo.tm_year-1), 100) -
3528 DIV((tm_lo.tm_year+299), 400) +
3530 tm_lo.tm_yday) * 86400 +
3531 (tptr->tm_hour - tm_lo.tm_hour) * 3600 +
3532 (tptr->tm_min - tm_lo.tm_min) * 60 +
3533 (tptr->tm_sec - (tm_lo.tm_sec == 60 ? 59 : tm_lo.tm_sec));
3542 guess2 = guess - 2 * 60 * 60;
3543 tm = LOCALTIME(&guess2, result);
3545 if (tptr->tm_hour != (tm->tm_hour + 2) % 24 ||
3546 tptr->tm_min != tm->tm_min ||
3547 tptr->tm_sec != tm->tm_sec) {
3548 guess2 -= (tm->tm_hour - tptr->tm_hour) * 60 * 60 +
3549 (tm->tm_min - tptr->tm_min) * 60 +
3550 (tm->tm_sec - tptr->tm_sec);
3551 if (tptr->tm_mday != tm->tm_mday)
3552 guess2 += 24 * 60 * 60;
3553 if (guess != guess2) {
3554 tm = LOCALTIME(&guess2, result);
3555 if (tm && tmcmp(tptr, tm) == 0) {
3567 guess2 = guess + 2 * 60 * 60;
3568 tm = LOCALTIME(&guess2, result);
3570 if ((tptr->tm_hour + 2) % 24 != tm->tm_hour ||
3571 tptr->tm_min != tm->tm_min ||
3572 tptr->tm_sec != tm->tm_sec) {
3573 guess2 -= (tm->tm_hour - tptr->tm_hour) * 60 * 60 +
3574 (tm->tm_min - tptr->tm_min) * 60 +
3575 (tm->tm_sec - tptr->tm_sec);
3576 if (tptr->tm_mday != tm->tm_mday)
3577 guess2 -= 24 * 60 * 60;
3578 if (guess != guess2) {
3579 tm = LOCALTIME(&guess2, result);
3580 if (tm && tmcmp(tptr, tm) == 0) {
3596 return "time out of range";
3599 return "gmtime/localtime error";
3603vtmcmp(
struct vtm *a,
struct vtm *b)
3605 if (ne(a->year, b->year))
3606 return lt(a->year, b->year) ? -1 : 1;
3607 else if (a->mon != b->mon)
3608 return a->mon < b->mon ? -1 : 1;
3609 else if (a->mday != b->mday)
3610 return a->mday < b->mday ? -1 : 1;
3611 else if (a->hour != b->hour)
3612 return a->hour < b->hour ? -1 : 1;
3613 else if (a->min != b->min)
3614 return a->min < b->min ? -1 : 1;
3615 else if (a->sec != b->sec)
3616 return a->sec < b->sec ? -1 : 1;
3617 else if (ne(a->subsecx, b->subsecx))
3618 return lt(a->subsecx, b->subsecx) ? -1 : 1;
3624tmcmp(
struct tm *a,
struct tm *b)
3626 if (a->tm_year != b->tm_year)
3627 return a->tm_year < b->tm_year ? -1 : 1;
3628 else if (a->tm_mon != b->tm_mon)
3629 return a->tm_mon < b->tm_mon ? -1 : 1;
3630 else if (a->tm_mday != b->tm_mday)
3631 return a->tm_mday < b->tm_mday ? -1 : 1;
3632 else if (a->tm_hour != b->tm_hour)
3633 return a->tm_hour < b->tm_hour ? -1 : 1;
3634 else if (a->tm_min != b->tm_min)
3635 return a->tm_min < b->tm_min ? -1 : 1;
3636 else if (a->tm_sec != b->tm_sec)
3637 return a->tm_sec < b->tm_sec ? -1 : 1;
3743time_s_mkutc(
int argc,
VALUE *argv,
VALUE klass)
3747 time_arg(argc, argv, &
vtm);
3748 return time_gmtime(time_new_timew(klass, timegmw(&
vtm)));
3769time_s_mktime(
int argc,
VALUE *argv,
VALUE klass)
3773 time_arg(argc, argv, &
vtm);
3774 return time_localtime(time_new_timew(klass, timelocalw(&
vtm)));
3794time_to_i(
VALUE time)
3798 GetTimeval(time, tobj);
3799 return w2v(wdiv(tobj->timew, WINT2FIXWV(TIME_SCALE)));
3823time_to_f(
VALUE time)
3827 GetTimeval(time, tobj);
3828 return rb_Float(rb_time_unmagnify_to_float(tobj->timew));
3844time_to_r(
VALUE time)
3849 GetTimeval(time, tobj);
3850 v = rb_time_unmagnify_to_rational(tobj->timew);
3872time_usec(
VALUE time)
3877 GetTimeval(time, tobj);
3879 w = wmod(tobj->timew, WINT2WV(TIME_SCALE));
3880 wmuldivmod(w, WINT2FIXWV(1000000), WINT2FIXWV(TIME_SCALE), &q, &r);
3899time_nsec(
VALUE time)
3903 GetTimeval(time, tobj);
3904 return rb_to_int(w2v(wmulquoll(wmod(tobj->timew, WINT2WV(TIME_SCALE)), 1000000000, TIME_SCALE)));
3925time_subsec(
VALUE time)
3929 GetTimeval(time, tobj);
3930 return quov(w2v(wmod(tobj->timew, WINT2FIXWV(TIME_SCALE))),
INT2FIX(TIME_SCALE));
3967 GetTimeval(time1, tobj1);
3968 if (IsTimeval(time2)) {
3969 GetTimeval(time2, tobj2);
3970 n = wcmp(tobj1->timew, tobj2->timew);
3973 return rb_invcmp(time1, time2);
3975 if (n == 0)
return INT2FIX(0);
3993 GetTimeval(time1, tobj1);
3994 if (IsTimeval(time2)) {
3995 GetTimeval(time2, tobj2);
3996 return rb_equal(w2v(tobj1->timew), w2v(tobj2->timew));
4029time_utc_p(
VALUE time)
4033 GetTimeval(time, tobj);
4034 return RBOOL(TZMODE_UTC_P(tobj));
4047time_hash(
VALUE time)
4051 GetTimeval(time, tobj);
4052 return rb_hash(w2v(tobj->timew));
4062 GetTimeval(time, tobj);
4063 GetNewTimeval(copy, tcopy);
4073 time_init_copy(dup, time);
4078time_localtime(
VALUE time)
4084 GetTimeval(time, tobj);
4085 if (TZMODE_LOCALTIME_P(tobj)) {
4086 if (tobj->vtm.tm_got)
4093 zone = tobj->vtm.zone;
4094 if (maybe_tzobj_p(zone) && zone_localtime(zone, time)) {
4098 if (!localtimew(tobj->timew, &
vtm))
4099 rb_raise(rb_eArgError,
"localtime error");
4100 time_set_vtm(time, tobj,
vtm);
4102 tobj->vtm.tm_got = 1;
4103 TZMODE_SET_LOCALTIME(tobj);
4111 if (zone_localtime(zone, time))
return time;
4115 if (
NIL_P(zone = find_timezone(time,
off))) invalid_utc_offset(
off);
4116 if (!zone_localtime(zone, time)) invalid_utc_offset(
off);
4119 else if (
off == UTC_ZONE) {
4120 return time_gmtime(time);
4122 validate_utc_offset(
off);
4124 time_set_utc_offset(time,
off);
4125 return time_fixoff(time);
4154time_localtime_m(
int argc,
VALUE *argv,
VALUE time)
4159 return time_zonelocal(time,
off);
4162 return time_localtime(time);
4180time_gmtime(
VALUE time)
4185 GetTimeval(time, tobj);
4186 if (TZMODE_UTC_P(tobj)) {
4187 if (tobj->vtm.tm_got)
4195 GMTIMEW(tobj->timew, &
vtm);
4196 time_set_vtm(time, tobj,
vtm);
4198 tobj->vtm.tm_got = 1;
4199 TZMODE_SET_UTC(tobj);
4204time_fixoff(
VALUE time)
4210 GetTimeval(time, tobj);
4211 if (TZMODE_FIXOFF_P(tobj)) {
4212 if (tobj->vtm.tm_got)
4219 if (TZMODE_FIXOFF_P(tobj))
4220 off = tobj->vtm.utc_offset;
4224 GMTIMEW(tobj->timew, &
vtm);
4226 zone = tobj->vtm.zone;
4227 vtm_add_offset(&
vtm,
off, +1);
4229 time_set_vtm(time, tobj,
vtm);
4230 RB_OBJ_WRITE_UNALIGNED(time, &tobj->vtm.zone, zone);
4232 tobj->vtm.tm_got = 1;
4233 TZMODE_SET_FIXOFF(time, tobj,
off);
4255time_getlocaltime(
int argc,
VALUE *argv,
VALUE time)
4261 if (maybe_tzobj_p(zone)) {
4262 VALUE t = time_dup(time);
4263 if (zone_localtime(
off, t))
return t;
4268 if (
NIL_P(zone = find_timezone(time,
off))) invalid_utc_offset(
off);
4269 time = time_dup(time);
4270 if (!zone_localtime(zone, time)) invalid_utc_offset(
off);
4273 else if (
off == UTC_ZONE) {
4274 return time_gmtime(time_dup(time));
4276 validate_utc_offset(
off);
4278 time = time_dup(time);
4279 time_set_utc_offset(time,
off);
4280 return time_fixoff(time);
4283 return time_localtime(time_dup(time));
4302time_getgmtime(
VALUE time)
4304 return time_gmtime(time_dup(time));
4310 if (TZMODE_UTC_P(tobj))
return time_gmtime(time);
4311 if (TZMODE_FIXOFF_P(tobj))
return time_fixoff(time);
4312 return time_localtime(time);
4315static VALUE strftime_cstr(
const char *fmt,
size_t len,
VALUE time, rb_encoding *enc);
4316#define strftimev(fmt, time, enc) strftime_cstr((fmt), rb_strlen_lit(fmt), (time), (enc))
4340time_asctime(
VALUE time)
4342 return strftimev(
"%a %b %e %T %Y", time, rb_usascii_encoding());
4362time_to_s(
VALUE time)
4366 GetTimeval(time, tobj);
4367 if (TZMODE_UTC_P(tobj))
4368 return strftimev(
"%Y-%m-%d %H:%M:%S UTC", time, rb_usascii_encoding());
4370 return strftimev(
"%Y-%m-%d %H:%M:%S %z", time, rb_usascii_encoding());
4390time_inspect(
VALUE time)
4395 GetTimeval(time, tobj);
4396 str = strftimev(
"%Y-%m-%d %H:%M:%S", time, rb_usascii_encoding());
4397 subsec = w2v(wmod(tobj->timew, WINT2FIXWV(TIME_SCALE)));
4402 rb_str_catf(str,
".%09ld",
FIX2LONG(subsec));
4403 for (
len=RSTRING_LEN(str); RSTRING_PTR(str)[
len-1] ==
'0' &&
len > 0;
len--)
4405 rb_str_resize(str,
len);
4409 subsec = quov(subsec,
INT2FIX(TIME_SCALE));
4412 if (TZMODE_UTC_P(tobj)) {
4418 char sign = (
off < 0) ? (
off = -
off,
'-') :
'+';
4420 int min = (
off /= 60) % 60;
4422 rb_str_catf(str,
" %c%.2d%.2d", sign, (
int)
off, min);
4423 if (sec) rb_str_catf(str,
"%.2d", sec);
4434 offset = num_exact(offset);
4436 result = time_new_timew(klass, wsub(tobj->timew, rb_time_magnify(v2w(offset))));
4438 result = time_new_timew(klass, wadd(tobj->timew, rb_time_magnify(v2w(offset))));
4439 GetTimeval(result, result_tobj);
4440 TZMODE_COPY(result_tobj, tobj);
4448 return time_add0(
rb_cTime, tobj, torig, offset, sign);
4469 GetTimeval(time1, tobj);
4471 if (IsTimeval(time2)) {
4474 return time_add(tobj, time1, time2, 1);
4504 GetTimeval(time1, tobj);
4505 if (IsTimeval(time2)) {
4508 GetTimeval(time2, tobj2);
4509 return rb_Float(rb_time_unmagnify_to_float(wsub(tobj->timew, tobj2->timew)));
4511 return time_add(tobj, time1, time2, -1);
4515ndigits_denominator(
VALUE ndigits)
4520 rb_raise(rb_eArgError,
"negative ndigits given");
4560 VALUE ndigits, v, den;
4566 den = ndigits_denominator(ndigits);
4568 GetTimeval(time, tobj);
4569 v = w2v(rb_time_unmagnify(tobj->timew));
4572 if (lt(v, quov(den,
INT2FIX(2))))
4573 return time_add(tobj, time, v, -1);
4575 return time_add(tobj, time, subv(den, v), 1);
4608 VALUE ndigits, v, den;
4614 den = ndigits_denominator(ndigits);
4616 GetTimeval(time, tobj);
4617 v = w2v(rb_time_unmagnify(tobj->timew));
4620 return time_add(tobj, time, v, -1);
4653 VALUE ndigits, v, den;
4659 den = ndigits_denominator(ndigits);
4661 GetTimeval(time, tobj);
4662 v = w2v(rb_time_unmagnify(tobj->timew));
4668 return time_add(tobj, time, v, 1);
4693 GetTimeval(time, tobj);
4694 MAKE_TM(time, tobj);
4695 return INT2FIX(tobj->vtm.sec);
4717 GetTimeval(time, tobj);
4718 MAKE_TM(time, tobj);
4719 return INT2FIX(tobj->vtm.min);
4737time_hour(
VALUE time)
4741 GetTimeval(time, tobj);
4742 MAKE_TM(time, tobj);
4743 return INT2FIX(tobj->vtm.hour);
4761time_mday(
VALUE time)
4765 GetTimeval(time, tobj);
4766 MAKE_TM(time, tobj);
4767 return INT2FIX(tobj->vtm.mday);
4789 GetTimeval(time, tobj);
4790 MAKE_TM(time, tobj);
4791 return INT2FIX(tobj->vtm.mon);
4808time_year(
VALUE time)
4812 GetTimeval(time, tobj);
4813 MAKE_TM(time, tobj);
4814 return tobj->vtm.year;
4833time_wday(
VALUE time)
4837 GetTimeval(time, tobj);
4838 MAKE_TM_ENSURE(time, tobj, tobj->vtm.wday != VTM_WDAY_INITVAL);
4839 return INT2FIX((
int)tobj->vtm.wday);
4843 return RBOOL(time_wday(time) == INT2FIX(n)); \
4859time_sunday(
VALUE time)
4877time_monday(
VALUE time)
4895time_tuesday(
VALUE time)
4913time_wednesday(
VALUE time)
4931time_thursday(
VALUE time)
4949time_friday(
VALUE time)
4967time_saturday(
VALUE time)
4983time_yday(
VALUE time)
4987 GetTimeval(time, tobj);
4988 MAKE_TM_ENSURE(time, tobj, tobj->vtm.yday != 0);
4989 return INT2FIX(tobj->vtm.yday);
5008time_isdst(
VALUE time)
5012 GetTimeval(time, tobj);
5013 MAKE_TM(time, tobj);
5014 if (tobj->vtm.isdst == VTM_ISDST_INITVAL) {
5017 return RBOOL(tobj->vtm.isdst);
5031time_zone(
VALUE time)
5036 GetTimeval(time, tobj);
5037 MAKE_TM(time, tobj);
5039 if (TZMODE_UTC_P(tobj)) {
5042 zone = tobj->vtm.zone;
5067 GetTimeval(time, tobj);
5069 if (TZMODE_UTC_P(tobj)) {
5073 MAKE_TM(time, tobj);
5074 return tobj->vtm.utc_offset;
5094time_to_a(
VALUE time)
5098 GetTimeval(time, tobj);
5099 MAKE_TM_ENSURE(time, tobj, tobj->vtm.yday != 0);
5109 RBOOL(tobj->vtm.isdst),
5155 GetTimeval(time, tobj);
5156 MAKE_TM_ENSURE(time, tobj, tobj->vtm.yday != 0);
5159 h = rb_hash_new_with_size(11);
5161 rb_hash_aset(h, sym_year, tobj->vtm.year);
5162 rb_hash_aset(h, sym_month,
INT2FIX(tobj->vtm.mon));
5163 rb_hash_aset(h, sym_day,
INT2FIX(tobj->vtm.mday));
5164 rb_hash_aset(h, sym_yday,
INT2FIX(tobj->vtm.yday));
5165 rb_hash_aset(h, sym_wday,
INT2FIX(tobj->vtm.wday));
5166 rb_hash_aset(h, sym_hour,
INT2FIX(tobj->vtm.hour));
5167 rb_hash_aset(h, sym_min,
INT2FIX(tobj->vtm.min));
5168 rb_hash_aset(h, sym_sec,
INT2FIX(tobj->vtm.sec));
5169 rb_hash_aset(h, sym_subsec,
5170 quov(w2v(wmod(tobj->timew, WINT2FIXWV(TIME_SCALE))),
INT2FIX(TIME_SCALE)));
5171 rb_hash_aset(h, sym_dst, RBOOL(tobj->vtm.isdst));
5172 rb_hash_aset(h, sym_zone, time_zone(time));
5178 "wrong argument type %"PRIsVALUE
" (expected Array or nil)",
5188 if (sym_year == key) rb_hash_aset(h, key, tobj->vtm.year);
5189 if (sym_month == key) rb_hash_aset(h, key,
INT2FIX(tobj->vtm.mon));
5190 if (sym_day == key) rb_hash_aset(h, key,
INT2FIX(tobj->vtm.mday));
5191 if (sym_yday == key) rb_hash_aset(h, key,
INT2FIX(tobj->vtm.yday));
5192 if (sym_wday == key) rb_hash_aset(h, key,
INT2FIX(tobj->vtm.wday));
5193 if (sym_hour == key) rb_hash_aset(h, key,
INT2FIX(tobj->vtm.hour));
5194 if (sym_min == key) rb_hash_aset(h, key,
INT2FIX(tobj->vtm.min));
5195 if (sym_sec == key) rb_hash_aset(h, key,
INT2FIX(tobj->vtm.sec));
5196 if (sym_subsec == key) {
5197 rb_hash_aset(h, key, quov(w2v(wmod(tobj->timew, WINT2FIXWV(TIME_SCALE))),
INT2FIX(TIME_SCALE)));
5199 if (sym_dst == key) rb_hash_aset(h, key, RBOOL(tobj->vtm.isdst));
5200 if (sym_zone == key) rb_hash_aset(h, key, time_zone(time));
5206rb_strftime_alloc(
const char *format,
size_t format_len, rb_encoding *enc,
5207 VALUE time,
struct vtm *
vtm, wideval_t timew,
int gmt)
5212 if (!timew2timespec_exact(timew, &ts))
5213 timev = w2v(rb_time_unmagnify(timew));
5216 return rb_strftime_timespec(format, format_len, enc, time,
vtm, &ts, gmt);
5219 return rb_strftime(format, format_len, enc, time,
vtm, timev, gmt);
5224strftime_cstr(
const char *fmt,
size_t len,
VALUE time, rb_encoding *enc)
5229 GetTimeval(time, tobj);
5230 MAKE_TM(time, tobj);
5231 str = rb_strftime_alloc(fmt,
len, enc, time, &tobj->vtm, tobj->timew, TZMODE_UTC_P(tobj));
5232 if (!str) rb_raise(rb_eArgError,
"invalid format: %s", fmt);
5254 GetTimeval(time, tobj);
5255 MAKE_TM_ENSURE(time, tobj, tobj->vtm.yday != 0);
5258 rb_raise(rb_eArgError,
"format should have ASCII compatible encoding");
5260 tmp = rb_str_tmp_frozen_acquire(format);
5261 fmt = RSTRING_PTR(tmp);
5262 len = RSTRING_LEN(tmp);
5263 enc = rb_enc_get(format);
5265 rb_warning(
"strftime called with empty format string");
5266 return rb_enc_str_new(0, 0, enc);
5269 VALUE str = rb_strftime_alloc(fmt,
len, enc, time, &tobj->vtm, tobj->timew,
5270 TZMODE_UTC_P(tobj));
5271 rb_str_tmp_frozen_release(format, tmp);
5272 if (!str) rb_raise(rb_eArgError,
"invalid format: %"PRIsVALUE, format);
5299time_xmlschema(
int argc,
VALUE *argv,
VALUE time)
5301 long fraction_digits = 0;
5304 fraction_digits =
NUM2LONG(argv[0]);
5305 if (fraction_digits < 0) {
5306 fraction_digits = 0;
5312 GetTimeval(time, tobj);
5313 MAKE_TM(time, tobj);
5315 const long size_after_year =
sizeof(
"-MM-DDTHH:MM:SS+ZH:ZM") + fraction_digits
5316 + (fraction_digits > 0);
5320# define fill_digits_long(len, prec, n) \
5321 for (int fill_it = 1, written = snprintf(ptr, len, "%0*ld", prec, n); \
5322 fill_it; ptr += written, fill_it = 0)
5325 long year =
FIX2LONG(tobj->vtm.year);
5327 int w = (year >= -9999 && year <= 9999 ? year_width : (year < 0) + (int)
DECIMAL_SIZE_OF(year));
5329 ptr = RSTRING_PTR(str);
5330 fill_digits_long(w + 1, year_width, year) {
5331 if (year >= -9999 && year <= 9999) {
5341 str = rb_int2str(tobj->vtm.year, 10);
5346# define fill_2(c, n) (*ptr++ = c, *ptr++ = '0' + (n) / 10, *ptr++ = '0' + (n) % 10)
5347 fill_2(
'-', tobj->vtm.mon);
5348 fill_2(
'-', tobj->vtm.mday);
5349 fill_2(
'T', tobj->vtm.hour);
5350 fill_2(
':', tobj->vtm.min);
5351 fill_2(
':', tobj->vtm.sec);
5353 if (fraction_digits > 0) {
5354 VALUE subsecx = tobj->vtm.subsecx;
5358 if (fraction_digits <= TIME_SCALE_NUMDIGITS) {
5359 digits = TIME_SCALE_NUMDIGITS - (int)fraction_digits;
5362 long w = fraction_digits - TIME_SCALE_NUMDIGITS;
5369 if (digits >= 0 && fraction_digits < INT_MAX) {
5371 if (digits > 0) subsec /= (long)pow(10, digits);
5372 fill_digits_long(fraction_digits + 1, (
int)fraction_digits, subsec) {
5377 subsecx = rb_int2str(subsecx, 10);
5378 long len = RSTRING_LEN(subsecx);
5379 if (fraction_digits >
len) {
5380 memset(ptr,
'0', fraction_digits -
len);
5383 len = fraction_digits;
5385 ptr += fraction_digits;
5386 memcpy(ptr -
len, RSTRING_PTR(subsecx),
len);
5390 if (TZMODE_UTC_P(tobj)) {
5396 char sign = offset < 0 ?
'-' :
'+';
5397 if (offset < 0) offset = -offset;
5399 fill_2(sign, offset / 60);
5400 fill_2(
':', offset % 60);
5402 const char *
const start = RSTRING_PTR(str);
5407int ruby_marshal_write_long(
long x,
char *buf);
5409enum {base_dump_size = 8};
5413time_mdump(
VALUE time)
5417 char buf[base_dump_size +
sizeof(long) + 1];
5424 VALUE subsecx, nano, subnano, v, zone;
5427 const int max_year = 1900+0xffff;
5429 GetTimeval(time, tobj);
5431 gmtimew(tobj->timew, &
vtm);
5435 if (year > max_year) {
5436 year_extend =
INT2FIX(year - max_year);
5439 else if (year < 1900) {
5440 year_extend =
LONG2NUM(1900 - year);
5445 if (rb_int_positive_p(
vtm.year)) {
5446 year_extend = rb_int_minus(
vtm.year,
INT2FIX(max_year));
5450 year_extend = rb_int_minus(
INT2FIX(1900),
vtm.year);
5455 subsecx =
vtm.subsecx;
5457 nano = mulquov(subsecx,
INT2FIX(1000000000),
INT2FIX(TIME_SCALE));
5458 divmodv(nano,
INT2FIX(1), &v, &subnano);
5463 nano = addv(
LONG2FIX(nsec), subnano);
5466 TZMODE_UTC_P(tobj) << 30 |
5471 s = (
unsigned long)
vtm.min << 26 |
5475 for (i=0; i<4; i++) {
5476 buf[i] = (
unsigned char)p;
5479 for (i=4; i<8; i++) {
5480 buf[i] = (
unsigned char)s;
5484 if (!
NIL_P(year_extend)) {
5491 size_t ysize = rb_absint_size(year_extend, NULL);
5492 char *p, *
const buf_year_extend = buf + base_dump_size;
5493 if (ysize > LONG_MAX ||
5494 (i = ruby_marshal_write_long((
long)ysize, buf_year_extend)) < 0) {
5495 rb_raise(rb_eArgError,
"year too %s to marshal: %"PRIsVALUE
" UTC",
5496 (year == 1900 ?
"small" :
"big"),
vtm.year);
5498 i += base_dump_size;
5500 p = RSTRING_PTR(str);
5511 rb_ivar_set(str, id_nano_num, RRATIONAL(nano)->num);
5512 rb_ivar_set(str, id_nano_den, RRATIONAL(nano)->den);
5528 int len = (int)
sizeof(buf);
5529 buf[1] = (char)((nsec % 10) << 4);
5531 buf[0] = (char)(nsec % 10);
5533 buf[0] |= (char)((nsec % 10) << 4);
5538 if (!TZMODE_UTC_P(tobj)) {
5545 zone = tobj->vtm.zone;
5546 if (maybe_tzobj_p(zone)) {
5547 zone = rb_funcallv(zone, id_name, 0, 0);
5560 str = time_mdump(time);
5566mload_findzone(
VALUE arg)
5569 VALUE time = argp[0], zone = argp[1];
5570 return find_timezone(time, zone);
5580 if (
NIL_P(z))
return rb_fstring(zone);
5585long ruby_marshal_read_long(
const char **buf,
long len);
5599 VALUE submicro, nano_num, nano_den, offset, zone, year;
5604#define get_attr(attr, iffound) \
5605 attr = rb_attr_delete(str, id_##attr); \
5606 if (!NIL_P(attr)) { \
5610 get_attr(nano_num, {});
5611 get_attr(nano_den, {});
5612 get_attr(submicro, {});
5613 get_attr(offset, (offset =
rb_rescue(validate_utc_offset, offset, 0,
Qnil)));
5614 get_attr(zone, (zone =
rb_rescue(validate_zone_name, zone, 0,
Qnil)));
5622 buf = (
unsigned char *)RSTRING_PTR(str);
5623 if (RSTRING_LEN(str) < base_dump_size) {
5624 goto invalid_format;
5628 for (i=0; i<4; i++) {
5629 p |= (
unsigned long)buf[i]<<(8*i);
5631 for (i=4; i<8; i++) {
5632 s |= (
unsigned long)buf[i]<<(8*(i-4));
5635 if ((p & (1UL<<31)) == 0) {
5641 timew = wadd(rb_time_magnify(TIMET2WV(sec)), wmulquoll(WINT2FIXWV(usec), TIME_SCALE, 1000000));
5645 gmt = (int)((p >> 30) & 0x1);
5648 year =
INT2FIX(((
int)(p >> 14) & 0xffff) + 1900);
5650 if (RSTRING_LEN(str) > base_dump_size) {
5651 long len = RSTRING_LEN(str) - base_dump_size;
5654 const char *ybuf = (
const char *)(buf += base_dump_size);
5655 ysize = ruby_marshal_read_long(&ybuf,
len);
5656 len -= ybuf - (
const char *)buf;
5657 if (ysize < 0 || ysize >
len)
goto invalid_format;
5660 year = rb_int_minus(year, year_extend);
5663 year = rb_int_plus(year, year_extend);
5666 unsigned int mon = ((int)(p >> 10) & 0xf);
5673 vtm.mday = (int)(p >> 5) & 0x1f;
5674 vtm.hour = (int) p & 0x1f;
5675 vtm.min = (int)(s >> 26) & 0x3f;
5676 vtm.sec = (int)(s >> 20) & 0x3f;
5680 vtm.zone = str_empty;
5682 usec = (long)(s & 0xfffff);
5687 if (nano_num !=
Qnil) {
5688 VALUE nano = quov(num_exact(nano_num), num_exact(nano_den));
5691 else if (submicro !=
Qnil) {
5696 len = RSTRING_LEN(submicro);
5699 if (10 <= (digit = ptr[0] >> 4))
goto end_submicro;
5700 nsec += digit * 100;
5701 if (10 <= (digit = ptr[0] & 0xf))
goto end_submicro;
5705 if (10 <= (digit = ptr[1] >> 4))
goto end_submicro;
5711 timew = timegmw(&
vtm);
5714 GetNewTimeval(time, tobj);
5715 TZMODE_SET_LOCALTIME(tobj);
5716 tobj->vtm.tm_got = 0;
5717 time_set_timew(time, tobj, timew);
5720 TZMODE_SET_UTC(tobj);
5722 else if (!
NIL_P(offset)) {
5723 time_set_utc_offset(time, offset);
5727 zone = mload_zone(time, zone);
5728 tobj->vtm.zone = zone;
5729 zone_localtime(zone, time);
5743 VALUE time = time_s_alloc(klass);
5745 time_mload(time, str);
5771 GetTimeval(time, tobj);
5772 tm = time_s_alloc(klass);
5773 ttm = RTYPEDDATA_GET_DATA(tm);
5775 GMTIMEW(ttm->timew = tobj->timew, v);
5776 ttm->timew = wsub(ttm->timew, v->subsecx);
5779 time_set_vtm(tm, ttm, *v);
5781 ttm->vtm.tm_got = 1;
5782 TZMODE_SET_UTC(ttm);
5795tm_initialize(
int argc,
VALUE *argv,
VALUE time)
5801 time_arg(argc, argv, &
vtm);
5803 struct time_object *tobj = RTYPEDDATA_GET_DATA(time);
5804 TZMODE_SET_UTC(tobj);
5805 time_set_timew(time, tobj, t);
5806 time_set_vtm(time, tobj,
vtm);
5823 struct time_object *tobj = RTYPEDDATA_GET_DATA(dup);
5831 return time_add0(
rb_obj_class(tm), get_timeval(tm), tm, offset, +1);
5837 return time_add0(
rb_obj_class(tm), get_timeval(tm), tm, offset, -1);
5841Init_tm(
VALUE outer,
const char *name)
5887rb_time_zone_abbreviation(
VALUE zone,
VALUE time)
5889 VALUE tm, abbr, strftime_args[2];
5892 if (!
NIL_P(abbr))
return abbr;
5894 tm = tm_from_time(rb_cTimeTM, time);
5896 if (!UNDEF_P(abbr)) {
5899#ifdef SUPPORT_TZINFO_ZONE_ABBREVIATION
5901 if (!UNDEF_P(abbr)) {
5902 abbr = rb_funcallv(abbr, rb_intern(
"abbreviation"), 0, 0);
5906 strftime_args[0] = rb_fstring_lit(
"%Z");
5907 strftime_args[1] = tm;
5909 if (!UNDEF_P(abbr)) {
5912 abbr = rb_check_funcall_default(zone, idName, 0, 0,
Qnil);
5914 return rb_obj_as_string(abbr);
5922 ruby_reset_timezone(getenv(
"TZ"));
5958 str_utc = rb_fstring_lit(
"UTC");
5959 rb_vm_register_global_object(str_utc);
5960 str_empty = rb_fstring_lit(
"");
5961 rb_vm_register_global_object(str_empty);
6045 if (debug_find_time_numguess) {
6047 find_time_numguess_getter, 0);
6050 rb_cTimeTM = Init_tm(
rb_cTime,
"tm");
6053#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.