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