Ruby 3.5.0dev (2025-08-09 revision 60ca525fce71b702ea8e5893c976044170a56d75)
time.c (60ca525fce71b702ea8e5893c976044170a56d75)
1/**********************************************************************
2
3 time.c -
4
5 $Author$
6 created at: Tue Dec 28 14:31:59 JST 1993
7
8 Copyright (C) 1993-2007 Yukihiro Matsumoto
9
10**********************************************************************/
11
12#define _DEFAULT_SOURCE
13#define _BSD_SOURCE
14#include "ruby/internal/config.h"
15
16#include <errno.h>
17#include <float.h>
18#include <math.h>
19#include <time.h>
20#include <sys/types.h>
21
22#ifdef HAVE_UNISTD_H
23# include <unistd.h>
24#endif
25
26#ifdef HAVE_STRINGS_H
27# include <strings.h>
28#endif
29
30#if defined(HAVE_SYS_TIME_H)
31# include <sys/time.h>
32#endif
33
34#include "id.h"
35#include "internal.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"
44#include "ruby/encoding.h"
45#include "ruby/util.h"
46#include "timev.h"
47
48#if defined(_WIN32)
49# include <timezoneapi.h> /* DYNAMIC_TIME_ZONE_INFORMATION */
50#endif
51
52#include "builtin.h"
53
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;
59
60// used by deconstruct_keys
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;
63
64#define id_quo idQuo
65#define id_div idDiv
66#define id_divmod idDivmod
67#define id_name idName
68#define UTC_ZONE Qundef
69
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)
76
77static int
78eq(VALUE x, VALUE y)
79{
80 if (FIXNUM_P(x) && FIXNUM_P(y)) {
81 return x == y;
82 }
83 return RTEST(rb_funcall(x, idEq, 1, y));
84}
85
86static int
87cmp(VALUE x, VALUE y)
88{
89 if (FIXNUM_P(x) && FIXNUM_P(y)) {
90 if ((long)x < (long)y)
91 return -1;
92 if ((long)x > (long)y)
93 return 1;
94 return 0;
95 }
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);
98}
99
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)
105
106static VALUE
107addv(VALUE x, VALUE y)
108{
109 if (FIXNUM_P(x) && FIXNUM_P(y)) {
110 return LONG2NUM(FIX2LONG(x) + FIX2LONG(y));
111 }
112 if (RB_BIGNUM_TYPE_P(x)) return rb_big_plus(x, y);
113 return rb_funcall(x, '+', 1, y);
114}
115
116static VALUE
117subv(VALUE x, VALUE y)
118{
119 if (FIXNUM_P(x) && FIXNUM_P(y)) {
120 return LONG2NUM(FIX2LONG(x) - FIX2LONG(y));
121 }
122 if (RB_BIGNUM_TYPE_P(x)) return rb_big_minus(x, y);
123 return rb_funcall(x, '-', 1, y);
124}
125
126static VALUE
127mulv(VALUE x, VALUE y)
128{
129 if (FIXNUM_P(x) && FIXNUM_P(y)) {
130 return rb_fix_mul_fix(x, y);
131 }
132 if (RB_BIGNUM_TYPE_P(x))
133 return rb_big_mul(x, y);
134 return rb_funcall(x, '*', 1, y);
135}
136
137static VALUE
138divv(VALUE x, VALUE y)
139{
140 if (FIXNUM_P(x) && FIXNUM_P(y)) {
141 return rb_fix_div_fix(x, y);
142 }
143 if (RB_BIGNUM_TYPE_P(x))
144 return rb_big_div(x, y);
145 return rb_funcall(x, id_div, 1, y);
146}
147
148static VALUE
149modv(VALUE x, VALUE y)
150{
151 if (FIXNUM_P(y)) {
152 if (FIX2LONG(y) == 0) rb_num_zerodiv();
153 if (FIXNUM_P(x)) return rb_fix_mod_fix(x, y);
154 }
155 if (RB_BIGNUM_TYPE_P(x)) return rb_big_modulo(x, y);
156 return rb_funcall(x, '%', 1, y);
157}
158
159#define neg(x) (subv(INT2FIX(0), (x)))
160
161static VALUE
162quor(VALUE x, VALUE y)
163{
164 if (FIXNUM_P(x) && FIXNUM_P(y)) {
165 long a, b, c;
166 a = FIX2LONG(x);
167 b = FIX2LONG(y);
168 if (b == 0) rb_num_zerodiv();
169 if (a == FIXNUM_MIN && b == -1) return LONG2NUM(-a);
170 c = a / b;
171 if (c * b == a) {
172 return LONG2FIX(c);
173 }
174 }
175 return rb_numeric_quo(x, y);
176}
177
178static VALUE
179quov(VALUE x, VALUE y)
180{
181 VALUE ret = quor(x, y);
182 if (RB_TYPE_P(ret, T_RATIONAL) &&
183 RRATIONAL(ret)->den == INT2FIX(1)) {
184 ret = RRATIONAL(ret)->num;
185 }
186 return ret;
187}
188
189#define mulquov(x,y,z) (((y) == (z)) ? (x) : quov(mulv((x),(y)),(z)))
190
191static void
192divmodv(VALUE n, VALUE d, VALUE *q, VALUE *r)
193{
194 VALUE tmp, ary;
195 if (FIXNUM_P(d)) {
196 if (FIX2LONG(d) == 0) rb_num_zerodiv();
197 if (FIXNUM_P(n)) {
198 rb_fix_divmod_fix(n, d, q, r);
199 return;
200 }
201 }
202 tmp = rb_funcall(n, id_divmod, 1, d);
203 ary = rb_check_array_type(tmp);
204 if (NIL_P(ary)) {
205 rb_raise(rb_eTypeError, "unexpected divmod result: into %"PRIsVALUE,
206 rb_obj_class(tmp));
207 }
208 *q = rb_ary_entry(ary, 0);
209 *r = rb_ary_entry(ary, 1);
210}
211
212#if SIZEOF_LONG == 8
213# define INT64toNUM(x) LONG2NUM(x)
214#elif defined(HAVE_LONG_LONG) && SIZEOF_LONG_LONG == 8
215# define INT64toNUM(x) LL2NUM(x)
216#endif
217
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))
235#else
236 typedef unsigned long uwideint_t;
237 typedef long wideint_t;
238 typedef VALUE WIDEVALUE;
239 typedef SIGNED_VALUE SIGNED_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))
250#endif
251
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)
257
258/* #define STRUCT_WIDEVAL */
259#ifdef STRUCT_WIDEVAL
260 /* for type checking */
261 typedef struct {
262 WIDEVALUE value;
263 } wideval_t;
264 static inline wideval_t WIDEVAL_WRAP(WIDEVALUE v) { wideval_t w = { v }; return w; }
265# define WIDEVAL_GET(w) ((w).value)
266#else
267 typedef WIDEVALUE wideval_t;
268# define WIDEVAL_WRAP(v) (v)
269# define WIDEVAL_GET(w) (w)
270#endif
271
272#if WIDEVALUE_IS_WIDER
273 static inline wideval_t
274 wint2wv(wideint_t wi)
275 {
276 if (FIXWVABLE(wi))
277 return WINT2FIXWV(wi);
278 else
279 return WIDEVAL_WRAP(INT64toNUM(wi));
280 }
281# define WINT2WV(wi) wint2wv(wi)
282#else
283# define WINT2WV(wi) WIDEVAL_WRAP(LONG2NUM(wi))
284#endif
285
286static inline VALUE
287w2v(wideval_t w)
288{
289#if WIDEVALUE_IS_WIDER
290 if (FIXWV_P(w))
291 return INT64toNUM(FIXWV2WINT(w));
292 return (VALUE)WIDEVAL_GET(w);
293#else
294 return WIDEVAL_GET(w);
295#endif
296}
297
298#if WIDEVALUE_IS_WIDER
299static wideval_t
300v2w_bignum(VALUE v)
301{
302 int sign;
303 uwideint_t u;
304 sign = rb_integer_pack(v, &u, 1, sizeof(u), 0,
306 if (sign == 0)
307 return WINT2FIXWV(0);
308 else if (sign == -1) {
309 if (u <= -FIXWV_MIN)
310 return WINT2FIXWV(-(wideint_t)u);
311 }
312 else if (sign == +1) {
313 if (u <= FIXWV_MAX)
314 return WINT2FIXWV((wideint_t)u);
315 }
316 return WIDEVAL_WRAP(v);
317}
318#endif
319
320static inline wideval_t
321v2w(VALUE v)
322{
323 if (RB_TYPE_P(v, T_RATIONAL)) {
324 if (RRATIONAL(v)->den != LONG2FIX(1))
325 return WIDEVAL_WRAP(v);
326 v = RRATIONAL(v)->num;
327 }
328#if WIDEVALUE_IS_WIDER
329 if (FIXNUM_P(v)) {
330 return WIDEVAL_WRAP((WIDEVALUE)(SIGNED_WIDEVALUE)(long)v);
331 }
332 else if (RB_BIGNUM_TYPE_P(v) &&
333 rb_absint_size(v, NULL) <= sizeof(WIDEVALUE)) {
334 return v2w_bignum(v);
335 }
336#endif
337 return WIDEVAL_WRAP(v);
338}
339
340#define NUM2WV(v) v2w(rb_Integer(v))
341
342static int
343weq(wideval_t wx, wideval_t wy)
344{
345#if WIDEVALUE_IS_WIDER
346 if (FIXWV_P(wx) && FIXWV_P(wy)) {
347 return WIDEVAL_GET(wx) == WIDEVAL_GET(wy);
348 }
349 return RTEST(rb_funcall(w2v(wx), idEq, 1, w2v(wy)));
350#else
351 return eq(WIDEVAL_GET(wx), WIDEVAL_GET(wy));
352#endif
353}
354
355static int
356wcmp(wideval_t wx, wideval_t wy)
357{
358 VALUE x, y;
359#if WIDEVALUE_IS_WIDER
360 if (FIXWV_P(wx) && FIXWV_P(wy)) {
361 wideint_t a, b;
362 a = FIXWV2WINT(wx);
363 b = FIXWV2WINT(wy);
364 if (a < b)
365 return -1;
366 if (a > b)
367 return 1;
368 return 0;
369 }
370#endif
371 x = w2v(wx);
372 y = w2v(wy);
373 return cmp(x, y);
374}
375
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)
381
382static wideval_t
383wadd(wideval_t wx, wideval_t wy)
384{
385#if WIDEVALUE_IS_WIDER
386 if (FIXWV_P(wx) && FIXWV_P(wy)) {
387 wideint_t r = FIXWV2WINT(wx) + FIXWV2WINT(wy);
388 return WINT2WV(r);
389 }
390#endif
391 return v2w(addv(w2v(wx), w2v(wy)));
392}
393
394static wideval_t
395wsub(wideval_t wx, wideval_t wy)
396{
397#if WIDEVALUE_IS_WIDER
398 if (FIXWV_P(wx) && FIXWV_P(wy)) {
399 wideint_t r = FIXWV2WINT(wx) - FIXWV2WINT(wy);
400 return WINT2WV(r);
401 }
402#endif
403 return v2w(subv(w2v(wx), w2v(wy)));
404}
405
406static wideval_t
407wmul(wideval_t wx, wideval_t wy)
408{
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));
413 }
414#endif
415 return v2w(mulv(w2v(wx), w2v(wy)));
416}
417
418static wideval_t
419wquo(wideval_t wx, wideval_t wy)
420{
421#if WIDEVALUE_IS_WIDER
422 if (FIXWV_P(wx) && FIXWV_P(wy)) {
423 wideint_t a, b, c;
424 a = FIXWV2WINT(wx);
425 b = FIXWV2WINT(wy);
426 if (b == 0) rb_num_zerodiv();
427 c = a / b;
428 if (c * b == a) {
429 return WINT2WV(c);
430 }
431 }
432#endif
433 return v2w(quov(w2v(wx), w2v(wy)));
434}
435
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)))
438
439#if WIDEVALUE_IS_WIDER
440static int
441wdivmod0(wideval_t wn, wideval_t wd, wideval_t *wq, wideval_t *wr)
442{
443 if (FIXWV_P(wn) && FIXWV_P(wd)) {
444 wideint_t n, d, q, r;
445 d = FIXWV2WINT(wd);
446 if (d == 0) rb_num_zerodiv();
447 if (d == 1) {
448 *wq = wn;
449 *wr = WINT2FIXWV(0);
450 return 1;
451 }
452 if (d == -1) {
453 wideint_t xneg = -FIXWV2WINT(wn);
454 *wq = WINT2WV(xneg);
455 *wr = WINT2FIXWV(0);
456 return 1;
457 }
458 n = FIXWV2WINT(wn);
459 if (n == 0) {
460 *wq = WINT2FIXWV(0);
461 *wr = WINT2FIXWV(0);
462 return 1;
463 }
464 q = n / d;
465 r = n % d;
466 if (d > 0 ? r < 0 : r > 0) {
467 q -= 1;
468 r += d;
469 }
470 *wq = WINT2FIXWV(q);
471 *wr = WINT2FIXWV(r);
472 return 1;
473 }
474 return 0;
475}
476#endif
477
478static void
479wdivmod(wideval_t wn, wideval_t wd, wideval_t *wq, wideval_t *wr)
480{
481 VALUE vq, vr;
482#if WIDEVALUE_IS_WIDER
483 if (wdivmod0(wn, wd, wq, wr)) return;
484#endif
485 divmodv(w2v(wn), w2v(wd), &vq, &vr);
486 *wq = v2w(vq);
487 *wr = v2w(vr);
488}
489
490static void
491wmuldivmod(wideval_t wx, wideval_t wy, wideval_t wz, wideval_t *wq, wideval_t *wr)
492{
493 if (WIDEVAL_GET(wy) == WIDEVAL_GET(wz)) {
494 *wq = wx;
495 *wr = WINT2FIXWV(0);
496 return;
497 }
498 wdivmod(wmul(wx,wy), wz, wq, wr);
499}
500
501static wideval_t
502wdiv(wideval_t wx, wideval_t wy)
503{
504#if WIDEVALUE_IS_WIDER
505 wideval_t q, dmy;
506 if (wdivmod0(wx, wy, &q, &dmy)) return q;
507#endif
508 return v2w(divv(w2v(wx), w2v(wy)));
509}
510
511static wideval_t
512wmod(wideval_t wx, wideval_t wy)
513{
514#if WIDEVALUE_IS_WIDER
515 wideval_t r, dmy;
516 if (wdivmod0(wx, wy, &dmy, &r)) return r;
517#endif
518 return v2w(modv(w2v(wx), w2v(wy)));
519}
520
521static VALUE
522num_exact_check(VALUE v)
523{
524 VALUE tmp;
525
526 switch (TYPE(v)) {
527 case T_FIXNUM:
528 case T_BIGNUM:
529 tmp = v;
530 break;
531
532 case T_RATIONAL:
533 tmp = rb_rational_canonicalize(v);
534 break;
535
536 default:
537 if (!UNDEF_P(tmp = rb_check_funcall(v, idTo_r, 0, NULL))) {
538 /* test to_int method availability to reject non-Numeric
539 * objects such as String, Time, etc which have to_r method. */
540 if (!rb_respond_to(v, idTo_int)) {
541 /* FALLTHROUGH */
542 }
543 else if (RB_INTEGER_TYPE_P(tmp)) {
544 break;
545 }
546 else if (RB_TYPE_P(tmp, T_RATIONAL)) {
547 tmp = rb_rational_canonicalize(tmp);
548 break;
549 }
550 }
551 else if (!NIL_P(tmp = rb_check_to_int(v))) {
552 return tmp;
553 }
554
555 case T_NIL:
556 case T_STRING:
557 return Qnil;
558 }
559 ASSUME(!NIL_P(tmp));
560 return tmp;
561}
562
563NORETURN(static void num_exact_fail(VALUE v));
564static void
565num_exact_fail(VALUE v)
566{
567 rb_raise(rb_eTypeError, "can't convert %"PRIsVALUE" into an exact number",
568 rb_obj_class(v));
569}
570
571static VALUE
572num_exact(VALUE v)
573{
574 VALUE num = num_exact_check(v);
575 if (NIL_P(num)) num_exact_fail(v);
576 return num;
577}
578
579/* time_t */
580
581/* TIME_SCALE should be 10000... */
582static const int TIME_SCALE_NUMDIGITS = rb_strlen_lit(STRINGIZE(TIME_SCALE)) - 1;
583
584static wideval_t
585rb_time_magnify(wideval_t w)
586{
587 return wmul(w, WINT2FIXWV(TIME_SCALE));
588}
589
590static VALUE
591rb_time_unmagnify_to_rational(wideval_t w)
592{
593 return quor(w2v(w), INT2FIX(TIME_SCALE));
594}
595
596static wideval_t
597rb_time_unmagnify(wideval_t w)
598{
599 return v2w(rb_time_unmagnify_to_rational(w));
600}
601
602static VALUE
603rb_time_unmagnify_to_float(wideval_t w)
604{
605 VALUE v;
606#if WIDEVALUE_IS_WIDER
607 if (FIXWV_P(w)) {
608 wideint_t a, b, c;
609 a = FIXWV2WINT(w);
610 b = TIME_SCALE;
611 c = a / b;
612 if (c * b == a) {
613 return DBL2NUM((double)c);
614 }
615 v = DBL2NUM((double)FIXWV2WINT(w));
616 return quov(v, DBL2NUM(TIME_SCALE));
617 }
618#endif
619 v = w2v(w);
620 if (RB_TYPE_P(v, T_RATIONAL))
621 return rb_Float(quov(v, INT2FIX(TIME_SCALE)));
622 else
623 return quov(v, DBL2NUM(TIME_SCALE));
624}
625
626static void
627split_second(wideval_t timew, wideval_t *timew_p, VALUE *subsecx_p)
628{
629 wideval_t q, r;
630 wdivmod(timew, WINT2FIXWV(TIME_SCALE), &q, &r);
631 *timew_p = q;
632 *subsecx_p = w2v(r);
633}
634
635static wideval_t
636timet2wv(time_t t)
637{
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);
643 }
644 }
645 else {
646 wideint_t wi = (wideint_t)t;
647 if (FIXWV_MIN <= wi && wi <= FIXWV_MAX) {
648 return WINT2FIXWV(wi);
649 }
650 }
651#endif
652 return v2w(TIMET2NUM(t));
653}
654#define TIMET2WV(t) timet2wv(t)
655
656static time_t
657wv2timet(wideval_t w)
658{
659#if WIDEVALUE_IS_WIDER
660 if (FIXWV_P(w)) {
661 wideint_t wi = FIXWV2WINT(w);
662 if (TIMET_MIN == 0) {
663 if (wi < 0)
664 rb_raise(rb_eRangeError, "negative value to convert into 'time_t'");
665 if (TIMET_MAX < (uwideint_t)wi)
666 rb_raise(rb_eRangeError, "too big to convert into 'time_t'");
667 }
668 else {
669 if (wi < TIMET_MIN || TIMET_MAX < wi)
670 rb_raise(rb_eRangeError, "too big to convert into 'time_t'");
671 }
672 return (time_t)wi;
673 }
674#endif
675 return NUM2TIMET(w2v(w));
676}
677#define WV2TIMET(t) wv2timet(t)
678
680static VALUE rb_cTimeTM;
681
682static int obj2int(VALUE obj);
683static uint32_t obj2ubits(VALUE obj, unsigned int bits);
684static VALUE obj2vint(VALUE obj);
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);
691
692static VALUE time_gmtime(VALUE);
693static VALUE time_localtime(VALUE);
694static VALUE time_fixoff(VALUE);
695static VALUE time_zonelocal(VALUE time, VALUE off);
696
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);
701
702static struct vtm *localtimew(wideval_t timew, struct vtm *result);
703
704static int leap_year_p(long y);
705#define leap_year_v_p(y) leap_year_p(NUM2LONG(modv((y), INT2FIX(400))))
706
707static VALUE tm_from_time(VALUE klass, VALUE time);
708
709bool ruby_tz_uptodate_p;
710
711#ifdef _WIN32
712enum {tzkey_max = numberof(((DYNAMIC_TIME_ZONE_INFORMATION *)NULL)->TimeZoneKeyName)};
713static struct {
714 char use_tzkey;
715 char name[tzkey_max * 4 + 1];
716} w32_tz;
717
718static char *
719get_tzname(int dst)
720{
721 if (w32_tz.use_tzkey) {
722 if (w32_tz.name[0]) {
723 return w32_tz.name;
724 }
725 else {
726 /*
727 * Use GetDynamicTimeZoneInformation::TimeZoneKeyName, Windows
728 * time zone ID, which is not localized because it is the key
729 * for "Dynamic DST" keys under the "Time Zones" registry.
730 * Available since Windows Vista and Windows Server 2008.
731 */
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,
739 NULL, NULL);
740 w32_tz.name[clen] = '\0';
741 return w32_tz.name;
742 }
743 }
744 }
745 return _tzname[_daylight && dst];
746}
747#endif
748
749void
750ruby_reset_timezone(const char *val)
751{
752 ruby_tz_uptodate_p = false;
753#ifdef _WIN32
754 w32_tz.use_tzkey = !val || !*val;
755#endif
756 ruby_reset_leap_second_info();
757}
758
759static void
760update_tz(void)
761{
762 if (ruby_tz_uptodate_p) return;
763 ruby_tz_uptodate_p = true;
764 tzset();
765}
766
767static struct tm *
768rb_localtime_r(const time_t *t, struct tm *result)
769{
770#if defined __APPLE__ && defined __LP64__
771 if (*t != (time_t)(int)*t) return NULL;
772#endif
773 update_tz();
774#ifdef HAVE_GMTIME_R
775 result = localtime_r(t, result);
776#else
777 {
778 struct tm *tmp = localtime(t);
779 if (tmp) *result = *tmp;
780 }
781#endif
782#if defined(HAVE_MKTIME) && defined(LOCALTIME_OVERFLOW_PROBLEM)
783 if (result) {
784 long gmtoff1 = 0;
785 long gmtoff2 = 0;
786 struct tm tmp = *result;
787 time_t t2;
788 t2 = mktime(&tmp);
789# if defined(HAVE_STRUCT_TM_TM_GMTOFF)
790 gmtoff1 = result->tm_gmtoff;
791 gmtoff2 = tmp.tm_gmtoff;
792# endif
793 if (*t + gmtoff1 != t2 + gmtoff2)
794 result = NULL;
795 }
796#endif
797 return result;
798}
799#define LOCALTIME(tm, result) rb_localtime_r((tm), &(result))
800
801#ifndef HAVE_STRUCT_TM_TM_GMTOFF
802static struct tm *
803rb_gmtime_r(const time_t *t, struct tm *result)
804{
805#ifdef HAVE_GMTIME_R
806 result = gmtime_r(t, result);
807#else
808 struct tm *tmp = gmtime(t);
809 if (tmp) *result = *tmp;
810#endif
811#if defined(HAVE_TIMEGM) && defined(LOCALTIME_OVERFLOW_PROBLEM)
812 if (result && *t != timegm(result)) {
813 return NULL;
814 }
815#endif
816 return result;
817}
818# define GMTIME(tm, result) rb_gmtime_r((tm), &(result))
819#endif
820
821static const int16_t common_year_yday_offset[] = {
822 -1,
823 -1 + 31,
824 -1 + 31 + 28,
825 -1 + 31 + 28 + 31,
826 -1 + 31 + 28 + 31 + 30,
827 -1 + 31 + 28 + 31 + 30 + 31,
828 -1 + 31 + 28 + 31 + 30 + 31 + 30,
829 -1 + 31 + 28 + 31 + 30 + 31 + 30 + 31,
830 -1 + 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31,
831 -1 + 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30,
832 -1 + 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31,
833 -1 + 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30
834 /* 1 2 3 4 5 6 7 8 9 10 11 */
835};
836static const int16_t leap_year_yday_offset[] = {
837 -1,
838 -1 + 31,
839 -1 + 31 + 29,
840 -1 + 31 + 29 + 31,
841 -1 + 31 + 29 + 31 + 30,
842 -1 + 31 + 29 + 31 + 30 + 31,
843 -1 + 31 + 29 + 31 + 30 + 31 + 30,
844 -1 + 31 + 29 + 31 + 30 + 31 + 30 + 31,
845 -1 + 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31,
846 -1 + 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30,
847 -1 + 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31,
848 -1 + 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30
849 /* 1 2 3 4 5 6 7 8 9 10 11 */
850};
851
852static const int8_t common_year_days_in_month[] = {
853 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
854};
855static const int8_t leap_year_days_in_month[] = {
856 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
857};
858
859#define days_in_month_of(leap) ((leap) ? leap_year_days_in_month : common_year_days_in_month)
860#define days_in_month_in(y) days_in_month_of(leap_year_p(y))
861#define days_in_month_in_v(y) days_in_month_of(leap_year_v_p(y))
862
863#define M28(m) \
864 (m),(m),(m),(m),(m),(m),(m),(m),(m),(m), \
865 (m),(m),(m),(m),(m),(m),(m),(m),(m),(m), \
866 (m),(m),(m),(m),(m),(m),(m),(m)
867#define M29(m) \
868 (m),(m),(m),(m),(m),(m),(m),(m),(m),(m), \
869 (m),(m),(m),(m),(m),(m),(m),(m),(m),(m), \
870 (m),(m),(m),(m),(m),(m),(m),(m),(m)
871#define M30(m) \
872 (m),(m),(m),(m),(m),(m),(m),(m),(m),(m), \
873 (m),(m),(m),(m),(m),(m),(m),(m),(m),(m), \
874 (m),(m),(m),(m),(m),(m),(m),(m),(m),(m)
875#define M31(m) \
876 (m),(m),(m),(m),(m),(m),(m),(m),(m),(m), \
877 (m),(m),(m),(m),(m),(m),(m),(m),(m),(m), \
878 (m),(m),(m),(m),(m),(m),(m),(m),(m),(m), (m)
879
880static const uint8_t common_year_mon_of_yday[] = {
881 M31(1), M28(2), M31(3), M30(4), M31(5), M30(6),
882 M31(7), M31(8), M30(9), M31(10), M30(11), M31(12)
883};
884static const uint8_t leap_year_mon_of_yday[] = {
885 M31(1), M29(2), M31(3), M30(4), M31(5), M30(6),
886 M31(7), M31(8), M30(9), M31(10), M30(11), M31(12)
887};
888
889#undef M28
890#undef M29
891#undef M30
892#undef M31
893
894#define D28 \
895 1,2,3,4,5,6,7,8,9, \
896 10,11,12,13,14,15,16,17,18,19, \
897 20,21,22,23,24,25,26,27,28
898#define D29 \
899 1,2,3,4,5,6,7,8,9, \
900 10,11,12,13,14,15,16,17,18,19, \
901 20,21,22,23,24,25,26,27,28,29
902#define D30 \
903 1,2,3,4,5,6,7,8,9, \
904 10,11,12,13,14,15,16,17,18,19, \
905 20,21,22,23,24,25,26,27,28,29,30
906#define D31 \
907 1,2,3,4,5,6,7,8,9, \
908 10,11,12,13,14,15,16,17,18,19, \
909 20,21,22,23,24,25,26,27,28,29,30,31
910
911static const uint8_t common_year_mday_of_yday[] = {
912 /* 1 2 3 4 5 6 7 8 9 10 11 12 */
913 D31, D28, D31, D30, D31, D30, D31, D31, D30, D31, D30, D31
914};
915static const uint8_t leap_year_mday_of_yday[] = {
916 D31, D29, D31, D30, D31, D30, D31, D31, D30, D31, D30, D31
917};
918
919#undef D28
920#undef D29
921#undef D30
922#undef D31
923
924static int
925calc_tm_yday(long tm_year, int tm_mon, int tm_mday)
926{
927 int tm_year_mod400 = (int)MOD(tm_year, 400);
928 int tm_yday = tm_mday;
929
930 if (leap_year_p(tm_year_mod400 + 1900))
931 tm_yday += leap_year_yday_offset[tm_mon];
932 else
933 tm_yday += common_year_yday_offset[tm_mon];
934
935 return tm_yday;
936}
937
938static wideval_t
939timegmw_noleapsecond(struct vtm *vtm)
940{
941 VALUE year1900;
942 VALUE q400, r400;
943 int year_mod400;
944 int yday;
945 long days_in400;
946 VALUE vdays, ret;
947 wideval_t wret;
948
949 year1900 = subv(vtm->year, INT2FIX(1900));
950
951 divmodv(year1900, INT2FIX(400), &q400, &r400);
952 year_mod400 = NUM2INT(r400);
953
954 yday = calc_tm_yday(year_mod400, vtm->mon-1, vtm->mday);
955
956 /*
957 * `Seconds Since the Epoch' in SUSv3:
958 * tm_sec + tm_min*60 + tm_hour*3600 + tm_yday*86400 +
959 * (tm_year-70)*31536000 + ((tm_year-69)/4)*86400 -
960 * ((tm_year-1)/100)*86400 + ((tm_year+299)/400)*86400
961 */
962 ret = LONG2NUM(vtm->sec
963 + vtm->min*60
964 + vtm->hour*3600);
965 days_in400 = yday
966 - 70*365
967 + DIV(year_mod400 - 69, 4)
968 - DIV(year_mod400 - 1, 100)
969 + (year_mod400 + 299) / 400;
970 vdays = LONG2NUM(days_in400);
971 vdays = addv(vdays, mulv(q400, INT2FIX(97)));
972 vdays = addv(vdays, mulv(year1900, INT2FIX(365)));
973 wret = wadd(rb_time_magnify(v2w(ret)), wmul(rb_time_magnify(v2w(vdays)), WINT2FIXWV(86400)));
974 wret = wadd(wret, v2w(vtm->subsecx));
975
976 return wret;
977}
978
979static VALUE
980zone_str(const char *zone)
981{
982 const char *p;
983 int ascii_only = 1;
984 VALUE str;
985 size_t len;
986
987 if (zone == NULL) {
988 return rb_fstring_lit("(NO-TIMEZONE-ABBREVIATION)");
989 }
990
991 for (p = zone; *p; p++) {
992 if (!ISASCII(*p)) {
993 ascii_only = 0;
994 p += strlen(p);
995 break;
996 }
997 }
998 len = p - zone;
999 if (ascii_only) {
1000 str = rb_usascii_str_new(zone, len);
1001 }
1002 else {
1003#ifdef _WIN32
1004 str = rb_utf8_str_new(zone, len);
1005 /* until we move to UTF-8 on Windows completely */
1006 str = rb_str_export_locale(str);
1007#else
1008 str = rb_enc_str_new(zone, len, rb_locale_encoding());
1009#endif
1010 }
1011 return rb_fstring(str);
1012}
1013
1014static void
1015gmtimew_noleapsecond(wideval_t timew, struct vtm *vtm)
1016{
1017 VALUE v;
1018 int n, x, y;
1019 int wday;
1020 VALUE timev;
1021 wideval_t timew2, w, w2;
1022 VALUE subsecx;
1023
1024 vtm->isdst = 0;
1025
1026 split_second(timew, &timew2, &subsecx);
1027 vtm->subsecx = subsecx;
1028
1029 wdivmod(timew2, WINT2FIXWV(86400), &w2, &w);
1030 timev = w2v(w2);
1031 v = w2v(w);
1032
1033 wday = NUM2INT(modv(timev, INT2FIX(7)));
1034 vtm->wday = (wday + 4) % 7;
1035
1036 n = NUM2INT(v);
1037 vtm->sec = n % 60; n = n / 60;
1038 vtm->min = n % 60; n = n / 60;
1039 vtm->hour = n;
1040
1041 /* 97 leap days in the 400 year cycle */
1042 divmodv(timev, INT2FIX(400*365 + 97), &timev, &v);
1043 vtm->year = mulv(timev, INT2FIX(400));
1044
1045 /* n is the days in the 400 year cycle.
1046 * the start of the cycle is 1970-01-01. */
1047
1048 n = NUM2INT(v);
1049 y = 1970;
1050
1051 /* 30 years including 7 leap days (1972, 1976, ... 1996),
1052 * 31 days in January 2000 and
1053 * 29 days in February 2000
1054 * from 1970-01-01 to 2000-02-29 */
1055 if (30*365+7+31+29-1 <= n) {
1056 /* 2000-02-29 or after */
1057 if (n < 31*365+8) {
1058 /* 2000-02-29 to 2000-12-31 */
1059 y += 30;
1060 n -= 30*365+7;
1061 goto found;
1062 }
1063 else {
1064 /* 2001-01-01 or after */
1065 n -= 1;
1066 }
1067 }
1068
1069 x = n / (365*100 + 24);
1070 n = n % (365*100 + 24);
1071 y += x * 100;
1072 if (30*365+7+31+29-1 <= n) {
1073 if (n < 31*365+7) {
1074 y += 30;
1075 n -= 30*365+7;
1076 goto found;
1077 }
1078 else
1079 n += 1;
1080 }
1081
1082 x = n / (365*4 + 1);
1083 n = n % (365*4 + 1);
1084 y += x * 4;
1085 if (365*2+31+29-1 <= n) {
1086 if (n < 365*2+366) {
1087 y += 2;
1088 n -= 365*2;
1089 goto found;
1090 }
1091 else
1092 n -= 1;
1093 }
1094
1095 x = n / 365;
1096 n = n % 365;
1097 y += x;
1098
1099 found:
1100 vtm->yday = n+1;
1101 vtm->year = addv(vtm->year, INT2NUM(y));
1102
1103 if (leap_year_p(y)) {
1104 vtm->mon = leap_year_mon_of_yday[n];
1105 vtm->mday = leap_year_mday_of_yday[n];
1106 }
1107 else {
1108 vtm->mon = common_year_mon_of_yday[n];
1109 vtm->mday = common_year_mday_of_yday[n];
1110 }
1111
1112 vtm->utc_offset = INT2FIX(0);
1113 vtm->zone = str_utc;
1114}
1115
1116static struct tm *
1117gmtime_with_leapsecond(const time_t *timep, struct tm *result)
1118{
1119#if defined(HAVE_STRUCT_TM_TM_GMTOFF)
1120 /* 4.4BSD counts leap seconds only with localtime, not with gmtime. */
1121 struct tm *t;
1122 int sign;
1123 int gmtoff_sec, gmtoff_min, gmtoff_hour, gmtoff_day;
1124 long gmtoff;
1125 t = LOCALTIME(timep, *result);
1126 if (t == NULL)
1127 return NULL;
1128
1129 /* subtract gmtoff */
1130 if (t->tm_gmtoff < 0) {
1131 sign = 1;
1132 gmtoff = -t->tm_gmtoff;
1133 }
1134 else {
1135 sign = -1;
1136 gmtoff = t->tm_gmtoff;
1137 }
1138 gmtoff_sec = (int)(gmtoff % 60);
1139 gmtoff = gmtoff / 60;
1140 gmtoff_min = (int)(gmtoff % 60);
1141 gmtoff = gmtoff / 60;
1142 gmtoff_hour = (int)gmtoff; /* <= 12 */
1143
1144 gmtoff_sec *= sign;
1145 gmtoff_min *= sign;
1146 gmtoff_hour *= sign;
1147
1148 gmtoff_day = 0;
1149
1150 if (gmtoff_sec) {
1151 /* If gmtoff_sec == 0, don't change result->tm_sec.
1152 * It may be 60 which is a leap second. */
1153 result->tm_sec += gmtoff_sec;
1154 if (result->tm_sec < 0) {
1155 result->tm_sec += 60;
1156 gmtoff_min -= 1;
1157 }
1158 if (60 <= result->tm_sec) {
1159 result->tm_sec -= 60;
1160 gmtoff_min += 1;
1161 }
1162 }
1163 if (gmtoff_min) {
1164 result->tm_min += gmtoff_min;
1165 if (result->tm_min < 0) {
1166 result->tm_min += 60;
1167 gmtoff_hour -= 1;
1168 }
1169 if (60 <= result->tm_min) {
1170 result->tm_min -= 60;
1171 gmtoff_hour += 1;
1172 }
1173 }
1174 if (gmtoff_hour) {
1175 result->tm_hour += gmtoff_hour;
1176 if (result->tm_hour < 0) {
1177 result->tm_hour += 24;
1178 gmtoff_day = -1;
1179 }
1180 if (24 <= result->tm_hour) {
1181 result->tm_hour -= 24;
1182 gmtoff_day = 1;
1183 }
1184 }
1185
1186 if (gmtoff_day) {
1187 if (gmtoff_day < 0) {
1188 if (result->tm_yday == 0) {
1189 result->tm_mday = 31;
1190 result->tm_mon = 11; /* December */
1191 result->tm_year--;
1192 result->tm_yday = leap_year_p(result->tm_year + 1900) ? 365 : 364;
1193 }
1194 else if (result->tm_mday == 1) {
1195 const int8_t *days_in_month = days_in_month_in(result->tm_year + 1900);
1196 result->tm_mon--;
1197 result->tm_mday = days_in_month[result->tm_mon];
1198 result->tm_yday--;
1199 }
1200 else {
1201 result->tm_mday--;
1202 result->tm_yday--;
1203 }
1204 result->tm_wday = (result->tm_wday + 6) % 7;
1205 }
1206 else {
1207 int leap = leap_year_p(result->tm_year + 1900);
1208 if (result->tm_yday == (leap ? 365 : 364)) {
1209 result->tm_year++;
1210 result->tm_mon = 0; /* January */
1211 result->tm_mday = 1;
1212 result->tm_yday = 0;
1213 }
1214 else if (result->tm_mday == days_in_month_of(leap)[result->tm_mon]) {
1215 result->tm_mon++;
1216 result->tm_mday = 1;
1217 result->tm_yday++;
1218 }
1219 else {
1220 result->tm_mday++;
1221 result->tm_yday++;
1222 }
1223 result->tm_wday = (result->tm_wday + 1) % 7;
1224 }
1225 }
1226 result->tm_isdst = 0;
1227 result->tm_gmtoff = 0;
1228#if defined(HAVE_TM_ZONE)
1229 result->tm_zone = (char *)"UTC";
1230#endif
1231 return result;
1232#else
1233 return GMTIME(timep, *result);
1234#endif
1235}
1236
1237static long this_year = 0;
1238static time_t known_leap_seconds_limit;
1239static int number_of_leap_seconds_known;
1240
1241static void
1242init_leap_second_info(void)
1243{
1244 /*
1245 * leap seconds are determined by IERS.
1246 * It is announced 6 months before the leap second.
1247 * So no one knows leap seconds in the future after the next year.
1248 */
1249 if (this_year == 0) {
1250 time_t now;
1251 struct tm *tm, result;
1252 struct vtm vtm;
1253 wideval_t timew;
1254 now = time(NULL);
1255#ifdef HAVE_GMTIME_R
1256 gmtime_r(&now, &result);
1257#else
1258 gmtime(&now);
1259#endif
1260 tm = gmtime_with_leapsecond(&now, &result);
1261 if (!tm) return;
1262 this_year = tm->tm_year;
1263
1264 if (TIMET_MAX - now < (time_t)(366*86400))
1265 known_leap_seconds_limit = TIMET_MAX;
1266 else
1267 known_leap_seconds_limit = now + (time_t)(366*86400);
1268
1269 if (!gmtime_with_leapsecond(&known_leap_seconds_limit, &result))
1270 return;
1271
1272 vtm.year = LONG2NUM(result.tm_year + 1900);
1273 vtm.mon = result.tm_mon + 1;
1274 vtm.mday = result.tm_mday;
1275 vtm.hour = result.tm_hour;
1276 vtm.min = result.tm_min;
1277 vtm.sec = result.tm_sec;
1278 vtm.subsecx = INT2FIX(0);
1279 vtm.utc_offset = INT2FIX(0);
1280
1281 timew = timegmw_noleapsecond(&vtm);
1282
1283 number_of_leap_seconds_known = NUM2INT(w2v(wsub(TIMET2WV(known_leap_seconds_limit), rb_time_unmagnify(timew))));
1284 }
1285}
1286
1287/* Use this if you want to re-run init_leap_second_info() */
1288void
1289ruby_reset_leap_second_info(void)
1290{
1291 this_year = 0;
1292}
1293
1294static wideval_t
1295timegmw(struct vtm *vtm)
1296{
1297 wideval_t timew;
1298 struct tm tm;
1299 time_t t;
1300 const char *errmsg;
1301
1302 /* The first leap second is 1972-06-30 23:59:60 UTC.
1303 * No leap seconds before. */
1304 if (gt(INT2FIX(1972), vtm->year))
1305 return timegmw_noleapsecond(vtm);
1306
1307 init_leap_second_info();
1308
1309 timew = timegmw_noleapsecond(vtm);
1310
1311
1312 if (number_of_leap_seconds_known == 0) {
1313 /* When init_leap_second_info() is executed, the timezone doesn't have
1314 * leap second information. Disable leap second for calculating gmtime.
1315 */
1316 return timew;
1317 }
1318 else if (wlt(rb_time_magnify(TIMET2WV(known_leap_seconds_limit)), timew)) {
1319 return wadd(timew, rb_time_magnify(WINT2WV(number_of_leap_seconds_known)));
1320 }
1321
1322 tm.tm_year = rb_long2int(NUM2LONG(vtm->year) - 1900);
1323 tm.tm_mon = vtm->mon - 1;
1324 tm.tm_mday = vtm->mday;
1325 tm.tm_hour = vtm->hour;
1326 tm.tm_min = vtm->min;
1327 tm.tm_sec = vtm->sec;
1328 tm.tm_isdst = 0;
1329
1330 errmsg = find_time_t(&tm, 1, &t);
1331 if (errmsg)
1332 rb_raise(rb_eArgError, "%s", errmsg);
1333 return wadd(rb_time_magnify(TIMET2WV(t)), v2w(vtm->subsecx));
1334}
1335
1336static struct vtm *
1337gmtimew(wideval_t timew, struct vtm *result)
1338{
1339 time_t t;
1340 struct tm tm;
1341 VALUE subsecx;
1342 wideval_t timew2;
1343
1344 if (wlt(timew, WINT2FIXWV(0))) {
1345 gmtimew_noleapsecond(timew, result);
1346 return result;
1347 }
1348
1349 init_leap_second_info();
1350
1351 if (number_of_leap_seconds_known == 0) {
1352 /* When init_leap_second_info() is executed, the timezone doesn't have
1353 * leap second information. Disable leap second for calculating gmtime.
1354 */
1355 gmtimew_noleapsecond(timew, result);
1356 return result;
1357 }
1358 else if (wlt(rb_time_magnify(TIMET2WV(known_leap_seconds_limit)), timew)) {
1359 timew = wsub(timew, rb_time_magnify(WINT2WV(number_of_leap_seconds_known)));
1360 gmtimew_noleapsecond(timew, result);
1361 return result;
1362 }
1363
1364 split_second(timew, &timew2, &subsecx);
1365
1366 t = WV2TIMET(timew2);
1367 if (!gmtime_with_leapsecond(&t, &tm))
1368 return NULL;
1369
1370 result->year = LONG2NUM((long)tm.tm_year + 1900);
1371 result->mon = tm.tm_mon + 1;
1372 result->mday = tm.tm_mday;
1373 result->hour = tm.tm_hour;
1374 result->min = tm.tm_min;
1375 result->sec = tm.tm_sec;
1376 result->subsecx = subsecx;
1377 result->utc_offset = INT2FIX(0);
1378 result->wday = tm.tm_wday;
1379 result->yday = tm.tm_yday+1;
1380 result->isdst = tm.tm_isdst;
1381
1382 return result;
1383}
1384
1385#define GMTIMEW(w, v) \
1386 (gmtimew(w, v) ? (void)0 : rb_raise(rb_eArgError, "gmtime error"))
1387
1388static struct tm *localtime_with_gmtoff_zone(const time_t *t, struct tm *result, long *gmtoff, VALUE *zone);
1389
1390/*
1391 * The idea, extrapolate localtime() function, is borrowed from Perl:
1392 * http://web.archive.org/web/20080211114141/http://use.perl.org/articles/08/02/07/197204.shtml
1393 *
1394 * compat_common_month_table is generated by the following program.
1395 * This table finds the last month which starts at the same day of a week.
1396 * The year 2037 is not used because:
1397 * https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=522949
1398 *
1399 * #!/usr/bin/ruby
1400 *
1401 * require 'date'
1402 *
1403 * h = {}
1404 * 2036.downto(2010) {|y|
1405 * 1.upto(12) {|m|
1406 * next if m == 2 && y % 4 == 0
1407 * d = Date.new(y,m,1)
1408 * h[m] ||= {}
1409 * h[m][d.wday] ||= y
1410 * }
1411 * }
1412 *
1413 * 1.upto(12) {|m|
1414 * print "{"
1415 * 0.upto(6) {|w|
1416 * y = h[m][w]
1417 * print " #{y},"
1418 * }
1419 * puts "},"
1420 * }
1421 *
1422 */
1423static const int compat_common_month_table[12][7] = {
1424 /* Sun Mon Tue Wed Thu Fri Sat */
1425 { 2034, 2035, 2036, 2031, 2032, 2027, 2033 }, /* January */
1426 { 2026, 2027, 2033, 2034, 2035, 2030, 2031 }, /* February */
1427 { 2026, 2032, 2033, 2034, 2035, 2030, 2036 }, /* March */
1428 { 2035, 2030, 2036, 2026, 2032, 2033, 2034 }, /* April */
1429 { 2033, 2034, 2035, 2030, 2036, 2026, 2032 }, /* May */
1430 { 2036, 2026, 2032, 2033, 2034, 2035, 2030 }, /* June */
1431 { 2035, 2030, 2036, 2026, 2032, 2033, 2034 }, /* July */
1432 { 2032, 2033, 2034, 2035, 2030, 2036, 2026 }, /* August */
1433 { 2030, 2036, 2026, 2032, 2033, 2034, 2035 }, /* September */
1434 { 2034, 2035, 2030, 2036, 2026, 2032, 2033 }, /* October */
1435 { 2026, 2032, 2033, 2034, 2035, 2030, 2036 }, /* November */
1436 { 2030, 2036, 2026, 2032, 2033, 2034, 2035 }, /* December */
1437};
1438
1439/*
1440 * compat_leap_month_table is generated by following program.
1441 *
1442 * #!/usr/bin/ruby
1443 *
1444 * require 'date'
1445 *
1446 * h = {}
1447 * 2037.downto(2010) {|y|
1448 * 1.upto(12) {|m|
1449 * next unless m == 2 && y % 4 == 0
1450 * d = Date.new(y,m,1)
1451 * h[m] ||= {}
1452 * h[m][d.wday] ||= y
1453 * }
1454 * }
1455 *
1456 * 2.upto(2) {|m|
1457 * 0.upto(6) {|w|
1458 * y = h[m][w]
1459 * print " #{y},"
1460 * }
1461 * puts
1462 * }
1463 */
1464static const int compat_leap_month_table[7] = {
1465/* Sun Mon Tue Wed Thu Fri Sat */
1466 2032, 2016, 2028, 2012, 2024, 2036, 2020, /* February */
1467};
1468
1469static int
1470calc_wday(int year_mod400, int month, int day)
1471{
1472 int a, y, m;
1473 int wday;
1474
1475 a = (14 - month) / 12;
1476 y = year_mod400 + 4800 - a;
1477 m = month + 12 * a - 3;
1478 wday = day + (153*m+2)/5 + 365*y + y/4 - y/100 + y/400 + 2;
1479 wday = wday % 7;
1480 return wday;
1481}
1482
1483static VALUE
1484guess_local_offset(struct vtm *vtm_utc, int *isdst_ret, VALUE *zone_ret)
1485{
1486 struct tm tm;
1487 long gmtoff;
1488 VALUE zone;
1489 time_t t;
1490 struct vtm vtm2;
1491 VALUE timev;
1492 int year_mod400, wday;
1493
1494 /* Daylight Saving Time was introduced in 1916.
1495 * So we don't need to care about DST before that. */
1496 if (lt(vtm_utc->year, INT2FIX(1916))) {
1497 VALUE off = INT2FIX(0);
1498 int isdst = 0;
1499 zone = str_utc;
1500
1501# if defined(NEGATIVE_TIME_T)
1502# if SIZEOF_TIME_T <= 4
1503 /* 1901-12-13 20:45:52 UTC : The oldest time in 32-bit signed time_t. */
1504# define THE_TIME_OLD_ENOUGH ((time_t)0x80000000)
1505# else
1506 /* Since the Royal Greenwich Observatory was commissioned in 1675,
1507 no timezone defined using GMT at 1600. */
1508# define THE_TIME_OLD_ENOUGH ((time_t)(1600-1970)*366*24*60*60)
1509# endif
1510 if (localtime_with_gmtoff_zone((t = THE_TIME_OLD_ENOUGH, &t), &tm, &gmtoff, &zone)) {
1511 off = LONG2FIX(gmtoff);
1512 isdst = tm.tm_isdst;
1513 }
1514 else
1515# endif
1516 /* 1970-01-01 00:00:00 UTC : The Unix epoch - the oldest time in portable time_t. */
1517 if (localtime_with_gmtoff_zone((t = 0, &t), &tm, &gmtoff, &zone)) {
1518 off = LONG2FIX(gmtoff);
1519 isdst = tm.tm_isdst;
1520 }
1521
1522 if (isdst_ret)
1523 *isdst_ret = isdst;
1524 if (zone_ret)
1525 *zone_ret = zone;
1526 return off;
1527 }
1528
1529 /* It is difficult to guess the future. */
1530
1531 vtm2 = *vtm_utc;
1532
1533 /* guess using a year before 2038. */
1534 year_mod400 = NUM2INT(modv(vtm_utc->year, INT2FIX(400)));
1535 wday = calc_wday(year_mod400, vtm_utc->mon, 1);
1536 if (vtm_utc->mon == 2 && leap_year_p(year_mod400))
1537 vtm2.year = INT2FIX(compat_leap_month_table[wday]);
1538 else
1539 vtm2.year = INT2FIX(compat_common_month_table[vtm_utc->mon-1][wday]);
1540
1541 timev = w2v(rb_time_unmagnify(timegmw(&vtm2)));
1542 t = NUM2TIMET(timev);
1543 zone = str_utc;
1544 if (localtime_with_gmtoff_zone(&t, &tm, &gmtoff, &zone)) {
1545 if (isdst_ret)
1546 *isdst_ret = tm.tm_isdst;
1547 if (zone_ret)
1548 *zone_ret = zone;
1549 return LONG2FIX(gmtoff);
1550 }
1551
1552 {
1553 /* Use the current time offset as a last resort. */
1554 static time_t now = 0;
1555 static long now_gmtoff = 0;
1556 static int now_isdst = 0;
1557 static VALUE now_zone;
1558 if (now == 0) {
1559 VALUE zone;
1560 now = time(NULL);
1561 localtime_with_gmtoff_zone(&now, &tm, &now_gmtoff, &zone);
1562 now_isdst = tm.tm_isdst;
1563 zone = rb_fstring(zone);
1564 rb_vm_register_global_object(zone);
1565 now_zone = zone;
1566 }
1567 if (isdst_ret)
1568 *isdst_ret = now_isdst;
1569 if (zone_ret)
1570 *zone_ret = now_zone;
1571 return LONG2FIX(now_gmtoff);
1572 }
1573}
1574
1575static VALUE
1576small_vtm_sub(struct vtm *vtm1, struct vtm *vtm2)
1577{
1578 int off;
1579
1580 off = vtm1->sec - vtm2->sec;
1581 off += (vtm1->min - vtm2->min) * 60;
1582 off += (vtm1->hour - vtm2->hour) * 3600;
1583 if (ne(vtm1->year, vtm2->year))
1584 off += lt(vtm1->year, vtm2->year) ? -24*3600 : 24*3600;
1585 else if (vtm1->mon != vtm2->mon)
1586 off += vtm1->mon < vtm2->mon ? -24*3600 : 24*3600;
1587 else if (vtm1->mday != vtm2->mday)
1588 off += vtm1->mday < vtm2->mday ? -24*3600 : 24*3600;
1589
1590 return INT2FIX(off);
1591}
1592
1593static wideval_t
1594timelocalw(struct vtm *vtm)
1595{
1596 time_t t;
1597 struct tm tm;
1598 VALUE v;
1599 wideval_t timew1, timew2;
1600 struct vtm vtm1, vtm2;
1601 int n;
1602
1603 if (FIXNUM_P(vtm->year)) {
1604 long l = FIX2LONG(vtm->year) - 1900;
1605 if (l < INT_MIN || INT_MAX < l)
1606 goto no_localtime;
1607 tm.tm_year = (int)l;
1608 }
1609 else {
1610 v = subv(vtm->year, INT2FIX(1900));
1611 if (lt(v, INT2NUM(INT_MIN)) || lt(INT2NUM(INT_MAX), v))
1612 goto no_localtime;
1613 tm.tm_year = NUM2INT(v);
1614 }
1615
1616 tm.tm_mon = vtm->mon-1;
1617 tm.tm_mday = vtm->mday;
1618 tm.tm_hour = vtm->hour;
1619 tm.tm_min = vtm->min;
1620 tm.tm_sec = vtm->sec;
1621 tm.tm_isdst = vtm->isdst == VTM_ISDST_INITVAL ? -1 : vtm->isdst;
1622
1623 if (find_time_t(&tm, 0, &t))
1624 goto no_localtime;
1625 return wadd(rb_time_magnify(TIMET2WV(t)), v2w(vtm->subsecx));
1626
1627 no_localtime:
1628 timew1 = timegmw(vtm);
1629
1630 if (!localtimew(timew1, &vtm1))
1631 rb_raise(rb_eArgError, "localtimew error");
1632
1633 n = vtmcmp(vtm, &vtm1);
1634 if (n == 0) {
1635 timew1 = wsub(timew1, rb_time_magnify(WINT2FIXWV(12*3600)));
1636 if (!localtimew(timew1, &vtm1))
1637 rb_raise(rb_eArgError, "localtimew error");
1638 n = 1;
1639 }
1640
1641 if (n < 0) {
1642 timew2 = timew1;
1643 vtm2 = vtm1;
1644 timew1 = wsub(timew1, rb_time_magnify(WINT2FIXWV(24*3600)));
1645 if (!localtimew(timew1, &vtm1))
1646 rb_raise(rb_eArgError, "localtimew error");
1647 }
1648 else {
1649 timew2 = wadd(timew1, rb_time_magnify(WINT2FIXWV(24*3600)));
1650 if (!localtimew(timew2, &vtm2))
1651 rb_raise(rb_eArgError, "localtimew error");
1652 }
1653 timew1 = wadd(timew1, rb_time_magnify(v2w(small_vtm_sub(vtm, &vtm1))));
1654 timew2 = wadd(timew2, rb_time_magnify(v2w(small_vtm_sub(vtm, &vtm2))));
1655
1656 if (weq(timew1, timew2))
1657 return timew1;
1658
1659 if (!localtimew(timew1, &vtm1))
1660 rb_raise(rb_eArgError, "localtimew error");
1661 if (vtm->hour != vtm1.hour || vtm->min != vtm1.min || vtm->sec != vtm1.sec)
1662 return timew2;
1663
1664 if (!localtimew(timew2, &vtm2))
1665 rb_raise(rb_eArgError, "localtimew error");
1666 if (vtm->hour != vtm2.hour || vtm->min != vtm2.min || vtm->sec != vtm2.sec)
1667 return timew1;
1668
1669 if (vtm->isdst)
1670 return lt(vtm1.utc_offset, vtm2.utc_offset) ? timew2 : timew1;
1671 else
1672 return lt(vtm1.utc_offset, vtm2.utc_offset) ? timew1 : timew2;
1673}
1674
1675static struct tm *
1676localtime_with_gmtoff_zone(const time_t *t, struct tm *result, long *gmtoff, VALUE *zone)
1677{
1678 struct tm tm;
1679
1680 if (LOCALTIME(t, tm)) {
1681#if defined(HAVE_STRUCT_TM_TM_GMTOFF)
1682 *gmtoff = tm.tm_gmtoff;
1683#else
1684 struct tm *u, *l;
1685 long off;
1686 struct tm tmbuf;
1687 l = &tm;
1688 u = GMTIME(t, tmbuf);
1689 if (!u)
1690 return NULL;
1691 if (l->tm_year != u->tm_year)
1692 off = l->tm_year < u->tm_year ? -1 : 1;
1693 else if (l->tm_mon != u->tm_mon)
1694 off = l->tm_mon < u->tm_mon ? -1 : 1;
1695 else if (l->tm_mday != u->tm_mday)
1696 off = l->tm_mday < u->tm_mday ? -1 : 1;
1697 else
1698 off = 0;
1699 off = off * 24 + l->tm_hour - u->tm_hour;
1700 off = off * 60 + l->tm_min - u->tm_min;
1701 off = off * 60 + l->tm_sec - u->tm_sec;
1702 *gmtoff = off;
1703#endif
1704
1705 if (zone) {
1706#if defined(HAVE_TM_ZONE)
1707 *zone = zone_str(tm.tm_zone);
1708#elif defined(_WIN32)
1709 *zone = zone_str(get_tzname(tm.tm_isdst));
1710#elif defined(HAVE_TZNAME) && defined(HAVE_DAYLIGHT)
1711 /* this needs tzset or localtime, instead of localtime_r */
1712 *zone = zone_str(tzname[daylight && tm.tm_isdst]);
1713#else
1714 {
1715 char buf[64];
1716 strftime(buf, sizeof(buf), "%Z", &tm);
1717 *zone = zone_str(buf);
1718 }
1719#endif
1720 }
1721
1722 *result = tm;
1723 return result;
1724 }
1725 return NULL;
1726}
1727
1728static int
1729timew_out_of_timet_range(wideval_t timew)
1730{
1731 VALUE timexv;
1732#if WIDEVALUE_IS_WIDER && SIZEOF_TIME_T < SIZEOF_INT64_T
1733 if (FIXWV_P(timew)) {
1734 wideint_t t = FIXWV2WINT(timew);
1735 if (t < TIME_SCALE * (wideint_t)TIMET_MIN ||
1736 TIME_SCALE * (1 + (wideint_t)TIMET_MAX) <= t)
1737 return 1;
1738 return 0;
1739 }
1740#endif
1741#if SIZEOF_TIME_T == SIZEOF_INT64_T
1742 if (FIXWV_P(timew)) {
1743 wideint_t t = FIXWV2WINT(timew);
1744 if (~(time_t)0 <= 0) {
1745 return 0;
1746 }
1747 else {
1748 if (t < 0)
1749 return 1;
1750 return 0;
1751 }
1752 }
1753#endif
1754 timexv = w2v(timew);
1755 if (lt(timexv, mulv(INT2FIX(TIME_SCALE), TIMET2NUM(TIMET_MIN))) ||
1756 le(mulv(INT2FIX(TIME_SCALE), addv(TIMET2NUM(TIMET_MAX), INT2FIX(1))), timexv))
1757 return 1;
1758 return 0;
1759}
1760
1761static struct vtm *
1762localtimew(wideval_t timew, struct vtm *result)
1763{
1764 VALUE subsecx, offset;
1765 VALUE zone;
1766 int isdst;
1767
1768 if (!timew_out_of_timet_range(timew)) {
1769 time_t t;
1770 struct tm tm;
1771 long gmtoff;
1772 wideval_t timew2;
1773
1774 split_second(timew, &timew2, &subsecx);
1775
1776 t = WV2TIMET(timew2);
1777
1778 if (localtime_with_gmtoff_zone(&t, &tm, &gmtoff, &zone)) {
1779 result->year = LONG2NUM((long)tm.tm_year + 1900);
1780 result->mon = tm.tm_mon + 1;
1781 result->mday = tm.tm_mday;
1782 result->hour = tm.tm_hour;
1783 result->min = tm.tm_min;
1784 result->sec = tm.tm_sec;
1785 result->subsecx = subsecx;
1786 result->wday = tm.tm_wday;
1787 result->yday = tm.tm_yday+1;
1788 result->isdst = tm.tm_isdst;
1789 result->utc_offset = LONG2NUM(gmtoff);
1790 result->zone = zone;
1791 return result;
1792 }
1793 }
1794
1795 if (!gmtimew(timew, result))
1796 return NULL;
1797
1798 offset = guess_local_offset(result, &isdst, &zone);
1799
1800 if (!gmtimew(wadd(timew, rb_time_magnify(v2w(offset))), result))
1801 return NULL;
1802
1803 result->utc_offset = offset;
1804 result->isdst = isdst;
1805 result->zone = zone;
1806
1807 return result;
1808}
1809
1810#define TIME_TZMODE_LOCALTIME 0
1811#define TIME_TZMODE_UTC 1
1812#define TIME_TZMODE_FIXOFF 2
1813#define TIME_TZMODE_UNINITIALIZED 3
1814
1816 wideval_t timew; /* time_t value * TIME_SCALE. possibly Rational. */
1817 struct vtm vtm;
1818};
1819
1820#define GetTimeval(obj, tobj) ((tobj) = get_timeval(obj))
1821#define GetNewTimeval(obj, tobj) ((tobj) = get_new_timeval(obj))
1822
1823#define IsTimeval(obj) rb_typeddata_is_kind_of((obj), &time_data_type)
1824#define TIME_INIT_P(tobj) ((tobj)->vtm.tzmode != TIME_TZMODE_UNINITIALIZED)
1825
1826#define TZMODE_UTC_P(tobj) ((tobj)->vtm.tzmode == TIME_TZMODE_UTC)
1827#define TZMODE_SET_UTC(tobj) ((tobj)->vtm.tzmode = TIME_TZMODE_UTC)
1828
1829#define TZMODE_LOCALTIME_P(tobj) ((tobj)->vtm.tzmode == TIME_TZMODE_LOCALTIME)
1830#define TZMODE_SET_LOCALTIME(tobj) ((tobj)->vtm.tzmode = TIME_TZMODE_LOCALTIME)
1831
1832#define TZMODE_FIXOFF_P(tobj) ((tobj)->vtm.tzmode == TIME_TZMODE_FIXOFF)
1833#define TZMODE_SET_FIXOFF(time, tobj, off) do { \
1834 (tobj)->vtm.tzmode = TIME_TZMODE_FIXOFF; \
1835 RB_OBJ_WRITE_UNALIGNED(time, &(tobj)->vtm.utc_offset, off); \
1836} while (0)
1837
1838#define TZMODE_COPY(tobj1, tobj2) \
1839 ((tobj1)->vtm.tzmode = (tobj2)->vtm.tzmode, \
1840 (tobj1)->vtm.utc_offset = (tobj2)->vtm.utc_offset, \
1841 (tobj1)->vtm.zone = (tobj2)->vtm.zone)
1842
1843static int zone_localtime(VALUE zone, VALUE time);
1844static VALUE time_get_tm(VALUE, struct time_object *);
1845#define MAKE_TM(time, tobj) \
1846 do { \
1847 if ((tobj)->vtm.tm_got == 0) { \
1848 time_get_tm((time), (tobj)); \
1849 } \
1850 } while (0)
1851#define MAKE_TM_ENSURE(time, tobj, cond) \
1852 do { \
1853 MAKE_TM(time, tobj); \
1854 if (!(cond)) { \
1855 force_make_tm(time, tobj); \
1856 } \
1857 } while (0)
1858
1859static void
1860time_set_timew(VALUE time, struct time_object *tobj, wideval_t timew)
1861{
1862 tobj->timew = timew;
1863 if (!FIXWV_P(timew)) {
1864 RB_OBJ_WRITTEN(time, Qnil, w2v(timew));
1865 }
1866}
1867
1868static void
1869time_set_vtm(VALUE time, struct time_object *tobj, struct vtm vtm)
1870{
1871 tobj->vtm = vtm;
1872
1873 RB_OBJ_WRITTEN(time, Qnil, tobj->vtm.year);
1874 RB_OBJ_WRITTEN(time, Qnil, tobj->vtm.subsecx);
1875 RB_OBJ_WRITTEN(time, Qnil, tobj->vtm.utc_offset);
1876 RB_OBJ_WRITTEN(time, Qnil, tobj->vtm.zone);
1877}
1878
1879static inline void
1880force_make_tm(VALUE time, struct time_object *tobj)
1881{
1882 VALUE zone = tobj->vtm.zone;
1883 if (!NIL_P(zone) && zone != str_empty && zone != str_utc) {
1884 if (zone_localtime(zone, time)) return;
1885 }
1886 tobj->vtm.tm_got = 0;
1887 time_get_tm(time, tobj);
1888}
1889
1890static void
1891time_mark_and_move(void *ptr)
1892{
1893 struct time_object *tobj = ptr;
1894 if (!FIXWV_P(tobj->timew)) {
1895 rb_gc_mark_and_move(&WIDEVAL_GET(tobj->timew));
1896 }
1897 rb_gc_mark_and_move(&tobj->vtm.year);
1898 rb_gc_mark_and_move(&tobj->vtm.subsecx);
1899 rb_gc_mark_and_move(&tobj->vtm.utc_offset);
1900 rb_gc_mark_and_move(&tobj->vtm.zone);
1901}
1902
1903static const rb_data_type_t time_data_type = {
1904 .wrap_struct_name = "time",
1905 .function = {
1906 .dmark = time_mark_and_move,
1907 .dfree = RUBY_TYPED_DEFAULT_FREE,
1908 .dsize = NULL,
1909 .dcompact = time_mark_and_move,
1910 },
1911 .flags = RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_FROZEN_SHAREABLE | RUBY_TYPED_WB_PROTECTED | RUBY_TYPED_EMBEDDABLE,
1912};
1913
1914static VALUE
1915time_s_alloc(VALUE klass)
1916{
1917 VALUE obj;
1918 struct time_object *tobj;
1919
1920 obj = TypedData_Make_Struct(klass, struct time_object, &time_data_type, tobj);
1921 tobj->vtm.tzmode = TIME_TZMODE_UNINITIALIZED;
1922 tobj->vtm.tm_got = 0;
1923 time_set_timew(obj, tobj, WINT2FIXWV(0));
1924 tobj->vtm.zone = Qnil;
1925
1926 return obj;
1927}
1928
1929static struct time_object *
1930get_timeval(VALUE obj)
1931{
1932 struct time_object *tobj;
1933 TypedData_Get_Struct(obj, struct time_object, &time_data_type, tobj);
1934 if (!TIME_INIT_P(tobj)) {
1935 rb_raise(rb_eTypeError, "uninitialized %"PRIsVALUE, rb_obj_class(obj));
1936 }
1937 return tobj;
1938}
1939
1940static struct time_object *
1941get_new_timeval(VALUE obj)
1942{
1943 struct time_object *tobj;
1944 TypedData_Get_Struct(obj, struct time_object, &time_data_type, tobj);
1945 if (TIME_INIT_P(tobj)) {
1946 rb_raise(rb_eTypeError, "already initialized %"PRIsVALUE, rb_obj_class(obj));
1947 }
1948 return tobj;
1949}
1950
1951static void
1952time_modify(VALUE time)
1953{
1954 rb_check_frozen(time);
1955}
1956
1957static wideval_t
1958timenano2timew(wideint_t sec, long nsec)
1959{
1960 wideval_t timew;
1961
1962 timew = rb_time_magnify(WINT2WV(sec));
1963 if (nsec)
1964 timew = wadd(timew, wmulquoll(WINT2WV(nsec), TIME_SCALE, 1000000000));
1965 return timew;
1966}
1967
1968static struct timespec
1969timew2timespec(wideval_t timew)
1970{
1971 VALUE subsecx;
1972 struct timespec ts;
1973 wideval_t timew2;
1974
1975 if (timew_out_of_timet_range(timew))
1976 rb_raise(rb_eArgError, "time out of system range");
1977 split_second(timew, &timew2, &subsecx);
1978 ts.tv_sec = WV2TIMET(timew2);
1979 ts.tv_nsec = NUM2LONG(mulquov(subsecx, INT2FIX(1000000000), INT2FIX(TIME_SCALE)));
1980 return ts;
1981}
1982
1983static struct timespec *
1984timew2timespec_exact(wideval_t timew, struct timespec *ts)
1985{
1986 VALUE subsecx;
1987 wideval_t timew2;
1988 VALUE nsecv;
1989
1990 if (timew_out_of_timet_range(timew))
1991 return NULL;
1992 split_second(timew, &timew2, &subsecx);
1993 ts->tv_sec = WV2TIMET(timew2);
1994 nsecv = mulquov(subsecx, INT2FIX(1000000000), INT2FIX(TIME_SCALE));
1995 if (!FIXNUM_P(nsecv))
1996 return NULL;
1997 ts->tv_nsec = NUM2LONG(nsecv);
1998 return ts;
1999}
2000
2001void
2003{
2004#ifdef HAVE_CLOCK_GETTIME
2005 if (clock_gettime(CLOCK_REALTIME, ts) == -1) {
2006 rb_sys_fail("clock_gettime");
2007 }
2008#else
2009 {
2010 struct timeval tv;
2011 if (gettimeofday(&tv, 0) < 0) {
2012 rb_sys_fail("gettimeofday");
2013 }
2014 ts->tv_sec = tv.tv_sec;
2015 ts->tv_nsec = tv.tv_usec * 1000;
2016 }
2017#endif
2018}
2019
2020/*
2021 * Sets the current time information into _time_.
2022 * Returns _time_.
2023 */
2024static VALUE
2025time_init_now(rb_execution_context_t *ec, VALUE time, VALUE zone)
2026{
2027 struct time_object *tobj;
2028 struct timespec ts;
2029
2030 time_modify(time);
2031 GetNewTimeval(time, tobj);
2032 TZMODE_SET_LOCALTIME(tobj);
2033 tobj->vtm.tm_got=0;
2034 rb_timespec_now(&ts);
2035 time_set_timew(time, tobj, timenano2timew(ts.tv_sec, ts.tv_nsec));
2036
2037 if (!NIL_P(zone)) {
2038 time_zonelocal(time, zone);
2039 }
2040 return time;
2041}
2042
2043static VALUE
2044time_s_now(rb_execution_context_t *ec, VALUE klass, VALUE zone)
2045{
2046 VALUE t = time_s_alloc(klass);
2047 return time_init_now(ec, t, zone);
2048}
2049
2050static VALUE
2051time_set_utc_offset(VALUE time, VALUE off)
2052{
2053 struct time_object *tobj;
2054 off = num_exact(off);
2055
2056 time_modify(time);
2057 GetTimeval(time, tobj);
2058
2059 tobj->vtm.tm_got = 0;
2060 tobj->vtm.zone = Qnil;
2061 TZMODE_SET_FIXOFF(time, tobj, off);
2062
2063 return time;
2064}
2065
2066static void
2067vtm_add_offset(struct vtm *vtm, VALUE off, int sign)
2068{
2069 VALUE subsec, v;
2070 int sec, min, hour;
2071 int day;
2072
2073 if (lt(off, INT2FIX(0))) {
2074 sign = -sign;
2075 off = neg(off);
2076 }
2077 divmodv(off, INT2FIX(1), &off, &subsec);
2078 divmodv(off, INT2FIX(60), &off, &v);
2079 sec = NUM2INT(v);
2080 divmodv(off, INT2FIX(60), &off, &v);
2081 min = NUM2INT(v);
2082 divmodv(off, INT2FIX(24), &off, &v);
2083 hour = NUM2INT(v);
2084
2085 if (sign < 0) {
2086 subsec = neg(subsec);
2087 sec = -sec;
2088 min = -min;
2089 hour = -hour;
2090 }
2091
2092 day = 0;
2093
2094 if (!rb_equal(subsec, INT2FIX(0))) {
2095 vtm->subsecx = addv(vtm->subsecx, w2v(rb_time_magnify(v2w(subsec))));
2096 if (lt(vtm->subsecx, INT2FIX(0))) {
2097 vtm->subsecx = addv(vtm->subsecx, INT2FIX(TIME_SCALE));
2098 sec -= 1;
2099 }
2100 if (le(INT2FIX(TIME_SCALE), vtm->subsecx)) {
2101 vtm->subsecx = subv(vtm->subsecx, INT2FIX(TIME_SCALE));
2102 sec += 1;
2103 }
2104 }
2105 if (sec) {
2106 /* If sec + subsec == 0, don't change vtm->sec.
2107 * It may be 60 which is a leap second. */
2108 sec += vtm->sec;
2109 if (sec < 0) {
2110 sec += 60;
2111 min -= 1;
2112 }
2113 if (60 <= sec) {
2114 sec -= 60;
2115 min += 1;
2116 }
2117 vtm->sec = sec;
2118 }
2119 if (min) {
2120 min += vtm->min;
2121 if (min < 0) {
2122 min += 60;
2123 hour -= 1;
2124 }
2125 if (60 <= min) {
2126 min -= 60;
2127 hour += 1;
2128 }
2129 vtm->min = min;
2130 }
2131 if (hour) {
2132 hour += vtm->hour;
2133 if (hour < 0) {
2134 hour += 24;
2135 day = -1;
2136 }
2137 if (24 <= hour) {
2138 hour -= 24;
2139 day = 1;
2140 }
2141 vtm->hour = hour;
2142 }
2143
2144 vtm_add_day(vtm, day);
2145}
2146
2147static void
2148vtm_add_day(struct vtm *vtm, int day)
2149{
2150 if (day) {
2151 if (day < 0) {
2152 if (vtm->mon == 1 && vtm->mday == 1) {
2153 vtm->mday = 31;
2154 vtm->mon = 12; /* December */
2155 vtm->year = subv(vtm->year, INT2FIX(1));
2156 if (vtm->yday != 0)
2157 vtm->yday = leap_year_v_p(vtm->year) ? 366 : 365;
2158 }
2159 else if (vtm->mday == 1) {
2160 const int8_t *days_in_month = days_in_month_in_v(vtm->year);
2161 vtm->mon--;
2162 vtm->mday = days_in_month[vtm->mon-1];
2163 if (vtm->yday != 0) vtm->yday--;
2164 }
2165 else {
2166 vtm->mday--;
2167 if (vtm->yday != 0) vtm->yday--;
2168 }
2169 if (vtm->wday != VTM_WDAY_INITVAL) vtm->wday = (vtm->wday + 6) % 7;
2170 }
2171 else {
2172 int leap = leap_year_v_p(vtm->year);
2173 if (vtm->mon == 12 && vtm->mday == 31) {
2174 vtm->year = addv(vtm->year, INT2FIX(1));
2175 vtm->mon = 1; /* January */
2176 vtm->mday = 1;
2177 vtm->yday = 1;
2178 }
2179 else if (vtm->mday == days_in_month_of(leap)[vtm->mon-1]) {
2180 vtm->mon++;
2181 vtm->mday = 1;
2182 if (vtm->yday != 0) vtm->yday++;
2183 }
2184 else {
2185 vtm->mday++;
2186 if (vtm->yday != 0) vtm->yday++;
2187 }
2188 if (vtm->wday != VTM_WDAY_INITVAL) vtm->wday = (vtm->wday + 1) % 7;
2189 }
2190 }
2191}
2192
2193static int
2194maybe_tzobj_p(VALUE obj)
2195{
2196 if (NIL_P(obj)) return FALSE;
2197 if (RB_INTEGER_TYPE_P(obj)) return FALSE;
2198 if (RB_TYPE_P(obj, T_STRING)) return FALSE;
2199 return TRUE;
2200}
2201
2202NORETURN(static void invalid_utc_offset(VALUE));
2203static void
2204invalid_utc_offset(VALUE zone)
2205{
2206 rb_raise(rb_eArgError, "\"+HH:MM\", \"-HH:MM\", \"UTC\" or "
2207 "\"A\"..\"I\",\"K\"..\"Z\" expected for utc_offset: %"PRIsVALUE,
2208 zone);
2209}
2210
2211#define have_2digits(ptr) (ISDIGIT((ptr)[0]) && ISDIGIT((ptr)[1]))
2212#define num_from_2digits(ptr) ((ptr)[0] * 10 + (ptr)[1] - '0' * 11)
2213
2214static VALUE
2215utc_offset_arg(VALUE arg)
2216{
2217 VALUE tmp;
2218 if (!NIL_P(tmp = rb_check_string_type(arg))) {
2219 int n = 0;
2220 const char *s = RSTRING_PTR(tmp), *min = NULL, *sec = NULL;
2221 if (!rb_enc_str_asciicompat_p(tmp)) {
2222 goto invalid_utc_offset;
2223 }
2224 switch (RSTRING_LEN(tmp)) {
2225 case 1:
2226 if (s[0] == 'Z') {
2227 return UTC_ZONE;
2228 }
2229 /* Military Time Zone Names */
2230 if (s[0] >= 'A' && s[0] <= 'I') {
2231 n = (int)s[0] - 'A' + 1;
2232 }
2233 /* No 'J' zone */
2234 else if (s[0] >= 'K' && s[0] <= 'M') {
2235 n = (int)s[0] - 'A';
2236 }
2237 else if (s[0] >= 'N' && s[0] <= 'Y') {
2238 n = 'M' - (int)s[0];
2239 }
2240 else {
2241 goto invalid_utc_offset;
2242 }
2243 n *= 3600;
2244 return INT2FIX(n);
2245 case 3:
2246 if (STRNCASECMP("UTC", s, 3) == 0) {
2247 return UTC_ZONE;
2248 }
2249 break; /* +HH */
2250 case 7: /* +HHMMSS */
2251 sec = s+5;
2252 /* fallthrough */
2253 case 5: /* +HHMM */
2254 min = s+3;
2255 break;
2256 case 9: /* +HH:MM:SS */
2257 if (s[6] != ':') goto invalid_utc_offset;
2258 sec = s+7;
2259 /* fallthrough */
2260 case 6: /* +HH:MM */
2261 if (s[3] != ':') goto invalid_utc_offset;
2262 min = s+4;
2263 break;
2264 default:
2265 goto invalid_utc_offset;
2266 }
2267 if (sec) {
2268 if (!have_2digits(sec)) goto invalid_utc_offset;
2269 if (sec[0] > '5') goto invalid_utc_offset;
2270 n += num_from_2digits(sec);
2271 ASSUME(min);
2272 }
2273 if (min) {
2274 if (!have_2digits(min)) goto invalid_utc_offset;
2275 if (min[0] > '5') goto invalid_utc_offset;
2276 n += num_from_2digits(min) * 60;
2277 }
2278 if (s[0] != '+' && s[0] != '-') goto invalid_utc_offset;
2279 if (!have_2digits(s+1)) goto invalid_utc_offset;
2280 n += num_from_2digits(s+1) * 3600;
2281 if (s[0] == '-') {
2282 if (n == 0) return UTC_ZONE;
2283 n = -n;
2284 }
2285 return INT2FIX(n);
2286 }
2287 else {
2288 return num_exact(arg);
2289 }
2290 invalid_utc_offset:
2291 return Qnil;
2292}
2293
2294static void
2295zone_set_offset(VALUE zone, struct time_object *tobj,
2296 wideval_t tlocal, wideval_t tutc, VALUE time)
2297{
2298 /* tlocal and tutc must be unmagnified and in seconds */
2299 wideval_t w = wsub(tlocal, tutc);
2300 VALUE off = w2v(w);
2301 validate_utc_offset(off);
2302 RB_OBJ_WRITE(time, &tobj->vtm.utc_offset, off);
2303 RB_OBJ_WRITE(time, &tobj->vtm.zone, zone);
2304 TZMODE_SET_LOCALTIME(tobj);
2305}
2306
2307static wideval_t
2308extract_time(VALUE time)
2309{
2310 wideval_t t;
2311 const ID id_to_i = idTo_i;
2312
2313#define EXTRACT_TIME() do { \
2314 t = NUM2WV(AREF(to_i)); \
2315 } while (0)
2316
2317 if (rb_typeddata_is_kind_of(time, &time_data_type)) {
2318 struct time_object *tobj = RTYPEDDATA_GET_DATA(time);
2319
2320 time_gmtime(time); /* ensure tm got */
2321 t = rb_time_unmagnify(tobj->timew);
2322
2323 RB_GC_GUARD(time);
2324 }
2325 else if (RB_TYPE_P(time, T_STRUCT)) {
2326#define AREF(x) rb_struct_aref(time, ID2SYM(id_##x))
2327 EXTRACT_TIME();
2328#undef AREF
2329 }
2330 else {
2331#define AREF(x) rb_funcallv(time, id_##x, 0, 0)
2332 EXTRACT_TIME();
2333#undef AREF
2334 }
2335#undef EXTRACT_TIME
2336
2337 return t;
2338}
2339
2340static wideval_t
2341extract_vtm(VALUE time, VALUE orig_time, struct time_object *orig_tobj, VALUE subsecx)
2342{
2343 wideval_t t;
2344 const ID id_to_i = idTo_i;
2345 struct vtm *vtm = &orig_tobj->vtm;
2346
2347#define EXTRACT_VTM() do { \
2348 VALUE subsecx; \
2349 vtm->year = obj2vint(AREF(year)); \
2350 vtm->mon = month_arg(AREF(mon)); \
2351 vtm->mday = obj2ubits(AREF(mday), 5); \
2352 vtm->hour = obj2ubits(AREF(hour), 5); \
2353 vtm->min = obj2ubits(AREF(min), 6); \
2354 vtm->sec = obj2subsecx(AREF(sec), &subsecx); \
2355 vtm->isdst = RTEST(AREF(isdst)); \
2356 vtm->utc_offset = Qnil; \
2357 t = NUM2WV(AREF(to_i)); \
2358 } while (0)
2359
2360 if (rb_typeddata_is_kind_of(time, &time_data_type)) {
2361 struct time_object *tobj = RTYPEDDATA_GET_DATA(time);
2362
2363 time_get_tm(time, tobj);
2364 time_set_vtm(orig_time, orig_tobj, tobj->vtm);
2365 t = rb_time_unmagnify(tobj->timew);
2366 if (TZMODE_FIXOFF_P(tobj) && vtm->utc_offset != INT2FIX(0))
2367 t = wadd(t, v2w(vtm->utc_offset));
2368
2369 RB_GC_GUARD(time);
2370 }
2371 else if (RB_TYPE_P(time, T_STRUCT)) {
2372#define AREF(x) rb_struct_aref(time, ID2SYM(id_##x))
2373 EXTRACT_VTM();
2374#undef AREF
2375 }
2376 else if (rb_integer_type_p(time)) {
2377 t = v2w(time);
2378 struct vtm temp_vtm = *vtm;
2379 GMTIMEW(rb_time_magnify(t), &temp_vtm);
2380 time_set_vtm(orig_time, orig_tobj, temp_vtm);
2381 }
2382 else {
2383#define AREF(x) rb_funcallv(time, id_##x, 0, 0)
2384 EXTRACT_VTM();
2385#undef AREF
2386 }
2387#undef EXTRACT_VTM
2388
2389 RB_OBJ_WRITE_UNALIGNED(orig_time, &vtm->subsecx, subsecx);
2390
2391 validate_vtm(vtm);
2392 return t;
2393}
2394
2395static void
2396zone_set_dst(VALUE zone, struct time_object *tobj, VALUE tm)
2397{
2398 ID id_dst_p;
2399 VALUE dst;
2400 CONST_ID(id_dst_p, "dst?");
2401 dst = rb_check_funcall(zone, id_dst_p, 1, &tm);
2402 tobj->vtm.isdst = (!UNDEF_P(dst) && RTEST(dst));
2403}
2404
2405static int
2406zone_timelocal(VALUE zone, VALUE time)
2407{
2408 VALUE utc, tm;
2409 struct time_object *tobj = RTYPEDDATA_GET_DATA(time);
2410 wideval_t t, s;
2411
2412 wdivmod(tobj->timew, WINT2FIXWV(TIME_SCALE), &t, &s);
2413 tm = tm_from_time(rb_cTimeTM, time);
2414 utc = rb_check_funcall(zone, id_local_to_utc, 1, &tm);
2415 if (UNDEF_P(utc)) return 0;
2416
2417 s = extract_time(utc);
2418 zone_set_offset(zone, tobj, t, s, time);
2419 s = rb_time_magnify(s);
2420 if (tobj->vtm.subsecx != INT2FIX(0)) {
2421 s = wadd(s, v2w(tobj->vtm.subsecx));
2422 }
2423 time_set_timew(time, tobj, s);
2424
2425 zone_set_dst(zone, tobj, tm);
2426
2427 RB_GC_GUARD(time);
2428
2429 return 1;
2430}
2431
2432static int
2433zone_localtime(VALUE zone, VALUE time)
2434{
2435 VALUE local, tm, subsecx;
2436 struct time_object *tobj = RTYPEDDATA_GET_DATA(time);
2437 wideval_t t, s;
2438
2439 split_second(tobj->timew, &t, &subsecx);
2440 tm = tm_from_time(rb_cTimeTM, time);
2441
2442 local = rb_check_funcall(zone, id_utc_to_local, 1, &tm);
2443 if (UNDEF_P(local)) return 0;
2444
2445 s = extract_vtm(local, time, tobj, subsecx);
2446 tobj->vtm.tm_got = 1;
2447 zone_set_offset(zone, tobj, s, t, time);
2448 zone_set_dst(zone, tobj, tm);
2449
2450 RB_GC_GUARD(time);
2451
2452 return 1;
2453}
2454
2455static VALUE
2456find_timezone(VALUE time, VALUE zone)
2457{
2458 VALUE klass = CLASS_OF(time);
2459
2460 return rb_check_funcall_default(klass, id_find_timezone, 1, &zone, Qnil);
2461}
2462
2463/* Turn the special case 24:00:00 of already validated vtm into
2464 * 00:00:00 the next day */
2465static void
2466vtm_day_wraparound(struct vtm *vtm)
2467{
2468 if (vtm->hour < 24) return;
2469
2470 /* Assuming UTC and no care of DST, just reset hour and advance
2471 * date, not to discard the validated vtm. */
2472 vtm->hour = 0;
2473 vtm_add_day(vtm, 1);
2474}
2475
2476static VALUE time_init_vtm(VALUE time, struct vtm vtm, VALUE zone);
2477
2478/*
2479 * Sets the broken-out time information into _time_.
2480 * Returns _time_.
2481 */
2482static VALUE
2483time_init_args(rb_execution_context_t *ec, VALUE time, VALUE year, VALUE mon, VALUE mday,
2484 VALUE hour, VALUE min, VALUE sec, VALUE zone)
2485{
2486 struct vtm vtm;
2487
2488 vtm.wday = VTM_WDAY_INITVAL;
2489 vtm.yday = 0;
2490 vtm.zone = str_empty;
2491
2492 vtm.year = obj2vint(year);
2493
2494 vtm.mon = NIL_P(mon) ? 1 : month_arg(mon);
2495
2496 vtm.mday = NIL_P(mday) ? 1 : obj2ubits(mday, 5);
2497
2498 vtm.hour = NIL_P(hour) ? 0 : obj2ubits(hour, 5);
2499
2500 vtm.min = NIL_P(min) ? 0 : obj2ubits(min, 6);
2501
2502 if (NIL_P(sec)) {
2503 vtm.sec = 0;
2504 vtm.subsecx = INT2FIX(0);
2505 }
2506 else {
2507 VALUE subsecx;
2508 vtm.sec = obj2subsecx(sec, &subsecx);
2509 vtm.subsecx = subsecx;
2510 }
2511
2512 return time_init_vtm(time, vtm, zone);
2513}
2514
2515static VALUE
2516time_init_vtm(VALUE time, struct vtm vtm, VALUE zone)
2517{
2518 VALUE utc = Qnil;
2519 struct time_object *tobj;
2520
2521 vtm.isdst = VTM_ISDST_INITVAL;
2522 vtm.utc_offset = Qnil;
2523 const VALUE arg = zone;
2524 if (!NIL_P(arg)) {
2525 zone = Qnil;
2526 if (arg == ID2SYM(rb_intern("dst")))
2527 vtm.isdst = 1;
2528 else if (arg == ID2SYM(rb_intern("std")))
2529 vtm.isdst = 0;
2530 else if (maybe_tzobj_p(arg))
2531 zone = arg;
2532 else if (!NIL_P(utc = utc_offset_arg(arg)))
2533 vtm.utc_offset = utc == UTC_ZONE ? INT2FIX(0) : utc;
2534 else if (NIL_P(zone = find_timezone(time, arg)))
2535 invalid_utc_offset(arg);
2536 }
2537
2538 validate_vtm(&vtm);
2539
2540 time_modify(time);
2541 GetNewTimeval(time, tobj);
2542
2543 if (!NIL_P(zone)) {
2544 time_set_timew(time, tobj, timegmw(&vtm));
2545 vtm_day_wraparound(&vtm);
2546 time_set_vtm(time, tobj, vtm);
2547 tobj->vtm.tm_got = 1;
2548 TZMODE_SET_LOCALTIME(tobj);
2549 if (zone_timelocal(zone, time)) {
2550 return time;
2551 }
2552 else if (NIL_P(vtm.utc_offset = utc_offset_arg(zone))) {
2553 if (NIL_P(zone = find_timezone(time, zone)) || !zone_timelocal(zone, time))
2554 invalid_utc_offset(arg);
2555 }
2556 }
2557
2558 if (utc == UTC_ZONE) {
2559 time_set_timew(time, tobj, timegmw(&vtm));
2560 vtm.isdst = 0; /* No DST in UTC */
2561 vtm_day_wraparound(&vtm);
2562 time_set_vtm(time, tobj, vtm);
2563 tobj->vtm.tm_got = 1;
2564 TZMODE_SET_UTC(tobj);
2565 return time;
2566 }
2567
2568 TZMODE_SET_LOCALTIME(tobj);
2569 tobj->vtm.tm_got=0;
2570
2571 if (!NIL_P(vtm.utc_offset)) {
2572 VALUE off = vtm.utc_offset;
2573 vtm_add_offset(&vtm, off, -1);
2574 vtm.utc_offset = Qnil;
2575 time_set_timew(time, tobj, timegmw(&vtm));
2576
2577 return time_set_utc_offset(time, off);
2578 }
2579 else {
2580 time_set_timew(time, tobj, timelocalw(&vtm));
2581
2582 return time_localtime(time);
2583 }
2584}
2585
2586static int
2587two_digits(const char *ptr, const char *end, const char **endp, const char *name)
2588{
2589 ssize_t len = end - ptr;
2590 if (len < 2 || !have_2digits(ptr) || ((len > 2) && ISDIGIT(ptr[2]))) {
2591 VALUE mesg = rb_sprintf("two digits %s is expected", name);
2592 if (ptr[-1] == '-' || ptr[-1] == ':') {
2593 rb_str_catf(mesg, " after '%c'", ptr[-1]);
2594 }
2595 rb_str_catf(mesg, ": %.*s", ((len > 10) ? 10 : (int)(end - ptr)) + 1, ptr - 1);
2596 rb_exc_raise(rb_exc_new_str(rb_eArgError, mesg));
2597 }
2598 *endp = ptr + 2;
2599 return num_from_2digits(ptr);
2600}
2601
2602static VALUE
2603parse_int(const char *ptr, const char *end, const char **endp, size_t *ndigits, bool sign)
2604{
2605 ssize_t len = (end - ptr);
2606 int flags = sign ? RB_INT_PARSE_SIGN : 0;
2607 return rb_int_parse_cstr(ptr, len, (char **)endp, ndigits, 10, flags);
2608}
2609
2610/*
2611 * Parses _str_ and sets the broken-out time information into _time_.
2612 * If _str_ is not a String, returns +nil+, otherwise returns _time_.
2613 */
2614static VALUE
2615time_init_parse(rb_execution_context_t *ec, VALUE time, VALUE str, VALUE zone, VALUE precision)
2616{
2617 if (NIL_P(str = rb_check_string_type(str))) return Qnil;
2618 if (!rb_enc_str_asciicompat_p(str)) {
2619 rb_raise(rb_eArgError, "time string should have ASCII compatible encoding");
2620 }
2621
2622 const char *const begin = RSTRING_PTR(str);
2623 const char *const end = RSTRING_END(str);
2624 const char *ptr = begin;
2625 VALUE year = Qnil, subsec = Qnil;
2626 int mon = -1, mday = -1, hour = -1, min = -1, sec = -1;
2627 size_t ndigits;
2628 size_t prec = NIL_P(precision) ? SIZE_MAX : NUM2SIZET(precision);
2629
2630 if ((ptr < end) && (ISSPACE(*ptr) || ISSPACE(*(end-1)))) {
2631 rb_raise(rb_eArgError, "can't parse: %+"PRIsVALUE, str);
2632 }
2633 year = parse_int(ptr, end, &ptr, &ndigits, true);
2634 if (NIL_P(year)) {
2635 rb_raise(rb_eArgError, "can't parse: %+"PRIsVALUE, str);
2636 }
2637 else if (ndigits < 4) {
2638 rb_raise(rb_eArgError, "year must be 4 or more digits: %.*s", (int)ndigits, ptr - ndigits);
2639 }
2640 else if (ptr == end) {
2641 goto only_year;
2642 }
2643 do {
2644#define peekable_p(n) ((ptrdiff_t)(n) < (end - ptr))
2645#define peek_n(c, n) (peekable_p(n) && ((unsigned char)ptr[n] == (c)))
2646#define peek(c) peek_n(c, 0)
2647#define peekc_n(n) (peekable_p(n) ? (int)(unsigned char)ptr[n] : -1)
2648#define peekc() peekc_n(0)
2649#define expect_two_digits(x, bits) \
2650 (((unsigned int)(x = two_digits(ptr + 1, end, &ptr, #x)) > (1U << bits) - 1) ? \
2651 rb_raise(rb_eArgError, #x" out of range") : (void)0)
2652 if (!peek('-')) break;
2653 expect_two_digits(mon, 4);
2654 if (!peek('-')) break;
2655 expect_two_digits(mday, 5);
2656 if (!peek(' ') && !peek('T')) break;
2657 const char *const time_part = ptr + 1;
2658 if (!ISDIGIT(peekc_n(1))) break;
2659#define nofraction(x) \
2660 if (peek('.')) { \
2661 rb_raise(rb_eArgError, "fraction " #x " is not supported: %.*s", \
2662 (int)(ptr + 1 - time_part), time_part); \
2663 }
2664#define need_colon(x) \
2665 if (!peek(':')) { \
2666 rb_raise(rb_eArgError, "missing " #x " part: %.*s", \
2667 (int)(ptr + 1 - time_part), time_part); \
2668 }
2669 expect_two_digits(hour, 5);
2670 nofraction(hour);
2671 need_colon(min);
2672 expect_two_digits(min, 6);
2673 nofraction(min);
2674 need_colon(sec);
2675 expect_two_digits(sec, 6);
2676 if (peek('.')) {
2677 ptr++;
2678 for (ndigits = 0; ndigits < prec && ISDIGIT(peekc_n(ndigits)); ++ndigits);
2679 if (!ndigits) {
2680 int clen = rb_enc_precise_mbclen(ptr, end, rb_enc_get(str));
2681 if (clen < 0) clen = 0;
2682 rb_raise(rb_eArgError, "subsecond expected after dot: %.*s",
2683 (int)(ptr - time_part) + clen, time_part);
2684 }
2685 subsec = parse_int(ptr, ptr + ndigits, &ptr, &ndigits, false);
2686 if (NIL_P(subsec)) break;
2687 while (ptr < end && ISDIGIT(*ptr)) ptr++;
2688 }
2689 } while (0);
2690 while (ptr < end && ISSPACE(*ptr)) ptr++;
2691 const char *const zstr = ptr;
2692 while (ptr < end && !ISSPACE(*ptr)) ptr++;
2693 const char *const zend = ptr;
2694 while (ptr < end && ISSPACE(*ptr)) ptr++;
2695 if (ptr < end) {
2696 VALUE mesg = rb_str_new_cstr("can't parse at: ");
2697 rb_str_cat(mesg, ptr, end - ptr);
2698 rb_exc_raise(rb_exc_new_str(rb_eArgError, mesg));
2699 }
2700 if (zend > zstr) {
2701 zone = rb_str_subseq(str, zstr - begin, zend - zstr);
2702 }
2703 else if (hour == -1) {
2704 rb_raise(rb_eArgError, "no time information");
2705 }
2706 if (!NIL_P(subsec)) {
2707 /* subseconds is the last using ndigits */
2708 if (ndigits < (size_t)TIME_SCALE_NUMDIGITS) {
2709 VALUE mul = rb_int_positive_pow(10, TIME_SCALE_NUMDIGITS - ndigits);
2710 subsec = rb_int_mul(subsec, mul);
2711 }
2712 else if (ndigits > (size_t)TIME_SCALE_NUMDIGITS) {
2713 VALUE num = rb_int_positive_pow(10, ndigits - TIME_SCALE_NUMDIGITS);
2714 subsec = rb_rational_new(subsec, num);
2715 }
2716 }
2717
2718only_year:
2719 ;
2720
2721 struct vtm vtm = {
2722 .wday = VTM_WDAY_INITVAL,
2723 .yday = 0,
2724 .zone = str_empty,
2725 .year = year,
2726 .mon = (mon < 0) ? 1 : mon,
2727 .mday = (mday < 0) ? 1 : mday,
2728 .hour = (hour < 0) ? 0 : hour,
2729 .min = (min < 0) ? 0 : min,
2730 .sec = (sec < 0) ? 0 : sec,
2731 .subsecx = NIL_P(subsec) ? INT2FIX(0) : subsec,
2732 };
2733 return time_init_vtm(time, vtm, zone);
2734}
2735
2736static void
2737subsec_normalize(wideint_t *secp, long *subsecp, const long maxsubsec)
2738{
2739 wideint_t sec = *secp;
2740 long subsec = *subsecp;
2741 long sec2;
2742
2743 if (UNLIKELY(subsec >= maxsubsec)) { /* subsec positive overflow */
2744 sec2 = subsec / maxsubsec;
2745 if (WIDEINT_MAX - sec2 < sec) {
2746 rb_raise(rb_eRangeError, "out of Time range");
2747 }
2748 subsec -= sec2 * maxsubsec;
2749 sec += sec2;
2750 }
2751 else if (UNLIKELY(subsec < 0)) { /* subsec negative overflow */
2752 sec2 = NDIV(subsec, maxsubsec); /* negative div */
2753 if (sec < WIDEINT_MIN - sec2) {
2754 rb_raise(rb_eRangeError, "out of Time range");
2755 }
2756 subsec -= sec2 * maxsubsec;
2757 sec += sec2;
2758 }
2759 *secp = sec;
2760 *subsecp = subsec;
2761}
2762
2763#define time_usec_normalize(secp, usecp) subsec_normalize(secp, usecp, 1000000)
2764#define time_nsec_normalize(secp, nsecp) subsec_normalize(secp, nsecp, 1000000000)
2765
2766static VALUE
2767time_new_timew(VALUE klass, wideval_t timew)
2768{
2769 VALUE time = time_s_alloc(klass);
2770 struct time_object *tobj;
2771
2772 tobj = RTYPEDDATA_GET_DATA(time); /* skip type check */
2773 TZMODE_SET_LOCALTIME(tobj);
2774 time_set_timew(time, tobj, timew);
2775
2776 return time;
2777}
2778
2779static wideint_t
2780TIMETtoWIDEINT(time_t t)
2781{
2782#if SIZEOF_TIME_T * CHAR_BIT - (SIGNEDNESS_OF_TIME_T < 0) > \
2783 SIZEOF_WIDEINT * CHAR_BIT - 1
2784 /* compare in bit size without sign bit */
2785 if (t > WIDEINT_MAX) rb_raise(rb_eArgError, "out of Time range");
2786#endif
2787 return (wideint_t)t;
2788}
2789
2790VALUE
2791rb_time_new(time_t sec, long usec)
2792{
2793 wideint_t isec = TIMETtoWIDEINT(sec);
2794 time_usec_normalize(&isec, &usec);
2795 return time_new_timew(rb_cTime, timenano2timew(isec, usec * 1000));
2796}
2797
2798/* returns localtime time object */
2799VALUE
2800rb_time_nano_new(time_t sec, long nsec)
2801{
2802 wideint_t isec = TIMETtoWIDEINT(sec);
2803 time_nsec_normalize(&isec, &nsec);
2804 return time_new_timew(rb_cTime, timenano2timew(isec, nsec));
2805}
2806
2807VALUE
2808rb_time_timespec_new(const struct timespec *ts, int offset)
2809{
2810 struct time_object *tobj;
2811 VALUE time = rb_time_nano_new(ts->tv_sec, ts->tv_nsec);
2812
2813 if (-86400 < offset && offset < 86400) { /* fixoff */
2814 GetTimeval(time, tobj);
2815 TZMODE_SET_FIXOFF(time, tobj, INT2FIX(offset));
2816 }
2817 else if (offset == INT_MAX) { /* localtime */
2818 }
2819 else if (offset == INT_MAX-1) { /* UTC */
2820 GetTimeval(time, tobj);
2821 TZMODE_SET_UTC(tobj);
2822 }
2823 else {
2824 rb_raise(rb_eArgError, "utc_offset out of range");
2825 }
2826
2827 return time;
2828}
2829
2830VALUE
2832{
2833 VALUE time = time_new_timew(rb_cTime, rb_time_magnify(v2w(timev)));
2834
2835 if (!NIL_P(off)) {
2836 VALUE zone = off;
2837
2838 if (maybe_tzobj_p(zone)) {
2839 time_gmtime(time);
2840 if (zone_timelocal(zone, time)) return time;
2841 }
2842 if (NIL_P(off = utc_offset_arg(off))) {
2843 off = zone;
2844 if (NIL_P(zone = find_timezone(time, off))) invalid_utc_offset(off);
2845 time_gmtime(time);
2846 if (!zone_timelocal(zone, time)) invalid_utc_offset(off);
2847 return time;
2848 }
2849 else if (off == UTC_ZONE) {
2850 return time_gmtime(time);
2851 }
2852
2853 validate_utc_offset(off);
2854 time_set_utc_offset(time, off);
2855 return time;
2856 }
2857
2858 return time;
2859}
2860
2861static struct timespec
2862time_timespec(VALUE num, int interval)
2863{
2864 struct timespec t;
2865 const char *const tstr = interval ? "time interval" : "time";
2866 VALUE i, f, ary;
2867
2868#ifndef NEGATIVE_TIME_T
2869# define arg_range_check(v) \
2870 (((v) < 0) ? \
2871 rb_raise(rb_eArgError, "%s must not be negative", tstr) : \
2872 (void)0)
2873#else
2874# define arg_range_check(v) \
2875 ((interval && (v) < 0) ? \
2876 rb_raise(rb_eArgError, "time interval must not be negative") : \
2877 (void)0)
2878#endif
2879
2880 if (FIXNUM_P(num)) {
2881 t.tv_sec = NUM2TIMET(num);
2882 arg_range_check(t.tv_sec);
2883 t.tv_nsec = 0;
2884 }
2885 else if (RB_FLOAT_TYPE_P(num)) {
2886 double x = RFLOAT_VALUE(num);
2887 arg_range_check(x);
2888 {
2889 double f, d;
2890
2891 d = modf(x, &f);
2892 if (d >= 0) {
2893 t.tv_nsec = (int)(d*1e9+0.5);
2894 if (t.tv_nsec >= 1000000000) {
2895 t.tv_nsec -= 1000000000;
2896 f += 1;
2897 }
2898 }
2899 else if ((t.tv_nsec = (int)(-d*1e9+0.5)) > 0) {
2900 t.tv_nsec = 1000000000 - t.tv_nsec;
2901 f -= 1;
2902 }
2903 t.tv_sec = (time_t)f;
2904 if (f != t.tv_sec) {
2905 rb_raise(rb_eRangeError, "%f out of Time range", x);
2906 }
2907 }
2908 }
2909 else if (RB_BIGNUM_TYPE_P(num)) {
2910 t.tv_sec = NUM2TIMET(num);
2911 arg_range_check(t.tv_sec);
2912 t.tv_nsec = 0;
2913 }
2914 else {
2915 i = INT2FIX(1);
2916 ary = rb_check_funcall(num, id_divmod, 1, &i);
2917 if (!UNDEF_P(ary) && !NIL_P(ary = rb_check_array_type(ary))) {
2918 i = rb_ary_entry(ary, 0);
2919 f = rb_ary_entry(ary, 1);
2920 t.tv_sec = NUM2TIMET(i);
2921 arg_range_check(t.tv_sec);
2922 f = rb_funcall(f, '*', 1, INT2FIX(1000000000));
2923 t.tv_nsec = NUM2LONG(f);
2924 }
2925 else {
2926 rb_raise(rb_eTypeError, "can't convert %"PRIsVALUE" into %s",
2927 rb_obj_class(num), tstr);
2928 }
2929 }
2930 return t;
2931#undef arg_range_check
2932}
2933
2934static struct timeval
2935time_timeval(VALUE num, int interval)
2936{
2937 struct timespec ts;
2938 struct timeval tv;
2939
2940 ts = time_timespec(num, interval);
2941 tv.tv_sec = (TYPEOF_TIMEVAL_TV_SEC)ts.tv_sec;
2942 tv.tv_usec = (TYPEOF_TIMEVAL_TV_USEC)(ts.tv_nsec / 1000);
2943
2944 return tv;
2945}
2946
2947struct timeval
2949{
2950 return time_timeval(num, TRUE);
2951}
2952
2953struct timeval
2955{
2956 struct time_object *tobj;
2957 struct timeval t;
2958 struct timespec ts;
2959
2960 if (IsTimeval(time)) {
2961 GetTimeval(time, tobj);
2962 ts = timew2timespec(tobj->timew);
2963 t.tv_sec = (TYPEOF_TIMEVAL_TV_SEC)ts.tv_sec;
2964 t.tv_usec = (TYPEOF_TIMEVAL_TV_USEC)(ts.tv_nsec / 1000);
2965 return t;
2966 }
2967 return time_timeval(time, FALSE);
2968}
2969
2970struct timespec
2972{
2973 struct time_object *tobj;
2974 struct timespec t;
2975
2976 if (IsTimeval(time)) {
2977 GetTimeval(time, tobj);
2978 t = timew2timespec(tobj->timew);
2979 return t;
2980 }
2981 return time_timespec(time, FALSE);
2982}
2983
2984struct timespec
2986{
2987 return time_timespec(num, TRUE);
2988}
2989
2990static int
2991get_scale(VALUE unit)
2992{
2993 if (unit == ID2SYM(id_nanosecond) || unit == ID2SYM(id_nsec)) {
2994 return 1000000000;
2995 }
2996 else if (unit == ID2SYM(id_microsecond) || unit == ID2SYM(id_usec)) {
2997 return 1000000;
2998 }
2999 else if (unit == ID2SYM(id_millisecond)) {
3000 return 1000;
3001 }
3002 else {
3003 rb_raise(rb_eArgError, "unexpected unit: %"PRIsVALUE, unit);
3004 }
3005}
3006
3007static VALUE
3008time_s_at(rb_execution_context_t *ec, VALUE klass, VALUE time, VALUE subsec, VALUE unit, VALUE zone)
3009{
3010 VALUE t;
3011 wideval_t timew;
3012
3013 if (subsec) {
3014 int scale = get_scale(unit);
3015 time = num_exact(time);
3016 t = num_exact(subsec);
3017 timew = wadd(rb_time_magnify(v2w(time)), wmulquoll(v2w(t), TIME_SCALE, scale));
3018 t = time_new_timew(klass, timew);
3019 }
3020 else if (IsTimeval(time)) {
3021 struct time_object *tobj, *tobj2;
3022 GetTimeval(time, tobj);
3023 t = time_new_timew(klass, tobj->timew);
3024 GetTimeval(t, tobj2);
3025 TZMODE_COPY(tobj2, tobj);
3026 }
3027 else {
3028 timew = rb_time_magnify(v2w(num_exact(time)));
3029 t = time_new_timew(klass, timew);
3030 }
3031 if (!NIL_P(zone)) {
3032 time_zonelocal(t, zone);
3033 }
3034
3035 return t;
3036}
3037
3038static VALUE
3039time_s_at1(rb_execution_context_t *ec, VALUE klass, VALUE time)
3040{
3041 return time_s_at(ec, klass, time, Qfalse, ID2SYM(id_microsecond), Qnil);
3042}
3043
3044static const char months[][4] = {
3045 "jan", "feb", "mar", "apr", "may", "jun",
3046 "jul", "aug", "sep", "oct", "nov", "dec",
3047};
3048
3049static int
3050obj2int(VALUE obj)
3051{
3052 if (RB_TYPE_P(obj, T_STRING)) {
3053 obj = rb_str_to_inum(obj, 10, TRUE);
3054 }
3055
3056 return NUM2INT(obj);
3057}
3058
3059/* bits should be 0 <= x <= 31 */
3060static uint32_t
3061obj2ubits(VALUE obj, unsigned int bits)
3062{
3063 const unsigned int usable_mask = (1U << bits) - 1;
3064 unsigned int rv = (unsigned int)obj2int(obj);
3065
3066 if ((rv & usable_mask) != rv)
3067 rb_raise(rb_eArgError, "argument out of range");
3068 return (uint32_t)rv;
3069}
3070
3071static VALUE
3072obj2vint(VALUE obj)
3073{
3074 if (RB_TYPE_P(obj, T_STRING)) {
3075 obj = rb_str_to_inum(obj, 10, TRUE);
3076 }
3077 else {
3078 obj = rb_to_int(obj);
3079 }
3080
3081 return obj;
3082}
3083
3084static uint32_t
3085obj2subsecx(VALUE obj, VALUE *subsecx)
3086{
3087 VALUE subsec;
3088
3089 if (RB_TYPE_P(obj, T_STRING)) {
3090 obj = rb_str_to_inum(obj, 10, TRUE);
3091 *subsecx = INT2FIX(0);
3092 }
3093 else {
3094 divmodv(num_exact(obj), INT2FIX(1), &obj, &subsec);
3095 *subsecx = w2v(rb_time_magnify(v2w(subsec)));
3096 }
3097 return obj2ubits(obj, 6); /* vtm->sec */
3098}
3099
3100static VALUE
3101usec2subsecx(VALUE obj)
3102{
3103 if (RB_TYPE_P(obj, T_STRING)) {
3104 obj = rb_str_to_inum(obj, 10, TRUE);
3105 }
3106
3107 return mulquov(num_exact(obj), INT2FIX(TIME_SCALE), INT2FIX(1000000));
3108}
3109
3110static uint32_t
3111month_arg(VALUE arg)
3112{
3113 int i, mon;
3114
3115 if (FIXNUM_P(arg)) {
3116 return obj2ubits(arg, 4);
3117 }
3118
3119 mon = 0;
3120 VALUE s = rb_check_string_type(arg);
3121 if (!NIL_P(s) && RSTRING_LEN(s) > 0) {
3122 arg = s;
3123 for (i=0; i<12; i++) {
3124 if (RSTRING_LEN(s) == 3 &&
3125 STRNCASECMP(months[i], RSTRING_PTR(s), 3) == 0) {
3126 mon = i+1;
3127 break;
3128 }
3129 }
3130 }
3131 if (mon == 0) {
3132 mon = obj2ubits(arg, 4);
3133 }
3134 return mon;
3135}
3136
3137static VALUE
3138validate_utc_offset(VALUE utc_offset)
3139{
3140 if (le(utc_offset, INT2FIX(-86400)) || ge(utc_offset, INT2FIX(86400)))
3141 rb_raise(rb_eArgError, "utc_offset out of range");
3142 return utc_offset;
3143}
3144
3145static VALUE
3146validate_zone_name(VALUE zone_name)
3147{
3148 StringValueCStr(zone_name);
3149 return zone_name;
3150}
3151
3152static void
3153validate_vtm(struct vtm *vtm)
3154{
3155#define validate_vtm_range(mem, b, e) \
3156 ((vtm->mem < b || vtm->mem > e) ? \
3157 rb_raise(rb_eArgError, #mem" out of range") : (void)0)
3158 validate_vtm_range(mon, 1, 12);
3159 validate_vtm_range(mday, 1, 31);
3160 validate_vtm_range(hour, 0, 24);
3161 validate_vtm_range(min, 0, (vtm->hour == 24 ? 0 : 59));
3162 validate_vtm_range(sec, 0, (vtm->hour == 24 ? 0 : 60));
3163 if (lt(vtm->subsecx, INT2FIX(0)) || ge(vtm->subsecx, INT2FIX(TIME_SCALE)))
3164 rb_raise(rb_eArgError, "subsecx out of range");
3165 if (!NIL_P(vtm->utc_offset)) validate_utc_offset(vtm->utc_offset);
3166#undef validate_vtm_range
3167}
3168
3169static void
3170time_arg(int argc, const VALUE *argv, struct vtm *vtm)
3171{
3172 VALUE v[8];
3173 VALUE subsecx = INT2FIX(0);
3174
3175 vtm->year = INT2FIX(0);
3176 vtm->mon = 0;
3177 vtm->mday = 0;
3178 vtm->hour = 0;
3179 vtm->min = 0;
3180 vtm->sec = 0;
3181 vtm->subsecx = INT2FIX(0);
3182 vtm->utc_offset = Qnil;
3183 vtm->wday = 0;
3184 vtm->yday = 0;
3185 vtm->isdst = 0;
3186 vtm->zone = str_empty;
3187
3188 if (argc == 10) {
3189 v[0] = argv[5];
3190 v[1] = argv[4];
3191 v[2] = argv[3];
3192 v[3] = argv[2];
3193 v[4] = argv[1];
3194 v[5] = argv[0];
3195 v[6] = Qnil;
3196 vtm->isdst = RTEST(argv[8]) ? 1 : 0;
3197 }
3198 else {
3199 rb_scan_args(argc, argv, "17", &v[0],&v[1],&v[2],&v[3],&v[4],&v[5],&v[6],&v[7]);
3200 /* v[6] may be usec or zone (parsedate) */
3201 /* v[7] is wday (parsedate; ignored) */
3202 vtm->wday = VTM_WDAY_INITVAL;
3203 vtm->isdst = VTM_ISDST_INITVAL;
3204 }
3205
3206 vtm->year = obj2vint(v[0]);
3207
3208 if (NIL_P(v[1])) {
3209 vtm->mon = 1;
3210 }
3211 else {
3212 vtm->mon = month_arg(v[1]);
3213 }
3214
3215 if (NIL_P(v[2])) {
3216 vtm->mday = 1;
3217 }
3218 else {
3219 vtm->mday = obj2ubits(v[2], 5);
3220 }
3221
3222 /* normalize month-mday */
3223 switch (vtm->mon) {
3224 case 2:
3225 {
3226 /* this drops higher bits but it's not a problem to calc leap year */
3227 unsigned int mday2 = leap_year_v_p(vtm->year) ? 29 : 28;
3228 if (vtm->mday > mday2) {
3229 vtm->mday -= mday2;
3230 vtm->mon++;
3231 }
3232 }
3233 break;
3234 case 4:
3235 case 6:
3236 case 9:
3237 case 11:
3238 if (vtm->mday == 31) {
3239 vtm->mon++;
3240 vtm->mday = 1;
3241 }
3242 break;
3243 }
3244
3245 vtm->hour = NIL_P(v[3])?0:obj2ubits(v[3], 5);
3246
3247 vtm->min = NIL_P(v[4])?0:obj2ubits(v[4], 6);
3248
3249 if (!NIL_P(v[6]) && argc == 7) {
3250 vtm->sec = NIL_P(v[5])?0:obj2ubits(v[5],6);
3251 subsecx = usec2subsecx(v[6]);
3252 }
3253 else {
3254 /* when argc == 8, v[6] is timezone, but ignored */
3255 if (NIL_P(v[5])) {
3256 vtm->sec = 0;
3257 }
3258 else {
3259 vtm->sec = obj2subsecx(v[5], &subsecx);
3260 }
3261 }
3262 vtm->subsecx = subsecx;
3263
3264 validate_vtm(vtm);
3265 RB_GC_GUARD(subsecx);
3266}
3267
3268static int
3269leap_year_p(long y)
3270{
3271 /* TODO:
3272 * ensure about negative years in proleptic Gregorian calendar.
3273 */
3274 unsigned long uy = (unsigned long)(LIKELY(y >= 0) ? y : -y);
3275
3276 if (LIKELY(uy % 4 != 0)) return 0;
3277
3278 unsigned long century = uy / 100;
3279 if (LIKELY(uy != century * 100)) return 1;
3280 return century % 4 == 0;
3281}
3282
3283static time_t
3284timegm_noleapsecond(struct tm *tm)
3285{
3286 long tm_year = tm->tm_year;
3287 int tm_yday = calc_tm_yday(tm->tm_year, tm->tm_mon, tm->tm_mday);
3288
3289 /*
3290 * `Seconds Since the Epoch' in SUSv3:
3291 * tm_sec + tm_min*60 + tm_hour*3600 + tm_yday*86400 +
3292 * (tm_year-70)*31536000 + ((tm_year-69)/4)*86400 -
3293 * ((tm_year-1)/100)*86400 + ((tm_year+299)/400)*86400
3294 */
3295 return tm->tm_sec + tm->tm_min*60 + tm->tm_hour*3600 +
3296 (time_t)(tm_yday +
3297 (tm_year-70)*365 +
3298 DIV(tm_year-69,4) -
3299 DIV(tm_year-1,100) +
3300 DIV(tm_year+299,400))*86400;
3301}
3302
3303#if 0
3304#define DEBUG_FIND_TIME_NUMGUESS
3305#define DEBUG_GUESSRANGE
3306#endif
3307
3308static const bool debug_guessrange =
3309#ifdef DEBUG_GUESSRANGE
3310 true;
3311#else
3312 false;
3313#endif
3314
3315#define DEBUG_REPORT_GUESSRANGE \
3316 (debug_guessrange ? debug_report_guessrange(guess_lo, guess_hi) : (void)0)
3317
3318static inline void
3319debug_report_guessrange(time_t guess_lo, time_t guess_hi)
3320{
3321 time_t guess_diff = guess_hi - guess_lo;
3322 fprintf(stderr, "find time guess range: %"PRI_TIMET_PREFIX"d - "
3323 "%"PRI_TIMET_PREFIX"d : %"PRI_TIMET_PREFIX"u\n",
3324 guess_lo, guess_hi, guess_diff);
3325}
3326
3327static const bool debug_find_time_numguess =
3328#ifdef DEBUG_FIND_TIME_NUMGUESS
3329 true;
3330#else
3331 false;
3332#endif
3333
3334#define DEBUG_FIND_TIME_NUMGUESS_INC \
3335 (void)(debug_find_time_numguess && find_time_numguess++),
3336static unsigned long long find_time_numguess;
3337
3338static VALUE
3339find_time_numguess_getter(ID name, VALUE *data)
3340{
3341 unsigned long long *numguess = (void *)data;
3342 return ULL2NUM(*numguess);
3343}
3344
3345static const char *
3346find_time_t(struct tm *tptr, int utc_p, time_t *tp)
3347{
3348 time_t guess, guess0, guess_lo, guess_hi;
3349 struct tm *tm, tm0, tm_lo, tm_hi;
3350 int d;
3351 int find_dst;
3352 struct tm result;
3353 int status;
3354 int tptr_tm_yday;
3355
3356#define GUESS(p) (DEBUG_FIND_TIME_NUMGUESS_INC (utc_p ? gmtime_with_leapsecond((p), &result) : LOCALTIME((p), result)))
3357
3358 guess_lo = TIMET_MIN;
3359 guess_hi = TIMET_MAX;
3360
3361 find_dst = 0 < tptr->tm_isdst;
3362
3363 /* /etc/localtime might be changed. reload it. */
3364 update_tz();
3365
3366 tm0 = *tptr;
3367 if (tm0.tm_mon < 0) {
3368 tm0.tm_mon = 0;
3369 tm0.tm_mday = 1;
3370 tm0.tm_hour = 0;
3371 tm0.tm_min = 0;
3372 tm0.tm_sec = 0;
3373 }
3374 else if (11 < tm0.tm_mon) {
3375 tm0.tm_mon = 11;
3376 tm0.tm_mday = 31;
3377 tm0.tm_hour = 23;
3378 tm0.tm_min = 59;
3379 tm0.tm_sec = 60;
3380 }
3381 else if (tm0.tm_mday < 1) {
3382 tm0.tm_mday = 1;
3383 tm0.tm_hour = 0;
3384 tm0.tm_min = 0;
3385 tm0.tm_sec = 0;
3386 }
3387 else if ((d = days_in_month_in(1900 + tm0.tm_year)[tm0.tm_mon]) < tm0.tm_mday) {
3388 tm0.tm_mday = d;
3389 tm0.tm_hour = 23;
3390 tm0.tm_min = 59;
3391 tm0.tm_sec = 60;
3392 }
3393 else if (tm0.tm_hour < 0) {
3394 tm0.tm_hour = 0;
3395 tm0.tm_min = 0;
3396 tm0.tm_sec = 0;
3397 }
3398 else if (23 < tm0.tm_hour) {
3399 tm0.tm_hour = 23;
3400 tm0.tm_min = 59;
3401 tm0.tm_sec = 60;
3402 }
3403 else if (tm0.tm_min < 0) {
3404 tm0.tm_min = 0;
3405 tm0.tm_sec = 0;
3406 }
3407 else if (59 < tm0.tm_min) {
3408 tm0.tm_min = 59;
3409 tm0.tm_sec = 60;
3410 }
3411 else if (tm0.tm_sec < 0) {
3412 tm0.tm_sec = 0;
3413 }
3414 else if (60 < tm0.tm_sec) {
3415 tm0.tm_sec = 60;
3416 }
3417
3418 DEBUG_REPORT_GUESSRANGE;
3419 guess0 = guess = timegm_noleapsecond(&tm0);
3420 tm = GUESS(&guess);
3421 if (tm) {
3422 d = tmcmp(tptr, tm);
3423 if (d == 0) { goto found; }
3424 if (d < 0) {
3425 guess_hi = guess;
3426 guess -= 24 * 60 * 60;
3427 }
3428 else {
3429 guess_lo = guess;
3430 guess += 24 * 60 * 60;
3431 }
3432 DEBUG_REPORT_GUESSRANGE;
3433 if (guess_lo < guess && guess < guess_hi && (tm = GUESS(&guess)) != NULL) {
3434 d = tmcmp(tptr, tm);
3435 if (d == 0) { goto found; }
3436 if (d < 0)
3437 guess_hi = guess;
3438 else
3439 guess_lo = guess;
3440 DEBUG_REPORT_GUESSRANGE;
3441 }
3442 }
3443
3444 tm = GUESS(&guess_lo);
3445 if (!tm) goto error;
3446 d = tmcmp(tptr, tm);
3447 if (d < 0) goto out_of_range;
3448 if (d == 0) { guess = guess_lo; goto found; }
3449 tm_lo = *tm;
3450
3451 tm = GUESS(&guess_hi);
3452 if (!tm) goto error;
3453 d = tmcmp(tptr, tm);
3454 if (d > 0) goto out_of_range;
3455 if (d == 0) { guess = guess_hi; goto found; }
3456 tm_hi = *tm;
3457
3458 DEBUG_REPORT_GUESSRANGE;
3459
3460 status = 1;
3461
3462 while (guess_lo + 1 < guess_hi) {
3463 binsearch:
3464 if (status == 0) {
3465 guess = guess_lo / 2 + guess_hi / 2;
3466 if (guess <= guess_lo)
3467 guess = guess_lo + 1;
3468 else if (guess >= guess_hi)
3469 guess = guess_hi - 1;
3470 status = 1;
3471 }
3472 else {
3473 if (status == 1) {
3474 time_t guess0_hi = timegm_noleapsecond(&tm_hi);
3475 guess = guess_hi - (guess0_hi - guess0);
3476 if (guess == guess_hi) /* hh:mm:60 tends to cause this condition. */
3477 guess--;
3478 status = 2;
3479 }
3480 else if (status == 2) {
3481 time_t guess0_lo = timegm_noleapsecond(&tm_lo);
3482 guess = guess_lo + (guess0 - guess0_lo);
3483 if (guess == guess_lo)
3484 guess++;
3485 status = 0;
3486 }
3487 if (guess <= guess_lo || guess_hi <= guess) {
3488 /* Previous guess is invalid. try binary search. */
3489 if (debug_guessrange) {
3490 if (guess <= guess_lo) {
3491 fprintf(stderr, "too small guess: %"PRI_TIMET_PREFIX"d"\
3492 " <= %"PRI_TIMET_PREFIX"d\n", guess, guess_lo);
3493 }
3494 if (guess_hi <= guess) {
3495 fprintf(stderr, "too big guess: %"PRI_TIMET_PREFIX"d"\
3496 " <= %"PRI_TIMET_PREFIX"d\n", guess_hi, guess);
3497 }
3498 }
3499 status = 0;
3500 goto binsearch;
3501 }
3502 }
3503
3504 tm = GUESS(&guess);
3505 if (!tm) goto error;
3506
3507 d = tmcmp(tptr, tm);
3508
3509 if (d < 0) {
3510 guess_hi = guess;
3511 tm_hi = *tm;
3512 DEBUG_REPORT_GUESSRANGE;
3513 }
3514 else if (d > 0) {
3515 guess_lo = guess;
3516 tm_lo = *tm;
3517 DEBUG_REPORT_GUESSRANGE;
3518 }
3519 else {
3520 goto found;
3521 }
3522 }
3523
3524 /* Given argument has no corresponding time_t. Let's extrapolate. */
3525 /*
3526 * `Seconds Since the Epoch' in SUSv3:
3527 * tm_sec + tm_min*60 + tm_hour*3600 + tm_yday*86400 +
3528 * (tm_year-70)*31536000 + ((tm_year-69)/4)*86400 -
3529 * ((tm_year-1)/100)*86400 + ((tm_year+299)/400)*86400
3530 */
3531
3532 tptr_tm_yday = calc_tm_yday(tptr->tm_year, tptr->tm_mon, tptr->tm_mday);
3533
3534 *tp = guess_lo +
3535 ((tptr->tm_year - tm_lo.tm_year) * 365 +
3536 DIV((tptr->tm_year-69), 4) -
3537 DIV((tptr->tm_year-1), 100) +
3538 DIV((tptr->tm_year+299), 400) -
3539 DIV((tm_lo.tm_year-69), 4) +
3540 DIV((tm_lo.tm_year-1), 100) -
3541 DIV((tm_lo.tm_year+299), 400) +
3542 tptr_tm_yday -
3543 tm_lo.tm_yday) * 86400 +
3544 (tptr->tm_hour - tm_lo.tm_hour) * 3600 +
3545 (tptr->tm_min - tm_lo.tm_min) * 60 +
3546 (tptr->tm_sec - (tm_lo.tm_sec == 60 ? 59 : tm_lo.tm_sec));
3547
3548 return NULL;
3549
3550 found:
3551 if (!utc_p) {
3552 /* If localtime is nonmonotonic, another result may exist. */
3553 time_t guess2;
3554 if (find_dst) {
3555 guess2 = guess - 2 * 60 * 60;
3556 tm = LOCALTIME(&guess2, result);
3557 if (tm) {
3558 if (tptr->tm_hour != (tm->tm_hour + 2) % 24 ||
3559 tptr->tm_min != tm->tm_min ||
3560 tptr->tm_sec != tm->tm_sec) {
3561 guess2 -= (tm->tm_hour - tptr->tm_hour) * 60 * 60 +
3562 (tm->tm_min - tptr->tm_min) * 60 +
3563 (tm->tm_sec - tptr->tm_sec);
3564 if (tptr->tm_mday != tm->tm_mday)
3565 guess2 += 24 * 60 * 60;
3566 if (guess != guess2) {
3567 tm = LOCALTIME(&guess2, result);
3568 if (tm && tmcmp(tptr, tm) == 0) {
3569 if (guess < guess2)
3570 *tp = guess;
3571 else
3572 *tp = guess2;
3573 return NULL;
3574 }
3575 }
3576 }
3577 }
3578 }
3579 else {
3580 guess2 = guess + 2 * 60 * 60;
3581 tm = LOCALTIME(&guess2, result);
3582 if (tm) {
3583 if ((tptr->tm_hour + 2) % 24 != tm->tm_hour ||
3584 tptr->tm_min != tm->tm_min ||
3585 tptr->tm_sec != tm->tm_sec) {
3586 guess2 -= (tm->tm_hour - tptr->tm_hour) * 60 * 60 +
3587 (tm->tm_min - tptr->tm_min) * 60 +
3588 (tm->tm_sec - tptr->tm_sec);
3589 if (tptr->tm_mday != tm->tm_mday)
3590 guess2 -= 24 * 60 * 60;
3591 if (guess != guess2) {
3592 tm = LOCALTIME(&guess2, result);
3593 if (tm && tmcmp(tptr, tm) == 0) {
3594 if (guess < guess2)
3595 *tp = guess2;
3596 else
3597 *tp = guess;
3598 return NULL;
3599 }
3600 }
3601 }
3602 }
3603 }
3604 }
3605 *tp = guess;
3606 return NULL;
3607
3608 out_of_range:
3609 return "time out of range";
3610
3611 error:
3612 return "gmtime/localtime error";
3613}
3614
3615static int
3616vtmcmp(struct vtm *a, struct vtm *b)
3617{
3618 if (ne(a->year, b->year))
3619 return lt(a->year, b->year) ? -1 : 1;
3620 else if (a->mon != b->mon)
3621 return a->mon < b->mon ? -1 : 1;
3622 else if (a->mday != b->mday)
3623 return a->mday < b->mday ? -1 : 1;
3624 else if (a->hour != b->hour)
3625 return a->hour < b->hour ? -1 : 1;
3626 else if (a->min != b->min)
3627 return a->min < b->min ? -1 : 1;
3628 else if (a->sec != b->sec)
3629 return a->sec < b->sec ? -1 : 1;
3630 else if (ne(a->subsecx, b->subsecx))
3631 return lt(a->subsecx, b->subsecx) ? -1 : 1;
3632 else
3633 return 0;
3634}
3635
3636static int
3637tmcmp(struct tm *a, struct tm *b)
3638{
3639 if (a->tm_year != b->tm_year)
3640 return a->tm_year < b->tm_year ? -1 : 1;
3641 else if (a->tm_mon != b->tm_mon)
3642 return a->tm_mon < b->tm_mon ? -1 : 1;
3643 else if (a->tm_mday != b->tm_mday)
3644 return a->tm_mday < b->tm_mday ? -1 : 1;
3645 else if (a->tm_hour != b->tm_hour)
3646 return a->tm_hour < b->tm_hour ? -1 : 1;
3647 else if (a->tm_min != b->tm_min)
3648 return a->tm_min < b->tm_min ? -1 : 1;
3649 else if (a->tm_sec != b->tm_sec)
3650 return a->tm_sec < b->tm_sec ? -1 : 1;
3651 else
3652 return 0;
3653}
3654
3655/*
3656 * call-seq:
3657 * Time.utc(year, month = 1, mday = 1, hour = 0, min = 0, sec = 0, usec = 0) -> new_time
3658 * Time.utc(sec, min, hour, mday, month, year, dummy, dummy, dummy, dummy) -> new_time
3659 *
3660 * Returns a new +Time+ object based the on given arguments,
3661 * in the UTC timezone.
3662 *
3663 * With one to seven arguments given,
3664 * the arguments are interpreted as in the first calling sequence above:
3665 *
3666 * Time.utc(year, month = 1, mday = 1, hour = 0, min = 0, sec = 0, usec = 0)
3667 *
3668 * Examples:
3669 *
3670 * Time.utc(2000) # => 2000-01-01 00:00:00 UTC
3671 * Time.utc(-2000) # => -2000-01-01 00:00:00 UTC
3672 *
3673 * There are no minimum and maximum values for the required argument +year+.
3674 *
3675 * For the optional arguments:
3676 *
3677 * - +month+: Month in range (1..12), or case-insensitive
3678 * 3-letter month name:
3679 *
3680 * Time.utc(2000, 1) # => 2000-01-01 00:00:00 UTC
3681 * Time.utc(2000, 12) # => 2000-12-01 00:00:00 UTC
3682 * Time.utc(2000, 'jan') # => 2000-01-01 00:00:00 UTC
3683 * Time.utc(2000, 'JAN') # => 2000-01-01 00:00:00 UTC
3684 *
3685 * - +mday+: Month day in range(1..31):
3686 *
3687 * Time.utc(2000, 1, 1) # => 2000-01-01 00:00:00 UTC
3688 * Time.utc(2000, 1, 31) # => 2000-01-31 00:00:00 UTC
3689 *
3690 * - +hour+: Hour in range (0..23), or 24 if +min+, +sec+, and +usec+
3691 * are zero:
3692 *
3693 * Time.utc(2000, 1, 1, 0) # => 2000-01-01 00:00:00 UTC
3694 * Time.utc(2000, 1, 1, 23) # => 2000-01-01 23:00:00 UTC
3695 * Time.utc(2000, 1, 1, 24) # => 2000-01-02 00:00:00 UTC
3696 *
3697 * - +min+: Minute in range (0..59):
3698 *
3699 * Time.utc(2000, 1, 1, 0, 0) # => 2000-01-01 00:00:00 UTC
3700 * Time.utc(2000, 1, 1, 0, 59) # => 2000-01-01 00:59:00 UTC
3701 *
3702 * - +sec+: Second in range (0..59), or 60 if +usec+ is zero:
3703 *
3704 * Time.utc(2000, 1, 1, 0, 0, 0) # => 2000-01-01 00:00:00 UTC
3705 * Time.utc(2000, 1, 1, 0, 0, 59) # => 2000-01-01 00:00:59 UTC
3706 * Time.utc(2000, 1, 1, 0, 0, 60) # => 2000-01-01 00:01:00 UTC
3707 *
3708 * - +usec+: Microsecond in range (0..999999):
3709 *
3710 * Time.utc(2000, 1, 1, 0, 0, 0, 0) # => 2000-01-01 00:00:00 UTC
3711 * Time.utc(2000, 1, 1, 0, 0, 0, 999999) # => 2000-01-01 00:00:00.999999 UTC
3712 *
3713 * The values may be:
3714 *
3715 * - Integers, as above.
3716 * - Numerics convertible to integers:
3717 *
3718 * Time.utc(Float(0.0), Rational(1, 1), 1.0, 0.0, 0.0, 0.0, 0.0)
3719 * # => 0000-01-01 00:00:00 UTC
3720 *
3721 * - String integers:
3722 *
3723 * a = %w[0 1 1 0 0 0 0 0]
3724 * # => ["0", "1", "1", "0", "0", "0", "0", "0"]
3725 * Time.utc(*a) # => 0000-01-01 00:00:00 UTC
3726 *
3727 * When exactly ten arguments are given,
3728 * the arguments are interpreted as in the second calling sequence above:
3729 *
3730 * Time.utc(sec, min, hour, mday, month, year, dummy, dummy, dummy, dummy)
3731 *
3732 * where the +dummy+ arguments are ignored:
3733 *
3734 * a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
3735 * # => [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
3736 * Time.utc(*a) # => 0005-04-03 02:01:00 UTC
3737 *
3738 * This form is useful for creating a +Time+ object from a 10-element
3739 * array returned by Time.to_a:
3740 *
3741 * t = Time.new(2000, 1, 2, 3, 4, 5, 6) # => 2000-01-02 03:04:05 +000006
3742 * a = t.to_a # => [5, 4, 3, 2, 1, 2000, 0, 2, false, nil]
3743 * Time.utc(*a) # => 2000-01-02 03:04:05 UTC
3744 *
3745 * The two forms have their first six arguments in common,
3746 * though in different orders;
3747 * the ranges of these common arguments are the same for both forms; see above.
3748 *
3749 * Raises an exception if the number of arguments is eight, nine,
3750 * or greater than ten.
3751 *
3752 * Related: Time.local.
3753 *
3754 */
3755static VALUE
3756time_s_mkutc(int argc, VALUE *argv, VALUE klass)
3757{
3758 struct vtm vtm;
3759
3760 time_arg(argc, argv, &vtm);
3761 return time_gmtime(time_new_timew(klass, timegmw(&vtm)));
3762}
3763
3764/*
3765 * call-seq:
3766 * Time.local(year, month = 1, mday = 1, hour = 0, min = 0, sec = 0, usec = 0) -> new_time
3767 * Time.local(sec, min, hour, mday, month, year, dummy, dummy, dummy, dummy) -> new_time
3768 *
3769 * Like Time.utc, except that the returned +Time+ object
3770 * has the local timezone, not the UTC timezone:
3771 *
3772 * # With seven arguments.
3773 * Time.local(0, 1, 2, 3, 4, 5, 6)
3774 * # => 0000-01-02 03:04:05.000006 -0600
3775 * # With exactly ten arguments.
3776 * Time.local(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
3777 * # => 0005-04-03 02:01:00 -0600
3778 *
3779 */
3780
3781static VALUE
3782time_s_mktime(int argc, VALUE *argv, VALUE klass)
3783{
3784 struct vtm vtm;
3785
3786 time_arg(argc, argv, &vtm);
3787 return time_localtime(time_new_timew(klass, timelocalw(&vtm)));
3788}
3789
3790/*
3791 * call-seq:
3792 * to_i -> integer
3793 *
3794 * Returns the value of +self+ as integer
3795 * {Epoch seconds}[rdoc-ref:Time@Epoch+Seconds];
3796 * subseconds are truncated (not rounded):
3797 *
3798 * Time.utc(1970, 1, 1, 0, 0, 0).to_i # => 0
3799 * Time.utc(1970, 1, 1, 0, 0, 0, 999999).to_i # => 0
3800 * Time.utc(1950, 1, 1, 0, 0, 0).to_i # => -631152000
3801 * Time.utc(1990, 1, 1, 0, 0, 0).to_i # => 631152000
3802 *
3803 * Related: Time#to_f Time#to_r.
3804 */
3805
3806static VALUE
3807time_to_i(VALUE time)
3808{
3809 struct time_object *tobj;
3810
3811 GetTimeval(time, tobj);
3812 return w2v(wdiv(tobj->timew, WINT2FIXWV(TIME_SCALE)));
3813}
3814
3815/*
3816 * call-seq:
3817 * to_f -> float
3818 *
3819 * Returns the value of +self+ as a Float number
3820 * {Epoch seconds}[rdoc-ref:Time@Epoch+Seconds];
3821 * subseconds are included.
3822 *
3823 * The stored value of +self+ is a
3824 * {Rational}[rdoc-ref:Rational@#method-i-to_f],
3825 * which means that the returned value may be approximate:
3826 *
3827 * Time.utc(1970, 1, 1, 0, 0, 0).to_f # => 0.0
3828 * Time.utc(1970, 1, 1, 0, 0, 0, 999999).to_f # => 0.999999
3829 * Time.utc(1950, 1, 1, 0, 0, 0).to_f # => -631152000.0
3830 * Time.utc(1990, 1, 1, 0, 0, 0).to_f # => 631152000.0
3831 *
3832 * Related: Time#to_i, Time#to_r.
3833 */
3834
3835static VALUE
3836time_to_f(VALUE time)
3837{
3838 struct time_object *tobj;
3839
3840 GetTimeval(time, tobj);
3841 return rb_Float(rb_time_unmagnify_to_float(tobj->timew));
3842}
3843
3844/*
3845 * call-seq:
3846 * to_r -> rational
3847 *
3848 * Returns the value of +self+ as a Rational exact number of
3849 * {Epoch seconds}[rdoc-ref:Time@Epoch+Seconds];
3850 *
3851 * Time.now.to_r # => (16571402750320203/10000000)
3852 *
3853 * Related: Time#to_f, Time#to_i.
3854 */
3855
3856static VALUE
3857time_to_r(VALUE time)
3858{
3859 struct time_object *tobj;
3860 VALUE v;
3861
3862 GetTimeval(time, tobj);
3863 v = rb_time_unmagnify_to_rational(tobj->timew);
3864 if (!RB_TYPE_P(v, T_RATIONAL)) {
3865 v = rb_Rational1(v);
3866 }
3867 return v;
3868}
3869
3870/*
3871 * call-seq:
3872 * usec -> integer
3873 *
3874 * Returns the number of microseconds in the subseconds part of +self+
3875 * in the range (0..999_999);
3876 * lower-order digits are truncated, not rounded:
3877 *
3878 * t = Time.now # => 2022-07-11 14:59:47.5484697 -0500
3879 * t.usec # => 548469
3880 *
3881 * Related: Time#subsec (returns exact subseconds).
3882 */
3883
3884static VALUE
3885time_usec(VALUE time)
3886{
3887 struct time_object *tobj;
3888 wideval_t w, q, r;
3889
3890 GetTimeval(time, tobj);
3891
3892 w = wmod(tobj->timew, WINT2WV(TIME_SCALE));
3893 wmuldivmod(w, WINT2FIXWV(1000000), WINT2FIXWV(TIME_SCALE), &q, &r);
3894 return rb_to_int(w2v(q));
3895}
3896
3897/*
3898 * call-seq:
3899 * nsec -> integer
3900 *
3901 * Returns the number of nanoseconds in the subseconds part of +self+
3902 * in the range (0..999_999_999);
3903 * lower-order digits are truncated, not rounded:
3904 *
3905 * t = Time.now # => 2022-07-11 15:04:53.3219637 -0500
3906 * t.nsec # => 321963700
3907 *
3908 * Related: Time#subsec (returns exact subseconds).
3909 */
3910
3911static VALUE
3912time_nsec(VALUE time)
3913{
3914 struct time_object *tobj;
3915
3916 GetTimeval(time, tobj);
3917 return rb_to_int(w2v(wmulquoll(wmod(tobj->timew, WINT2WV(TIME_SCALE)), 1000000000, TIME_SCALE)));
3918}
3919
3920/*
3921 * call-seq:
3922 * subsec -> numeric
3923 *
3924 * Returns the exact subseconds for +self+ as a Numeric
3925 * (Integer or Rational):
3926 *
3927 * t = Time.now # => 2022-07-11 15:11:36.8490302 -0500
3928 * t.subsec # => (4245151/5000000)
3929 *
3930 * If the subseconds is zero, returns integer zero:
3931 *
3932 * t = Time.new(2000, 1, 1, 2, 3, 4) # => 2000-01-01 02:03:04 -0600
3933 * t.subsec # => 0
3934 *
3935 */
3936
3937static VALUE
3938time_subsec(VALUE time)
3939{
3940 struct time_object *tobj;
3941
3942 GetTimeval(time, tobj);
3943 return quov(w2v(wmod(tobj->timew, WINT2FIXWV(TIME_SCALE))), INT2FIX(TIME_SCALE));
3944}
3945
3946/*
3947 * call-seq:
3948 * self <=> other_time -> -1, 0, +1, or nil
3949 *
3950 * Compares +self+ with +other_time+; returns:
3951 *
3952 * - +-1+, if +self+ is less than +other_time+.
3953 * - +0+, if +self+ is equal to +other_time+.
3954 * - +1+, if +self+ is greater then +other_time+.
3955 * - +nil+, if +self+ and +other_time+ are incomparable.
3956 *
3957 * Examples:
3958 *
3959 * t = Time.now # => 2007-11-19 08:12:12 -0600
3960 * t2 = t + 2592000 # => 2007-12-19 08:12:12 -0600
3961 * t <=> t2 # => -1
3962 * t2 <=> t # => 1
3963 *
3964 * t = Time.now # => 2007-11-19 08:13:38 -0600
3965 * t2 = t + 0.1 # => 2007-11-19 08:13:38 -0600
3966 * t.nsec # => 98222999
3967 * t2.nsec # => 198222999
3968 * t <=> t2 # => -1
3969 * t2 <=> t # => 1
3970 * t <=> t # => 0
3971 *
3972 */
3973
3974static VALUE
3975time_cmp(VALUE time1, VALUE time2)
3976{
3977 struct time_object *tobj1, *tobj2;
3978 int n;
3979
3980 GetTimeval(time1, tobj1);
3981 if (IsTimeval(time2)) {
3982 GetTimeval(time2, tobj2);
3983 n = wcmp(tobj1->timew, tobj2->timew);
3984 }
3985 else {
3986 return rb_invcmp(time1, time2);
3987 }
3988 if (n == 0) return INT2FIX(0);
3989 if (n > 0) return INT2FIX(1);
3990 return INT2FIX(-1);
3991}
3992
3993/*
3994 * call-seq:
3995 * eql?(other_time)
3996 *
3997 * Returns +true+ if +self+ and +other_time+ are
3998 * both +Time+ objects with the exact same time value.
3999 */
4000
4001static VALUE
4002time_eql(VALUE time1, VALUE time2)
4003{
4004 struct time_object *tobj1, *tobj2;
4005
4006 GetTimeval(time1, tobj1);
4007 if (IsTimeval(time2)) {
4008 GetTimeval(time2, tobj2);
4009 return rb_equal(w2v(tobj1->timew), w2v(tobj2->timew));
4010 }
4011 return Qfalse;
4012}
4013
4014/*
4015 * call-seq:
4016 * utc? -> true or false
4017 *
4018 * Returns +true+ if +self+ represents a time in UTC (GMT):
4019 *
4020 * now = Time.now
4021 * # => 2022-08-18 10:24:13.5398485 -0500
4022 * now.utc? # => false
4023 * now.getutc.utc? # => true
4024 * utc = Time.utc(2000, 1, 1, 20, 15, 1)
4025 * # => 2000-01-01 20:15:01 UTC
4026 * utc.utc? # => true
4027 *
4028 * +Time+ objects created with these methods are considered to be in
4029 * UTC:
4030 *
4031 * * Time.utc
4032 * * Time#utc
4033 * * Time#getutc
4034 *
4035 * Objects created in other ways will not be treated as UTC even if
4036 * the environment variable "TZ" is "UTC".
4037 *
4038 * Related: Time.utc.
4039 */
4040
4041static VALUE
4042time_utc_p(VALUE time)
4043{
4044 struct time_object *tobj;
4045
4046 GetTimeval(time, tobj);
4047 return RBOOL(TZMODE_UTC_P(tobj));
4048}
4049
4050/*
4051 * call-seq:
4052 * hash -> integer
4053 *
4054 * Returns the integer hash code for +self+.
4055 *
4056 * Related: Object#hash.
4057 */
4058
4059static VALUE
4060time_hash(VALUE time)
4061{
4062 struct time_object *tobj;
4063
4064 GetTimeval(time, tobj);
4065 return rb_hash(w2v(tobj->timew));
4066}
4067
4068/* :nodoc: */
4069static VALUE
4070time_init_copy(VALUE copy, VALUE time)
4071{
4072 struct time_object *tobj, *tcopy;
4073
4074 if (!OBJ_INIT_COPY(copy, time)) return copy;
4075 GetTimeval(time, tobj);
4076 GetNewTimeval(copy, tcopy);
4077
4078 time_set_timew(copy, tcopy, tobj->timew);
4079 time_set_vtm(copy, tcopy, tobj->vtm);
4080
4081 return copy;
4082}
4083
4084static VALUE
4085time_dup(VALUE time)
4086{
4087 VALUE dup = time_s_alloc(rb_obj_class(time));
4088 time_init_copy(dup, time);
4089 return dup;
4090}
4091
4092static VALUE
4093time_localtime(VALUE time)
4094{
4095 struct time_object *tobj;
4096 struct vtm vtm;
4097 VALUE zone;
4098
4099 GetTimeval(time, tobj);
4100 if (TZMODE_LOCALTIME_P(tobj)) {
4101 if (tobj->vtm.tm_got)
4102 return time;
4103 }
4104 else {
4105 time_modify(time);
4106 }
4107
4108 zone = tobj->vtm.zone;
4109 if (maybe_tzobj_p(zone) && zone_localtime(zone, time)) {
4110 return time;
4111 }
4112
4113 if (!localtimew(tobj->timew, &vtm))
4114 rb_raise(rb_eArgError, "localtime error");
4115 time_set_vtm(time, tobj, vtm);
4116
4117 tobj->vtm.tm_got = 1;
4118 TZMODE_SET_LOCALTIME(tobj);
4119 return time;
4120}
4121
4122static VALUE
4123time_zonelocal(VALUE time, VALUE off)
4124{
4125 VALUE zone = off;
4126 if (zone_localtime(zone, time)) return time;
4127
4128 if (NIL_P(off = utc_offset_arg(off))) {
4129 off = zone;
4130 if (NIL_P(zone = find_timezone(time, off))) invalid_utc_offset(off);
4131 if (!zone_localtime(zone, time)) invalid_utc_offset(off);
4132 return time;
4133 }
4134 else if (off == UTC_ZONE) {
4135 return time_gmtime(time);
4136 }
4137 validate_utc_offset(off);
4138
4139 time_set_utc_offset(time, off);
4140 return time_fixoff(time);
4141}
4142
4143/*
4144 * call-seq:
4145 * localtime -> self or new_time
4146 * localtime(zone) -> new_time
4147 *
4148 * With no argument given:
4149 *
4150 * - Returns +self+ if +self+ is a local time.
4151 * - Otherwise returns a new +Time+ in the user's local timezone:
4152 *
4153 * t = Time.utc(2000, 1, 1, 20, 15, 1) # => 2000-01-01 20:15:01 UTC
4154 * t.localtime # => 2000-01-01 14:15:01 -0600
4155 *
4156 * With argument +zone+ given,
4157 * returns the new +Time+ object created by converting
4158 * +self+ to the given time zone:
4159 *
4160 * t = Time.utc(2000, 1, 1, 20, 15, 1) # => 2000-01-01 20:15:01 UTC
4161 * t.localtime("-09:00") # => 2000-01-01 11:15:01 -0900
4162 *
4163 * For forms of argument +zone+, see
4164 * {Timezone Specifiers}[rdoc-ref:Time@Timezone+Specifiers].
4165 *
4166 */
4167
4168static VALUE
4169time_localtime_m(int argc, VALUE *argv, VALUE time)
4170{
4171 VALUE off;
4172
4173 if (rb_check_arity(argc, 0, 1) && !NIL_P(off = argv[0])) {
4174 return time_zonelocal(time, off);
4175 }
4176
4177 return time_localtime(time);
4178}
4179
4180/*
4181 * call-seq:
4182 * utc -> self
4183 *
4184 * Returns +self+, converted to the UTC timezone:
4185 *
4186 * t = Time.new(2000) # => 2000-01-01 00:00:00 -0600
4187 * t.utc? # => false
4188 * t.utc # => 2000-01-01 06:00:00 UTC
4189 * t.utc? # => true
4190 *
4191 * Related: Time#getutc (returns a new converted +Time+ object).
4192 */
4193
4194static VALUE
4195time_gmtime(VALUE time)
4196{
4197 struct time_object *tobj;
4198 struct vtm vtm;
4199
4200 GetTimeval(time, tobj);
4201 if (TZMODE_UTC_P(tobj)) {
4202 if (tobj->vtm.tm_got)
4203 return time;
4204 }
4205 else {
4206 time_modify(time);
4207 }
4208
4209 vtm.zone = str_utc;
4210 GMTIMEW(tobj->timew, &vtm);
4211 time_set_vtm(time, tobj, vtm);
4212
4213 tobj->vtm.tm_got = 1;
4214 TZMODE_SET_UTC(tobj);
4215 return time;
4216}
4217
4218static VALUE
4219time_fixoff(VALUE time)
4220{
4221 struct time_object *tobj;
4222 struct vtm vtm;
4223 VALUE off, zone;
4224
4225 GetTimeval(time, tobj);
4226 if (TZMODE_FIXOFF_P(tobj)) {
4227 if (tobj->vtm.tm_got)
4228 return time;
4229 }
4230 else {
4231 time_modify(time);
4232 }
4233
4234 if (TZMODE_FIXOFF_P(tobj))
4235 off = tobj->vtm.utc_offset;
4236 else
4237 off = INT2FIX(0);
4238
4239 GMTIMEW(tobj->timew, &vtm);
4240
4241 zone = tobj->vtm.zone;
4242 vtm_add_offset(&vtm, off, +1);
4243
4244 time_set_vtm(time, tobj, vtm);
4245 RB_OBJ_WRITE_UNALIGNED(time, &tobj->vtm.zone, zone);
4246
4247 tobj->vtm.tm_got = 1;
4248 TZMODE_SET_FIXOFF(time, tobj, off);
4249 return time;
4250}
4251
4252/*
4253 * call-seq:
4254 * getlocal(zone = nil) -> new_time
4255 *
4256 * Returns a new +Time+ object representing the value of +self+
4257 * converted to a given timezone;
4258 * if +zone+ is +nil+, the local timezone is used:
4259 *
4260 * t = Time.utc(2000) # => 2000-01-01 00:00:00 UTC
4261 * t.getlocal # => 1999-12-31 18:00:00 -0600
4262 * t.getlocal('+12:00') # => 2000-01-01 12:00:00 +1200
4263 *
4264 * For forms of argument +zone+, see
4265 * {Timezone Specifiers}[rdoc-ref:Time@Timezone+Specifiers].
4266 *
4267 */
4268
4269static VALUE
4270time_getlocaltime(int argc, VALUE *argv, VALUE time)
4271{
4272 VALUE off;
4273
4274 if (rb_check_arity(argc, 0, 1) && !NIL_P(off = argv[0])) {
4275 VALUE zone = off;
4276 if (maybe_tzobj_p(zone)) {
4277 VALUE t = time_dup(time);
4278 if (zone_localtime(off, t)) return t;
4279 }
4280
4281 if (NIL_P(off = utc_offset_arg(off))) {
4282 off = zone;
4283 if (NIL_P(zone = find_timezone(time, off))) invalid_utc_offset(off);
4284 time = time_dup(time);
4285 if (!zone_localtime(zone, time)) invalid_utc_offset(off);
4286 return time;
4287 }
4288 else if (off == UTC_ZONE) {
4289 return time_gmtime(time_dup(time));
4290 }
4291 validate_utc_offset(off);
4292
4293 time = time_dup(time);
4294 time_set_utc_offset(time, off);
4295 return time_fixoff(time);
4296 }
4297
4298 return time_localtime(time_dup(time));
4299}
4300
4301/*
4302 * call-seq:
4303 * getutc -> new_time
4304 *
4305 * Returns a new +Time+ object representing the value of +self+
4306 * converted to the UTC timezone:
4307 *
4308 * local = Time.local(2000) # => 2000-01-01 00:00:00 -0600
4309 * local.utc? # => false
4310 * utc = local.getutc # => 2000-01-01 06:00:00 UTC
4311 * utc.utc? # => true
4312 * utc == local # => true
4313 *
4314 */
4315
4316static VALUE
4317time_getgmtime(VALUE time)
4318{
4319 return time_gmtime(time_dup(time));
4320}
4321
4322static VALUE
4323time_get_tm(VALUE time, struct time_object *tobj)
4324{
4325 if (TZMODE_UTC_P(tobj)) return time_gmtime(time);
4326 if (TZMODE_FIXOFF_P(tobj)) return time_fixoff(time);
4327 return time_localtime(time);
4328}
4329
4330static VALUE strftime_cstr(const char *fmt, size_t len, VALUE time, rb_encoding *enc);
4331#define strftimev(fmt, time, enc) strftime_cstr((fmt), rb_strlen_lit(fmt), (time), (enc))
4332
4333/*
4334 * call-seq:
4335 * ctime -> string
4336 *
4337 * Returns a string representation of +self+,
4338 * formatted by <tt>strftime('%a %b %e %T %Y')</tt>
4339 * or its shorthand version <tt>strftime('%c')</tt>;
4340 * see {Formats for Dates and Times}[rdoc-ref:strftime_formatting.rdoc]:
4341 *
4342 * t = Time.new(2000, 12, 31, 23, 59, 59, 0.5)
4343 * t.ctime # => "Sun Dec 31 23:59:59 2000"
4344 * t.strftime('%a %b %e %T %Y') # => "Sun Dec 31 23:59:59 2000"
4345 * t.strftime('%c') # => "Sun Dec 31 23:59:59 2000"
4346 *
4347 * Related: Time#to_s, Time#inspect:
4348 *
4349 * t.inspect # => "2000-12-31 23:59:59.5 +000001"
4350 * t.to_s # => "2000-12-31 23:59:59 +0000"
4351 *
4352 */
4353
4354static VALUE
4355time_asctime(VALUE time)
4356{
4357 return strftimev("%a %b %e %T %Y", time, rb_usascii_encoding());
4358}
4359
4360/*
4361 * call-seq:
4362 * to_s -> string
4363 *
4364 * Returns a string representation of +self+, without subseconds:
4365 *
4366 * t = Time.new(2000, 12, 31, 23, 59, 59, 0.5)
4367 * t.to_s # => "2000-12-31 23:59:59 +0000"
4368 *
4369 * Related: Time#ctime, Time#inspect:
4370 *
4371 * t.ctime # => "Sun Dec 31 23:59:59 2000"
4372 * t.inspect # => "2000-12-31 23:59:59.5 +000001"
4373 *
4374 */
4375
4376static VALUE
4377time_to_s(VALUE time)
4378{
4379 struct time_object *tobj;
4380
4381 GetTimeval(time, tobj);
4382 if (TZMODE_UTC_P(tobj))
4383 return strftimev("%Y-%m-%d %H:%M:%S UTC", time, rb_usascii_encoding());
4384 else
4385 return strftimev("%Y-%m-%d %H:%M:%S %z", time, rb_usascii_encoding());
4386}
4387
4388/*
4389 * call-seq:
4390 * inspect -> string
4391 *
4392 * Returns a string representation of +self+ with subseconds:
4393 *
4394 * t = Time.new(2000, 12, 31, 23, 59, 59, 0.5)
4395 * t.inspect # => "2000-12-31 23:59:59.5 +000001"
4396 *
4397 * Related: Time#ctime, Time#to_s:
4398 *
4399 * t.ctime # => "Sun Dec 31 23:59:59 2000"
4400 * t.to_s # => "2000-12-31 23:59:59 +0000"
4401 *
4402 */
4403
4404static VALUE
4405time_inspect(VALUE time)
4406{
4407 struct time_object *tobj;
4408 VALUE str, subsec;
4409
4410 GetTimeval(time, tobj);
4411 str = strftimev("%Y-%m-%d %H:%M:%S", time, rb_usascii_encoding());
4412 subsec = w2v(wmod(tobj->timew, WINT2FIXWV(TIME_SCALE)));
4413 if (subsec == INT2FIX(0)) {
4414 }
4415 else if (FIXNUM_P(subsec) && FIX2LONG(subsec) < TIME_SCALE) {
4416 long len;
4417 rb_str_catf(str, ".%09ld", FIX2LONG(subsec));
4418 for (len=RSTRING_LEN(str); RSTRING_PTR(str)[len-1] == '0' && len > 0; len--)
4419 ;
4420 rb_str_resize(str, len);
4421 }
4422 else {
4423 rb_str_cat_cstr(str, " ");
4424 subsec = quov(subsec, INT2FIX(TIME_SCALE));
4425 rb_str_concat(str, rb_obj_as_string(subsec));
4426 }
4427 if (TZMODE_UTC_P(tobj)) {
4428 rb_str_cat_cstr(str, " UTC");
4429 }
4430 else {
4431 /* ?TODO: subsecond offset */
4432 long off = NUM2LONG(rb_funcall(tobj->vtm.utc_offset, rb_intern("round"), 0));
4433 char sign = (off < 0) ? (off = -off, '-') : '+';
4434 int sec = off % 60;
4435 int min = (off /= 60) % 60;
4436 off /= 60;
4437 rb_str_catf(str, " %c%.2d%.2d", sign, (int)off, min);
4438 if (sec) rb_str_catf(str, "%.2d", sec);
4439 }
4440 return str;
4441}
4442
4443static VALUE
4444time_add0(VALUE klass, const struct time_object *tobj, VALUE torig, VALUE offset, int sign)
4445{
4446 VALUE result;
4447 struct time_object *result_tobj;
4448
4449 offset = num_exact(offset);
4450 if (sign < 0)
4451 result = time_new_timew(klass, wsub(tobj->timew, rb_time_magnify(v2w(offset))));
4452 else
4453 result = time_new_timew(klass, wadd(tobj->timew, rb_time_magnify(v2w(offset))));
4454 GetTimeval(result, result_tobj);
4455 TZMODE_COPY(result_tobj, tobj);
4456
4457 return result;
4458}
4459
4460static VALUE
4461time_add(const struct time_object *tobj, VALUE torig, VALUE offset, int sign)
4462{
4463 return time_add0(rb_cTime, tobj, torig, offset, sign);
4464}
4465
4466/*
4467 * call-seq:
4468 * self + numeric -> new_time
4469 *
4470 * Returns a new +Time+ object whose value is the sum of the numeric value
4471 * of +self+ and the given +numeric+:
4472 *
4473 * t = Time.new(2000) # => 2000-01-01 00:00:00 -0600
4474 * t + (60 * 60 * 24) # => 2000-01-02 00:00:00 -0600
4475 * t + 0.5 # => 2000-01-01 00:00:00.5 -0600
4476 *
4477 * Related: Time#-.
4478 */
4479
4480static VALUE
4481time_plus(VALUE time1, VALUE time2)
4482{
4483 struct time_object *tobj;
4484 GetTimeval(time1, tobj);
4485
4486 if (IsTimeval(time2)) {
4487 rb_raise(rb_eTypeError, "time + time?");
4488 }
4489 return time_add(tobj, time1, time2, 1);
4490}
4491
4492/*
4493 * call-seq:
4494 * self - numeric -> new_time
4495 * self - other_time -> float
4496 *
4497 * When +numeric+ is given,
4498 * returns a new +Time+ object whose value is the difference
4499 * of the numeric value of +self+ and +numeric+:
4500 *
4501 * t = Time.new(2000) # => 2000-01-01 00:00:00 -0600
4502 * t - (60 * 60 * 24) # => 1999-12-31 00:00:00 -0600
4503 * t - 0.5 # => 1999-12-31 23:59:59.5 -0600
4504 *
4505 * When +other_time+ is given,
4506 * returns a Float whose value is the difference
4507 * of the numeric values of +self+ and +other_time+ in seconds:
4508 *
4509 * t - t # => 0.0
4510 *
4511 * Related: Time#+.
4512 */
4513
4514static VALUE
4515time_minus(VALUE time1, VALUE time2)
4516{
4517 struct time_object *tobj;
4518
4519 GetTimeval(time1, tobj);
4520 if (IsTimeval(time2)) {
4521 struct time_object *tobj2;
4522
4523 GetTimeval(time2, tobj2);
4524 return rb_Float(rb_time_unmagnify_to_float(wsub(tobj->timew, tobj2->timew)));
4525 }
4526 return time_add(tobj, time1, time2, -1);
4527}
4528
4529static VALUE
4530ndigits_denominator(VALUE ndigits)
4531{
4532 long nd = NUM2LONG(ndigits);
4533
4534 if (nd < 0) {
4535 rb_raise(rb_eArgError, "negative ndigits given");
4536 }
4537 if (nd == 0) {
4538 return INT2FIX(1);
4539 }
4540 return rb_rational_new(INT2FIX(1),
4541 rb_int_positive_pow(10, (unsigned long)nd));
4542}
4543
4544/*
4545 * call-seq:
4546 * round(ndigits = 0) -> new_time
4547 *
4548 * Returns a new +Time+ object whose numeric value is that of +self+,
4549 * with its seconds value rounded to precision +ndigits+:
4550 *
4551 * t = Time.utc(2010, 3, 30, 5, 43, 25.123456789r)
4552 * t # => 2010-03-30 05:43:25.123456789 UTC
4553 * t.round # => 2010-03-30 05:43:25 UTC
4554 * t.round(0) # => 2010-03-30 05:43:25 UTC
4555 * t.round(1) # => 2010-03-30 05:43:25.1 UTC
4556 * t.round(2) # => 2010-03-30 05:43:25.12 UTC
4557 * t.round(3) # => 2010-03-30 05:43:25.123 UTC
4558 * t.round(4) # => 2010-03-30 05:43:25.1235 UTC
4559 *
4560 * t = Time.utc(1999, 12,31, 23, 59, 59)
4561 * t # => 1999-12-31 23:59:59 UTC
4562 * (t + 0.4).round # => 1999-12-31 23:59:59 UTC
4563 * (t + 0.49).round # => 1999-12-31 23:59:59 UTC
4564 * (t + 0.5).round # => 2000-01-01 00:00:00 UTC
4565 * (t + 1.4).round # => 2000-01-01 00:00:00 UTC
4566 * (t + 1.49).round # => 2000-01-01 00:00:00 UTC
4567 * (t + 1.5).round # => 2000-01-01 00:00:01 UTC
4568 *
4569 * Related: Time#ceil, Time#floor.
4570 */
4571
4572static VALUE
4573time_round(int argc, VALUE *argv, VALUE time)
4574{
4575 VALUE ndigits, v, den;
4576 struct time_object *tobj;
4577
4578 if (!rb_check_arity(argc, 0, 1) || NIL_P(ndigits = argv[0]))
4579 den = INT2FIX(1);
4580 else
4581 den = ndigits_denominator(ndigits);
4582
4583 GetTimeval(time, tobj);
4584 v = w2v(rb_time_unmagnify(tobj->timew));
4585
4586 v = modv(v, den);
4587 if (lt(v, quov(den, INT2FIX(2))))
4588 return time_add(tobj, time, v, -1);
4589 else
4590 return time_add(tobj, time, subv(den, v), 1);
4591}
4592
4593/*
4594 * call-seq:
4595 * floor(ndigits = 0) -> new_time
4596 *
4597 * Returns a new +Time+ object whose numerical value
4598 * is less than or equal to +self+ with its seconds
4599 * truncated to precision +ndigits+:
4600 *
4601 * t = Time.utc(2010, 3, 30, 5, 43, 25.123456789r)
4602 * t # => 2010-03-30 05:43:25.123456789 UTC
4603 * t.floor # => 2010-03-30 05:43:25 UTC
4604 * t.floor(2) # => 2010-03-30 05:43:25.12 UTC
4605 * t.floor(4) # => 2010-03-30 05:43:25.1234 UTC
4606 * t.floor(6) # => 2010-03-30 05:43:25.123456 UTC
4607 * t.floor(8) # => 2010-03-30 05:43:25.12345678 UTC
4608 * t.floor(10) # => 2010-03-30 05:43:25.123456789 UTC
4609 *
4610 * t = Time.utc(1999, 12, 31, 23, 59, 59)
4611 * t # => 1999-12-31 23:59:59 UTC
4612 * (t + 0.4).floor # => 1999-12-31 23:59:59 UTC
4613 * (t + 0.9).floor # => 1999-12-31 23:59:59 UTC
4614 * (t + 1.4).floor # => 2000-01-01 00:00:00 UTC
4615 * (t + 1.9).floor # => 2000-01-01 00:00:00 UTC
4616 *
4617 * Related: Time#ceil, Time#round.
4618 */
4619
4620static VALUE
4621time_floor(int argc, VALUE *argv, VALUE time)
4622{
4623 VALUE ndigits, v, den;
4624 struct time_object *tobj;
4625
4626 if (!rb_check_arity(argc, 0, 1) || NIL_P(ndigits = argv[0]))
4627 den = INT2FIX(1);
4628 else
4629 den = ndigits_denominator(ndigits);
4630
4631 GetTimeval(time, tobj);
4632 v = w2v(rb_time_unmagnify(tobj->timew));
4633
4634 v = modv(v, den);
4635 return time_add(tobj, time, v, -1);
4636}
4637
4638/*
4639 * call-seq:
4640 * ceil(ndigits = 0) -> new_time
4641 *
4642 * Returns a new +Time+ object whose numerical value
4643 * is greater than or equal to +self+ with its seconds
4644 * truncated to precision +ndigits+:
4645 *
4646 * t = Time.utc(2010, 3, 30, 5, 43, 25.123456789r)
4647 * t # => 2010-03-30 05:43:25.123456789 UTC
4648 * t.ceil # => 2010-03-30 05:43:26 UTC
4649 * t.ceil(2) # => 2010-03-30 05:43:25.13 UTC
4650 * t.ceil(4) # => 2010-03-30 05:43:25.1235 UTC
4651 * t.ceil(6) # => 2010-03-30 05:43:25.123457 UTC
4652 * t.ceil(8) # => 2010-03-30 05:43:25.12345679 UTC
4653 * t.ceil(10) # => 2010-03-30 05:43:25.123456789 UTC
4654 *
4655 * t = Time.utc(1999, 12, 31, 23, 59, 59)
4656 * t # => 1999-12-31 23:59:59 UTC
4657 * (t + 0.4).ceil # => 2000-01-01 00:00:00 UTC
4658 * (t + 0.9).ceil # => 2000-01-01 00:00:00 UTC
4659 * (t + 1.4).ceil # => 2000-01-01 00:00:01 UTC
4660 * (t + 1.9).ceil # => 2000-01-01 00:00:01 UTC
4661 *
4662 * Related: Time#floor, Time#round.
4663 */
4664
4665static VALUE
4666time_ceil(int argc, VALUE *argv, VALUE time)
4667{
4668 VALUE ndigits, v, den;
4669 struct time_object *tobj;
4670
4671 if (!rb_check_arity(argc, 0, 1) || NIL_P(ndigits = argv[0]))
4672 den = INT2FIX(1);
4673 else
4674 den = ndigits_denominator(ndigits);
4675
4676 GetTimeval(time, tobj);
4677 v = w2v(rb_time_unmagnify(tobj->timew));
4678
4679 v = modv(v, den);
4680 if (!rb_equal(v, INT2FIX(0))) {
4681 v = subv(den, v);
4682 }
4683 return time_add(tobj, time, v, 1);
4684}
4685
4686/*
4687 * call-seq:
4688 * sec -> integer
4689 *
4690 * Returns the integer second of the minute for +self+,
4691 * in range (0..60):
4692 *
4693 * t = Time.new(2000, 1, 2, 3, 4, 5, 6)
4694 * # => 2000-01-02 03:04:05 +000006
4695 * t.sec # => 5
4696 *
4697 * Note: the second value may be 60 when there is a
4698 * {leap second}[https://en.wikipedia.org/wiki/Leap_second].
4699 *
4700 * Related: Time#year, Time#mon, Time#min.
4701 */
4702
4703static VALUE
4704time_sec(VALUE time)
4705{
4706 struct time_object *tobj;
4707
4708 GetTimeval(time, tobj);
4709 MAKE_TM(time, tobj);
4710 return INT2FIX(tobj->vtm.sec);
4711}
4712
4713/*
4714 * call-seq:
4715 * min -> integer
4716 *
4717 * Returns the integer minute of the hour for +self+,
4718 * in range (0..59):
4719 *
4720 * t = Time.new(2000, 1, 2, 3, 4, 5, 6)
4721 * # => 2000-01-02 03:04:05 +000006
4722 * t.min # => 4
4723 *
4724 * Related: Time#year, Time#mon, Time#sec.
4725 */
4726
4727static VALUE
4728time_min(VALUE time)
4729{
4730 struct time_object *tobj;
4731
4732 GetTimeval(time, tobj);
4733 MAKE_TM(time, tobj);
4734 return INT2FIX(tobj->vtm.min);
4735}
4736
4737/*
4738 * call-seq:
4739 * hour -> integer
4740 *
4741 * Returns the integer hour of the day for +self+,
4742 * in range (0..23):
4743 *
4744 * t = Time.new(2000, 1, 2, 3, 4, 5, 6)
4745 * # => 2000-01-02 03:04:05 +000006
4746 * t.hour # => 3
4747 *
4748 * Related: Time#year, Time#mon, Time#min.
4749 */
4750
4751static VALUE
4752time_hour(VALUE time)
4753{
4754 struct time_object *tobj;
4755
4756 GetTimeval(time, tobj);
4757 MAKE_TM(time, tobj);
4758 return INT2FIX(tobj->vtm.hour);
4759}
4760
4761/*
4762 * call-seq:
4763 * mday -> integer
4764 *
4765 * Returns the integer day of the month for +self+,
4766 * in range (1..31):
4767 *
4768 * t = Time.new(2000, 1, 2, 3, 4, 5, 6)
4769 * # => 2000-01-02 03:04:05 +000006
4770 * t.mday # => 2
4771 *
4772 * Related: Time#year, Time#hour, Time#min.
4773 */
4774
4775static VALUE
4776time_mday(VALUE time)
4777{
4778 struct time_object *tobj;
4779
4780 GetTimeval(time, tobj);
4781 MAKE_TM(time, tobj);
4782 return INT2FIX(tobj->vtm.mday);
4783}
4784
4785/*
4786 * call-seq:
4787 * mon -> integer
4788 *
4789 * Returns the integer month of the year for +self+,
4790 * in range (1..12):
4791 *
4792 * t = Time.new(2000, 1, 2, 3, 4, 5, 6)
4793 * # => 2000-01-02 03:04:05 +000006
4794 * t.mon # => 1
4795 *
4796 * Related: Time#year, Time#hour, Time#min.
4797 */
4798
4799static VALUE
4800time_mon(VALUE time)
4801{
4802 struct time_object *tobj;
4803
4804 GetTimeval(time, tobj);
4805 MAKE_TM(time, tobj);
4806 return INT2FIX(tobj->vtm.mon);
4807}
4808
4809/*
4810 * call-seq:
4811 * year -> integer
4812 *
4813 * Returns the integer year for +self+:
4814 *
4815 * t = Time.new(2000, 1, 2, 3, 4, 5, 6)
4816 * # => 2000-01-02 03:04:05 +000006
4817 * t.year # => 2000
4818 *
4819 * Related: Time#mon, Time#hour, Time#min.
4820 */
4821
4822static VALUE
4823time_year(VALUE time)
4824{
4825 struct time_object *tobj;
4826
4827 GetTimeval(time, tobj);
4828 MAKE_TM(time, tobj);
4829 return tobj->vtm.year;
4830}
4831
4832/*
4833 * call-seq:
4834 * wday -> integer
4835 *
4836 * Returns the integer day of the week for +self+,
4837 * in range (0..6), with Sunday as zero.
4838 *
4839 * t = Time.new(2000, 1, 2, 3, 4, 5, 6)
4840 * # => 2000-01-02 03:04:05 +000006
4841 * t.wday # => 0
4842 * t.sunday? # => true
4843 *
4844 * Related: Time#year, Time#hour, Time#min.
4845 */
4846
4847static VALUE
4848time_wday(VALUE time)
4849{
4850 struct time_object *tobj;
4851
4852 GetTimeval(time, tobj);
4853 MAKE_TM_ENSURE(time, tobj, tobj->vtm.wday != VTM_WDAY_INITVAL);
4854 return INT2FIX((int)tobj->vtm.wday);
4855}
4856
4857#define wday_p(n) {\
4858 return RBOOL(time_wday(time) == INT2FIX(n)); \
4859}
4860
4861/*
4862 * call-seq:
4863 * sunday? -> true or false
4864 *
4865 * Returns +true+ if +self+ represents a Sunday, +false+ otherwise:
4866 *
4867 * t = Time.utc(2000, 1, 2) # => 2000-01-02 00:00:00 UTC
4868 * t.sunday? # => true
4869 *
4870 * Related: Time#monday?, Time#tuesday?, Time#wednesday?.
4871 */
4872
4873static VALUE
4874time_sunday(VALUE time)
4875{
4876 wday_p(0);
4877}
4878
4879/*
4880 * call-seq:
4881 * monday? -> true or false
4882 *
4883 * Returns +true+ if +self+ represents a Monday, +false+ otherwise:
4884 *
4885 * t = Time.utc(2000, 1, 3) # => 2000-01-03 00:00:00 UTC
4886 * t.monday? # => true
4887 *
4888 * Related: Time#tuesday?, Time#wednesday?, Time#thursday?.
4889 */
4890
4891static VALUE
4892time_monday(VALUE time)
4893{
4894 wday_p(1);
4895}
4896
4897/*
4898 * call-seq:
4899 * tuesday? -> true or false
4900 *
4901 * Returns +true+ if +self+ represents a Tuesday, +false+ otherwise:
4902 *
4903 * t = Time.utc(2000, 1, 4) # => 2000-01-04 00:00:00 UTC
4904 * t.tuesday? # => true
4905 *
4906 * Related: Time#wednesday?, Time#thursday?, Time#friday?.
4907 */
4908
4909static VALUE
4910time_tuesday(VALUE time)
4911{
4912 wday_p(2);
4913}
4914
4915/*
4916 * call-seq:
4917 * wednesday? -> true or false
4918 *
4919 * Returns +true+ if +self+ represents a Wednesday, +false+ otherwise:
4920 *
4921 * t = Time.utc(2000, 1, 5) # => 2000-01-05 00:00:00 UTC
4922 * t.wednesday? # => true
4923 *
4924 * Related: Time#thursday?, Time#friday?, Time#saturday?.
4925 */
4926
4927static VALUE
4928time_wednesday(VALUE time)
4929{
4930 wday_p(3);
4931}
4932
4933/*
4934 * call-seq:
4935 * thursday? -> true or false
4936 *
4937 * Returns +true+ if +self+ represents a Thursday, +false+ otherwise:
4938 *
4939 * t = Time.utc(2000, 1, 6) # => 2000-01-06 00:00:00 UTC
4940 * t.thursday? # => true
4941 *
4942 * Related: Time#friday?, Time#saturday?, Time#sunday?.
4943 */
4944
4945static VALUE
4946time_thursday(VALUE time)
4947{
4948 wday_p(4);
4949}
4950
4951/*
4952 * call-seq:
4953 * friday? -> true or false
4954 *
4955 * Returns +true+ if +self+ represents a Friday, +false+ otherwise:
4956 *
4957 * t = Time.utc(2000, 1, 7) # => 2000-01-07 00:00:00 UTC
4958 * t.friday? # => true
4959 *
4960 * Related: Time#saturday?, Time#sunday?, Time#monday?.
4961 */
4962
4963static VALUE
4964time_friday(VALUE time)
4965{
4966 wday_p(5);
4967}
4968
4969/*
4970 * call-seq:
4971 * saturday? -> true or false
4972 *
4973 * Returns +true+ if +self+ represents a Saturday, +false+ otherwise:
4974 *
4975 * t = Time.utc(2000, 1, 1) # => 2000-01-01 00:00:00 UTC
4976 * t.saturday? # => true
4977 *
4978 * Related: Time#sunday?, Time#monday?, Time#tuesday?.
4979 */
4980
4981static VALUE
4982time_saturday(VALUE time)
4983{
4984 wday_p(6);
4985}
4986
4987/*
4988 * call-seq:
4989 * yday -> integer
4990 *
4991 * Returns the integer day of the year of +self+, in range (1..366).
4992 *
4993 * Time.new(2000, 1, 1).yday # => 1
4994 * Time.new(2000, 12, 31).yday # => 366
4995 */
4996
4997static VALUE
4998time_yday(VALUE time)
4999{
5000 struct time_object *tobj;
5001
5002 GetTimeval(time, tobj);
5003 MAKE_TM_ENSURE(time, tobj, tobj->vtm.yday != 0);
5004 return INT2FIX(tobj->vtm.yday);
5005}
5006
5007/*
5008 * call-seq:
5009 * dst? -> true or false
5010 *
5011 * Returns +true+ if +self+ is in daylight saving time, +false+ otherwise:
5012 *
5013 * t = Time.local(2000, 1, 1) # => 2000-01-01 00:00:00 -0600
5014 * t.zone # => "Central Standard Time"
5015 * t.dst? # => false
5016 * t = Time.local(2000, 7, 1) # => 2000-07-01 00:00:00 -0500
5017 * t.zone # => "Central Daylight Time"
5018 * t.dst? # => true
5019 *
5020 */
5021
5022static VALUE
5023time_isdst(VALUE time)
5024{
5025 struct time_object *tobj;
5026
5027 GetTimeval(time, tobj);
5028 MAKE_TM(time, tobj);
5029 if (tobj->vtm.isdst == VTM_ISDST_INITVAL) {
5030 rb_raise(rb_eRuntimeError, "isdst is not set yet");
5031 }
5032 return RBOOL(tobj->vtm.isdst);
5033}
5034
5035/*
5036 * call-seq:
5037 * time.zone -> string or timezone
5038 *
5039 * Returns the string name of the time zone for +self+:
5040 *
5041 * Time.utc(2000, 1, 1).zone # => "UTC"
5042 * Time.new(2000, 1, 1).zone # => "Central Standard Time"
5043 */
5044
5045static VALUE
5046time_zone(VALUE time)
5047{
5048 struct time_object *tobj;
5049 VALUE zone;
5050
5051 GetTimeval(time, tobj);
5052 MAKE_TM(time, tobj);
5053
5054 if (TZMODE_UTC_P(tobj)) {
5055 return rb_usascii_str_new_cstr("UTC");
5056 }
5057 zone = tobj->vtm.zone;
5058 if (NIL_P(zone))
5059 return Qnil;
5060
5061 if (RB_TYPE_P(zone, T_STRING))
5062 zone = rb_str_dup(zone);
5063 return zone;
5064}
5065
5066/*
5067 * call-seq:
5068 * utc_offset -> integer
5069 *
5070 * Returns the offset in seconds between the timezones of UTC and +self+:
5071 *
5072 * Time.utc(2000, 1, 1).utc_offset # => 0
5073 * Time.local(2000, 1, 1).utc_offset # => -21600 # -6*3600, or minus six hours.
5074 *
5075 */
5076
5077VALUE
5079{
5080 struct time_object *tobj;
5081
5082 GetTimeval(time, tobj);
5083
5084 if (TZMODE_UTC_P(tobj)) {
5085 return INT2FIX(0);
5086 }
5087 else {
5088 MAKE_TM(time, tobj);
5089 return tobj->vtm.utc_offset;
5090 }
5091}
5092
5093/*
5094 * call-seq:
5095 * to_a -> array
5096 *
5097 * Returns a 10-element array of values representing +self+:
5098 *
5099 * Time.utc(2000, 1, 1).to_a
5100 * # => [0, 0, 0, 1, 1, 2000, 6, 1, false, "UTC"]
5101 * # [sec, min, hour, day, mon, year, wday, yday, dst?, zone]
5102 *
5103 * The returned array is suitable for use as an argument to Time.utc or Time.local
5104 * to create a new +Time+ object.
5105 *
5106 */
5107
5108static VALUE
5109time_to_a(VALUE time)
5110{
5111 struct time_object *tobj;
5112
5113 GetTimeval(time, tobj);
5114 MAKE_TM_ENSURE(time, tobj, tobj->vtm.yday != 0);
5115 return rb_ary_new3(10,
5116 INT2FIX(tobj->vtm.sec),
5117 INT2FIX(tobj->vtm.min),
5118 INT2FIX(tobj->vtm.hour),
5119 INT2FIX(tobj->vtm.mday),
5120 INT2FIX(tobj->vtm.mon),
5121 tobj->vtm.year,
5122 INT2FIX(tobj->vtm.wday),
5123 INT2FIX(tobj->vtm.yday),
5124 RBOOL(tobj->vtm.isdst),
5125 time_zone(time));
5126}
5127
5128/*
5129 * call-seq:
5130 * deconstruct_keys(array_of_names_or_nil) -> hash
5131 *
5132 * Returns a hash of the name/value pairs, to use in pattern matching.
5133 * Possible keys are: <tt>:year</tt>, <tt>:month</tt>, <tt>:day</tt>,
5134 * <tt>:yday</tt>, <tt>:wday</tt>, <tt>:hour</tt>, <tt>:min</tt>, <tt>:sec</tt>,
5135 * <tt>:subsec</tt>, <tt>:dst</tt>, <tt>:zone</tt>.
5136 *
5137 * Possible usages:
5138 *
5139 * t = Time.utc(2022, 10, 5, 21, 25, 30)
5140 *
5141 * if t in wday: 3, day: ..7 # uses deconstruct_keys underneath
5142 * puts "first Wednesday of the month"
5143 * end
5144 * #=> prints "first Wednesday of the month"
5145 *
5146 * case t
5147 * in year: ...2022
5148 * puts "too old"
5149 * in month: ..9
5150 * puts "quarter 1-3"
5151 * in wday: 1..5, month:
5152 * puts "working day in month #{month}"
5153 * end
5154 * #=> prints "working day in month 10"
5155 *
5156 * Note that deconstruction by pattern can also be combined with class check:
5157 *
5158 * if t in Time(wday: 3, day: ..7)
5159 * puts "first Wednesday of the month"
5160 * end
5161 *
5162 */
5163static VALUE
5164time_deconstruct_keys(VALUE time, VALUE keys)
5165{
5166 struct time_object *tobj;
5167 VALUE h;
5168 long i;
5169
5170 GetTimeval(time, tobj);
5171 MAKE_TM_ENSURE(time, tobj, tobj->vtm.yday != 0);
5172
5173 if (NIL_P(keys)) {
5174 h = rb_hash_new_with_size(11);
5175
5176 rb_hash_aset(h, sym_year, tobj->vtm.year);
5177 rb_hash_aset(h, sym_month, INT2FIX(tobj->vtm.mon));
5178 rb_hash_aset(h, sym_day, INT2FIX(tobj->vtm.mday));
5179 rb_hash_aset(h, sym_yday, INT2FIX(tobj->vtm.yday));
5180 rb_hash_aset(h, sym_wday, INT2FIX(tobj->vtm.wday));
5181 rb_hash_aset(h, sym_hour, INT2FIX(tobj->vtm.hour));
5182 rb_hash_aset(h, sym_min, INT2FIX(tobj->vtm.min));
5183 rb_hash_aset(h, sym_sec, INT2FIX(tobj->vtm.sec));
5184 rb_hash_aset(h, sym_subsec,
5185 quov(w2v(wmod(tobj->timew, WINT2FIXWV(TIME_SCALE))), INT2FIX(TIME_SCALE)));
5186 rb_hash_aset(h, sym_dst, RBOOL(tobj->vtm.isdst));
5187 rb_hash_aset(h, sym_zone, time_zone(time));
5188
5189 return h;
5190 }
5191 if (UNLIKELY(!RB_TYPE_P(keys, T_ARRAY))) {
5192 rb_raise(rb_eTypeError,
5193 "wrong argument type %"PRIsVALUE" (expected Array or nil)",
5194 rb_obj_class(keys));
5195
5196 }
5197
5198 h = rb_hash_new_with_size(RARRAY_LEN(keys));
5199
5200 for (i=0; i<RARRAY_LEN(keys); i++) {
5201 VALUE key = RARRAY_AREF(keys, i);
5202
5203 if (sym_year == key) rb_hash_aset(h, key, tobj->vtm.year);
5204 if (sym_month == key) rb_hash_aset(h, key, INT2FIX(tobj->vtm.mon));
5205 if (sym_day == key) rb_hash_aset(h, key, INT2FIX(tobj->vtm.mday));
5206 if (sym_yday == key) rb_hash_aset(h, key, INT2FIX(tobj->vtm.yday));
5207 if (sym_wday == key) rb_hash_aset(h, key, INT2FIX(tobj->vtm.wday));
5208 if (sym_hour == key) rb_hash_aset(h, key, INT2FIX(tobj->vtm.hour));
5209 if (sym_min == key) rb_hash_aset(h, key, INT2FIX(tobj->vtm.min));
5210 if (sym_sec == key) rb_hash_aset(h, key, INT2FIX(tobj->vtm.sec));
5211 if (sym_subsec == key) {
5212 rb_hash_aset(h, key, quov(w2v(wmod(tobj->timew, WINT2FIXWV(TIME_SCALE))), INT2FIX(TIME_SCALE)));
5213 }
5214 if (sym_dst == key) rb_hash_aset(h, key, RBOOL(tobj->vtm.isdst));
5215 if (sym_zone == key) rb_hash_aset(h, key, time_zone(time));
5216 }
5217 return h;
5218}
5219
5220static VALUE
5221rb_strftime_alloc(const char *format, size_t format_len, rb_encoding *enc,
5222 VALUE time, struct vtm *vtm, wideval_t timew, int gmt)
5223{
5224 VALUE timev = Qnil;
5225 struct timespec ts;
5226
5227 if (!timew2timespec_exact(timew, &ts))
5228 timev = w2v(rb_time_unmagnify(timew));
5229
5230 if (NIL_P(timev)) {
5231 return rb_strftime_timespec(format, format_len, enc, time, vtm, &ts, gmt);
5232 }
5233 else {
5234 return rb_strftime(format, format_len, enc, time, vtm, timev, gmt);
5235 }
5236}
5237
5238static VALUE
5239strftime_cstr(const char *fmt, size_t len, VALUE time, rb_encoding *enc)
5240{
5241 struct time_object *tobj;
5242 VALUE str;
5243
5244 GetTimeval(time, tobj);
5245 MAKE_TM(time, tobj);
5246 str = rb_strftime_alloc(fmt, len, enc, time, &tobj->vtm, tobj->timew, TZMODE_UTC_P(tobj));
5247 if (!str) rb_raise(rb_eArgError, "invalid format: %s", fmt);
5248 return str;
5249}
5250
5251/*
5252 * call-seq:
5253 * strftime(format_string) -> string
5254 *
5255 * Returns a string representation of +self+,
5256 * formatted according to the given string +format+.
5257 * See {Formats for Dates and Times}[rdoc-ref:strftime_formatting.rdoc].
5258 */
5259
5260static VALUE
5261time_strftime(VALUE time, VALUE format)
5262{
5263 struct time_object *tobj;
5264 const char *fmt;
5265 long len;
5266 rb_encoding *enc;
5267 VALUE tmp;
5268
5269 GetTimeval(time, tobj);
5270 MAKE_TM_ENSURE(time, tobj, tobj->vtm.yday != 0);
5271 StringValue(format);
5272 if (!rb_enc_str_asciicompat_p(format)) {
5273 rb_raise(rb_eArgError, "format should have ASCII compatible encoding");
5274 }
5275 tmp = rb_str_tmp_frozen_acquire(format);
5276 fmt = RSTRING_PTR(tmp);
5277 len = RSTRING_LEN(tmp);
5278 enc = rb_enc_get(format);
5279 if (len == 0) {
5280 rb_warning("strftime called with empty format string");
5281 return rb_enc_str_new(0, 0, enc);
5282 }
5283 else {
5284 VALUE str = rb_strftime_alloc(fmt, len, enc, time, &tobj->vtm, tobj->timew,
5285 TZMODE_UTC_P(tobj));
5286 rb_str_tmp_frozen_release(format, tmp);
5287 if (!str) rb_raise(rb_eArgError, "invalid format: %"PRIsVALUE, format);
5288 return str;
5289 }
5290}
5291
5292/*
5293 * call-seq:
5294 * xmlschema(fraction_digits=0) -> string
5295 *
5296 * Returns a string which represents the time as a dateTime defined by XML
5297 * Schema:
5298 *
5299 * CCYY-MM-DDThh:mm:ssTZD
5300 * CCYY-MM-DDThh:mm:ss.sssTZD
5301 *
5302 * where TZD is Z or [+-]hh:mm.
5303 *
5304 * If self is a UTC time, Z is used as TZD. [+-]hh:mm is used otherwise.
5305 *
5306 * +fraction_digits+ specifies a number of digits to use for fractional
5307 * seconds. Its default value is 0.
5308 *
5309 * t = Time.now
5310 * t.xmlschema # => "2011-10-05T22:26:12-04:00"
5311 */
5312
5313static VALUE
5314time_xmlschema(int argc, VALUE *argv, VALUE time)
5315{
5316 long fraction_digits = 0;
5317 rb_check_arity(argc, 0, 1);
5318 if (argc > 0) {
5319 fraction_digits = NUM2LONG(argv[0]);
5320 if (fraction_digits < 0) {
5321 fraction_digits = 0;
5322 }
5323 }
5324
5325 struct time_object *tobj;
5326
5327 GetTimeval(time, tobj);
5328 MAKE_TM(time, tobj);
5329
5330 const long size_after_year = sizeof("-MM-DDTHH:MM:SS+ZH:ZM") + fraction_digits
5331 + (fraction_digits > 0);
5332 VALUE str;
5333 char *ptr;
5334
5335# define fill_digits_long(len, prec, n) \
5336 for (int fill_it = 1, written = snprintf(ptr, len, "%0*ld", prec, n); \
5337 fill_it; ptr += written, fill_it = 0)
5338
5339 if (FIXNUM_P(tobj->vtm.year)) {
5340 long year = FIX2LONG(tobj->vtm.year);
5341 int year_width = (year < 0) + rb_strlen_lit("YYYY");
5342 int w = (year >= -9999 && year <= 9999 ? year_width : (year < 0) + (int)DECIMAL_SIZE_OF(year));
5343 str = rb_usascii_str_new(0, w + size_after_year);
5344 ptr = RSTRING_PTR(str);
5345 fill_digits_long(w + 1, year_width, year) {
5346 if (year >= -9999 && year <= 9999) {
5347 RUBY_ASSERT(written == year_width);
5348 }
5349 else {
5350 RUBY_ASSERT(written >= year_width);
5351 RUBY_ASSERT(written <= w);
5352 }
5353 }
5354 }
5355 else {
5356 str = rb_int2str(tobj->vtm.year, 10);
5357 rb_str_modify_expand(str, size_after_year);
5358 ptr = RSTRING_END(str);
5359 }
5360
5361# define fill_2(c, n) (*ptr++ = c, *ptr++ = '0' + (n) / 10, *ptr++ = '0' + (n) % 10)
5362 fill_2('-', tobj->vtm.mon);
5363 fill_2('-', tobj->vtm.mday);
5364 fill_2('T', tobj->vtm.hour);
5365 fill_2(':', tobj->vtm.min);
5366 fill_2(':', tobj->vtm.sec);
5367
5368 if (fraction_digits > 0) {
5369 VALUE subsecx = tobj->vtm.subsecx;
5370 long subsec;
5371 int digits = -1;
5372 *ptr++ = '.';
5373 if (fraction_digits <= TIME_SCALE_NUMDIGITS) {
5374 digits = TIME_SCALE_NUMDIGITS - (int)fraction_digits;
5375 }
5376 else {
5377 long w = fraction_digits - TIME_SCALE_NUMDIGITS; /* > 0 */
5378 subsecx = mulv(subsecx, rb_int_positive_pow(10, (unsigned long)w));
5379 if (!RB_INTEGER_TYPE_P(subsecx)) { /* maybe Rational */
5380 subsecx = rb_Integer(subsecx);
5381 }
5382 if (FIXNUM_P(subsecx)) digits = 0;
5383 }
5384 if (digits >= 0 && fraction_digits < INT_MAX) {
5385 subsec = NUM2LONG(subsecx);
5386 if (digits > 0) subsec /= (long)pow(10, digits);
5387 fill_digits_long(fraction_digits + 1, (int)fraction_digits, subsec) {
5388 RUBY_ASSERT(written == (int)fraction_digits);
5389 }
5390 }
5391 else {
5392 subsecx = rb_int2str(subsecx, 10);
5393 long len = RSTRING_LEN(subsecx);
5394 if (fraction_digits > len) {
5395 memset(ptr, '0', fraction_digits - len);
5396 }
5397 else {
5398 len = fraction_digits;
5399 }
5400 ptr += fraction_digits;
5401 memcpy(ptr - len, RSTRING_PTR(subsecx), len);
5402 }
5403 }
5404
5405 if (TZMODE_UTC_P(tobj)) {
5406 *ptr = 'Z';
5407 ptr++;
5408 }
5409 else {
5410 long offset = NUM2LONG(rb_time_utc_offset(time));
5411 char sign = offset < 0 ? '-' : '+';
5412 if (offset < 0) offset = -offset;
5413 offset /= 60;
5414 fill_2(sign, offset / 60);
5415 fill_2(':', offset % 60);
5416 }
5417 const char *const start = RSTRING_PTR(str);
5418 rb_str_set_len(str, ptr - start); // We could skip coderange scanning as we know it's full ASCII.
5419 return str;
5420}
5421
5422int ruby_marshal_write_long(long x, char *buf);
5423
5424enum {base_dump_size = 8};
5425
5426/* :nodoc: */
5427static VALUE
5428time_mdump(VALUE time)
5429{
5430 struct time_object *tobj;
5431 unsigned long p, s;
5432 char buf[base_dump_size + sizeof(long) + 1];
5433 int i;
5434 VALUE str;
5435
5436 struct vtm vtm;
5437 long year;
5438 long usec, nsec;
5439 VALUE subsecx, nano, subnano, v, zone;
5440
5441 VALUE year_extend = Qnil;
5442 const int max_year = 1900+0xffff;
5443
5444 GetTimeval(time, tobj);
5445
5446 gmtimew(tobj->timew, &vtm);
5447
5448 if (FIXNUM_P(vtm.year)) {
5449 year = FIX2LONG(vtm.year);
5450 if (year > max_year) {
5451 year_extend = INT2FIX(year - max_year);
5452 year = max_year;
5453 }
5454 else if (year < 1900) {
5455 year_extend = LONG2NUM(1900 - year);
5456 year = 1900;
5457 }
5458 }
5459 else {
5460 if (rb_int_positive_p(vtm.year)) {
5461 year_extend = rb_int_minus(vtm.year, INT2FIX(max_year));
5462 year = max_year;
5463 }
5464 else {
5465 year_extend = rb_int_minus(INT2FIX(1900), vtm.year);
5466 year = 1900;
5467 }
5468 }
5469
5470 subsecx = vtm.subsecx;
5471
5472 nano = mulquov(subsecx, INT2FIX(1000000000), INT2FIX(TIME_SCALE));
5473 divmodv(nano, INT2FIX(1), &v, &subnano);
5474 nsec = FIX2LONG(v);
5475 usec = nsec / 1000;
5476 nsec = nsec % 1000;
5477
5478 nano = addv(LONG2FIX(nsec), subnano);
5479
5480 p = 0x1UL << 31 | /* 1 */
5481 TZMODE_UTC_P(tobj) << 30 | /* 1 */
5482 (year-1900) << 14 | /* 16 */
5483 (vtm.mon-1) << 10 | /* 4 */
5484 vtm.mday << 5 | /* 5 */
5485 vtm.hour; /* 5 */
5486 s = (unsigned long)vtm.min << 26 | /* 6 */
5487 vtm.sec << 20 | /* 6 */
5488 usec; /* 20 */
5489
5490 for (i=0; i<4; i++) {
5491 buf[i] = (unsigned char)p;
5492 p = RSHIFT(p, 8);
5493 }
5494 for (i=4; i<8; i++) {
5495 buf[i] = (unsigned char)s;
5496 s = RSHIFT(s, 8);
5497 }
5498
5499 if (!NIL_P(year_extend)) {
5500 /*
5501 * Append extended year distance from 1900..(1900+0xffff). In
5502 * each cases, there is no sign as the value is positive. The
5503 * format is length (marshaled long) + little endian packed
5504 * binary (like as Integer).
5505 */
5506 size_t ysize = rb_absint_size(year_extend, NULL);
5507 char *p, *const buf_year_extend = buf + base_dump_size;
5508 if (ysize > LONG_MAX ||
5509 (i = ruby_marshal_write_long((long)ysize, buf_year_extend)) < 0) {
5510 rb_raise(rb_eArgError, "year too %s to marshal: %"PRIsVALUE" UTC",
5511 (year == 1900 ? "small" : "big"), vtm.year);
5512 }
5513 i += base_dump_size;
5514 str = rb_str_new(NULL, i + ysize);
5515 p = RSTRING_PTR(str);
5516 memcpy(p, buf, i);
5517 p += i;
5518 rb_integer_pack(year_extend, p, ysize, 1, 0, INTEGER_PACK_LITTLE_ENDIAN);
5519 }
5520 else {
5521 str = rb_str_new(buf, base_dump_size);
5522 }
5523 rb_copy_generic_ivar(str, time);
5524 if (!rb_equal(nano, INT2FIX(0))) {
5525 if (RB_TYPE_P(nano, T_RATIONAL)) {
5526 rb_ivar_set(str, id_nano_num, RRATIONAL(nano)->num);
5527 rb_ivar_set(str, id_nano_den, RRATIONAL(nano)->den);
5528 }
5529 else {
5530 rb_ivar_set(str, id_nano_num, nano);
5531 rb_ivar_set(str, id_nano_den, INT2FIX(1));
5532 }
5533 }
5534 if (nsec) { /* submicro is only for Ruby 1.9.1 compatibility */
5535 /*
5536 * submicro is formatted in fixed-point packed BCD (without sign).
5537 * It represent digits under microsecond.
5538 * For nanosecond resolution, 3 digits (2 bytes) are used.
5539 * However it can be longer.
5540 * Extra digits are ignored for loading.
5541 */
5542 char buf[2];
5543 int len = (int)sizeof(buf);
5544 buf[1] = (char)((nsec % 10) << 4);
5545 nsec /= 10;
5546 buf[0] = (char)(nsec % 10);
5547 nsec /= 10;
5548 buf[0] |= (char)((nsec % 10) << 4);
5549 if (buf[1] == 0)
5550 len = 1;
5551 rb_ivar_set(str, id_submicro, rb_str_new(buf, len));
5552 }
5553 if (!TZMODE_UTC_P(tobj)) {
5554 VALUE off = rb_time_utc_offset(time), div, mod;
5555 divmodv(off, INT2FIX(1), &div, &mod);
5556 if (rb_equal(mod, INT2FIX(0)))
5557 off = rb_Integer(div);
5558 rb_ivar_set(str, id_offset, off);
5559 }
5560 zone = tobj->vtm.zone;
5561 if (maybe_tzobj_p(zone)) {
5562 zone = rb_funcallv(zone, id_name, 0, 0);
5563 }
5564 rb_ivar_set(str, id_zone, zone);
5565 return str;
5566}
5567
5568/* :nodoc: */
5569static VALUE
5570time_dump(int argc, VALUE *argv, VALUE time)
5571{
5572 VALUE str;
5573
5574 rb_check_arity(argc, 0, 1);
5575 str = time_mdump(time);
5576
5577 return str;
5578}
5579
5580static VALUE
5581mload_findzone(VALUE arg)
5582{
5583 VALUE *argp = (VALUE *)arg;
5584 VALUE time = argp[0], zone = argp[1];
5585 return find_timezone(time, zone);
5586}
5587
5588static VALUE
5589mload_zone(VALUE time, VALUE zone)
5590{
5591 VALUE z, args[2];
5592 args[0] = time;
5593 args[1] = zone;
5594 z = rb_rescue(mload_findzone, (VALUE)args, 0, Qnil);
5595 if (NIL_P(z)) return rb_fstring(zone);
5596 if (RB_TYPE_P(z, T_STRING)) return rb_fstring(z);
5597 return z;
5598}
5599
5600long ruby_marshal_read_long(const char **buf, long len);
5601
5602/* :nodoc: */
5603static VALUE
5604time_mload(VALUE time, VALUE str)
5605{
5606 struct time_object *tobj;
5607 unsigned long p, s;
5608 time_t sec;
5609 long usec;
5610 unsigned char *buf;
5611 struct vtm vtm;
5612 int i, gmt;
5613 long nsec;
5614 VALUE submicro, nano_num, nano_den, offset, zone, year;
5615 wideval_t timew;
5616
5617 time_modify(time);
5618
5619#define get_attr(attr, iffound) \
5620 attr = rb_attr_delete(str, id_##attr); \
5621 if (!NIL_P(attr)) { \
5622 iffound; \
5623 }
5624
5625 get_attr(nano_num, {});
5626 get_attr(nano_den, {});
5627 get_attr(submicro, {});
5628 get_attr(offset, (offset = rb_rescue(validate_utc_offset, offset, 0, Qnil)));
5629 get_attr(zone, (zone = rb_rescue(validate_zone_name, zone, 0, Qnil)));
5630 get_attr(year, {});
5631
5632#undef get_attr
5633
5634 rb_copy_generic_ivar(time, str);
5635
5636 StringValue(str);
5637 buf = (unsigned char *)RSTRING_PTR(str);
5638 if (RSTRING_LEN(str) < base_dump_size) {
5639 goto invalid_format;
5640 }
5641
5642 p = s = 0;
5643 for (i=0; i<4; i++) {
5644 p |= (unsigned long)buf[i]<<(8*i);
5645 }
5646 for (i=4; i<8; i++) {
5647 s |= (unsigned long)buf[i]<<(8*(i-4));
5648 }
5649
5650 if ((p & (1UL<<31)) == 0) {
5651 gmt = 0;
5652 offset = Qnil;
5653 sec = p;
5654 usec = s;
5655 nsec = usec * 1000;
5656 timew = wadd(rb_time_magnify(TIMET2WV(sec)), wmulquoll(WINT2FIXWV(usec), TIME_SCALE, 1000000));
5657 }
5658 else {
5659 p &= ~(1UL<<31);
5660 gmt = (int)((p >> 30) & 0x1);
5661
5662 if (NIL_P(year)) {
5663 year = INT2FIX(((int)(p >> 14) & 0xffff) + 1900);
5664 }
5665 if (RSTRING_LEN(str) > base_dump_size) {
5666 long len = RSTRING_LEN(str) - base_dump_size;
5667 long ysize = 0;
5668 VALUE year_extend;
5669 const char *ybuf = (const char *)(buf += base_dump_size);
5670 ysize = ruby_marshal_read_long(&ybuf, len);
5671 len -= ybuf - (const char *)buf;
5672 if (ysize < 0 || ysize > len) goto invalid_format;
5673 year_extend = rb_integer_unpack(ybuf, ysize, 1, 0, INTEGER_PACK_LITTLE_ENDIAN);
5674 if (year == INT2FIX(1900)) {
5675 year = rb_int_minus(year, year_extend);
5676 }
5677 else {
5678 year = rb_int_plus(year, year_extend);
5679 }
5680 }
5681 unsigned int mon = ((int)(p >> 10) & 0xf); /* 0...12 */
5682 if (mon >= 12) {
5683 mon -= 12;
5684 year = addv(year, LONG2FIX(1));
5685 }
5686 vtm.year = year;
5687 vtm.mon = mon + 1;
5688 vtm.mday = (int)(p >> 5) & 0x1f;
5689 vtm.hour = (int) p & 0x1f;
5690 vtm.min = (int)(s >> 26) & 0x3f;
5691 vtm.sec = (int)(s >> 20) & 0x3f;
5692 vtm.utc_offset = INT2FIX(0);
5693 vtm.yday = vtm.wday = 0;
5694 vtm.isdst = 0;
5695 vtm.zone = str_empty;
5696
5697 usec = (long)(s & 0xfffff);
5698 nsec = usec * 1000;
5699
5700
5701 vtm.subsecx = mulquov(LONG2FIX(nsec), INT2FIX(TIME_SCALE), LONG2FIX(1000000000));
5702 if (nano_num != Qnil) {
5703 VALUE nano = quov(num_exact(nano_num), num_exact(nano_den));
5704 vtm.subsecx = addv(vtm.subsecx, mulquov(nano, INT2FIX(TIME_SCALE), LONG2FIX(1000000000)));
5705 }
5706 else if (submicro != Qnil) { /* for Ruby 1.9.1 compatibility */
5707 unsigned char *ptr;
5708 long len;
5709 int digit;
5710 ptr = (unsigned char*)StringValuePtr(submicro);
5711 len = RSTRING_LEN(submicro);
5712 nsec = 0;
5713 if (0 < len) {
5714 if (10 <= (digit = ptr[0] >> 4)) goto end_submicro;
5715 nsec += digit * 100;
5716 if (10 <= (digit = ptr[0] & 0xf)) goto end_submicro;
5717 nsec += digit * 10;
5718 }
5719 if (1 < len) {
5720 if (10 <= (digit = ptr[1] >> 4)) goto end_submicro;
5721 nsec += digit;
5722 }
5723 vtm.subsecx = addv(vtm.subsecx, mulquov(LONG2FIX(nsec), INT2FIX(TIME_SCALE), LONG2FIX(1000000000)));
5724end_submicro: ;
5725 }
5726 timew = timegmw(&vtm);
5727 }
5728
5729 GetNewTimeval(time, tobj);
5730 TZMODE_SET_LOCALTIME(tobj);
5731 tobj->vtm.tm_got = 0;
5732 time_set_timew(time, tobj, timew);
5733
5734 if (gmt) {
5735 TZMODE_SET_UTC(tobj);
5736 }
5737 else if (!NIL_P(offset)) {
5738 time_set_utc_offset(time, offset);
5739 time_fixoff(time);
5740 }
5741 if (!NIL_P(zone)) {
5742 zone = mload_zone(time, zone);
5743 RB_OBJ_WRITE(time, &tobj->vtm.zone, zone);
5744 zone_localtime(zone, time);
5745 }
5746
5747 return time;
5748
5749 invalid_format:
5750 rb_raise(rb_eTypeError, "marshaled time format differ");
5752}
5753
5754/* :nodoc: */
5755static VALUE
5756time_load(VALUE klass, VALUE str)
5757{
5758 VALUE time = time_s_alloc(klass);
5759
5760 time_mload(time, str);
5761 return time;
5762}
5763
5764/* :nodoc:*/
5765/* Document-class: Time::tm
5766 *
5767 * A container class for timezone conversion.
5768 */
5769
5770/*
5771 * call-seq:
5772 * Time::tm.from_time(t) -> tm
5773 *
5774 * Creates new Time::tm object from a Time object.
5775 */
5776
5777static VALUE
5778tm_from_time(VALUE klass, VALUE time)
5779{
5780 struct time_object *tobj;
5781 struct vtm vtm, *v;
5782 VALUE tm;
5783 struct time_object *ttm;
5784
5785 GetTimeval(time, tobj);
5786 tm = time_s_alloc(klass);
5787 ttm = RTYPEDDATA_GET_DATA(tm);
5788 v = &vtm;
5789
5790 WIDEVALUE timew = tobj->timew;
5791 GMTIMEW(timew, v);
5792 time_set_timew(tm, ttm, wsub(timew, v->subsecx));
5793 v->subsecx = INT2FIX(0);
5794 v->zone = Qnil;
5795 time_set_vtm(tm, ttm, *v);
5796
5797 ttm->vtm.tm_got = 1;
5798 TZMODE_SET_UTC(ttm);
5799 return tm;
5800}
5801
5802/*
5803 * call-seq:
5804 * Time::tm.new(year, month=nil, day=nil, hour=nil, min=nil, sec=nil, zone=nil) -> tm
5805 *
5806 * Creates new Time::tm object.
5807 */
5808
5809static VALUE
5810tm_initialize(int argc, VALUE *argv, VALUE time)
5811{
5812 struct vtm vtm;
5813 wideval_t t;
5814
5815 if (rb_check_arity(argc, 1, 7) > 6) argc = 6;
5816 time_arg(argc, argv, &vtm);
5817 t = timegmw(&vtm);
5818 struct time_object *tobj = RTYPEDDATA_GET_DATA(time);
5819 TZMODE_SET_UTC(tobj);
5820 time_set_timew(time, tobj, t);
5821 time_set_vtm(time, tobj, vtm);
5822
5823 return time;
5824}
5825
5826/* call-seq:
5827 * tm.to_time -> time
5828 *
5829 * Returns a new Time object.
5830 */
5831
5832static VALUE
5833tm_to_time(VALUE tm)
5834{
5835 struct time_object *torig = get_timeval(tm);
5836 VALUE dup = time_s_alloc(rb_cTime);
5837 struct time_object *tobj = RTYPEDDATA_GET_DATA(dup);
5838 *tobj = *torig;
5839 return dup;
5840}
5841
5842static VALUE
5843tm_plus(VALUE tm, VALUE offset)
5844{
5845 return time_add0(rb_obj_class(tm), get_timeval(tm), tm, offset, +1);
5846}
5847
5848static VALUE
5849tm_minus(VALUE tm, VALUE offset)
5850{
5851 return time_add0(rb_obj_class(tm), get_timeval(tm), tm, offset, -1);
5852}
5853
5854static VALUE
5855Init_tm(VALUE outer, const char *name)
5856{
5857 /* :stopdoc:*/
5858 VALUE tm;
5859 tm = rb_define_class_under(outer, name, rb_cObject);
5860 rb_define_alloc_func(tm, time_s_alloc);
5861 rb_define_method(tm, "sec", time_sec, 0);
5862 rb_define_method(tm, "min", time_min, 0);
5863 rb_define_method(tm, "hour", time_hour, 0);
5864 rb_define_method(tm, "mday", time_mday, 0);
5865 rb_define_method(tm, "day", time_mday, 0);
5866 rb_define_method(tm, "mon", time_mon, 0);
5867 rb_define_method(tm, "month", time_mon, 0);
5868 rb_define_method(tm, "year", time_year, 0);
5869 rb_define_method(tm, "isdst", time_isdst, 0);
5870 rb_define_method(tm, "dst?", time_isdst, 0);
5871 rb_define_method(tm, "zone", time_zone, 0);
5872 rb_define_method(tm, "gmtoff", rb_time_utc_offset, 0);
5873 rb_define_method(tm, "gmt_offset", rb_time_utc_offset, 0);
5874 rb_define_method(tm, "utc_offset", rb_time_utc_offset, 0);
5875 rb_define_method(tm, "utc?", time_utc_p, 0);
5876 rb_define_method(tm, "gmt?", time_utc_p, 0);
5877 rb_define_method(tm, "to_s", time_to_s, 0);
5878 rb_define_method(tm, "inspect", time_inspect, 0);
5879 rb_define_method(tm, "to_a", time_to_a, 0);
5880 rb_define_method(tm, "tv_sec", time_to_i, 0);
5881 rb_define_method(tm, "tv_usec", time_usec, 0);
5882 rb_define_method(tm, "usec", time_usec, 0);
5883 rb_define_method(tm, "tv_nsec", time_nsec, 0);
5884 rb_define_method(tm, "nsec", time_nsec, 0);
5885 rb_define_method(tm, "subsec", time_subsec, 0);
5886 rb_define_method(tm, "to_i", time_to_i, 0);
5887 rb_define_method(tm, "to_f", time_to_f, 0);
5888 rb_define_method(tm, "to_r", time_to_r, 0);
5889 rb_define_method(tm, "+", tm_plus, 1);
5890 rb_define_method(tm, "-", tm_minus, 1);
5891 rb_define_method(tm, "initialize", tm_initialize, -1);
5892 rb_define_method(tm, "utc", tm_to_time, 0);
5893 rb_alias(tm, rb_intern_const("to_time"), rb_intern_const("utc"));
5894 rb_define_singleton_method(tm, "from_time", tm_from_time, 1);
5895 /* :startdoc:*/
5896
5897 return tm;
5898}
5899
5900VALUE
5901rb_time_zone_abbreviation(VALUE zone, VALUE time)
5902{
5903 VALUE tm, abbr, strftime_args[2];
5904
5905 abbr = rb_check_string_type(zone);
5906 if (!NIL_P(abbr)) return abbr;
5907
5908 tm = tm_from_time(rb_cTimeTM, time);
5909 abbr = rb_check_funcall(zone, rb_intern("abbr"), 1, &tm);
5910 if (!UNDEF_P(abbr)) {
5911 goto found;
5912 }
5913#ifdef SUPPORT_TZINFO_ZONE_ABBREVIATION
5914 abbr = rb_check_funcall(zone, rb_intern("period_for_utc"), 1, &tm);
5915 if (!UNDEF_P(abbr)) {
5916 abbr = rb_funcallv(abbr, rb_intern("abbreviation"), 0, 0);
5917 goto found;
5918 }
5919#endif
5920 strftime_args[0] = rb_fstring_lit("%Z");
5921 strftime_args[1] = tm;
5922 abbr = rb_check_funcall(zone, rb_intern("strftime"), 2, strftime_args);
5923 if (!UNDEF_P(abbr)) {
5924 goto found;
5925 }
5926 abbr = rb_check_funcall_default(zone, idName, 0, 0, Qnil);
5927 found:
5928 return rb_obj_as_string(abbr);
5929}
5930
5931//
5932void
5933Init_Time(void)
5934{
5935#ifdef _WIN32
5936 ruby_reset_timezone(getenv("TZ"));
5937#endif
5938
5939 id_submicro = rb_intern_const("submicro");
5940 id_nano_num = rb_intern_const("nano_num");
5941 id_nano_den = rb_intern_const("nano_den");
5942 id_offset = rb_intern_const("offset");
5943 id_zone = rb_intern_const("zone");
5944 id_nanosecond = rb_intern_const("nanosecond");
5945 id_microsecond = rb_intern_const("microsecond");
5946 id_millisecond = rb_intern_const("millisecond");
5947 id_nsec = rb_intern_const("nsec");
5948 id_usec = rb_intern_const("usec");
5949 id_local_to_utc = rb_intern_const("local_to_utc");
5950 id_utc_to_local = rb_intern_const("utc_to_local");
5951 id_year = rb_intern_const("year");
5952 id_mon = rb_intern_const("mon");
5953 id_mday = rb_intern_const("mday");
5954 id_hour = rb_intern_const("hour");
5955 id_min = rb_intern_const("min");
5956 id_sec = rb_intern_const("sec");
5957 id_isdst = rb_intern_const("isdst");
5958 id_find_timezone = rb_intern_const("find_timezone");
5959
5960 sym_year = ID2SYM(rb_intern_const("year"));
5961 sym_month = ID2SYM(rb_intern_const("month"));
5962 sym_yday = ID2SYM(rb_intern_const("yday"));
5963 sym_wday = ID2SYM(rb_intern_const("wday"));
5964 sym_day = ID2SYM(rb_intern_const("day"));
5965 sym_hour = ID2SYM(rb_intern_const("hour"));
5966 sym_min = ID2SYM(rb_intern_const("min"));
5967 sym_sec = ID2SYM(rb_intern_const("sec"));
5968 sym_subsec = ID2SYM(rb_intern_const("subsec"));
5969 sym_dst = ID2SYM(rb_intern_const("dst"));
5970 sym_zone = ID2SYM(rb_intern_const("zone"));
5971
5972 str_utc = rb_fstring_lit("UTC");
5973 rb_vm_register_global_object(str_utc);
5974 str_empty = rb_fstring_lit("");
5975 rb_vm_register_global_object(str_empty);
5976
5977 rb_cTime = rb_define_class("Time", rb_cObject);
5980
5981 rb_define_alloc_func(rb_cTime, time_s_alloc);
5982 rb_define_singleton_method(rb_cTime, "utc", time_s_mkutc, -1);
5983 rb_define_singleton_method(rb_cTime, "local", time_s_mktime, -1);
5984 rb_define_alias(scTime, "gm", "utc");
5985 rb_define_alias(scTime, "mktime", "local");
5986
5987 rb_define_method(rb_cTime, "to_i", time_to_i, 0);
5988 rb_define_method(rb_cTime, "to_f", time_to_f, 0);
5989 rb_define_method(rb_cTime, "to_r", time_to_r, 0);
5990 rb_define_method(rb_cTime, "<=>", time_cmp, 1);
5991 rb_define_method(rb_cTime, "eql?", time_eql, 1);
5992 rb_define_method(rb_cTime, "hash", time_hash, 0);
5993 rb_define_method(rb_cTime, "initialize_copy", time_init_copy, 1);
5994
5995 rb_define_method(rb_cTime, "localtime", time_localtime_m, -1);
5996 rb_define_method(rb_cTime, "gmtime", time_gmtime, 0);
5997 rb_define_method(rb_cTime, "utc", time_gmtime, 0);
5998 rb_define_method(rb_cTime, "getlocal", time_getlocaltime, -1);
5999 rb_define_method(rb_cTime, "getgm", time_getgmtime, 0);
6000 rb_define_method(rb_cTime, "getutc", time_getgmtime, 0);
6001
6002 rb_define_method(rb_cTime, "ctime", time_asctime, 0);
6003 rb_define_method(rb_cTime, "asctime", time_asctime, 0);
6004 rb_define_method(rb_cTime, "to_s", time_to_s, 0);
6005 rb_define_method(rb_cTime, "inspect", time_inspect, 0);
6006 rb_define_method(rb_cTime, "to_a", time_to_a, 0);
6007 rb_define_method(rb_cTime, "deconstruct_keys", time_deconstruct_keys, 1);
6008
6009 rb_define_method(rb_cTime, "+", time_plus, 1);
6010 rb_define_method(rb_cTime, "-", time_minus, 1);
6011
6012 rb_define_method(rb_cTime, "round", time_round, -1);
6013 rb_define_method(rb_cTime, "floor", time_floor, -1);
6014 rb_define_method(rb_cTime, "ceil", time_ceil, -1);
6015
6016 rb_define_method(rb_cTime, "sec", time_sec, 0);
6017 rb_define_method(rb_cTime, "min", time_min, 0);
6018 rb_define_method(rb_cTime, "hour", time_hour, 0);
6019 rb_define_method(rb_cTime, "mday", time_mday, 0);
6020 rb_define_method(rb_cTime, "day", time_mday, 0);
6021 rb_define_method(rb_cTime, "mon", time_mon, 0);
6022 rb_define_method(rb_cTime, "month", time_mon, 0);
6023 rb_define_method(rb_cTime, "year", time_year, 0);
6024 rb_define_method(rb_cTime, "wday", time_wday, 0);
6025 rb_define_method(rb_cTime, "yday", time_yday, 0);
6026 rb_define_method(rb_cTime, "isdst", time_isdst, 0);
6027 rb_define_method(rb_cTime, "dst?", time_isdst, 0);
6028 rb_define_method(rb_cTime, "zone", time_zone, 0);
6029 rb_define_method(rb_cTime, "gmtoff", rb_time_utc_offset, 0);
6030 rb_define_method(rb_cTime, "gmt_offset", rb_time_utc_offset, 0);
6031 rb_define_method(rb_cTime, "utc_offset", rb_time_utc_offset, 0);
6032
6033 rb_define_method(rb_cTime, "utc?", time_utc_p, 0);
6034 rb_define_method(rb_cTime, "gmt?", time_utc_p, 0);
6035
6036 rb_define_method(rb_cTime, "sunday?", time_sunday, 0);
6037 rb_define_method(rb_cTime, "monday?", time_monday, 0);
6038 rb_define_method(rb_cTime, "tuesday?", time_tuesday, 0);
6039 rb_define_method(rb_cTime, "wednesday?", time_wednesday, 0);
6040 rb_define_method(rb_cTime, "thursday?", time_thursday, 0);
6041 rb_define_method(rb_cTime, "friday?", time_friday, 0);
6042 rb_define_method(rb_cTime, "saturday?", time_saturday, 0);
6043
6044 rb_define_method(rb_cTime, "tv_sec", time_to_i, 0);
6045 rb_define_method(rb_cTime, "tv_usec", time_usec, 0);
6046 rb_define_method(rb_cTime, "usec", time_usec, 0);
6047 rb_define_method(rb_cTime, "tv_nsec", time_nsec, 0);
6048 rb_define_method(rb_cTime, "nsec", time_nsec, 0);
6049 rb_define_method(rb_cTime, "subsec", time_subsec, 0);
6050
6051 rb_define_method(rb_cTime, "strftime", time_strftime, 1);
6052 rb_define_method(rb_cTime, "xmlschema", time_xmlschema, -1);
6053 rb_define_alias(rb_cTime, "iso8601", "xmlschema");
6054
6055 /* methods for marshaling */
6056 rb_define_private_method(rb_cTime, "_dump", time_dump, -1);
6057 rb_define_private_method(scTime, "_load", time_load, 1);
6058
6059 if (debug_find_time_numguess) {
6060 rb_define_hooked_variable("$find_time_numguess", (VALUE *)&find_time_numguess,
6061 find_time_numguess_getter, 0);
6062 }
6063
6064 rb_cTimeTM = Init_tm(rb_cTime, "tm");
6065}
6066
6067#include "timev.rbinc"
#define RUBY_ASSERT(...)
Asserts that the given expression is truthy if and only if RUBY_DEBUG is truthy.
Definition assert.h:219
#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.
Definition class.c:1691
VALUE rb_define_class(const char *name, VALUE super)
Defines a top-level class.
Definition class.c:1474
VALUE rb_singleton_class(VALUE obj)
Finds or creates the singleton class of the passed object.
Definition class.c:2795
VALUE rb_define_class_under(VALUE outer, const char *name, VALUE super)
Defines a class under the namespace of outer.
Definition class.c:1510
void rb_define_alias(VALUE klass, const char *name1, const char *name2)
Defines an alias of a method.
Definition class.c:2843
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.
Definition class.c:3133
#define TYPE(_)
Old name of rb_type.
Definition value_type.h:108
#define RB_INTEGER_TYPE_P
Old name of rb_integer_type_p.
Definition value_type.h:87
#define OBJ_INIT_COPY(obj, orig)
Old name of RB_OBJ_INIT_COPY.
Definition object.h:41
#define ISSPACE
Old name of rb_isspace.
Definition ctype.h:88
#define RFLOAT_VALUE
Old name of rb_float_value.
Definition double.h:28
#define T_STRING
Old name of RUBY_T_STRING.
Definition value_type.h:78
#define Qundef
Old name of RUBY_Qundef.
#define INT2FIX
Old name of RB_INT2FIX.
Definition long.h:48
#define T_NIL
Old name of RUBY_T_NIL.
Definition value_type.h:72
#define ID2SYM
Old name of RB_ID2SYM.
Definition symbol.h:44
#define T_BIGNUM
Old name of RUBY_T_BIGNUM.
Definition value_type.h:57
#define T_STRUCT
Old name of RUBY_T_STRUCT.
Definition value_type.h:79
#define T_FIXNUM
Old name of RUBY_T_FIXNUM.
Definition value_type.h:63
#define UNREACHABLE_RETURN
Old name of RBIMPL_UNREACHABLE_RETURN.
Definition assume.h:29
#define CLASS_OF
Old name of rb_class_of.
Definition globals.h:206
#define LONG2FIX
Old name of RB_INT2FIX.
Definition long.h:49
#define FIX2INT
Old name of RB_FIX2INT.
Definition int.h:41
#define ISDIGIT
Old name of rb_isdigit.
Definition ctype.h:93
#define ASSUME
Old name of RBIMPL_ASSUME.
Definition assume.h:27
#define T_RATIONAL
Old name of RUBY_T_RATIONAL.
Definition value_type.h:76
#define rb_ary_new3
Old name of rb_ary_new_from_args.
Definition array.h:658
#define LONG2NUM
Old name of RB_LONG2NUM.
Definition long.h:50
#define STRNCASECMP
Old name of st_locale_insensitive_strncasecmp.
Definition ctype.h:103
#define ISASCII
Old name of rb_isascii.
Definition ctype.h:85
#define ULL2NUM
Old name of RB_ULL2NUM.
Definition long_long.h:31
#define FIXNUM_MIN
Old name of RUBY_FIXNUM_MIN.
Definition fixnum.h:27
#define NUM2INT
Old name of RB_NUM2INT.
Definition int.h:44
#define INT2NUM
Old name of RB_INT2NUM.
Definition int.h:43
#define Qnil
Old name of RUBY_Qnil.
#define Qfalse
Old name of RUBY_Qfalse.
#define FIX2LONG
Old name of RB_FIX2LONG.
Definition long.h:46
#define T_ARRAY
Old name of RUBY_T_ARRAY.
Definition value_type.h:56
#define NIL_P
Old name of RB_NIL_P.
#define DBL2NUM
Old name of rb_float_new.
Definition double.h:29
#define NUM2LONG
Old name of RB_NUM2LONG.
Definition long.h:51
#define FIXNUM_P
Old name of RB_FIXNUM_P.
#define CONST_ID
Old name of RUBY_CONST_ID.
Definition symbol.h:47
#define NUM2SIZET
Old name of RB_NUM2SIZE.
Definition size_t.h:61
void rb_exc_raise(VALUE mesg)
Raises an exception in the current thread.
Definition eval.c:682
int rb_typeddata_is_kind_of(VALUE obj, const rb_data_type_t *data_type)
Checks if the given object is of given kind.
Definition error.c:1380
VALUE rb_eRangeError
RangeError exception.
Definition error.c:1434
VALUE rb_eTypeError
TypeError exception.
Definition error.c:1430
VALUE rb_eRuntimeError
RuntimeError exception.
Definition error.c:1428
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.
Definition error.c:1481
void rb_warning(const char *fmt,...)
Issues a warning.
Definition error.c:497
VALUE rb_cTime
Time class.
Definition time.c:679
VALUE rb_Float(VALUE val)
This is the logic behind Kernel#Float.
Definition object.c:3653
VALUE rb_check_to_int(VALUE val)
Identical to rb_check_to_integer(), except it uses #to_int for conversion.
Definition object.c:3227
VALUE rb_Integer(VALUE val)
This is the logic behind Kernel#Integer.
Definition object.c:3296
VALUE rb_obj_class(VALUE obj)
Queries the class of an object.
Definition object.c:243
VALUE rb_equal(VALUE lhs, VALUE rhs)
This function is an optimised version of calling #==.
Definition object.c:175
VALUE rb_mComparable
Comparable module.
Definition compar.c:19
VALUE rb_to_int(VALUE val)
Identical to rb_check_to_int(), except it raises in case of conversion mismatch.
Definition object.c:3221
#define RB_OBJ_WRITTEN(old, oldv, young)
Identical to RB_OBJ_WRITE(), except it doesn't write any values, but only a WB declaration.
Definition gc.h:615
#define RB_OBJ_WRITE(old, slot, young)
Declaration of a "back" pointer.
Definition gc.h:603
Encoding relates APIs.
static bool rb_enc_str_asciicompat_p(VALUE str)
Queries if the passed string is in an ASCII-compatible encoding.
Definition encoding.h:789
VALUE rb_funcall(VALUE recv, ID mid, int n,...)
Calls a method.
Definition vm_eval.c:1117
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'...
Definition bignum.h:546
#define INTEGER_PACK_LITTLE_ENDIAN
Little endian combination.
Definition bignum.h:567
static int rb_check_arity(int argc, int min, int max)
Ensures that the passed integer is in the passed range.
Definition error.h:284
void rb_num_zerodiv(void)
Just always raises an exception.
Definition numeric.c:206
VALUE rb_int_positive_pow(long x, unsigned long y)
Raises the passed x to the power of y.
Definition numeric.c:4559
VALUE rb_rational_new(VALUE num, VALUE den)
Constructs a Rational, with reduction.
Definition rational.c:1974
#define rb_Rational1(x)
Shorthand of (x/1)r.
Definition rational.h:116
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...
Definition string.c:3113
#define rb_str_new(str, len)
Allocates an instance of rb_cString.
Definition string.h:1498
#define rb_usascii_str_new(str, len)
Identical to rb_str_new, except it generates a string of "US ASCII" encoding.
Definition string.h:1532
VALUE rb_str_dup(VALUE str)
Duplicates a string.
Definition string.c:1956
VALUE rb_str_cat(VALUE dst, const char *src, long srclen)
Destructively appends the passed contents to the string.
Definition string.c:3525
#define rb_usascii_str_new_cstr(str)
Identical to rb_str_new_cstr, except it generates a string of "US ASCII" encoding.
Definition string.h:1567
void rb_str_set_len(VALUE str, long len)
Overwrites the length of the string.
Definition string.c:3347
VALUE rb_str_concat(VALUE dst, VALUE src)
Identical to rb_str_append(), except it also accepts an integer as a codepoint.
Definition string.c:3994
#define rb_strlen_lit(str)
Length of a string literal.
Definition string.h:1692
VALUE rb_check_string_type(VALUE obj)
Try converting an object to its stringised representation using its to_str method,...
Definition string.c:2910
#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.
Definition string.h:1656
#define rb_utf8_str_new(str, len)
Identical to rb_str_new, except it generates a string of "UTF-8" encoding.
Definition string.h:1549
void rb_str_modify_expand(VALUE str, long capa)
Identical to rb_str_modify(), except it additionally expands the capacity of the receiver.
Definition string.c:2704
#define rb_str_new_cstr(str)
Identical to rb_str_new, except it assumes the passed pointer is a pointer to a C string.
Definition string.h:1514
VALUE rb_obj_as_string(VALUE obj)
Try converting an object to its stringised representation using its to_s method, if any.
Definition string.c:1815
VALUE rb_time_nano_new(time_t sec, long nsec)
Identical to rb_time_new(), except it accepts the time in nanoseconds resolution.
Definition time.c:2800
void rb_timespec_now(struct timespec *ts)
Fills the current time into the given struct.
Definition time.c:2002
VALUE rb_time_timespec_new(const struct timespec *ts, int offset)
Creates an instance of rb_cTime, with given time and offset.
Definition time.c:2808
struct timespec rb_time_timespec(VALUE time)
Identical to rb_time_timeval(), except for return type.
Definition time.c:2971
VALUE rb_time_new(time_t sec, long usec)
Creates an instance of rb_cTime with the given time and the local timezone.
Definition time.c:2791
struct timeval rb_time_timeval(VALUE time)
Converts an instance of rb_cTime to a struct timeval that represents the identical point of time.
Definition time.c:2954
struct timeval rb_time_interval(VALUE num)
Creates a "time interval".
Definition time.c:2948
VALUE rb_time_num_new(VALUE timev, VALUE off)
Identical to rb_time_timespec_new(), except it takes Ruby values instead of C structs.
Definition time.c:2831
VALUE rb_time_utc_offset(VALUE time)
Queries the offset, in seconds between the time zone of the time and the UTC.
Definition time.c:5078
struct timespec rb_time_timespec_interval(VALUE num)
Identical to rb_time_interval(), except for return type.
Definition time.c:2985
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.
Definition variable.c:2080
int rb_respond_to(VALUE obj, ID mid)
Queries if the object responds to the method.
Definition vm_method.c:3309
void rb_alias(VALUE klass, ID dst, ID src)
Resembles alias.
Definition vm_method.c:2606
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.
Definition vm_eval.c:686
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().
Definition symbol.h:284
int off
Offset inside of ptr.
Definition io.h:5
int len
Length of the buffer.
Definition io.h:8
#define DECIMAL_SIZE_OF(expr)
An approximation of decimal representation size.
Definition util.h:48
#define rb_long2int
Just another name of rb_long2int_inline.
Definition long.h:62
#define RB_GC_GUARD(v)
Prevents premature destruction of local objects.
Definition memory.h:167
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.
Definition variable.c:2278
#define RARRAY_LEN
Just another name of rb_array_len.
Definition rarray.h:51
#define RARRAY_AREF(a, i)
Definition rarray.h:403
#define StringValue(v)
Ensures that the parameter object is a String.
Definition rstring.h:66
#define StringValuePtr(v)
Identical to StringValue, except it returns a char*.
Definition rstring.h:76
VALUE rb_str_export_locale(VALUE obj)
Identical to rb_str_export(), except it converts into the locale encoding instead.
Definition string.c:1416
static char * RSTRING_END(VALUE str)
Queries the end of the contents pointer of the string.
Definition rstring.h:442
#define StringValueCStr(v)
Identical to StringValuePtr, except it additionally checks for the contents for viability as a C stri...
Definition rstring.h:89
#define RUBY_TYPED_DEFAULT_FREE
This is a value you can set to rb_data_type_struct::dfree.
Definition rtypeddata.h:79
#define TypedData_Get_Struct(obj, type, data_type, sval)
Obtains a C struct from inside of a wrapper Ruby object.
Definition rtypeddata.h:515
#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...
Definition rtypeddata.h:497
#define RTEST
This is an old name of RB_TEST.
This is the struct that holds necessary info for a struct.
Definition rtypeddata.h:203
const char * wrap_struct_name
Name of structs of this kind.
Definition rtypeddata.h:210
Definition timev.h:5
intptr_t SIGNED_VALUE
A signed integer type that has the same width with VALUE.
Definition value.h:63
uintptr_t ID
Type that represents a Ruby identifier such as a variable name.
Definition value.h:52
uintptr_t VALUE
Type that represents a Ruby object.
Definition value.h:40
static bool RB_FLOAT_TYPE_P(VALUE obj)
Queries if the object is an instance of rb_cFloat.
Definition value_type.h:264
static bool rb_integer_type_p(VALUE obj)
Queries if the object is an instance of rb_cInteger.
Definition value_type.h:204
static bool RB_TYPE_P(VALUE obj, enum ruby_value_type t)
Queries if the given object is of given type.
Definition value_type.h:376