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);
205 rb_raise(
rb_eTypeError,
"unexpected divmod result: into %"PRIsVALUE,
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 SIZEOF_WIDEINT SIZEOF_INT64_T
253#define POSFIXWVABLE(wi) ((wi) < FIXWV_MAX+1)
254#define NEGFIXWVABLE(wi) ((wi) >= FIXWV_MIN)
255#define FIXWV_P(w) FIXWINT_P(WIDEVAL_GET(w))
256#define MUL_OVERFLOW_FIXWV_P(a, b) MUL_OVERFLOW_SIGNED_INTEGER_P(a, b, FIXWV_MIN, FIXWV_MAX)
264 static inline wideval_t WIDEVAL_WRAP(WIDEVALUE v) { wideval_t w = { v };
return w; }
265# define WIDEVAL_GET(w) ((w).value)
267 typedef WIDEVALUE wideval_t;
268# define WIDEVAL_WRAP(v) (v)
269# define WIDEVAL_GET(w) (w)
272#if WIDEVALUE_IS_WIDER
273 static inline wideval_t
274 wint2wv(wideint_t wi)
277 return WINT2FIXWV(wi);
279 return WIDEVAL_WRAP(INT64toNUM(wi));
281# define WINT2WV(wi) wint2wv(wi)
283# define WINT2WV(wi) WIDEVAL_WRAP(LONG2NUM(wi))
289#if WIDEVALUE_IS_WIDER
291 return INT64toNUM(FIXWV2WINT(w));
292 return (
VALUE)WIDEVAL_GET(w);
294 return WIDEVAL_GET(w);
298#if WIDEVALUE_IS_WIDER
304 sign = rb_integer_pack(v, &u, 1,
sizeof(u), 0,
307 return WINT2FIXWV(0);
308 else if (sign == -1) {
310 return WINT2FIXWV(-(wideint_t)u);
312 else if (sign == +1) {
314 return WINT2FIXWV((wideint_t)u);
316 return WIDEVAL_WRAP(v);
320static inline wideval_t
324 if (RRATIONAL(v)->den !=
LONG2FIX(1))
325 return WIDEVAL_WRAP(v);
326 v = RRATIONAL(v)->num;
328#if WIDEVALUE_IS_WIDER
330 return WIDEVAL_WRAP((WIDEVALUE)(SIGNED_WIDEVALUE)(
long)v);
332 else if (RB_BIGNUM_TYPE_P(v) &&
333 rb_absint_size(v, NULL) <=
sizeof(WIDEVALUE)) {
334 return v2w_bignum(v);
337 return WIDEVAL_WRAP(v);
340#define NUM2WV(v) v2w(rb_Integer(v))
343weq(wideval_t wx, wideval_t wy)
345#if WIDEVALUE_IS_WIDER
346 if (FIXWV_P(wx) && FIXWV_P(wy)) {
347 return WIDEVAL_GET(wx) == WIDEVAL_GET(wy);
351 return eq(WIDEVAL_GET(wx), WIDEVAL_GET(wy));
356wcmp(wideval_t wx, wideval_t wy)
359#if WIDEVALUE_IS_WIDER
360 if (FIXWV_P(wx) && FIXWV_P(wy)) {
376#define wne(x,y) (!weq((x),(y)))
377#define wlt(x,y) (wcmp((x),(y)) < 0)
378#define wgt(x,y) (wcmp((x),(y)) > 0)
379#define wle(x,y) (wcmp((x),(y)) <= 0)
380#define wge(x,y) (wcmp((x),(y)) >= 0)
383wadd(wideval_t wx, wideval_t wy)
385#if WIDEVALUE_IS_WIDER
386 if (FIXWV_P(wx) && FIXWV_P(wy)) {
387 wideint_t r = FIXWV2WINT(wx) + FIXWV2WINT(wy);
391 return v2w(addv(w2v(wx), w2v(wy)));
395wsub(wideval_t wx, wideval_t wy)
397#if WIDEVALUE_IS_WIDER
398 if (FIXWV_P(wx) && FIXWV_P(wy)) {
399 wideint_t r = FIXWV2WINT(wx) - FIXWV2WINT(wy);
403 return v2w(subv(w2v(wx), w2v(wy)));
407wmul(wideval_t wx, wideval_t wy)
409#if WIDEVALUE_IS_WIDER
410 if (FIXWV_P(wx) && FIXWV_P(wy)) {
411 if (!MUL_OVERFLOW_FIXWV_P(FIXWV2WINT(wx), FIXWV2WINT(wy)))
412 return WINT2WV(FIXWV2WINT(wx) * FIXWV2WINT(wy));
415 return v2w(mulv(w2v(wx), w2v(wy)));
419wquo(wideval_t wx, wideval_t wy)
421#if WIDEVALUE_IS_WIDER
422 if (FIXWV_P(wx) && FIXWV_P(wy)) {
433 return v2w(quov(w2v(wx), w2v(wy)));
436#define wmulquo(x,y,z) ((WIDEVAL_GET(y) == WIDEVAL_GET(z)) ? (x) : wquo(wmul((x),(y)),(z)))
437#define wmulquoll(x,y,z) (((y) == (z)) ? (x) : wquo(wmul((x),WINT2WV(y)),WINT2WV(z)))
439#if WIDEVALUE_IS_WIDER
441wdivmod0(wideval_t wn, wideval_t wd, wideval_t *wq, wideval_t *wr)
443 if (FIXWV_P(wn) && FIXWV_P(wd)) {
444 wideint_t n, d, q, r;
453 wideint_t xneg = -FIXWV2WINT(wn);
466 if (d > 0 ? r < 0 : r > 0) {
479wdivmod(wideval_t wn, wideval_t wd, wideval_t *wq, wideval_t *wr)
482#if WIDEVALUE_IS_WIDER
483 if (wdivmod0(wn, wd, wq, wr))
return;
485 divmodv(w2v(wn), w2v(wd), &vq, &vr);
491wmuldivmod(wideval_t wx, wideval_t wy, wideval_t wz, wideval_t *wq, wideval_t *wr)
493 if (WIDEVAL_GET(wy) == WIDEVAL_GET(wz)) {
498 wdivmod(wmul(wx,wy), wz, wq, wr);
502wdiv(wideval_t wx, wideval_t wy)
504#if WIDEVALUE_IS_WIDER
506 if (wdivmod0(wx, wy, &q, &dmy))
return q;
508 return v2w(divv(w2v(wx), w2v(wy)));
512wmod(wideval_t wx, wideval_t wy)
514#if WIDEVALUE_IS_WIDER
516 if (wdivmod0(wx, wy, &dmy, &r))
return r;
518 return v2w(modv(w2v(wx), w2v(wy)));
522num_exact_check(
VALUE v)
533 tmp = rb_rational_canonicalize(v);
547 tmp = rb_rational_canonicalize(tmp);
563NORETURN(
static void num_exact_fail(
VALUE v));
565num_exact_fail(
VALUE v)
567 rb_raise(
rb_eTypeError,
"can't convert %"PRIsVALUE
" into an exact number",
574 VALUE num = num_exact_check(v);
575 if (
NIL_P(num)) num_exact_fail(v);
582static const int TIME_SCALE_NUMDIGITS =
rb_strlen_lit(STRINGIZE(TIME_SCALE)) - 1;
585rb_time_magnify(wideval_t w)
587 return wmul(w, WINT2FIXWV(TIME_SCALE));
591rb_time_unmagnify_to_rational(wideval_t w)
593 return quor(w2v(w),
INT2FIX(TIME_SCALE));
597rb_time_unmagnify(wideval_t w)
599 return v2w(rb_time_unmagnify_to_rational(w));
603rb_time_unmagnify_to_float(wideval_t w)
606#if WIDEVALUE_IS_WIDER
615 v =
DBL2NUM((
double)FIXWV2WINT(w));
616 return quov(v,
DBL2NUM(TIME_SCALE));
623 return quov(v,
DBL2NUM(TIME_SCALE));
627split_second(wideval_t timew, wideval_t *timew_p,
VALUE *subsecx_p)
630 wdivmod(timew, WINT2FIXWV(TIME_SCALE), &q, &r);
638#if WIDEVALUE_IS_WIDER
639 if (TIMET_MIN == 0) {
640 uwideint_t wi = (uwideint_t)t;
641 if (wi <= FIXWV_MAX) {
642 return WINT2FIXWV(wi);
646 wideint_t wi = (wideint_t)t;
647 if (FIXWV_MIN <= wi && wi <= FIXWV_MAX) {
648 return WINT2FIXWV(wi);
652 return v2w(TIMET2NUM(t));
654#define TIMET2WV(t) timet2wv(t)
659#if WIDEVALUE_IS_WIDER
661 wideint_t wi = FIXWV2WINT(w);
662 if (TIMET_MIN == 0) {
664 rb_raise(
rb_eRangeError,
"negative value to convert into 'time_t'");
665 if (TIMET_MAX < (uwideint_t)wi)
669 if (wi < TIMET_MIN || TIMET_MAX < wi)
675 return NUM2TIMET(w2v(w));
677#define WV2TIMET(t) wv2timet(t)
680static VALUE rb_cTimeTM;
682static int obj2int(
VALUE obj);
683static uint32_t obj2ubits(
VALUE obj,
unsigned int bits);
685static uint32_t month_arg(
VALUE arg);
686static VALUE validate_utc_offset(
VALUE utc_offset);
687static VALUE validate_zone_name(
VALUE zone_name);
688static void validate_vtm(
struct vtm *
vtm);
689static void vtm_add_day(
struct vtm *
vtm,
int day);
690static uint32_t obj2subsecx(
VALUE obj,
VALUE *subsecx);
697static time_t timegm_noleapsecond(
struct tm *tm);
698static int tmcmp(
struct tm *a,
struct tm *b);
699static int vtmcmp(
struct vtm *a,
struct vtm *b);
700static const char *find_time_t(
struct tm *tptr,
int utc_p, time_t *tp);
702static struct vtm *localtimew(wideval_t timew,
struct vtm *result);
704static int leap_year_p(
long y);
705#define leap_year_v_p(y) leap_year_p(NUM2LONG(modv((y), INT2FIX(400))))
709bool ruby_tz_uptodate_p;
712enum {tzkey_max = numberof(((DYNAMIC_TIME_ZONE_INFORMATION *)NULL)->TimeZoneKeyName)};
715 char name[tzkey_max * 4 + 1];
721 if (w32_tz.use_tzkey) {
722 if (w32_tz.name[0]) {
732 DYNAMIC_TIME_ZONE_INFORMATION tzi;
733 WCHAR *
const wtzkey = tzi.TimeZoneKeyName;
734 DWORD tzret = GetDynamicTimeZoneInformation(&tzi);
735 if (tzret != TIME_ZONE_ID_INVALID && *wtzkey) {
736 int wlen = (int)wcsnlen(wtzkey, tzkey_max);
737 int clen = WideCharToMultiByte(CP_UTF8, 0, wtzkey, wlen,
738 w32_tz.name,
sizeof(w32_tz.name) - 1,
740 w32_tz.name[clen] =
'\0';
745 return _tzname[_daylight && dst];
749static void ruby_reset_leap_second_info(
void);
752ruby_reset_timezone(
const char *val)
754 ruby_tz_uptodate_p =
false;
756 w32_tz.use_tzkey = !val || !*val;
758 ruby_reset_leap_second_info();
764 if (ruby_tz_uptodate_p)
return;
765 ruby_tz_uptodate_p =
true;
770rb_localtime_r(
const time_t *t,
struct tm *result)
772#if defined __APPLE__ && defined __LP64__
773 if (*t != (time_t)(
int)*t)
return NULL;
777 result = localtime_r(t, result);
780 struct tm *tmp = localtime(t);
781 if (tmp) *result = *tmp;
784#if defined(HAVE_MKTIME) && defined(LOCALTIME_OVERFLOW_PROBLEM)
788 struct tm tmp = *result;
791# if defined(HAVE_STRUCT_TM_TM_GMTOFF)
792 gmtoff1 = result->tm_gmtoff;
793 gmtoff2 = tmp.tm_gmtoff;
795 if (*t + gmtoff1 != t2 + gmtoff2)
801#define LOCALTIME(tm, result) rb_localtime_r((tm), &(result))
803#ifndef HAVE_STRUCT_TM_TM_GMTOFF
805rb_gmtime_r(
const time_t *t,
struct tm *result)
808 result = gmtime_r(t, result);
810 struct tm *tmp = gmtime(t);
811 if (tmp) *result = *tmp;
813#if defined(HAVE_TIMEGM) && defined(LOCALTIME_OVERFLOW_PROBLEM)
814 if (result && *t != timegm(result)) {
820# define GMTIME(tm, result) rb_gmtime_r((tm), &(result))
823static const int16_t common_year_yday_offset[] = {
828 -1 + 31 + 28 + 31 + 30,
829 -1 + 31 + 28 + 31 + 30 + 31,
830 -1 + 31 + 28 + 31 + 30 + 31 + 30,
831 -1 + 31 + 28 + 31 + 30 + 31 + 30 + 31,
832 -1 + 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31,
833 -1 + 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30,
834 -1 + 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31,
835 -1 + 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30
838static const int16_t leap_year_yday_offset[] = {
843 -1 + 31 + 29 + 31 + 30,
844 -1 + 31 + 29 + 31 + 30 + 31,
845 -1 + 31 + 29 + 31 + 30 + 31 + 30,
846 -1 + 31 + 29 + 31 + 30 + 31 + 30 + 31,
847 -1 + 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31,
848 -1 + 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30,
849 -1 + 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31,
850 -1 + 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30
854static const int8_t common_year_days_in_month[] = {
855 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
857static const int8_t leap_year_days_in_month[] = {
858 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
861#define days_in_month_of(leap) ((leap) ? leap_year_days_in_month : common_year_days_in_month)
862#define days_in_month_in(y) days_in_month_of(leap_year_p(y))
863#define days_in_month_in_v(y) days_in_month_of(leap_year_v_p(y))
866 (m),(m),(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)
870 (m),(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)
874 (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)
878 (m),(m),(m),(m),(m),(m),(m),(m),(m),(m), \
879 (m),(m),(m),(m),(m),(m),(m),(m),(m),(m), \
880 (m),(m),(m),(m),(m),(m),(m),(m),(m),(m), (m)
882static const uint8_t common_year_mon_of_yday[] = {
883 M31(1), M28(2), M31(3), M30(4), M31(5), M30(6),
884 M31(7), M31(8), M30(9), M31(10), M30(11), M31(12)
886static const uint8_t leap_year_mon_of_yday[] = {
887 M31(1), M29(2), M31(3), M30(4), M31(5), M30(6),
888 M31(7), M31(8), M30(9), M31(10), M30(11), M31(12)
898 10,11,12,13,14,15,16,17,18,19, \
899 20,21,22,23,24,25,26,27,28
902 10,11,12,13,14,15,16,17,18,19, \
903 20,21,22,23,24,25,26,27,28,29
906 10,11,12,13,14,15,16,17,18,19, \
907 20,21,22,23,24,25,26,27,28,29,30
910 10,11,12,13,14,15,16,17,18,19, \
911 20,21,22,23,24,25,26,27,28,29,30,31
913static const uint8_t common_year_mday_of_yday[] = {
915 D31, D28, D31, D30, D31, D30, D31, D31, D30, D31, D30, D31
917static const uint8_t leap_year_mday_of_yday[] = {
918 D31, D29, D31, D30, D31, D30, D31, D31, D30, D31, D30, D31
927calc_tm_yday(
long tm_year,
int tm_mon,
int tm_mday)
929 int tm_year_mod400 = (int)MOD(tm_year, 400);
930 int tm_yday = tm_mday;
932 if (leap_year_p(tm_year_mod400 + 1900))
933 tm_yday += leap_year_yday_offset[tm_mon];
935 tm_yday += common_year_yday_offset[tm_mon];
941timegmw_noleapsecond(
struct vtm *
vtm)
953 divmodv(year1900,
INT2FIX(400), &q400, &r400);
956 yday = calc_tm_yday(year_mod400,
vtm->mon-1,
vtm->mday);
969 + DIV(year_mod400 - 69, 4)
970 - DIV(year_mod400 - 1, 100)
971 + (year_mod400 + 299) / 400;
973 vdays = addv(vdays, mulv(q400,
INT2FIX(97)));
974 vdays = addv(vdays, mulv(year1900,
INT2FIX(365)));
975 wret = wadd(rb_time_magnify(v2w(ret)), wmul(rb_time_magnify(v2w(vdays)), WINT2FIXWV(86400)));
976 wret = wadd(wret, v2w(
vtm->subsecx));
982zone_str(
const char *zone)
989 return rb_fstring_lit(
"(NO-TIMEZONE-ABBREVIATION)");
992 for (p = zone; *p; p++) {
1008 return rb_fstring(str);
1016gmtimew_noleapsecond(wideval_t timew,
struct vtm *
vtm)
1022 wideval_t timew2, w, w2;
1027 split_second(timew, &timew2, &subsecx);
1028 vtm->subsecx = subsecx;
1030 wdivmod(timew2, WINT2FIXWV(86400), &w2, &w);
1035 vtm->wday = (wday + 4) % 7;
1038 vtm->sec = n % 60; n = n / 60;
1039 vtm->min = n % 60; n = n / 60;
1043 divmodv(timev,
INT2FIX(400*365 + 97), &timev, &v);
1056 if (30*365+7+31+29-1 <= n) {
1070 x = n / (365*100 + 24);
1071 n = n % (365*100 + 24);
1073 if (30*365+7+31+29-1 <= n) {
1083 x = n / (365*4 + 1);
1084 n = n % (365*4 + 1);
1086 if (365*2+31+29-1 <= n) {
1087 if (n < 365*2+366) {
1104 if (leap_year_p(y)) {
1105 vtm->mon = leap_year_mon_of_yday[n];
1106 vtm->mday = leap_year_mday_of_yday[n];
1109 vtm->mon = common_year_mon_of_yday[n];
1110 vtm->mday = common_year_mday_of_yday[n];
1114 vtm->zone = str_utc;
1118gmtime_with_leapsecond(
const time_t *timep,
struct tm *result)
1120#if defined(HAVE_STRUCT_TM_TM_GMTOFF)
1124 int gmtoff_sec, gmtoff_min, gmtoff_hour, gmtoff_day;
1126 t = LOCALTIME(timep, *result);
1131 if (t->tm_gmtoff < 0) {
1133 gmtoff = -t->tm_gmtoff;
1137 gmtoff = t->tm_gmtoff;
1139 gmtoff_sec = (int)(gmtoff % 60);
1140 gmtoff = gmtoff / 60;
1141 gmtoff_min = (int)(gmtoff % 60);
1142 gmtoff = gmtoff / 60;
1143 gmtoff_hour = (int)gmtoff;
1147 gmtoff_hour *= sign;
1154 result->tm_sec += gmtoff_sec;
1155 if (result->tm_sec < 0) {
1156 result->tm_sec += 60;
1159 if (60 <= result->tm_sec) {
1160 result->tm_sec -= 60;
1165 result->tm_min += gmtoff_min;
1166 if (result->tm_min < 0) {
1167 result->tm_min += 60;
1170 if (60 <= result->tm_min) {
1171 result->tm_min -= 60;
1176 result->tm_hour += gmtoff_hour;
1177 if (result->tm_hour < 0) {
1178 result->tm_hour += 24;
1181 if (24 <= result->tm_hour) {
1182 result->tm_hour -= 24;
1188 if (gmtoff_day < 0) {
1189 if (result->tm_yday == 0) {
1190 result->tm_mday = 31;
1191 result->tm_mon = 11;
1193 result->tm_yday = leap_year_p(result->tm_year + 1900) ? 365 : 364;
1195 else if (result->tm_mday == 1) {
1196 const int8_t *days_in_month = days_in_month_in(result->tm_year + 1900);
1198 result->tm_mday = days_in_month[result->tm_mon];
1205 result->tm_wday = (result->tm_wday + 6) % 7;
1208 int leap = leap_year_p(result->tm_year + 1900);
1209 if (result->tm_yday == (leap ? 365 : 364)) {
1212 result->tm_mday = 1;
1213 result->tm_yday = 0;
1215 else if (result->tm_mday == days_in_month_of(leap)[result->tm_mon]) {
1217 result->tm_mday = 1;
1224 result->tm_wday = (result->tm_wday + 1) % 7;
1227 result->tm_isdst = 0;
1228 result->tm_gmtoff = 0;
1229#if defined(HAVE_TM_ZONE)
1230 result->tm_zone = (
char *)
"UTC";
1234 return GMTIME(timep, *result);
1238static long this_year = 0;
1239static time_t known_leap_seconds_limit;
1240static int number_of_leap_seconds_known;
1243init_leap_second_info(
void)
1250 if (this_year == 0) {
1252 struct tm *tm, result;
1257 gmtime_r(&now, &result);
1261 tm = gmtime_with_leapsecond(&now, &result);
1263 this_year = tm->tm_year;
1265 if (TIMET_MAX - now < (time_t)(366*86400))
1266 known_leap_seconds_limit = TIMET_MAX;
1268 known_leap_seconds_limit = now + (time_t)(366*86400);
1270 if (!gmtime_with_leapsecond(&known_leap_seconds_limit, &result))
1274 vtm.mon = result.tm_mon + 1;
1275 vtm.mday = result.tm_mday;
1276 vtm.hour = result.tm_hour;
1277 vtm.min = result.tm_min;
1278 vtm.sec = result.tm_sec;
1282 timew = timegmw_noleapsecond(&
vtm);
1284 number_of_leap_seconds_known =
NUM2INT(w2v(wsub(TIMET2WV(known_leap_seconds_limit), rb_time_unmagnify(timew))));
1290ruby_reset_leap_second_info(
void)
1306 return timegmw_noleapsecond(
vtm);
1308 init_leap_second_info();
1310 timew = timegmw_noleapsecond(
vtm);
1313 if (number_of_leap_seconds_known == 0) {
1319 else if (wlt(rb_time_magnify(TIMET2WV(known_leap_seconds_limit)), timew)) {
1320 return wadd(timew, rb_time_magnify(WINT2WV(number_of_leap_seconds_known)));
1324 tm.tm_mon =
vtm->mon - 1;
1325 tm.tm_mday =
vtm->mday;
1326 tm.tm_hour =
vtm->hour;
1327 tm.tm_min =
vtm->min;
1328 tm.tm_sec =
vtm->sec;
1331 errmsg = find_time_t(&tm, 1, &t);
1333 rb_raise(rb_eArgError,
"%s", errmsg);
1334 return wadd(rb_time_magnify(TIMET2WV(t)), v2w(
vtm->subsecx));
1338gmtimew(wideval_t timew,
struct vtm *result)
1345 if (wlt(timew, WINT2FIXWV(0))) {
1346 gmtimew_noleapsecond(timew, result);
1350 init_leap_second_info();
1352 if (number_of_leap_seconds_known == 0) {
1356 gmtimew_noleapsecond(timew, result);
1359 else if (wlt(rb_time_magnify(TIMET2WV(known_leap_seconds_limit)), timew)) {
1360 timew = wsub(timew, rb_time_magnify(WINT2WV(number_of_leap_seconds_known)));
1361 gmtimew_noleapsecond(timew, result);
1365 split_second(timew, &timew2, &subsecx);
1367 t = WV2TIMET(timew2);
1368 if (!gmtime_with_leapsecond(&t, &tm))
1371 result->year =
LONG2NUM((
long)tm.tm_year + 1900);
1372 result->mon = tm.tm_mon + 1;
1373 result->mday = tm.tm_mday;
1374 result->hour = tm.tm_hour;
1375 result->min = tm.tm_min;
1376 result->sec = tm.tm_sec;
1377 result->subsecx = subsecx;
1378 result->utc_offset =
INT2FIX(0);
1379 result->wday = tm.tm_wday;
1380 result->yday = tm.tm_yday+1;
1381 result->isdst = tm.tm_isdst;
1386#define GMTIMEW(w, v) \
1387 (gmtimew(w, v) ? (void)0 : rb_raise(rb_eArgError, "gmtime error"))
1389static struct tm *localtime_with_gmtoff_zone(
const time_t *t,
struct tm *result,
long *gmtoff,
VALUE *zone);
1424static const int compat_common_month_table[12][7] = {
1426 { 2034, 2035, 2036, 2031, 2032, 2027, 2033 },
1427 { 2026, 2027, 2033, 2034, 2035, 2030, 2031 },
1428 { 2026, 2032, 2033, 2034, 2035, 2030, 2036 },
1429 { 2035, 2030, 2036, 2026, 2032, 2033, 2034 },
1430 { 2033, 2034, 2035, 2030, 2036, 2026, 2032 },
1431 { 2036, 2026, 2032, 2033, 2034, 2035, 2030 },
1432 { 2035, 2030, 2036, 2026, 2032, 2033, 2034 },
1433 { 2032, 2033, 2034, 2035, 2030, 2036, 2026 },
1434 { 2030, 2036, 2026, 2032, 2033, 2034, 2035 },
1435 { 2034, 2035, 2030, 2036, 2026, 2032, 2033 },
1436 { 2026, 2032, 2033, 2034, 2035, 2030, 2036 },
1437 { 2030, 2036, 2026, 2032, 2033, 2034, 2035 },
1465static const int compat_leap_month_table[7] = {
1467 2032, 2016, 2028, 2012, 2024, 2036, 2020,
1471calc_wday(
int year_mod400,
int month,
int day)
1476 a = (14 - month) / 12;
1477 y = year_mod400 + 4800 - a;
1478 m = month + 12 * a - 3;
1479 wday = day + (153*m+2)/5 + 365*y + y/4 - y/100 + y/400 + 2;
1485guess_local_offset(
struct vtm *vtm_utc,
int *isdst_ret,
VALUE *zone_ret)
1493 int year_mod400, wday;
1497 if (lt(vtm_utc->year,
INT2FIX(1916))) {
1502# if defined(NEGATIVE_TIME_T)
1503# if SIZEOF_TIME_T <= 4
1505# define THE_TIME_OLD_ENOUGH ((time_t)0x80000000)
1509# define THE_TIME_OLD_ENOUGH ((time_t)(1600-1970)*366*24*60*60)
1511 if (localtime_with_gmtoff_zone((t = THE_TIME_OLD_ENOUGH, &t), &tm, &gmtoff, &zone)) {
1513 isdst = tm.tm_isdst;
1518 if (localtime_with_gmtoff_zone((t = 0, &t), &tm, &gmtoff, &zone)) {
1520 isdst = tm.tm_isdst;
1536 wday = calc_wday(year_mod400, vtm_utc->mon, 1);
1537 if (vtm_utc->mon == 2 && leap_year_p(year_mod400))
1538 vtm2.year =
INT2FIX(compat_leap_month_table[wday]);
1540 vtm2.year =
INT2FIX(compat_common_month_table[vtm_utc->mon-1][wday]);
1542 timev = w2v(rb_time_unmagnify(timegmw(&vtm2)));
1543 t = NUM2TIMET(timev);
1545 if (localtime_with_gmtoff_zone(&t, &tm, &gmtoff, &zone)) {
1547 *isdst_ret = tm.tm_isdst;
1555 static time_t now = 0;
1556 static long now_gmtoff = 0;
1557 static int now_isdst = 0;
1558 static VALUE now_zone;
1562 localtime_with_gmtoff_zone(&now, &tm, &now_gmtoff, &zone);
1563 now_isdst = tm.tm_isdst;
1564 zone = rb_fstring(zone);
1565 rb_vm_register_global_object(zone);
1569 *isdst_ret = now_isdst;
1571 *zone_ret = now_zone;
1577small_vtm_sub(
struct vtm *vtm1,
struct vtm *vtm2)
1581 off = vtm1->sec - vtm2->sec;
1582 off += (vtm1->min - vtm2->min) * 60;
1583 off += (vtm1->hour - vtm2->hour) * 3600;
1584 if (ne(vtm1->year, vtm2->year))
1585 off += lt(vtm1->year, vtm2->year) ? -24*3600 : 24*3600;
1586 else if (vtm1->mon != vtm2->mon)
1587 off += vtm1->mon < vtm2->mon ? -24*3600 : 24*3600;
1588 else if (vtm1->mday != vtm2->mday)
1589 off += vtm1->mday < vtm2->mday ? -24*3600 : 24*3600;
1595timelocalw(
struct vtm *
vtm)
1600 wideval_t timew1, timew2;
1601 struct vtm vtm1, vtm2;
1606 if (l < INT_MIN || INT_MAX < l)
1608 tm.tm_year = (int)l;
1617 tm.tm_mon =
vtm->mon-1;
1618 tm.tm_mday =
vtm->mday;
1619 tm.tm_hour =
vtm->hour;
1620 tm.tm_min =
vtm->min;
1621 tm.tm_sec =
vtm->sec;
1622 tm.tm_isdst =
vtm->isdst == VTM_ISDST_INITVAL ? -1 :
vtm->isdst;
1624 if (find_time_t(&tm, 0, &t))
1626 return wadd(rb_time_magnify(TIMET2WV(t)), v2w(
vtm->subsecx));
1629 timew1 = timegmw(
vtm);
1631 if (!localtimew(timew1, &vtm1))
1632 rb_raise(rb_eArgError,
"localtimew error");
1634 n = vtmcmp(
vtm, &vtm1);
1636 timew1 = wsub(timew1, rb_time_magnify(WINT2FIXWV(12*3600)));
1637 if (!localtimew(timew1, &vtm1))
1638 rb_raise(rb_eArgError,
"localtimew error");
1645 timew1 = wsub(timew1, rb_time_magnify(WINT2FIXWV(24*3600)));
1646 if (!localtimew(timew1, &vtm1))
1647 rb_raise(rb_eArgError,
"localtimew error");
1650 timew2 = wadd(timew1, rb_time_magnify(WINT2FIXWV(24*3600)));
1651 if (!localtimew(timew2, &vtm2))
1652 rb_raise(rb_eArgError,
"localtimew error");
1654 timew1 = wadd(timew1, rb_time_magnify(v2w(small_vtm_sub(
vtm, &vtm1))));
1655 timew2 = wadd(timew2, rb_time_magnify(v2w(small_vtm_sub(
vtm, &vtm2))));
1657 if (weq(timew1, timew2))
1660 if (!localtimew(timew1, &vtm1))
1661 rb_raise(rb_eArgError,
"localtimew error");
1662 if (
vtm->hour != vtm1.hour ||
vtm->min != vtm1.min ||
vtm->sec != vtm1.sec)
1665 if (!localtimew(timew2, &vtm2))
1666 rb_raise(rb_eArgError,
"localtimew error");
1667 if (
vtm->hour != vtm2.hour ||
vtm->min != vtm2.min ||
vtm->sec != vtm2.sec)
1671 return lt(vtm1.utc_offset, vtm2.utc_offset) ? timew2 : timew1;
1673 return lt(vtm1.utc_offset, vtm2.utc_offset) ? timew1 : timew2;
1677localtime_with_gmtoff_zone(
const time_t *t,
struct tm *result,
long *gmtoff,
VALUE *zone)
1681 if (LOCALTIME(t, tm)) {
1682#if defined(HAVE_STRUCT_TM_TM_GMTOFF)
1683 *gmtoff = tm.tm_gmtoff;
1689 u = GMTIME(t, tmbuf);
1692 if (l->tm_year != u->tm_year)
1693 off = l->tm_year < u->tm_year ? -1 : 1;
1694 else if (l->tm_mon != u->tm_mon)
1695 off = l->tm_mon < u->tm_mon ? -1 : 1;
1696 else if (l->tm_mday != u->tm_mday)
1697 off = l->tm_mday < u->tm_mday ? -1 : 1;
1700 off =
off * 24 + l->tm_hour - u->tm_hour;
1701 off =
off * 60 + l->tm_min - u->tm_min;
1702 off =
off * 60 + l->tm_sec - u->tm_sec;
1707#if defined(HAVE_TM_ZONE)
1708 *zone = zone_str(tm.tm_zone);
1709#elif defined(_WIN32)
1710 *zone = zone_str(get_tzname(tm.tm_isdst));
1711#elif defined(HAVE_TZNAME) && defined(HAVE_DAYLIGHT)
1713 *zone = zone_str(tzname[daylight && tm.tm_isdst]);
1717 strftime(buf,
sizeof(buf),
"%Z", &tm);
1718 *zone = zone_str(buf);
1730timew_out_of_timet_range(wideval_t timew)
1733#if WIDEVALUE_IS_WIDER && SIZEOF_TIME_T < SIZEOF_INT64_T
1734 if (FIXWV_P(timew)) {
1735 wideint_t t = FIXWV2WINT(timew);
1736 if (t < TIME_SCALE * (wideint_t)TIMET_MIN ||
1737 TIME_SCALE * (1 + (wideint_t)TIMET_MAX) <= t)
1742#if SIZEOF_TIME_T == SIZEOF_INT64_T
1743 if (FIXWV_P(timew)) {
1744 wideint_t t = FIXWV2WINT(timew);
1745 if (~(time_t)0 <= 0) {
1755 timexv = w2v(timew);
1756 if (lt(timexv, mulv(
INT2FIX(TIME_SCALE), TIMET2NUM(TIMET_MIN))) ||
1757 le(mulv(
INT2FIX(TIME_SCALE), addv(TIMET2NUM(TIMET_MAX),
INT2FIX(1))), timexv))
1763localtimew(wideval_t timew,
struct vtm *result)
1765 VALUE subsecx, offset;
1769 if (!timew_out_of_timet_range(timew)) {
1775 split_second(timew, &timew2, &subsecx);
1777 t = WV2TIMET(timew2);
1779 if (localtime_with_gmtoff_zone(&t, &tm, &gmtoff, &zone)) {
1780 result->year =
LONG2NUM((
long)tm.tm_year + 1900);
1781 result->mon = tm.tm_mon + 1;
1782 result->mday = tm.tm_mday;
1783 result->hour = tm.tm_hour;
1784 result->min = tm.tm_min;
1785 result->sec = tm.tm_sec;
1786 result->subsecx = subsecx;
1787 result->wday = tm.tm_wday;
1788 result->yday = tm.tm_yday+1;
1789 result->isdst = tm.tm_isdst;
1790 result->utc_offset =
LONG2NUM(gmtoff);
1791 result->zone = zone;
1796 if (!gmtimew(timew, result))
1799 offset = guess_local_offset(result, &isdst, &zone);
1801 if (!gmtimew(wadd(timew, rb_time_magnify(v2w(offset))), result))
1804 result->utc_offset = offset;
1805 result->isdst = isdst;
1806 result->zone = zone;
1811#define TIME_TZMODE_LOCALTIME 0
1812#define TIME_TZMODE_UTC 1
1813#define TIME_TZMODE_FIXOFF 2
1814#define TIME_TZMODE_UNINITIALIZED 3
1821#define GetTimeval(obj, tobj) ((tobj) = get_timeval(obj))
1822#define GetNewTimeval(obj, tobj) ((tobj) = get_new_timeval(obj))
1824#define IsTimeval(obj) rb_typeddata_is_kind_of((obj), &time_data_type)
1825#define TIME_INIT_P(tobj) ((tobj)->vtm.tzmode != TIME_TZMODE_UNINITIALIZED)
1827#define TZMODE_UTC_P(tobj) ((tobj)->vtm.tzmode == TIME_TZMODE_UTC)
1828#define TZMODE_SET_UTC(tobj) ((tobj)->vtm.tzmode = TIME_TZMODE_UTC)
1830#define TZMODE_LOCALTIME_P(tobj) ((tobj)->vtm.tzmode == TIME_TZMODE_LOCALTIME)
1831#define TZMODE_SET_LOCALTIME(tobj) ((tobj)->vtm.tzmode = TIME_TZMODE_LOCALTIME)
1833#define TZMODE_FIXOFF_P(tobj) ((tobj)->vtm.tzmode == TIME_TZMODE_FIXOFF)
1834#define TZMODE_SET_FIXOFF(time, tobj, off) do { \
1835 (tobj)->vtm.tzmode = TIME_TZMODE_FIXOFF; \
1836 RB_OBJ_WRITE_UNALIGNED(time, &(tobj)->vtm.utc_offset, off); \
1839#define TZMODE_COPY(tobj1, tobj2) \
1840 ((tobj1)->vtm.tzmode = (tobj2)->vtm.tzmode, \
1841 (tobj1)->vtm.utc_offset = (tobj2)->vtm.utc_offset, \
1842 (tobj1)->vtm.zone = (tobj2)->vtm.zone)
1844static int zone_localtime(
VALUE zone,
VALUE time);
1846#define MAKE_TM(time, tobj) \
1848 if ((tobj)->vtm.tm_got == 0) { \
1849 time_get_tm((time), (tobj)); \
1852#define MAKE_TM_ENSURE(time, tobj, cond) \
1854 MAKE_TM(time, tobj); \
1856 force_make_tm(time, tobj); \
1863 tobj->timew = timew;
1864 if (!FIXWV_P(timew)) {
1883 VALUE zone = tobj->vtm.zone;
1884 if (!
NIL_P(zone) && zone != str_empty && zone != str_utc) {
1885 if (zone_localtime(zone, time))
return;
1887 tobj->vtm.tm_got = 0;
1888 time_get_tm(time, tobj);
1892time_mark_and_move(
void *ptr)
1895 if (!WIDEVALUE_IS_WIDER || !FIXWV_P(tobj->timew)) {
1896 rb_gc_mark_and_move((
VALUE *)&WIDEVAL_GET(tobj->timew));
1898 rb_gc_mark_and_move(&tobj->vtm.year);
1899 rb_gc_mark_and_move(&tobj->vtm.subsecx);
1900 rb_gc_mark_and_move(&tobj->vtm.utc_offset);
1901 rb_gc_mark_and_move(&tobj->vtm.zone);
1907 .dmark = time_mark_and_move,
1910 .dcompact = time_mark_and_move,
1912 .flags = RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_FROZEN_SHAREABLE | RUBY_TYPED_WB_PROTECTED | RUBY_TYPED_EMBEDDABLE,
1916time_s_alloc(
VALUE klass)
1922 tobj->vtm.tzmode = TIME_TZMODE_UNINITIALIZED;
1923 tobj->vtm.tm_got = 0;
1924 time_set_timew(obj, tobj, WINT2FIXWV(0));
1925 tobj->vtm.zone =
Qnil;
1931get_timeval(
VALUE obj)
1935 if (!TIME_INIT_P(tobj)) {
1942get_new_timeval(
VALUE obj)
1946 if (TIME_INIT_P(tobj)) {
1953time_modify(
VALUE time)
1955 rb_check_frozen(time);
1959timenano2timew(wideint_t sec,
long nsec)
1963 timew = rb_time_magnify(WINT2WV(sec));
1965 timew = wadd(timew, wmulquoll(WINT2WV(nsec), TIME_SCALE, 1000000000));
1970timew2timespec(wideval_t timew)
1976 if (timew_out_of_timet_range(timew))
1977 rb_raise(rb_eArgError,
"time out of system range");
1978 split_second(timew, &timew2, &subsecx);
1979 ts.tv_sec = WV2TIMET(timew2);
1985timew2timespec_exact(wideval_t timew,
struct timespec *ts)
1991 if (timew_out_of_timet_range(timew))
1993 split_second(timew, &timew2, &subsecx);
1994 ts->tv_sec = WV2TIMET(timew2);
1995 nsecv = mulquov(subsecx,
INT2FIX(1000000000),
INT2FIX(TIME_SCALE));
2005#ifdef HAVE_CLOCK_GETTIME
2006 if (clock_gettime(CLOCK_REALTIME, ts) == -1) {
2007 rb_sys_fail(
"clock_gettime");
2012 if (gettimeofday(&tv, 0) < 0) {
2013 rb_sys_fail(
"gettimeofday");
2015 ts->tv_sec = tv.tv_sec;
2016 ts->tv_nsec = tv.tv_usec * 1000;
2032 GetNewTimeval(time, tobj);
2033 TZMODE_SET_LOCALTIME(tobj);
2036 time_set_timew(time, tobj, timenano2timew(ts.tv_sec, ts.tv_nsec));
2039 time_zonelocal(time, zone);
2047 VALUE t = time_s_alloc(klass);
2048 return time_init_now(ec, t, zone);
2058 GetTimeval(time, tobj);
2060 tobj->vtm.tm_got = 0;
2061 tobj->vtm.zone =
Qnil;
2062 TZMODE_SET_FIXOFF(time, tobj,
off);
2087 subsec = neg(subsec);
2096 vtm->subsecx = addv(
vtm->subsecx, w2v(rb_time_magnify(v2w(subsec))));
2145 vtm_add_day(
vtm, day);
2149vtm_add_day(
struct vtm *
vtm,
int day)
2153 if (
vtm->mon == 1 &&
vtm->mday == 1) {
2158 vtm->yday = leap_year_v_p(
vtm->year) ? 366 : 365;
2160 else if (
vtm->mday == 1) {
2161 const int8_t *days_in_month = days_in_month_in_v(
vtm->year);
2163 vtm->mday = days_in_month[
vtm->mon-1];
2164 if (
vtm->yday != 0)
vtm->yday--;
2168 if (
vtm->yday != 0)
vtm->yday--;
2170 if (
vtm->wday != VTM_WDAY_INITVAL)
vtm->wday = (
vtm->wday + 6) % 7;
2173 int leap = leap_year_v_p(
vtm->year);
2174 if (
vtm->mon == 12 &&
vtm->mday == 31) {
2180 else if (
vtm->mday == days_in_month_of(leap)[
vtm->mon-1]) {
2183 if (
vtm->yday != 0)
vtm->yday++;
2187 if (
vtm->yday != 0)
vtm->yday++;
2189 if (
vtm->wday != VTM_WDAY_INITVAL)
vtm->wday = (
vtm->wday + 1) % 7;
2195maybe_tzobj_p(
VALUE obj)
2197 if (
NIL_P(obj))
return FALSE;
2203NORETURN(
static void invalid_utc_offset(
VALUE));
2205invalid_utc_offset(
VALUE zone)
2207 rb_raise(rb_eArgError,
"\"+HH:MM\", \"-HH:MM\", \"UTC\" or "
2208 "\"A\"..\"I\",\"K\"..\"Z\" expected for utc_offset: %"PRIsVALUE,
2212#define have_2digits(ptr) (ISDIGIT((ptr)[0]) && ISDIGIT((ptr)[1]))
2213#define num_from_2digits(ptr) ((ptr)[0] * 10 + (ptr)[1] - '0' * 11)
2216utc_offset_arg(
VALUE arg)
2221 const char *s = RSTRING_PTR(tmp), *min = NULL, *sec = NULL;
2223 goto invalid_utc_offset;
2225 switch (RSTRING_LEN(tmp)) {
2231 if (s[0] >=
'A' && s[0] <=
'I') {
2232 n = (int)s[0] -
'A' + 1;
2235 else if (s[0] >=
'K' && s[0] <=
'M') {
2236 n = (int)s[0] -
'A';
2238 else if (s[0] >=
'N' && s[0] <=
'Y') {
2239 n =
'M' - (int)s[0];
2242 goto invalid_utc_offset;
2258 if (s[6] !=
':')
goto invalid_utc_offset;
2262 if (s[3] !=
':')
goto invalid_utc_offset;
2266 goto invalid_utc_offset;
2269 if (!have_2digits(sec))
goto invalid_utc_offset;
2270 if (sec[0] >
'5')
goto invalid_utc_offset;
2271 n += num_from_2digits(sec);
2275 if (!have_2digits(min))
goto invalid_utc_offset;
2276 if (min[0] >
'5')
goto invalid_utc_offset;
2277 n += num_from_2digits(min) * 60;
2279 if (s[0] !=
'+' && s[0] !=
'-')
goto invalid_utc_offset;
2280 if (!have_2digits(s+1))
goto invalid_utc_offset;
2281 n += num_from_2digits(s+1) * 3600;
2283 if (n == 0)
return UTC_ZONE;
2289 return num_exact(arg);
2297 wideval_t tlocal, wideval_t tutc,
VALUE time)
2300 wideval_t w = wsub(tlocal, tutc);
2302 validate_utc_offset(
off);
2305 TZMODE_SET_LOCALTIME(tobj);
2309extract_time(
VALUE time)
2312 const ID id_to_i = idTo_i;
2314#define EXTRACT_TIME() do { \
2315 t = NUM2WV(AREF(to_i)); \
2319 struct time_object *tobj = RTYPEDDATA_GET_DATA(time);
2322 t = rb_time_unmagnify(tobj->timew);
2327#define AREF(x) rb_struct_aref(time, ID2SYM(id_##x))
2332#define AREF(x) rb_funcallv(time, id_##x, 0, 0)
2345 const ID id_to_i = idTo_i;
2346 struct vtm *
vtm = &orig_tobj->vtm;
2348#define EXTRACT_VTM() do { \
2350 vtm->year = obj2vint(AREF(year)); \
2351 vtm->mon = month_arg(AREF(mon)); \
2352 vtm->mday = obj2ubits(AREF(mday), 5); \
2353 vtm->hour = obj2ubits(AREF(hour), 5); \
2354 vtm->min = obj2ubits(AREF(min), 6); \
2355 vtm->sec = obj2subsecx(AREF(sec), &subsecx); \
2356 vtm->isdst = RTEST(AREF(isdst)); \
2357 vtm->utc_offset = Qnil; \
2358 t = NUM2WV(AREF(to_i)); \
2362 struct time_object *tobj = RTYPEDDATA_GET_DATA(time);
2364 time_get_tm(time, tobj);
2365 time_set_vtm(orig_time, orig_tobj, tobj->vtm);
2366 t = rb_time_unmagnify(tobj->timew);
2367 if (TZMODE_FIXOFF_P(tobj) &&
vtm->utc_offset !=
INT2FIX(0))
2368 t = wadd(t, v2w(
vtm->utc_offset));
2373#define AREF(x) rb_struct_aref(time, ID2SYM(id_##x))
2379 struct vtm temp_vtm = *
vtm;
2380 GMTIMEW(rb_time_magnify(t), &temp_vtm);
2381 time_set_vtm(orig_time, orig_tobj, temp_vtm);
2384#define AREF(x) rb_funcallv(time, id_##x, 0, 0)
2390 RB_OBJ_WRITE_UNALIGNED(orig_time, &
vtm->subsecx, subsecx);
2403 tobj->vtm.isdst = (!UNDEF_P(dst) &&
RTEST(dst));
2410 struct time_object *tobj = RTYPEDDATA_GET_DATA(time);
2413 wdivmod(tobj->timew, WINT2FIXWV(TIME_SCALE), &t, &s);
2414 tm = tm_from_time(rb_cTimeTM, time);
2416 if (UNDEF_P(utc))
return 0;
2418 s = extract_time(utc);
2419 zone_set_offset(zone, tobj, t, s, time);
2420 s = rb_time_magnify(s);
2421 if (tobj->vtm.subsecx !=
INT2FIX(0)) {
2422 s = wadd(s, v2w(tobj->vtm.subsecx));
2424 time_set_timew(time, tobj, s);
2426 zone_set_dst(zone, tobj, tm);
2436 VALUE local, tm, subsecx;
2437 struct time_object *tobj = RTYPEDDATA_GET_DATA(time);
2440 split_second(tobj->timew, &t, &subsecx);
2441 tm = tm_from_time(rb_cTimeTM, time);
2444 if (UNDEF_P(local))
return 0;
2446 s = extract_vtm(local, time, tobj, subsecx);
2447 tobj->vtm.tm_got = 1;
2448 zone_set_offset(zone, tobj, s, t, time);
2449 zone_set_dst(zone, tobj, tm);
2461 return rb_check_funcall_default(klass, id_find_timezone, 1, &zone,
Qnil);
2467vtm_day_wraparound(
struct vtm *
vtm)
2469 if (
vtm->hour < 24)
return;
2474 vtm_add_day(
vtm, 1);
2489 vtm.wday = VTM_WDAY_INITVAL;
2491 vtm.zone = str_empty;
2493 vtm.year = obj2vint(year);
2495 vtm.mon =
NIL_P(mon) ? 1 : month_arg(mon);
2497 vtm.mday =
NIL_P(mday) ? 1 : obj2ubits(mday, 5);
2499 vtm.hour =
NIL_P(hour) ? 0 : obj2ubits(hour, 5);
2501 vtm.min =
NIL_P(min) ? 0 : obj2ubits(min, 6);
2509 vtm.sec = obj2subsecx(sec, &subsecx);
2510 vtm.subsecx = subsecx;
2513 return time_init_vtm(time,
vtm, zone);
2522 vtm.isdst = VTM_ISDST_INITVAL;
2524 const VALUE arg = zone;
2527 if (arg ==
ID2SYM(rb_intern(
"dst")))
2529 else if (arg ==
ID2SYM(rb_intern(
"std")))
2531 else if (maybe_tzobj_p(arg))
2533 else if (!
NIL_P(utc = utc_offset_arg(arg)))
2534 vtm.utc_offset = utc == UTC_ZONE ?
INT2FIX(0) : utc;
2535 else if (
NIL_P(zone = find_timezone(time, arg)))
2536 invalid_utc_offset(arg);
2542 GetNewTimeval(time, tobj);
2545 time_set_timew(time, tobj, timegmw(&
vtm));
2546 vtm_day_wraparound(&
vtm);
2547 time_set_vtm(time, tobj,
vtm);
2548 tobj->vtm.tm_got = 1;
2549 TZMODE_SET_LOCALTIME(tobj);
2550 if (zone_timelocal(zone, time)) {
2553 else if (
NIL_P(
vtm.utc_offset = utc_offset_arg(zone))) {
2554 if (
NIL_P(zone = find_timezone(time, zone)) || !zone_timelocal(zone, time))
2555 invalid_utc_offset(arg);
2559 if (utc == UTC_ZONE) {
2560 time_set_timew(time, tobj, timegmw(&
vtm));
2562 vtm_day_wraparound(&
vtm);
2563 time_set_vtm(time, tobj,
vtm);
2564 tobj->vtm.tm_got = 1;
2565 TZMODE_SET_UTC(tobj);
2569 TZMODE_SET_LOCALTIME(tobj);
2574 vtm_add_offset(&
vtm,
off, -1);
2576 time_set_timew(time, tobj, timegmw(&
vtm));
2578 return time_set_utc_offset(time,
off);
2581 time_set_timew(time, tobj, timelocalw(&
vtm));
2583 return time_localtime(time);
2588two_digits(
const char *ptr,
const char *end,
const char **endp,
const char *name)
2590 ssize_t
len = end - ptr;
2591 if (
len < 2 || !have_2digits(ptr) || ((
len > 2) &&
ISDIGIT(ptr[2]))) {
2592 VALUE mesg = rb_sprintf(
"two digits %s is expected", name);
2593 if (ptr[-1] ==
'-' || ptr[-1] ==
':') {
2594 rb_str_catf(mesg,
" after '%c'", ptr[-1]);
2596 rb_str_catf(mesg,
": %.*s", ((
len > 10) ? 10 : (int)(end - ptr)) + 1, ptr - 1);
2600 return num_from_2digits(ptr);
2604parse_int(
const char *ptr,
const char *end,
const char **endp,
size_t *ndigits,
bool sign)
2606 ssize_t
len = (end - ptr);
2607 int flags = sign ? RB_INT_PARSE_SIGN : 0;
2608 return rb_int_parse_cstr(ptr,
len, (
char **)endp, ndigits, 10, flags);
2620 rb_raise(rb_eArgError,
"time string should have ASCII compatible encoding");
2623 const char *
const begin = RSTRING_PTR(str);
2625 const char *ptr = begin;
2627 int mon = -1, mday = -1, hour = -1, min = -1, sec = -1;
2629 size_t prec =
NIL_P(precision) ? SIZE_MAX :
NUM2SIZET(precision);
2632 rb_raise(rb_eArgError,
"can't parse: %+"PRIsVALUE, str);
2634 year = parse_int(ptr, end, &ptr, &ndigits,
true);
2636 rb_raise(rb_eArgError,
"can't parse: %+"PRIsVALUE, str);
2638 else if (ndigits < 4) {
2639 rb_raise(rb_eArgError,
"year must be 4 or more digits: %.*s", (
int)ndigits, ptr - ndigits);
2641 else if (ptr == end) {
2645#define peekable_p(n) ((ptrdiff_t)(n) < (end - ptr))
2646#define peek_n(c, n) (peekable_p(n) && ((unsigned char)ptr[n] == (c)))
2647#define peek(c) peek_n(c, 0)
2648#define peekc_n(n) (peekable_p(n) ? (int)(unsigned char)ptr[n] : -1)
2649#define peekc() peekc_n(0)
2650#define expect_two_digits(x, bits) \
2651 (((unsigned int)(x = two_digits(ptr + 1, end, &ptr, #x)) > (1U << bits) - 1) ? \
2652 rb_raise(rb_eArgError, #x" out of range") : (void)0)
2653 if (!peek(
'-'))
break;
2654 expect_two_digits(mon, 4);
2655 if (!peek(
'-'))
break;
2656 expect_two_digits(mday, 5);
2657 if (!peek(
' ') && !peek(
'T'))
break;
2658 const char *
const time_part = ptr + 1;
2659 if (!
ISDIGIT(peekc_n(1)))
break;
2660#define nofraction(x) \
2662 rb_raise(rb_eArgError, "fraction " #x " is not supported: %.*s", \
2663 (int)(ptr + 1 - time_part), time_part); \
2665#define need_colon(x) \
2667 rb_raise(rb_eArgError, "missing " #x " part: %.*s", \
2668 (int)(ptr + 1 - time_part), time_part); \
2670 expect_two_digits(hour, 5);
2673 expect_two_digits(min, 6);
2676 expect_two_digits(sec, 6);
2679 for (ndigits = 0; ndigits < prec &&
ISDIGIT(peekc_n(ndigits)); ++ndigits);
2681 int clen = rb_enc_precise_mbclen(ptr, end, rb_enc_get(str));
2682 if (clen < 0) clen = 0;
2683 rb_raise(rb_eArgError,
"subsecond expected after dot: %.*s",
2684 (
int)(ptr - time_part) + clen, time_part);
2686 subsec = parse_int(ptr, ptr + ndigits, &ptr, &ndigits,
false);
2687 if (
NIL_P(subsec))
break;
2688 while (ptr < end &&
ISDIGIT(*ptr)) ptr++;
2691 while (ptr < end &&
ISSPACE(*ptr)) ptr++;
2692 const char *
const zstr = ptr;
2693 while (ptr < end && !
ISSPACE(*ptr)) ptr++;
2694 const char *
const zend = ptr;
2695 while (ptr < end &&
ISSPACE(*ptr)) ptr++;
2704 else if (hour == -1) {
2705 rb_raise(rb_eArgError,
"no time information");
2707 if (!
NIL_P(subsec)) {
2709 if (ndigits < (
size_t)TIME_SCALE_NUMDIGITS) {
2711 subsec = rb_int_mul(subsec, mul);
2713 else if (ndigits > (
size_t)TIME_SCALE_NUMDIGITS) {
2723 .wday = VTM_WDAY_INITVAL,
2727 .mon = (mon < 0) ? 1 : mon,
2728 .mday = (mday < 0) ? 1 : mday,
2729 .hour = (hour < 0) ? 0 : hour,
2730 .min = (min < 0) ? 0 : min,
2731 .sec = (sec < 0) ? 0 : sec,
2734 return time_init_vtm(time,
vtm, zone);
2738subsec_normalize(wideint_t *secp,
long *subsecp,
const long maxsubsec)
2740 wideint_t sec = *secp;
2741 long subsec = *subsecp;
2744 if (UNLIKELY(subsec >= maxsubsec)) {
2745 sec2 = subsec / maxsubsec;
2746 if (WIDEINT_MAX - sec2 < sec) {
2749 subsec -= sec2 * maxsubsec;
2752 else if (UNLIKELY(subsec < 0)) {
2753 sec2 = NDIV(subsec, maxsubsec);
2754 if (sec < WIDEINT_MIN - sec2) {
2757 subsec -= sec2 * maxsubsec;
2764#define time_usec_normalize(secp, usecp) subsec_normalize(secp, usecp, 1000000)
2765#define time_nsec_normalize(secp, nsecp) subsec_normalize(secp, nsecp, 1000000000)
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);
2781TIMETtoWIDEINT(time_t t)
2783#if SIZEOF_TIME_T * CHAR_BIT - (SIGNEDNESS_OF_TIME_T < 0) > \
2784 SIZEOF_WIDEINT * CHAR_BIT - 1
2786 if (t > WIDEINT_MAX) rb_raise(rb_eArgError,
"out of Time range");
2788 return (wideint_t)t;
2794 wideint_t isec = TIMETtoWIDEINT(sec);
2795 time_usec_normalize(&isec, &usec);
2796 return time_new_timew(
rb_cTime, timenano2timew(isec, usec * 1000));
2803 wideint_t isec = TIMETtoWIDEINT(sec);
2804 time_nsec_normalize(&isec, &nsec);
2805 return time_new_timew(
rb_cTime, timenano2timew(isec, nsec));
2814 if (-86400 < offset && offset < 86400) {
2815 GetTimeval(time, tobj);
2816 TZMODE_SET_FIXOFF(time, tobj,
INT2FIX(offset));
2818 else if (offset == INT_MAX) {
2820 else if (offset == INT_MAX-1) {
2821 GetTimeval(time, tobj);
2822 TZMODE_SET_UTC(tobj);
2825 rb_raise(rb_eArgError,
"utc_offset out of range");
2834 VALUE time = time_new_timew(
rb_cTime, rb_time_magnify(v2w(timev)));
2839 if (maybe_tzobj_p(zone)) {
2841 if (zone_timelocal(zone, time))
return time;
2845 if (
NIL_P(zone = find_timezone(time,
off))) invalid_utc_offset(
off);
2847 if (!zone_timelocal(zone, time)) invalid_utc_offset(
off);
2850 else if (
off == UTC_ZONE) {
2851 return time_gmtime(time);
2854 validate_utc_offset(
off);
2855 time_set_utc_offset(time,
off);
2863time_timespec(
VALUE num, int interval)
2866 const char *
const tstr = interval ?
"time interval" :
"time";
2869#ifndef NEGATIVE_TIME_T
2870# define arg_range_check(v) \
2872 rb_raise(rb_eArgError, "%s must not be negative", tstr) : \
2875# define arg_range_check(v) \
2876 ((interval && (v) < 0) ? \
2877 rb_raise(rb_eArgError, "time interval must not be negative") : \
2882 t.tv_sec = NUM2TIMET(num);
2883 arg_range_check(t.tv_sec);
2894 t.tv_nsec = (int)(d*1e9+0.5);
2895 if (t.tv_nsec >= 1000000000) {
2896 t.tv_nsec -= 1000000000;
2900 else if ((t.tv_nsec = (
int)(-d*1e9+0.5)) > 0) {
2901 t.tv_nsec = 1000000000 - t.tv_nsec;
2904 t.tv_sec = (time_t)f;
2905 if (f != t.tv_sec) {
2910 else if (RB_BIGNUM_TYPE_P(num)) {
2911 t.tv_sec = NUM2TIMET(num);
2912 arg_range_check(t.tv_sec);
2921 t.tv_sec = NUM2TIMET(i);
2922 arg_range_check(t.tv_sec);
2927 rb_raise(
rb_eTypeError,
"can't convert %"PRIsVALUE
" into %s",
2932#undef arg_range_check
2936time_timeval(
VALUE num, int interval)
2941 ts = time_timespec(num, interval);
2942 tv.tv_sec = (TYPEOF_TIMEVAL_TV_SEC)ts.tv_sec;
2943 tv.tv_usec = (TYPEOF_TIMEVAL_TV_USEC)(ts.tv_nsec / 1000);
2951 return time_timeval(num, TRUE);
2961 if (IsTimeval(time)) {
2962 GetTimeval(time, tobj);
2963 ts = timew2timespec(tobj->timew);
2964 t.tv_sec = (TYPEOF_TIMEVAL_TV_SEC)ts.tv_sec;
2965 t.tv_usec = (TYPEOF_TIMEVAL_TV_USEC)(ts.tv_nsec / 1000);
2968 return time_timeval(time, FALSE);
2977 if (IsTimeval(time)) {
2978 GetTimeval(time, tobj);
2979 t = timew2timespec(tobj->timew);
2982 return time_timespec(time, FALSE);
2988 return time_timespec(num, TRUE);
2992get_scale(
VALUE unit)
2994 if (unit ==
ID2SYM(id_nanosecond) || unit ==
ID2SYM(id_nsec)) {
2997 else if (unit ==
ID2SYM(id_microsecond) || unit ==
ID2SYM(id_usec)) {
3000 else if (unit ==
ID2SYM(id_millisecond)) {
3004 rb_raise(rb_eArgError,
"unexpected unit: %"PRIsVALUE, unit);
3015 int scale = get_scale(unit);
3016 time = num_exact(time);
3017 t = num_exact(subsec);
3018 timew = wadd(rb_time_magnify(v2w(time)), wmulquoll(v2w(t), TIME_SCALE, scale));
3019 t = time_new_timew(klass, timew);
3021 else if (IsTimeval(time)) {
3023 GetTimeval(time, tobj);
3024 t = time_new_timew(klass, tobj->timew);
3025 GetTimeval(t, tobj2);
3026 TZMODE_COPY(tobj2, tobj);
3029 timew = rb_time_magnify(v2w(num_exact(time)));
3030 t = time_new_timew(klass, timew);
3033 time_zonelocal(t, zone);
3045static const char months[][4] = {
3046 "jan",
"feb",
"mar",
"apr",
"may",
"jun",
3047 "jul",
"aug",
"sep",
"oct",
"nov",
"dec",
3054 obj = rb_str_to_inum(obj, 10, TRUE);
3062obj2ubits(
VALUE obj,
unsigned int bits)
3064 const unsigned int usable_mask = (1U << bits) - 1;
3065 unsigned int rv = (
unsigned int)obj2int(obj);
3067 if ((rv & usable_mask) != rv)
3068 rb_raise(rb_eArgError,
"argument out of range");
3069 return (uint32_t)rv;
3076 obj = rb_str_to_inum(obj, 10, TRUE);
3091 obj = rb_str_to_inum(obj, 10, TRUE);
3095 divmodv(num_exact(obj),
INT2FIX(1), &obj, &subsec);
3096 *subsecx = w2v(rb_time_magnify(v2w(subsec)));
3098 return obj2ubits(obj, 6);
3102usec2subsecx(
VALUE obj)
3105 obj = rb_str_to_inum(obj, 10, TRUE);
3108 return mulquov(num_exact(obj),
INT2FIX(TIME_SCALE),
INT2FIX(1000000));
3117 return obj2ubits(arg, 4);
3122 if (!
NIL_P(s) && RSTRING_LEN(s) > 0) {
3124 for (i=0; i<12; i++) {
3125 if (RSTRING_LEN(s) == 3 &&
3133 mon = obj2ubits(arg, 4);
3139validate_utc_offset(
VALUE utc_offset)
3141 if (le(utc_offset,
INT2FIX(-86400)) || ge(utc_offset,
INT2FIX(86400)))
3142 rb_raise(rb_eArgError,
"utc_offset out of range");
3147validate_zone_name(
VALUE zone_name)
3154validate_vtm(
struct vtm *
vtm)
3156#define validate_vtm_range(mem, b, e) \
3157 ((vtm->mem < b || vtm->mem > e) ? \
3158 rb_raise(rb_eArgError, #mem" out of range") : (void)0)
3159 validate_vtm_range(mon, 1, 12);
3160 validate_vtm_range(mday, 1, 31);
3161 validate_vtm_range(hour, 0, 24);
3162 validate_vtm_range(min, 0, (
vtm->hour == 24 ? 0 : 59));
3163 validate_vtm_range(sec, 0, (
vtm->hour == 24 ? 0 : 60));
3165 rb_raise(rb_eArgError,
"subsecx out of range");
3166 if (!
NIL_P(
vtm->utc_offset)) validate_utc_offset(
vtm->utc_offset);
3167#undef validate_vtm_range
3171time_arg(
int argc,
const VALUE *argv,
struct vtm *
vtm)
3187 vtm->zone = str_empty;
3197 vtm->isdst =
RTEST(argv[8]) ? 1 : 0;
3200 rb_scan_args(argc, argv,
"17", &v[0],&v[1],&v[2],&v[3],&v[4],&v[5],&v[6],&v[7]);
3203 vtm->wday = VTM_WDAY_INITVAL;
3204 vtm->isdst = VTM_ISDST_INITVAL;
3207 vtm->year = obj2vint(v[0]);
3213 vtm->mon = month_arg(v[1]);
3220 vtm->mday = obj2ubits(v[2], 5);
3228 unsigned int mday2 = leap_year_v_p(
vtm->year) ? 29 : 28;
3229 if (
vtm->mday > mday2) {
3239 if (
vtm->mday == 31) {
3246 vtm->hour =
NIL_P(v[3])?0:obj2ubits(v[3], 5);
3248 vtm->min =
NIL_P(v[4])?0:obj2ubits(v[4], 6);
3250 if (!
NIL_P(v[6]) && argc == 7) {
3251 vtm->sec =
NIL_P(v[5])?0:obj2ubits(v[5],6);
3252 subsecx = usec2subsecx(v[6]);
3260 vtm->sec = obj2subsecx(v[5], &subsecx);
3263 vtm->subsecx = subsecx;
3275 unsigned long uy = (
unsigned long)(LIKELY(y >= 0) ? y : -y);
3277 if (LIKELY(uy % 4 != 0))
return 0;
3279 unsigned long century = uy / 100;
3280 if (LIKELY(uy != century * 100))
return 1;
3281 return century % 4 == 0;
3285timegm_noleapsecond(
struct tm *tm)
3287 long tm_year = tm->tm_year;
3288 int tm_yday = calc_tm_yday(tm->tm_year, tm->tm_mon, tm->tm_mday);
3296 return tm->tm_sec + tm->tm_min*60 + tm->tm_hour*3600 +
3300 DIV(tm_year-1,100) +
3301 DIV(tm_year+299,400))*86400;
3305#define DEBUG_FIND_TIME_NUMGUESS
3306#define DEBUG_GUESSRANGE
3309static const bool debug_guessrange =
3310#ifdef DEBUG_GUESSRANGE
3316#define DEBUG_REPORT_GUESSRANGE \
3317 (debug_guessrange ? debug_report_guessrange(guess_lo, guess_hi) : (void)0)
3320debug_report_guessrange(time_t guess_lo, time_t guess_hi)
3322 time_t guess_diff = guess_hi - guess_lo;
3323 fprintf(stderr,
"find time guess range: %"PRI_TIMET_PREFIX
"d - "
3324 "%"PRI_TIMET_PREFIX
"d : %"PRI_TIMET_PREFIX
"u\n",
3325 guess_lo, guess_hi, guess_diff);
3328static const bool debug_find_time_numguess =
3329#ifdef DEBUG_FIND_TIME_NUMGUESS
3335#define DEBUG_FIND_TIME_NUMGUESS_INC \
3336 (void)(debug_find_time_numguess && find_time_numguess++),
3337static unsigned long long find_time_numguess;
3340find_time_numguess_getter(
ID name,
VALUE *data)
3342 unsigned long long *numguess = (
void *)data;
3347find_time_t(
struct tm *tptr,
int utc_p, time_t *tp)
3349 time_t guess, guess0, guess_lo, guess_hi;
3350 struct tm *tm, tm0, tm_lo, tm_hi;
3357#define GUESS(p) (DEBUG_FIND_TIME_NUMGUESS_INC (utc_p ? gmtime_with_leapsecond((p), &result) : LOCALTIME((p), result)))
3359 guess_lo = TIMET_MIN;
3360 guess_hi = TIMET_MAX;
3362 find_dst = 0 < tptr->tm_isdst;
3368 if (tm0.tm_mon < 0) {
3375 else if (11 < tm0.tm_mon) {
3382 else if (tm0.tm_mday < 1) {
3388 else if ((d = days_in_month_in(1900 + tm0.tm_year)[tm0.tm_mon]) < tm0.tm_mday) {
3394 else if (tm0.tm_hour < 0) {
3399 else if (23 < tm0.tm_hour) {
3404 else if (tm0.tm_min < 0) {
3408 else if (59 < tm0.tm_min) {
3412 else if (tm0.tm_sec < 0) {
3415 else if (60 < tm0.tm_sec) {
3419 DEBUG_REPORT_GUESSRANGE;
3420 guess0 = guess = timegm_noleapsecond(&tm0);
3423 d = tmcmp(tptr, tm);
3424 if (d == 0) {
goto found; }
3427 guess -= 24 * 60 * 60;
3431 guess += 24 * 60 * 60;
3433 DEBUG_REPORT_GUESSRANGE;
3434 if (guess_lo < guess && guess < guess_hi && (tm = GUESS(&guess)) != NULL) {
3435 d = tmcmp(tptr, tm);
3436 if (d == 0) {
goto found; }
3441 DEBUG_REPORT_GUESSRANGE;
3445 tm = GUESS(&guess_lo);
3446 if (!tm)
goto error;
3447 d = tmcmp(tptr, tm);
3448 if (d < 0)
goto out_of_range;
3449 if (d == 0) { guess = guess_lo;
goto found; }
3452 tm = GUESS(&guess_hi);
3453 if (!tm)
goto error;
3454 d = tmcmp(tptr, tm);
3455 if (d > 0)
goto out_of_range;
3456 if (d == 0) { guess = guess_hi;
goto found; }
3459 DEBUG_REPORT_GUESSRANGE;
3463 while (guess_lo + 1 < guess_hi) {
3466 guess = guess_lo / 2 + guess_hi / 2;
3467 if (guess <= guess_lo)
3468 guess = guess_lo + 1;
3469 else if (guess >= guess_hi)
3470 guess = guess_hi - 1;
3475 time_t guess0_hi = timegm_noleapsecond(&tm_hi);
3476 guess = guess_hi - (guess0_hi - guess0);
3477 if (guess == guess_hi)
3481 else if (status == 2) {
3482 time_t guess0_lo = timegm_noleapsecond(&tm_lo);
3483 guess = guess_lo + (guess0 - guess0_lo);
3484 if (guess == guess_lo)
3488 if (guess <= guess_lo || guess_hi <= guess) {
3490 if (debug_guessrange) {
3491 if (guess <= guess_lo) {
3492 fprintf(stderr,
"too small guess: %"PRI_TIMET_PREFIX
"d"\
3493 " <= %"PRI_TIMET_PREFIX
"d\n", guess, guess_lo);
3495 if (guess_hi <= guess) {
3496 fprintf(stderr,
"too big guess: %"PRI_TIMET_PREFIX
"d"\
3497 " <= %"PRI_TIMET_PREFIX
"d\n", guess_hi, guess);
3506 if (!tm)
goto error;
3508 d = tmcmp(tptr, tm);
3513 DEBUG_REPORT_GUESSRANGE;
3518 DEBUG_REPORT_GUESSRANGE;
3533 tptr_tm_yday = calc_tm_yday(tptr->tm_year, tptr->tm_mon, tptr->tm_mday);
3536 ((tptr->tm_year - tm_lo.tm_year) * 365 +
3537 DIV((tptr->tm_year-69), 4) -
3538 DIV((tptr->tm_year-1), 100) +
3539 DIV((tptr->tm_year+299), 400) -
3540 DIV((tm_lo.tm_year-69), 4) +
3541 DIV((tm_lo.tm_year-1), 100) -
3542 DIV((tm_lo.tm_year+299), 400) +
3544 tm_lo.tm_yday) * 86400 +
3545 (tptr->tm_hour - tm_lo.tm_hour) * 3600 +
3546 (tptr->tm_min - tm_lo.tm_min) * 60 +
3547 (tptr->tm_sec - (tm_lo.tm_sec == 60 ? 59 : tm_lo.tm_sec));
3556 guess2 = guess - 2 * 60 * 60;
3557 tm = LOCALTIME(&guess2, result);
3559 if (tptr->tm_hour != (tm->tm_hour + 2) % 24 ||
3560 tptr->tm_min != tm->tm_min ||
3561 tptr->tm_sec != tm->tm_sec) {
3562 guess2 -= (tm->tm_hour - tptr->tm_hour) * 60 * 60 +
3563 (tm->tm_min - tptr->tm_min) * 60 +
3564 (tm->tm_sec - tptr->tm_sec);
3565 if (tptr->tm_mday != tm->tm_mday)
3566 guess2 += 24 * 60 * 60;
3567 if (guess != guess2) {
3568 tm = LOCALTIME(&guess2, result);
3569 if (tm && tmcmp(tptr, tm) == 0) {
3581 guess2 = guess + 2 * 60 * 60;
3582 tm = LOCALTIME(&guess2, result);
3584 if ((tptr->tm_hour + 2) % 24 != tm->tm_hour ||
3585 tptr->tm_min != tm->tm_min ||
3586 tptr->tm_sec != tm->tm_sec) {
3587 guess2 -= (tm->tm_hour - tptr->tm_hour) * 60 * 60 +
3588 (tm->tm_min - tptr->tm_min) * 60 +
3589 (tm->tm_sec - tptr->tm_sec);
3590 if (tptr->tm_mday != tm->tm_mday)
3591 guess2 -= 24 * 60 * 60;
3592 if (guess != guess2) {
3593 tm = LOCALTIME(&guess2, result);
3594 if (tm && tmcmp(tptr, tm) == 0) {
3610 return "time out of range";
3613 return "gmtime/localtime error";
3617vtmcmp(
struct vtm *a,
struct vtm *b)
3619 if (ne(a->year, b->year))
3620 return lt(a->year, b->year) ? -1 : 1;
3621 else if (a->mon != b->mon)
3622 return a->mon < b->mon ? -1 : 1;
3623 else if (a->mday != b->mday)
3624 return a->mday < b->mday ? -1 : 1;
3625 else if (a->hour != b->hour)
3626 return a->hour < b->hour ? -1 : 1;
3627 else if (a->min != b->min)
3628 return a->min < b->min ? -1 : 1;
3629 else if (a->sec != b->sec)
3630 return a->sec < b->sec ? -1 : 1;
3631 else if (ne(a->subsecx, b->subsecx))
3632 return lt(a->subsecx, b->subsecx) ? -1 : 1;
3638tmcmp(
struct tm *a,
struct tm *b)
3640 if (a->tm_year != b->tm_year)
3641 return a->tm_year < b->tm_year ? -1 : 1;
3642 else if (a->tm_mon != b->tm_mon)
3643 return a->tm_mon < b->tm_mon ? -1 : 1;
3644 else if (a->tm_mday != b->tm_mday)
3645 return a->tm_mday < b->tm_mday ? -1 : 1;
3646 else if (a->tm_hour != b->tm_hour)
3647 return a->tm_hour < b->tm_hour ? -1 : 1;
3648 else if (a->tm_min != b->tm_min)
3649 return a->tm_min < b->tm_min ? -1 : 1;
3650 else if (a->tm_sec != b->tm_sec)
3651 return a->tm_sec < b->tm_sec ? -1 : 1;
3757time_s_mkutc(
int argc,
VALUE *argv,
VALUE klass)
3761 time_arg(argc, argv, &
vtm);
3762 return time_gmtime(time_new_timew(klass, timegmw(&
vtm)));
3783time_s_mktime(
int argc,
VALUE *argv,
VALUE klass)
3787 time_arg(argc, argv, &
vtm);
3788 return time_localtime(time_new_timew(klass, timelocalw(&
vtm)));
3808time_to_i(
VALUE time)
3812 GetTimeval(time, tobj);
3813 return w2v(wdiv(tobj->timew, WINT2FIXWV(TIME_SCALE)));
3837time_to_f(
VALUE time)
3841 GetTimeval(time, tobj);
3842 return rb_Float(rb_time_unmagnify_to_float(tobj->timew));
3858time_to_r(
VALUE time)
3863 GetTimeval(time, tobj);
3864 v = rb_time_unmagnify_to_rational(tobj->timew);
3886time_usec(
VALUE time)
3891 GetTimeval(time, tobj);
3893 w = wmod(tobj->timew, WINT2WV(TIME_SCALE));
3894 wmuldivmod(w, WINT2FIXWV(1000000), WINT2FIXWV(TIME_SCALE), &q, &r);
3913time_nsec(
VALUE time)
3917 GetTimeval(time, tobj);
3918 return rb_to_int(w2v(wmulquoll(wmod(tobj->timew, WINT2WV(TIME_SCALE)), 1000000000, TIME_SCALE)));
3939time_subsec(
VALUE time)
3943 GetTimeval(time, tobj);
3944 return quov(w2v(wmod(tobj->timew, WINT2FIXWV(TIME_SCALE))),
INT2FIX(TIME_SCALE));
3981 GetTimeval(time1, tobj1);
3982 if (IsTimeval(time2)) {
3983 GetTimeval(time2, tobj2);
3984 n = wcmp(tobj1->timew, tobj2->timew);
3987 return rb_invcmp(time1, time2);
3989 if (n == 0)
return INT2FIX(0);
4007 GetTimeval(time1, tobj1);
4008 if (IsTimeval(time2)) {
4009 GetTimeval(time2, tobj2);
4010 return rb_equal(w2v(tobj1->timew), w2v(tobj2->timew));
4043time_utc_p(
VALUE time)
4047 GetTimeval(time, tobj);
4048 return RBOOL(TZMODE_UTC_P(tobj));
4061time_hash(
VALUE time)
4065 GetTimeval(time, tobj);
4066 return rb_hash(w2v(tobj->timew));
4076 GetTimeval(time, tobj);
4077 GetNewTimeval(copy, tcopy);
4079 time_set_timew(copy, tcopy, tobj->timew);
4080 time_set_vtm(copy, tcopy, tobj->vtm);
4089 time_init_copy(dup, time);
4094time_localtime(
VALUE time)
4100 GetTimeval(time, tobj);
4101 if (TZMODE_LOCALTIME_P(tobj)) {
4102 if (tobj->vtm.tm_got)
4109 zone = tobj->vtm.zone;
4110 if (maybe_tzobj_p(zone) && zone_localtime(zone, time)) {
4114 if (!localtimew(tobj->timew, &
vtm))
4115 rb_raise(rb_eArgError,
"localtime error");
4116 time_set_vtm(time, tobj,
vtm);
4118 tobj->vtm.tm_got = 1;
4119 TZMODE_SET_LOCALTIME(tobj);
4127 if (zone_localtime(zone, time))
return time;
4131 if (
NIL_P(zone = find_timezone(time,
off))) invalid_utc_offset(
off);
4132 if (!zone_localtime(zone, time)) invalid_utc_offset(
off);
4135 else if (
off == UTC_ZONE) {
4136 return time_gmtime(time);
4138 validate_utc_offset(
off);
4140 time_set_utc_offset(time,
off);
4141 return time_fixoff(time);
4170time_localtime_m(
int argc,
VALUE *argv,
VALUE time)
4175 return time_zonelocal(time,
off);
4178 return time_localtime(time);
4196time_gmtime(
VALUE time)
4201 GetTimeval(time, tobj);
4202 if (TZMODE_UTC_P(tobj)) {
4203 if (tobj->vtm.tm_got)
4211 GMTIMEW(tobj->timew, &
vtm);
4212 time_set_vtm(time, tobj,
vtm);
4214 tobj->vtm.tm_got = 1;
4215 TZMODE_SET_UTC(tobj);
4220time_fixoff(
VALUE time)
4226 GetTimeval(time, tobj);
4227 if (TZMODE_FIXOFF_P(tobj)) {
4228 if (tobj->vtm.tm_got)
4235 if (TZMODE_FIXOFF_P(tobj))
4236 off = tobj->vtm.utc_offset;
4240 GMTIMEW(tobj->timew, &
vtm);
4242 zone = tobj->vtm.zone;
4243 vtm_add_offset(&
vtm,
off, +1);
4245 time_set_vtm(time, tobj,
vtm);
4246 RB_OBJ_WRITE_UNALIGNED(time, &tobj->vtm.zone, zone);
4248 tobj->vtm.tm_got = 1;
4249 TZMODE_SET_FIXOFF(time, tobj,
off);
4271time_getlocaltime(
int argc,
VALUE *argv,
VALUE time)
4277 if (maybe_tzobj_p(zone)) {
4278 VALUE t = time_dup(time);
4279 if (zone_localtime(
off, t))
return t;
4284 if (
NIL_P(zone = find_timezone(time,
off))) invalid_utc_offset(
off);
4285 time = time_dup(time);
4286 if (!zone_localtime(zone, time)) invalid_utc_offset(
off);
4289 else if (
off == UTC_ZONE) {
4290 return time_gmtime(time_dup(time));
4292 validate_utc_offset(
off);
4294 time = time_dup(time);
4295 time_set_utc_offset(time,
off);
4296 return time_fixoff(time);
4299 return time_localtime(time_dup(time));
4318time_getgmtime(
VALUE time)
4320 return time_gmtime(time_dup(time));
4326 if (TZMODE_UTC_P(tobj))
return time_gmtime(time);
4327 if (TZMODE_FIXOFF_P(tobj))
return time_fixoff(time);
4328 return time_localtime(time);
4332#define strftimev(fmt, time, enc) strftime_cstr((fmt), rb_strlen_lit(fmt), (time), (enc))
4356time_asctime(
VALUE time)
4358 return strftimev(
"%a %b %e %T %Y", time, rb_usascii_encoding());
4378time_to_s(
VALUE time)
4382 GetTimeval(time, tobj);
4383 if (TZMODE_UTC_P(tobj))
4384 return strftimev(
"%Y-%m-%d %H:%M:%S UTC", time, rb_usascii_encoding());
4386 return strftimev(
"%Y-%m-%d %H:%M:%S %z", time, rb_usascii_encoding());
4406time_inspect(
VALUE time)
4411 GetTimeval(time, tobj);
4412 str = strftimev(
"%Y-%m-%d %H:%M:%S", time, rb_usascii_encoding());
4413 subsec = w2v(wmod(tobj->timew, WINT2FIXWV(TIME_SCALE)));
4418 rb_str_catf(str,
".%09ld",
FIX2LONG(subsec));
4419 for (
len=RSTRING_LEN(str); RSTRING_PTR(str)[
len-1] ==
'0' &&
len > 0;
len--)
4421 rb_str_resize(str,
len);
4425 subsec = quov(subsec,
INT2FIX(TIME_SCALE));
4428 if (TZMODE_UTC_P(tobj)) {
4434 char sign = (
off < 0) ? (
off = -
off,
'-') :
'+';
4436 int min = (
off /= 60) % 60;
4438 rb_str_catf(str,
" %c%.2d%.2d", sign, (
int)
off, min);
4439 if (sec) rb_str_catf(str,
"%.2d", sec);
4450 offset = num_exact(offset);
4452 result = time_new_timew(klass, wsub(tobj->timew, rb_time_magnify(v2w(offset))));
4454 result = time_new_timew(klass, wadd(tobj->timew, rb_time_magnify(v2w(offset))));
4455 GetTimeval(result, result_tobj);
4456 TZMODE_COPY(result_tobj, tobj);
4464 return time_add0(
rb_cTime, tobj, torig, offset, sign);
4485 GetTimeval(time1, tobj);
4487 if (IsTimeval(time2)) {
4490 return time_add(tobj, time1, time2, 1);
4520 GetTimeval(time1, tobj);
4521 if (IsTimeval(time2)) {
4524 GetTimeval(time2, tobj2);
4525 return rb_Float(rb_time_unmagnify_to_float(wsub(tobj->timew, tobj2->timew)));
4527 return time_add(tobj, time1, time2, -1);
4531ndigits_denominator(
VALUE ndigits)
4536 rb_raise(rb_eArgError,
"negative ndigits given");
4576 VALUE ndigits, v, den;
4582 den = ndigits_denominator(ndigits);
4584 GetTimeval(time, tobj);
4585 v = w2v(rb_time_unmagnify(tobj->timew));
4588 if (lt(v, quov(den,
INT2FIX(2))))
4589 return time_add(tobj, time, v, -1);
4591 return time_add(tobj, time, subv(den, v), 1);
4624 VALUE ndigits, v, den;
4630 den = ndigits_denominator(ndigits);
4632 GetTimeval(time, tobj);
4633 v = w2v(rb_time_unmagnify(tobj->timew));
4636 return time_add(tobj, time, v, -1);
4669 VALUE ndigits, v, den;
4675 den = ndigits_denominator(ndigits);
4677 GetTimeval(time, tobj);
4678 v = w2v(rb_time_unmagnify(tobj->timew));
4684 return time_add(tobj, time, v, 1);
4709 GetTimeval(time, tobj);
4710 MAKE_TM(time, tobj);
4711 return INT2FIX(tobj->vtm.sec);
4733 GetTimeval(time, tobj);
4734 MAKE_TM(time, tobj);
4735 return INT2FIX(tobj->vtm.min);
4753time_hour(
VALUE time)
4757 GetTimeval(time, tobj);
4758 MAKE_TM(time, tobj);
4759 return INT2FIX(tobj->vtm.hour);
4777time_mday(
VALUE time)
4781 GetTimeval(time, tobj);
4782 MAKE_TM(time, tobj);
4783 return INT2FIX(tobj->vtm.mday);
4805 GetTimeval(time, tobj);
4806 MAKE_TM(time, tobj);
4807 return INT2FIX(tobj->vtm.mon);
4824time_year(
VALUE time)
4828 GetTimeval(time, tobj);
4829 MAKE_TM(time, tobj);
4830 return tobj->vtm.year;
4849time_wday(
VALUE time)
4853 GetTimeval(time, tobj);
4854 MAKE_TM_ENSURE(time, tobj, tobj->vtm.wday != VTM_WDAY_INITVAL);
4855 return INT2FIX((
int)tobj->vtm.wday);
4859 return RBOOL(time_wday(time) == INT2FIX(n)); \
4875time_sunday(
VALUE time)
4893time_monday(
VALUE time)
4911time_tuesday(
VALUE time)
4929time_wednesday(
VALUE time)
4947time_thursday(
VALUE time)
4965time_friday(
VALUE time)
4983time_saturday(
VALUE time)
4999time_yday(
VALUE time)
5003 GetTimeval(time, tobj);
5004 MAKE_TM_ENSURE(time, tobj, tobj->vtm.yday != 0);
5005 return INT2FIX(tobj->vtm.yday);
5024time_isdst(
VALUE time)
5028 GetTimeval(time, tobj);
5029 MAKE_TM(time, tobj);
5030 if (tobj->vtm.isdst == VTM_ISDST_INITVAL) {
5033 return RBOOL(tobj->vtm.isdst);
5047time_zone(
VALUE time)
5052 GetTimeval(time, tobj);
5053 MAKE_TM(time, tobj);
5055 if (TZMODE_UTC_P(tobj)) {
5058 zone = tobj->vtm.zone;
5083 GetTimeval(time, tobj);
5085 if (TZMODE_UTC_P(tobj)) {
5089 MAKE_TM(time, tobj);
5090 return tobj->vtm.utc_offset;
5110time_to_a(
VALUE time)
5114 GetTimeval(time, tobj);
5115 MAKE_TM_ENSURE(time, tobj, tobj->vtm.yday != 0);
5125 RBOOL(tobj->vtm.isdst),
5171 GetTimeval(time, tobj);
5172 MAKE_TM_ENSURE(time, tobj, tobj->vtm.yday != 0);
5175 h = rb_hash_new_with_size(11);
5177 rb_hash_aset(h, sym_year, tobj->vtm.year);
5178 rb_hash_aset(h, sym_month,
INT2FIX(tobj->vtm.mon));
5179 rb_hash_aset(h, sym_day,
INT2FIX(tobj->vtm.mday));
5180 rb_hash_aset(h, sym_yday,
INT2FIX(tobj->vtm.yday));
5181 rb_hash_aset(h, sym_wday,
INT2FIX(tobj->vtm.wday));
5182 rb_hash_aset(h, sym_hour,
INT2FIX(tobj->vtm.hour));
5183 rb_hash_aset(h, sym_min,
INT2FIX(tobj->vtm.min));
5184 rb_hash_aset(h, sym_sec,
INT2FIX(tobj->vtm.sec));
5185 rb_hash_aset(h, sym_subsec,
5186 quov(w2v(wmod(tobj->timew, WINT2FIXWV(TIME_SCALE))),
INT2FIX(TIME_SCALE)));
5187 rb_hash_aset(h, sym_dst, RBOOL(tobj->vtm.isdst));
5188 rb_hash_aset(h, sym_zone, time_zone(time));
5194 "wrong argument type %"PRIsVALUE
" (expected Array or nil)",
5204 if (sym_year == key) rb_hash_aset(h, key, tobj->vtm.year);
5205 if (sym_month == key) rb_hash_aset(h, key,
INT2FIX(tobj->vtm.mon));
5206 if (sym_day == key) rb_hash_aset(h, key,
INT2FIX(tobj->vtm.mday));
5207 if (sym_yday == key) rb_hash_aset(h, key,
INT2FIX(tobj->vtm.yday));
5208 if (sym_wday == key) rb_hash_aset(h, key,
INT2FIX(tobj->vtm.wday));
5209 if (sym_hour == key) rb_hash_aset(h, key,
INT2FIX(tobj->vtm.hour));
5210 if (sym_min == key) rb_hash_aset(h, key,
INT2FIX(tobj->vtm.min));
5211 if (sym_sec == key) rb_hash_aset(h, key,
INT2FIX(tobj->vtm.sec));
5212 if (sym_subsec == key) {
5213 rb_hash_aset(h, key, quov(w2v(wmod(tobj->timew, WINT2FIXWV(TIME_SCALE))),
INT2FIX(TIME_SCALE)));
5215 if (sym_dst == key) rb_hash_aset(h, key, RBOOL(tobj->vtm.isdst));
5216 if (sym_zone == key) rb_hash_aset(h, key, time_zone(time));
5222rb_strftime_alloc(
const char *format,
size_t format_len,
rb_encoding *enc,
5223 VALUE time,
struct vtm *
vtm, wideval_t timew,
int gmt)
5228 if (!timew2timespec_exact(timew, &ts))
5229 timev = w2v(rb_time_unmagnify(timew));
5232 return rb_strftime_timespec(format, format_len, enc, time,
vtm, &ts, gmt);
5235 return rb_strftime(format, format_len, enc, time,
vtm, timev, gmt);
5245 GetTimeval(time, tobj);
5246 MAKE_TM(time, tobj);
5247 str = rb_strftime_alloc(fmt,
len, enc, time, &tobj->vtm, tobj->timew, TZMODE_UTC_P(tobj));
5248 if (!str) rb_raise(rb_eArgError,
"invalid format: %s", fmt);
5270 GetTimeval(time, tobj);
5271 MAKE_TM_ENSURE(time, tobj, tobj->vtm.yday != 0);
5274 rb_raise(rb_eArgError,
"format should have ASCII compatible encoding");
5276 tmp = rb_str_tmp_frozen_acquire(format);
5277 fmt = RSTRING_PTR(tmp);
5278 len = RSTRING_LEN(tmp);
5279 enc = rb_enc_get(format);
5281 rb_warning(
"strftime called with empty format string");
5282 return rb_enc_str_new(0, 0, enc);
5285 VALUE str = rb_strftime_alloc(fmt,
len, enc, time, &tobj->vtm, tobj->timew,
5286 TZMODE_UTC_P(tobj));
5287 rb_str_tmp_frozen_release(format, tmp);
5288 if (!str) rb_raise(rb_eArgError,
"invalid format: %"PRIsVALUE, format);
5315time_xmlschema(
int argc,
VALUE *argv,
VALUE time)
5317 long fraction_digits = 0;
5320 fraction_digits =
NUM2LONG(argv[0]);
5321 if (fraction_digits < 0) {
5322 fraction_digits = 0;
5328 GetTimeval(time, tobj);
5329 MAKE_TM(time, tobj);
5331 const long size_after_year =
sizeof(
"-MM-DDTHH:MM:SS+ZH:ZM") + fraction_digits
5332 + (fraction_digits > 0);
5336# define fill_digits_long(len, prec, n) \
5337 for (int fill_it = 1, written = snprintf(ptr, len, "%0*ld", prec, n); \
5338 fill_it; ptr += written, fill_it = 0)
5341 long year =
FIX2LONG(tobj->vtm.year);
5343 int w = (year >= -9999 && year <= 9999 ? year_width : (year < 0) + (
int)
DECIMAL_SIZE_OF(year));
5345 ptr = RSTRING_PTR(str);
5346 fill_digits_long(w + 1, year_width, year) {
5347 if (year >= -9999 && year <= 9999) {
5357 str = rb_int2str(tobj->vtm.year, 10);
5362# define fill_2(c, n) (*ptr++ = c, *ptr++ = '0' + (n) / 10, *ptr++ = '0' + (n) % 10)
5363 fill_2(
'-', tobj->vtm.mon);
5364 fill_2(
'-', tobj->vtm.mday);
5365 fill_2(
'T', tobj->vtm.hour);
5366 fill_2(
':', tobj->vtm.min);
5367 fill_2(
':', tobj->vtm.sec);
5369 if (fraction_digits > 0) {
5370 VALUE subsecx = tobj->vtm.subsecx;
5374 if (fraction_digits <= TIME_SCALE_NUMDIGITS) {
5375 digits = TIME_SCALE_NUMDIGITS - (int)fraction_digits;
5378 long w = fraction_digits - TIME_SCALE_NUMDIGITS;
5385 if (digits >= 0 && fraction_digits < INT_MAX) {
5387 if (digits > 0) subsec /= (long)pow(10, digits);
5388 fill_digits_long(fraction_digits + 1, (
int)fraction_digits, subsec) {
5393 subsecx = rb_int2str(subsecx, 10);
5394 long len = RSTRING_LEN(subsecx);
5395 if (fraction_digits >
len) {
5396 memset(ptr,
'0', fraction_digits -
len);
5399 len = fraction_digits;
5401 ptr += fraction_digits;
5402 memcpy(ptr -
len, RSTRING_PTR(subsecx),
len);
5406 if (TZMODE_UTC_P(tobj)) {
5412 char sign = offset < 0 ?
'-' :
'+';
5413 if (offset < 0) offset = -offset;
5415 fill_2(sign, offset / 60);
5416 fill_2(
':', offset % 60);
5418 const char *
const start = RSTRING_PTR(str);
5423int ruby_marshal_write_long(
long x,
char *buf);
5425enum {base_dump_size = 8};
5429time_mdump(
VALUE time)
5433 char buf[base_dump_size +
sizeof(long) + 1];
5440 VALUE subsecx, nano, subnano, v, zone;
5443 const int max_year = 1900+0xffff;
5445 GetTimeval(time, tobj);
5447 gmtimew(tobj->timew, &
vtm);
5451 if (year > max_year) {
5452 year_extend =
INT2FIX(year - max_year);
5455 else if (year < 1900) {
5456 year_extend =
LONG2NUM(1900 - year);
5461 if (rb_int_positive_p(
vtm.year)) {
5462 year_extend = rb_int_minus(
vtm.year,
INT2FIX(max_year));
5466 year_extend = rb_int_minus(
INT2FIX(1900),
vtm.year);
5471 subsecx =
vtm.subsecx;
5473 nano = mulquov(subsecx,
INT2FIX(1000000000),
INT2FIX(TIME_SCALE));
5474 divmodv(nano,
INT2FIX(1), &v, &subnano);
5479 nano = addv(
LONG2FIX(nsec), subnano);
5482 TZMODE_UTC_P(tobj) << 30 |
5487 s = (
unsigned long)
vtm.min << 26 |
5491 for (i=0; i<4; i++) {
5492 buf[i] = (
unsigned char)p;
5495 for (i=4; i<8; i++) {
5496 buf[i] = (
unsigned char)s;
5500 if (!
NIL_P(year_extend)) {
5507 size_t ysize = rb_absint_size(year_extend, NULL);
5508 char *p, *
const buf_year_extend = buf + base_dump_size;
5509 if (ysize > LONG_MAX ||
5510 (i = ruby_marshal_write_long((
long)ysize, buf_year_extend)) < 0) {
5511 rb_raise(rb_eArgError,
"year too %s to marshal: %"PRIsVALUE
" UTC",
5512 (year == 1900 ?
"small" :
"big"),
vtm.year);
5514 i += base_dump_size;
5516 p = RSTRING_PTR(str);
5527 rb_ivar_set(str, id_nano_num, RRATIONAL(nano)->num);
5528 rb_ivar_set(str, id_nano_den, RRATIONAL(nano)->den);
5544 int len = (int)
sizeof(buf);
5545 buf[1] = (char)((nsec % 10) << 4);
5547 buf[0] = (char)(nsec % 10);
5549 buf[0] |= (char)((nsec % 10) << 4);
5554 if (!TZMODE_UTC_P(tobj)) {
5561 zone = tobj->vtm.zone;
5562 if (maybe_tzobj_p(zone)) {
5563 zone = rb_funcallv(zone, id_name, 0, 0);
5576 str = time_mdump(time);
5582mload_findzone(
VALUE arg)
5585 VALUE time = argp[0], zone = argp[1];
5586 return find_timezone(time, zone);
5596 if (
NIL_P(z))
return rb_fstring(zone);
5601long ruby_marshal_read_long(
const char **buf,
long len);
5615 VALUE submicro, nano_num, nano_den, offset, zone, year;
5620#define get_attr(attr, iffound) \
5621 attr = rb_attr_delete(str, id_##attr); \
5622 if (!NIL_P(attr)) { \
5626 get_attr(nano_num, {});
5627 get_attr(nano_den, {});
5628 get_attr(submicro, {});
5629 get_attr(offset, (offset =
rb_rescue(validate_utc_offset, offset, 0,
Qnil)));
5630 get_attr(zone, (zone =
rb_rescue(validate_zone_name, zone, 0,
Qnil)));
5638 buf = (
unsigned char *)RSTRING_PTR(str);
5639 if (RSTRING_LEN(str) < base_dump_size) {
5640 goto invalid_format;
5644 for (i=0; i<4; i++) {
5645 p |= (
unsigned long)buf[i]<<(8*i);
5647 for (i=4; i<8; i++) {
5648 s |= (
unsigned long)buf[i]<<(8*(i-4));
5651 if ((p & (1UL<<31)) == 0) {
5657 timew = wadd(rb_time_magnify(TIMET2WV(sec)), wmulquoll(WINT2FIXWV(usec), TIME_SCALE, 1000000));
5661 gmt = (int)((p >> 30) & 0x1);
5664 year =
INT2FIX(((
int)(p >> 14) & 0xffff) + 1900);
5666 if (RSTRING_LEN(str) > base_dump_size) {
5667 long len = RSTRING_LEN(str) - base_dump_size;
5670 const char *ybuf = (
const char *)(buf += base_dump_size);
5671 ysize = ruby_marshal_read_long(&ybuf,
len);
5672 len -= ybuf - (
const char *)buf;
5673 if (ysize < 0 || ysize >
len)
goto invalid_format;
5676 year = rb_int_minus(year, year_extend);
5679 year = rb_int_plus(year, year_extend);
5682 unsigned int mon = ((int)(p >> 10) & 0xf);
5689 vtm.mday = (int)(p >> 5) & 0x1f;
5690 vtm.hour = (int) p & 0x1f;
5691 vtm.min = (int)(s >> 26) & 0x3f;
5692 vtm.sec = (int)(s >> 20) & 0x3f;
5696 vtm.zone = str_empty;
5698 usec = (long)(s & 0xfffff);
5703 if (nano_num !=
Qnil) {
5704 VALUE nano = quov(num_exact(nano_num), num_exact(nano_den));
5707 else if (submicro !=
Qnil) {
5712 len = RSTRING_LEN(submicro);
5715 if (10 <= (digit = ptr[0] >> 4))
goto end_submicro;
5716 nsec += digit * 100;
5717 if (10 <= (digit = ptr[0] & 0xf))
goto end_submicro;
5721 if (10 <= (digit = ptr[1] >> 4))
goto end_submicro;
5727 timew = timegmw(&
vtm);
5730 GetNewTimeval(time, tobj);
5731 TZMODE_SET_LOCALTIME(tobj);
5732 tobj->vtm.tm_got = 0;
5733 time_set_timew(time, tobj, timew);
5736 TZMODE_SET_UTC(tobj);
5738 else if (!
NIL_P(offset)) {
5739 time_set_utc_offset(time, offset);
5743 zone = mload_zone(time, zone);
5745 zone_localtime(zone, time);
5759 VALUE time = time_s_alloc(klass);
5761 time_mload(time, str);
5786 GetTimeval(time, tobj);
5787 tm = time_s_alloc(klass);
5788 ttm = RTYPEDDATA_GET_DATA(tm);
5791 WIDEVALUE timew = tobj->timew;
5793 time_set_timew(tm, ttm, wsub(timew, v->subsecx));
5796 time_set_vtm(tm, ttm, *v);
5798 ttm->vtm.tm_got = 1;
5799 TZMODE_SET_UTC(ttm);
5811tm_initialize(
int argc,
VALUE *argv,
VALUE time)
5817 time_arg(argc, argv, &
vtm);
5819 struct time_object *tobj = RTYPEDDATA_GET_DATA(time);
5820 TZMODE_SET_UTC(tobj);
5821 time_set_timew(time, tobj, t);
5822 time_set_vtm(time, tobj,
vtm);
5838 struct time_object *tobj = RTYPEDDATA_GET_DATA(dup);
5846 return time_add0(
rb_obj_class(tm), get_timeval(tm), tm, offset, +1);
5852 return time_add0(
rb_obj_class(tm), get_timeval(tm), tm, offset, -1);
5856Init_tm(
VALUE outer,
const char *name)
5902rb_time_zone_abbreviation(
VALUE zone,
VALUE time)
5904 VALUE tm, abbr, strftime_args[2];
5907 if (!
NIL_P(abbr))
return abbr;
5909 tm = tm_from_time(rb_cTimeTM, time);
5911 if (!UNDEF_P(abbr)) {
5914#ifdef SUPPORT_TZINFO_ZONE_ABBREVIATION
5916 if (!UNDEF_P(abbr)) {
5917 abbr = rb_funcallv(abbr, rb_intern(
"abbreviation"), 0, 0);
5921 strftime_args[0] = rb_fstring_lit(
"%Z");
5922 strftime_args[1] = tm;
5924 if (!UNDEF_P(abbr)) {
5927 abbr = rb_check_funcall_default(zone, idName, 0, 0,
Qnil);
5937 ruby_reset_timezone(getenv(
"TZ"));
5973 str_utc = rb_fstring_lit(
"UTC");
5974 rb_vm_register_global_object(str_utc);
5975 str_empty = rb_fstring_lit(
"");
5976 rb_vm_register_global_object(str_empty);
6060 if (debug_find_time_numguess) {
6062 find_time_numguess_getter, 0);
6065 rb_cTimeTM = Init_tm(
rb_cTime,
"tm");
6068#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.
void rb_exc_raise(VALUE mesg)
Raises an exception in the current thread.
int rb_typeddata_is_kind_of(VALUE obj, const rb_data_type_t *data_type)
Checks if the given object is of given kind.
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.
#define RB_OBJ_WRITE(old, slot, young)
Declaration of a "back" pointer.
static bool rb_enc_str_asciicompat_p(VALUE str)
Queries if the passed string is in an ASCII-compatible encoding.
VALUE rb_enc_interned_str(const char *ptr, long len, rb_encoding *enc)
Identical to rb_enc_str_new(), except it returns a "f"string.
VALUE rb_funcall(VALUE recv, ID mid, int n,...)
Calls a method.
Defines RBIMPL_HAS_BUILTIN.
VALUE rb_check_array_type(VALUE obj)
Try converting an object to its array representation using its to_ary method, if any.
VALUE rb_ary_entry(VALUE ary, long off)
Queries an element of an array.
#define INTEGER_PACK_NATIVE_BYTE_ORDER
Means either INTEGER_PACK_MSBYTE_FIRST or INTEGER_PACK_LSBYTE_FIRST, depending on the host processor'...
#define INTEGER_PACK_LITTLE_ENDIAN
Little endian combination.
static int rb_check_arity(int argc, int min, int max)
Ensures that the passed integer is in the passed range.
void rb_num_zerodiv(void)
Just always raises an exception.
VALUE rb_int_positive_pow(long x, unsigned long y)
Raises the passed x to the power of y.
VALUE rb_rational_new(VALUE num, VALUE den)
Constructs a Rational, with reduction.
#define rb_Rational1(x)
Shorthand of (x/1)r.
VALUE rb_str_subseq(VALUE str, long beg, long len)
Identical to rb_str_substr(), except the numbers are interpreted as byte offsets instead of character...
#define rb_str_new(str, len)
Allocates an instance of rb_cString.
#define rb_usascii_str_new(str, len)
Identical to rb_str_new, except it generates a string of "US ASCII" encoding.
VALUE rb_str_dup(VALUE str)
Duplicates a string.
VALUE rb_str_cat(VALUE dst, const char *src, long srclen)
Destructively appends the passed contents to the string.
#define rb_usascii_str_new_cstr(str)
Identical to rb_str_new_cstr, except it generates a string of "US ASCII" encoding.
void rb_str_set_len(VALUE str, long len)
Overwrites the length of the string.
VALUE rb_str_concat(VALUE dst, VALUE src)
Identical to rb_str_append(), except it also accepts an integer as a codepoint.
#define rb_strlen_lit(str)
Length of a string literal.
VALUE rb_check_string_type(VALUE obj)
Try converting an object to its stringised representation using its to_str method,...
#define rb_str_cat_cstr(buf, str)
Identical to rb_str_cat(), except it assumes the passed pointer is a pointer to a C string.
#define rb_utf8_str_new(str, len)
Identical to rb_str_new, except it generates a string of "UTF-8" encoding.
void rb_str_modify_expand(VALUE str, long capa)
Identical to rb_str_modify(), except it additionally expands the capacity of the receiver.
#define rb_str_new_cstr(str)
Identical to rb_str_new, except it assumes the passed pointer is a pointer to a C string.
VALUE rb_obj_as_string(VALUE obj)
Try converting an object to its stringised representation using its to_s method, if any.
VALUE rb_time_nano_new(time_t sec, long nsec)
Identical to rb_time_new(), except it accepts the time in nanoseconds resolution.
void rb_timespec_now(struct timespec *ts)
Fills the current time into the given struct.
VALUE rb_time_timespec_new(const struct timespec *ts, int offset)
Creates an instance of rb_cTime, with given time and offset.
struct timespec rb_time_timespec(VALUE time)
Identical to rb_time_timeval(), except for return type.
VALUE rb_time_new(time_t sec, long usec)
Creates an instance of rb_cTime with the given time and the local timezone.
struct timeval rb_time_timeval(VALUE time)
Converts an instance of rb_cTime to a struct timeval that represents the identical point of time.
struct timeval rb_time_interval(VALUE num)
Creates a "time interval".
VALUE rb_time_num_new(VALUE timev, VALUE off)
Identical to rb_time_timespec_new(), except it takes Ruby values instead of C structs.
VALUE rb_time_utc_offset(VALUE time)
Queries the offset, in seconds between the time zone of the time and the UTC.
struct timespec rb_time_timespec_interval(VALUE num)
Identical to rb_time_interval(), except for return type.
VALUE rb_ivar_set(VALUE obj, ID name, VALUE val)
Identical to rb_iv_set(), except it accepts the name as an ID instead of a C string.
int rb_respond_to(VALUE obj, ID mid)
Queries if the object responds to the method.
void rb_alias(VALUE klass, ID dst, ID src)
Resembles alias.
VALUE rb_check_funcall(VALUE recv, ID mid, int argc, const VALUE *argv)
Identical to rb_funcallv(), except it returns RUBY_Qundef instead of raising rb_eNoMethodError.
void rb_define_alloc_func(VALUE klass, rb_alloc_func_t func)
Sets the allocator function of a class.
static ID rb_intern_const(const char *str)
This is a "tiny optimisation" over rb_intern().
int off
Offset inside of ptr.
int len
Length of the buffer.
#define DECIMAL_SIZE_OF(expr)
An approximation of decimal representation size.
#define rb_long2int
Just another name of rb_long2int_inline.
#define RB_GC_GUARD(v)
Prevents premature destruction of local objects.
void rb_define_hooked_variable(const char *q, VALUE *w, type *e, void_type *r)
Define a function-backended global variable.
VALUE rb_rescue(type *q, VALUE w, type *e, VALUE r)
An equivalent of rescue clause.
void rb_copy_generic_ivar(VALUE clone, VALUE obj)
Copies the list of instance variables.
#define RARRAY_LEN
Just another name of rb_array_len.
#define RARRAY_AREF(a, i)
#define StringValue(v)
Ensures that the parameter object is a String.
#define StringValuePtr(v)
Identical to StringValue, except it returns a char*.
VALUE rb_str_export_locale(VALUE obj)
Identical to rb_str_export(), except it converts into the locale encoding instead.
static char * RSTRING_END(VALUE str)
Queries the end of the contents pointer of the string.
#define StringValueCStr(v)
Identical to StringValuePtr, except it additionally checks for the contents for viability as a C stri...
#define RUBY_TYPED_DEFAULT_FREE
This is a value you can set to rb_data_type_struct::dfree.
#define TypedData_Get_Struct(obj, type, data_type, sval)
Obtains a C struct from inside of a wrapper Ruby object.
#define TypedData_Make_Struct(klass, type, data_type, sval)
Identical to TypedData_Wrap_Struct, except it allocates a new data region internally instead of takin...
#define RTEST
This is an old name of RB_TEST.
This is the struct that holds necessary info for a struct.
const char * wrap_struct_name
Name of structs of this kind.
intptr_t SIGNED_VALUE
A signed integer type that has the same width with VALUE.
uintptr_t ID
Type that represents a Ruby identifier such as a variable name.
uintptr_t VALUE
Type that represents a Ruby object.
static bool RB_FLOAT_TYPE_P(VALUE obj)
Queries if the object is an instance of rb_cFloat.
static bool rb_integer_type_p(VALUE obj)
Queries if the object is an instance of rb_cInteger.
static bool RB_TYPE_P(VALUE obj, enum ruby_value_type t)
Queries if the given object is of given type.