Ruby 2.7.0p0(2019-12-25revision647ee6f091eafcce70ffb75ddf7e121e192ab217)

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
38 static ID id_submicro, id_nano_num, id_nano_den, id_offset, id_zone;
39 static ID id_nanosecond, id_microsecond, id_millisecond, id_nsec, id_usec;
40 static ID id_local_to_utc, id_utc_to_local, id_find_timezone;
41 static 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
59 static int
60 eq(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
68 static int
69 cmp(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
88 static VALUE
89 addv(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
98 static VALUE
99 subv(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
108 static VALUE
109 mulv(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
119 static VALUE
120 divv(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
130 static VALUE
131 modv(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
143 static VALUE
144 quor(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
160 static VALUE
161 quov(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
173 static void
174 divmodv(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;
204 typedef int64_t SIGNED_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;
220 typedef VALUE WIDEVALUE;
221 typedef SIGNED_VALUE SIGNED_WIDEVALUE;
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
248 typedef WIDEVALUE wideval_t;
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
267 static inline VALUE
268 w2v(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
280 static wideval_t
281 v2w_bignum(VALUE v)
282 {
283 int sign;
284 uwideint_t u;
285 sign = rb_integer_pack(v, &u, 1, sizeof(u), 0,
286 INTEGER_PACK_NATIVE_BYTE_ORDER);
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
301 static inline wideval_t
302 v2w(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)) {
311 return WIDEVAL_WRAP((WIDEVALUE)(SIGNED_WIDEVALUE)(long)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
321 static int
322 weq(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
334 static int
335 wcmp(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
361 static wideval_t
362 wadd(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
373 static wideval_t
374 wsub(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
385 static wideval_t
386 wmul(wideval_t wx, wideval_t wy)
387 {
388 #if WIDEVALUE_IS_WIDER
389 if (FIXWV_P(wx) && FIXWV_P(wy)) {
390 if (!MUL_OVERFLOW_FIXWV_P(FIXWV2WINT(wx), FIXWV2WINT(wy)))
391 return WINT2WV(FIXWV2WINT(wx) * FIXWV2WINT(wy));
392 }
393 #endif
394 return v2w(mulv(w2v(wx), w2v(wy)));
395 }
396
397 static wideval_t
398 wquo(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
419 static int
420 wdivmod0(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
457 static void
458 wdivmod(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
469 static void
470 wmuldivmod(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
480 static wideval_t
481 wdiv(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
490 static wideval_t
491 wmod(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
500 static VALUE
501 num_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
550 static wideval_t
551 rb_time_magnify(wideval_t w)
552 {
553 return wmul(w, WINT2FIXWV(TIME_SCALE));
554 }
555
556 static VALUE
557 rb_time_unmagnify_to_rational(wideval_t w)
558 {
559 return quor(w2v(w), INT2FIX(TIME_SCALE));
560 }
561
562 static wideval_t
563 rb_time_unmagnify(wideval_t w)
564 {
565 return v2w(rb_time_unmagnify_to_rational(w));
566 }
567
568 static VALUE
569 rb_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
592 static void
593 split_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
601 static wideval_t
602 timet2wv(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
622 static time_t
623 wv2timet(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
645 VALUE rb_cTime;
646 static VALUE rb_cTimeTM;
647
648 static int obj2int(VALUE obj);
649 static uint32_t obj2ubits(VALUE obj, size_t bits);
650 static VALUE obj2vint(VALUE obj);
651 static uint32_t month_arg(VALUE arg);
652 static VALUE validate_utc_offset(VALUE utc_offset);
653 static VALUE validate_zone_name(VALUE zone_name);
654 static void validate_vtm(struct vtm *vtm);
655 static uint32_t obj2subsecx(VALUE obj, VALUE *subsecx);
656
657 static VALUE time_gmtime(VALUE);
658 static VALUE time_localtime(VALUE);
659 static VALUE time_fixoff(VALUE);
660 static VALUE time_zonelocal(VALUE time, VALUE off);
661
662 static time_t timegm_noleapsecond(struct tm *tm);
663 static int tmcmp(struct tm *a, struct tm *b);
664 static int vtmcmp(struct vtm *a, struct vtm *b);
665 static const char *find_time_t(struct tm *tptr, int utc_p, time_t *tp);
666
667 static struct vtm *localtimew(wideval_t timew, struct vtm *result);
668
669 static int leap_year_p(long y);
670 #define leap_year_v_p(y) leap_year_p(NUM2LONG(modv((y), INT2FIX(400))))
671
672 static VALUE tm_from_time(VALUE klass, VALUE time);
673
674 bool ruby_tz_uptodate_p;
675
676 static void
677 update_tz(void)
678 {
679 if (ruby_tz_uptodate_p) return;
680 ruby_tz_uptodate_p = true;
681 tzset();
682 }
683
684 static struct tm *
685 rb_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
719 static struct tm *
720 rb_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
738 static 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 };
753 static 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
769 static const int common_year_days_in_month[] = {
770 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
771 };
772 static 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
793 static 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 };
797 static 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
824 static 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 };
828 static 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
837 static int
838 calc_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
851 static wideval_t
852 timegmw_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
892 static VALUE
893 zone_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) {
911 str = rb_usascii_str_new(zone, len);
912 }
913 else {
914 str = rb_enc_str_new(zone, len, rb_locale_encoding());
915 }
916 return rb_fstring(str);
917 }
918
919 static void
920 gmtimew_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
1021 static struct tm *
1022 gmtime_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
1145 static long this_year = 0;
1146 static time_t known_leap_seconds_limit;
1147 static int number_of_leap_seconds_known;
1148
1149 static void
1150 init_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() */
1192 void
1193 ruby_reset_leap_second_info(void)
1194 {
1195 this_year = 0;
1196 }
1197
1198 static wideval_t
1199 timegmw(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
1240 static struct vtm *
1241 gmtimew(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
1295 static 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 */
1330 static 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 */
1371 static 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
1376 static int
1377 calc_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
1390 static VALUE
1391 guess_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);
1471 rb_gc_register_mark_object(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
1482 static VALUE
1483 small_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
1500 static wideval_t
1501 timelocalw(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
1582 static struct tm *
1583 localtime_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
1637 static int
1638 timew_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
1670 static struct vtm *
1671 localtimew(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
1724 PACKED_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
1753 static 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
1761 static void
1762 time_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
1773 static size_t
1774 time_memsize(const void *tobj)
1775 {
1776 return sizeof(struct time_object);
1777 }
1778
1779 static const rb_data_type_t time_data_type = {
1780 "time",
1781 {time_mark, RUBY_TYPED_DEFAULT_FREE, time_memsize,},
1782 0, 0, RUBY_TYPED_FREE_IMMEDIATELY
1783 };
1784
1785 static VALUE
1786 time_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
1800 static struct time_object *
1801 get_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
1811 static struct time_object *
1812 get_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
1822 static void
1823 time_modify(VALUE time)
1824 {
1825 rb_check_frozen(time);
1826 }
1827
1828 static wideval_t
1829 timespec2timew(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
1839 static struct timespec
1840 timew2timespec(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
1854 static struct timespec *
1855 timew2timespec_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
1872 void
1873 rb_timespec_now(struct timespec *ts)
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
1891 static VALUE
1892 time_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
1908 static VALUE
1909 time_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
1924 static void
1925 vtm_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
2049 static int
2050 maybe_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
2058 NORETURN(static void invalid_utc_offset(void));
2059 static void
2060 invalid_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);
2065 rb_exc_raise(rb_exc_new_str(rb_eArgError, str));
2066 }
2067
2068 static VALUE
2069 utc_offset_arg(VALUE arg)
2070 {
2071 VALUE tmp;
2072 if (!NIL_P(tmp = rb_check_string_type(arg))) {
2073 int n = 0;
2074 char *s = RSTRING_PTR(tmp);
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 goto invalid_utc_offset;
2104 case 9:
2105 if (s[6] != ':') goto invalid_utc_offset;
2106 if (!ISDIGIT(s[7]) || !ISDIGIT(s[8])) goto invalid_utc_offset;
2107 n += (s[7] * 10 + s[8] - '0' * 11);
2108 /* fall through */
2109 case 6:
2110 if (s[0] != '+' && s[0] != '-') goto invalid_utc_offset;
2111 if (!ISDIGIT(s[1]) || !ISDIGIT(s[2])) goto invalid_utc_offset;
2112 if (s[3] != ':') goto invalid_utc_offset;
2113 if (!ISDIGIT(s[4]) || !ISDIGIT(s[5])) goto invalid_utc_offset;
2114 if (s[4] > '5') goto invalid_utc_offset;
2115 break;
2116 default:
2117 goto invalid_utc_offset;
2118 }
2119 n += (s[1] * 10 + s[2] - '0' * 11) * 3600;
2120 n += (s[4] * 10 + s[5] - '0' * 11) * 60;
2121 if (s[0] == '-')
2122 n = -n;
2123 return INT2FIX(n);
2124 }
2125 else {
2126 return num_exact(arg);
2127 }
2128 }
2129
2130 static void
2131 zone_set_offset(VALUE zone, struct time_object *tobj,
2132 wideval_t tlocal, wideval_t tutc)
2133 {
2134 /* tlocal and tutc must be unmagnified and in seconds */
2135 wideval_t w = wsub(tlocal, tutc);
2136 VALUE off = w2v(w);
2137 validate_utc_offset(off);
2138 tobj->vtm.utc_offset = off;
2139 tobj->vtm.zone = zone;
2140 tobj->tzmode = TIME_TZMODE_LOCALTIME;
2141 }
2142
2143 static wideval_t
2144 extract_time(VALUE time)
2145 {
2146 wideval_t t;
2147 const ID id_to_i = idTo_i;
2148
2149 #define EXTRACT_TIME() do { \
2150 t = v2w(rb_Integer(AREF(to_i))); \
2151 } while (0)
2152
2153 if (rb_typeddata_is_kind_of(time, &time_data_type)) {
2154 struct time_object *tobj = DATA_PTR(time);
2155
2156 time_gmtime(time); /* ensure tm got */
2157 t = rb_time_unmagnify(tobj->timew);
2158 }
2159 else if (RB_TYPE_P(time, T_STRUCT)) {
2160 #define AREF(x) rb_struct_aref(time, ID2SYM(id_##x))
2161 EXTRACT_TIME();
2162 #undef AREF
2163 }
2164 else {
2165 #define AREF(x) rb_funcallv(time, id_##x, 0, 0)
2166 EXTRACT_TIME();
2167 #undef AREF
2168 }
2169 #undef EXTRACT_TIME
2170
2171 return t;
2172 }
2173
2174 static wideval_t
2175 extract_vtm(VALUE time, struct vtm *vtm, VALUE subsecx)
2176 {
2177 wideval_t t;
2178 const ID id_to_i = idTo_i;
2179
2180 #define EXTRACT_VTM() do { \
2181 VALUE subsecx; \
2182 vtm->year = obj2vint(AREF(year)); \
2183 vtm->mon = month_arg(AREF(mon)); \
2184 vtm->mday = obj2ubits(AREF(mday), 5); \
2185 vtm->hour = obj2ubits(AREF(hour), 5); \
2186 vtm->min = obj2ubits(AREF(min), 6); \
2187 vtm->sec = obj2subsecx(AREF(sec), &subsecx); \
2188 vtm->isdst = RTEST(AREF(isdst)); \
2189 vtm->utc_offset = Qnil; \
2190 t = v2w(rb_Integer(AREF(to_i))); \
2191 } while (0)
2192
2193 if (rb_typeddata_is_kind_of(time, &time_data_type)) {
2194 struct time_object *tobj = DATA_PTR(time);
2195
2196 time_get_tm(time, tobj);
2197 *vtm = tobj->vtm;
2198 t = rb_time_unmagnify(tobj->timew);
2199 if (TZMODE_FIXOFF_P(tobj) && vtm->utc_offset != INT2FIX(0))
2200 t = wadd(t, v2w(vtm->utc_offset));
2201 }
2202 else if (RB_TYPE_P(time, T_STRUCT)) {
2203 #define AREF(x) rb_struct_aref(time, ID2SYM(id_##x))
2204 EXTRACT_VTM();
2205 #undef AREF
2206 }
2207 else if (rb_integer_type_p(time)) {
2208 t = v2w(time);
2209 GMTIMEW(rb_time_magnify(t), vtm);
2210 }
2211 else {
2212 #define AREF(x) rb_funcallv(time, id_##x, 0, 0)
2213 EXTRACT_VTM();
2214 #undef AREF
2215 }
2216 #undef EXTRACT_VTM
2217 vtm->subsecx = subsecx;
2218 validate_vtm(vtm);
2219 return t;
2220 }
2221
2222 static void
2223 zone_set_dst(VALUE zone, struct time_object *tobj, VALUE tm)
2224 {
2225 ID id_dst_p;
2226 VALUE dst;
2227 CONST_ID(id_dst_p, "dst?");
2228 dst = rb_check_funcall(zone, id_dst_p, 1, &tm);
2229 tobj->vtm.isdst = (dst != Qundef && RTEST(dst));
2230 }
2231
2232 static int
2233 zone_timelocal(VALUE zone, VALUE time)
2234 {
2235 VALUE utc, tm;
2236 struct time_object *tobj = DATA_PTR(time);
2237 wideval_t t, s;
2238
2239 t = rb_time_unmagnify(tobj->timew);
2240 tm = tm_from_time(rb_cTimeTM, time);
2241 utc = rb_check_funcall(zone, id_local_to_utc, 1, &tm);
2242 if (utc == Qundef) return 0;
2243
2244 s = extract_time(utc);
2245 zone_set_offset(zone, tobj, t, s);
2246 s = rb_time_magnify(s);
2247 if (tobj->vtm.subsecx != INT2FIX(0)) {
2248 s = wadd(s, v2w(tobj->vtm.subsecx));
2249 }
2250 tobj->timew = s;
2251 zone_set_dst(zone, tobj, tm);
2252 return 1;
2253 }
2254
2255 static int
2256 zone_localtime(VALUE zone, VALUE time)
2257 {
2258 VALUE local, tm, subsecx;
2259 struct time_object *tobj = DATA_PTR(time);
2260 wideval_t t, s;
2261
2262 split_second(tobj->timew, &t, &subsecx);
2263 tm = tm_from_time(rb_cTimeTM, time);
2264
2265 local = rb_check_funcall(zone, id_utc_to_local, 1, &tm);
2266 if (local == Qundef) return 0;
2267
2268 s = extract_vtm(local, &tobj->vtm, subsecx);
2269 tobj->tm_got = 1;
2270 zone_set_offset(zone, tobj, s, t);
2271 zone_set_dst(zone, tobj, tm);
2272 return 1;
2273 }
2274
2275 static VALUE
2276 find_timezone(VALUE time, VALUE zone)
2277 {
2278 VALUE klass = CLASS_OF(time);
2279
2280 return rb_check_funcall_default(klass, id_find_timezone, 1, &zone, Qnil);
2281 }
2282
2283 static VALUE
2284 time_init_1(int argc, VALUE *argv, VALUE time)
2285 {
2286 struct vtm vtm;
2287 VALUE zone = Qnil;
2288 VALUE utc = Qnil;
2289 VALUE v[7];
2290 struct time_object *tobj;
2291
2292 vtm.wday = VTM_WDAY_INITVAL;
2293 vtm.yday = 0;
2294 vtm.zone = rb_fstring_lit("");
2295
2296 /* year mon mday hour min sec off */
2297 rb_scan_args(argc, argv, "16", &v[0],&v[1],&v[2],&v[3],&v[4],&v[5],&v[6]);
2298
2299 vtm.year = obj2vint(v[0]);
2300
2301 vtm.mon = NIL_P(v[1]) ? 1 : month_arg(v[1]);
2302
2303 vtm.mday = NIL_P(v[2]) ? 1 : obj2ubits(v[2], 5);
2304
2305 vtm.hour = NIL_P(v[3]) ? 0 : obj2ubits(v[3], 5);
2306
2307 vtm.min = NIL_P(v[4]) ? 0 : obj2ubits(v[4], 6);
2308
2309 if (NIL_P(v[5])) {
2310 vtm.sec = 0;
2311 vtm.subsecx = INT2FIX(0);
2312 }
2313 else {
2314 VALUE subsecx;
2315 vtm.sec = obj2subsecx(v[5], &subsecx);
2316 vtm.subsecx = subsecx;
2317 }
2318
2319 vtm.isdst = VTM_ISDST_INITVAL;
2320 vtm.utc_offset = Qnil;
2321 if (!NIL_P(v[6])) {
2322 VALUE arg = v[6];
2323 if (arg == ID2SYM(rb_intern("dst")))
2324 vtm.isdst = 1;
2325 else if (arg == ID2SYM(rb_intern("std")))
2326 vtm.isdst = 0;
2327 else if (maybe_tzobj_p(arg))
2328 zone = arg;
2329 else if (!NIL_P(utc = utc_offset_arg(arg)))
2330 vtm.utc_offset = utc == UTC_ZONE ? INT2FIX(0) : utc;
2331 else if (NIL_P(zone = find_timezone(time, arg)))
2332 invalid_utc_offset();
2333 }
2334
2335 validate_vtm(&vtm);
2336
2337 time_modify(time);
2338 GetNewTimeval(time, tobj);
2339
2340 if (!NIL_P(zone)) {
2341 tobj->timew = timegmw(&vtm);
2342 tobj->vtm = vtm;
2343 tobj->tm_got = 1;
2344 TZMODE_SET_LOCALTIME(tobj);
2345 if (zone_timelocal(zone, time)) {
2346 return time;
2347 }
2348 else if (NIL_P(vtm.utc_offset = utc_offset_arg(zone))) {
2349 if (NIL_P(zone = find_timezone(time, zone)) || !zone_timelocal(zone, time))
2350 invalid_utc_offset();
2351 }
2352 }
2353
2354 if (utc == UTC_ZONE) {
2355 tobj->timew = timegmw(&vtm);
2356 tobj->vtm = vtm;
2357 tobj->tm_got = 1;
2358 TZMODE_SET_UTC(tobj);
2359 return time;
2360 }
2361
2362 tobj->tzmode = TIME_TZMODE_LOCALTIME;
2363 tobj->tm_got=0;
2364 tobj->timew = WINT2FIXWV(0);
2365
2366 if (!NIL_P(vtm.utc_offset)) {
2367 VALUE off = vtm.utc_offset;
2368 vtm_add_offset(&vtm, off, -1);
2369 vtm.utc_offset = Qnil;
2370 tobj->timew = timegmw(&vtm);
2371 return time_set_utc_offset(time, off);
2372 }
2373 else {
2374 tobj->timew = timelocalw(&vtm);
2375 return time_localtime(time);
2376 }
2377 }
2378
2379
2380 /*
2381 * call-seq:
2382 * Time.new -> time
2383 * Time.new(year, month=nil, day=nil, hour=nil, min=nil, sec=nil, tz=nil) -> time
2384 *
2385 * Returns a Time object.
2386 *
2387 * It is initialized to the current system time if no argument is given.
2388 *
2389 * *Note:* The new object will use the resolution available on your
2390 * system clock, and may include fractional seconds.
2391 *
2392 * If one or more arguments are specified, the time is initialized to the
2393 * specified time.
2394 *
2395 * +sec+ may have fraction if it is a rational.
2396 *
2397 * +tz+ specifies the timezone.
2398 * It can be an offset from UTC, given either as a string such as "+09:00"
2399 * or a single letter "A".."Z" excluding "J" (so-called military time zone),
2400 * or as a number of seconds such as 32400.
2401 * Or it can be a timezone object,
2402 * see {Timezone argument}[#class-Time-label-Timezone+argument] for details.
2403 *
2404 * a = Time.new #=> 2007-11-19 07:50:02 -0600
2405 * b = Time.new #=> 2007-11-19 07:50:02 -0600
2406 * a == b #=> false
2407 * "%.6f" % a.to_f #=> "1195480202.282373"
2408 * "%.6f" % b.to_f #=> "1195480202.283415"
2409 *
2410 * Time.new(2008,6,21, 13,30,0, "+09:00") #=> 2008-06-21 13:30:00 +0900
2411 *
2412 * # A trip for RubyConf 2007
2413 * t1 = Time.new(2007,11,1,15,25,0, "+09:00") # JST (Narita)
2414 * t2 = Time.new(2007,11,1,12, 5,0, "-05:00") # CDT (Minneapolis)
2415 * t3 = Time.new(2007,11,1,13,25,0, "-05:00") # CDT (Minneapolis)
2416 * t4 = Time.new(2007,11,1,16,53,0, "-04:00") # EDT (Charlotte)
2417 * t5 = Time.new(2007,11,5, 9,24,0, "-05:00") # EST (Charlotte)
2418 * t6 = Time.new(2007,11,5,11,21,0, "-05:00") # EST (Detroit)
2419 * t7 = Time.new(2007,11,5,13,45,0, "-05:00") # EST (Detroit)
2420 * t8 = Time.new(2007,11,6,17,10,0, "+09:00") # JST (Narita)
2421 * (t2-t1)/3600.0 #=> 10.666666666666666
2422 * (t4-t3)/3600.0 #=> 2.466666666666667
2423 * (t6-t5)/3600.0 #=> 1.95
2424 * (t8-t7)/3600.0 #=> 13.416666666666666
2425 *
2426 */
2427
2428 static VALUE
2429 time_init(int argc, VALUE *argv, VALUE time)
2430 {
2431 if (argc == 0)
2432 return time_init_0(time);
2433 else
2434 return time_init_1(argc, argv, time);
2435 }
2436
2437 static void
2438 time_overflow_p(time_t *secp, long *nsecp)
2439 {
2440 time_t sec = *secp;
2441 long nsec = *nsecp;
2442 long sec2;
2443
2444 if (nsec >= 1000000000) { /* nsec positive overflow */
2445 sec2 = nsec / 1000000000;
2446 if (TIMET_MAX - sec2 < sec) {
2447 rb_raise(rb_eRangeError, "out of Time range");
2448 }
2449 nsec -= sec2 * 1000000000;
2450 sec += sec2;
2451 }
2452 else if (nsec < 0) { /* nsec negative overflow */
2453 sec2 = NDIV(nsec,1000000000); /* negative div */
2454 if (sec < TIMET_MIN - sec2) {
2455 rb_raise(rb_eRangeError, "out of Time range");
2456 }
2457 nsec -= sec2 * 1000000000;
2458 sec += sec2;
2459 }
2460 #ifndef NEGATIVE_TIME_T
2461 if (sec < 0)
2462 rb_raise(rb_eArgError, "time must be positive");
2463 #endif
2464 *secp = sec;
2465 *nsecp = nsec;
2466 }
2467
2468 static wideval_t
2469 nsec2timew(time_t sec, long nsec)
2470 {
2471 struct timespec ts;
2472 time_overflow_p(&sec, &nsec);
2473 ts.tv_sec = sec;
2474 ts.tv_nsec = nsec;
2475 return timespec2timew(&ts);
2476 }
2477
2478 static VALUE
2479 time_new_timew(VALUE klass, wideval_t timew)
2480 {
2481 VALUE time = time_s_alloc(klass);
2482 struct time_object *tobj;
2483
2484 tobj = DATA_PTR(time); /* skip type check */
2485 tobj->tzmode = TIME_TZMODE_LOCALTIME;
2486 tobj->timew = timew;
2487
2488 return time;
2489 }
2490
2491 VALUE
2492 rb_time_new(time_t sec, long usec)
2493 {
2494 wideval_t timew;
2495
2496 if (usec >= 1000000) {
2497 long sec2 = usec / 1000000;
2498 if (sec > TIMET_MAX - sec2) {
2499 rb_raise(rb_eRangeError, "out of Time range");
2500 }
2501 usec -= sec2 * 1000000;
2502 sec += sec2;
2503 }
2504 else if (usec < 0) {
2505 long sec2 = NDIV(usec,1000000); /* negative div */
2506 if (sec < TIMET_MIN - sec2) {
2507 rb_raise(rb_eRangeError, "out of Time range");
2508 }
2509 usec -= sec2 * 1000000;
2510 sec += sec2;
2511 }
2512
2513 timew = nsec2timew(sec, usec * 1000);
2514 return time_new_timew(rb_cTime, timew);
2515 }
2516
2517 /* returns localtime time object */
2518 VALUE
2519 rb_time_nano_new(time_t sec, long nsec)
2520 {
2521 return time_new_timew(rb_cTime, nsec2timew(sec, nsec));
2522 }
2523
2529 VALUE
2530 rb_time_timespec_new(const struct timespec *ts, int offset)
2531 {
2532 struct time_object *tobj;
2533 VALUE time = time_new_timew(rb_cTime, nsec2timew(ts->tv_sec, ts->tv_nsec));
2534
2535 if (-86400 < offset && offset < 86400) { /* fixoff */
2536 GetTimeval(time, tobj);
2537 TZMODE_SET_FIXOFF(tobj, INT2FIX(offset));
2538 }
2539 else if (offset == INT_MAX) { /* localtime */
2540 }
2541 else if (offset == INT_MAX-1) { /* UTC */
2542 GetTimeval(time, tobj);
2543 TZMODE_SET_UTC(tobj);
2544 }
2545 else {
2546 rb_raise(rb_eArgError, "utc_offset out of range");
2547 }
2548
2549 return time;
2550 }
2551
2552 VALUE
2553 rb_time_num_new(VALUE timev, VALUE off)
2554 {
2555 VALUE time = time_new_timew(rb_cTime, rb_time_magnify(v2w(timev)));
2556
2557 if (!NIL_P(off)) {
2558 VALUE zone = off;
2559
2560 if (maybe_tzobj_p(zone)) {
2561 time_gmtime(time);
2562 if (zone_timelocal(zone, time)) return time;
2563 }
2564 if (NIL_P(off = utc_offset_arg(off))) {
2565 if (NIL_P(zone = find_timezone(time, zone))) invalid_utc_offset();
2566 time_gmtime(time);
2567 if (!zone_timelocal(zone, time)) invalid_utc_offset();
2568 return time;
2569 }
2570 else if (off == UTC_ZONE) {
2571 return time_gmtime(time);
2572 }
2573
2574 validate_utc_offset(off);
2575 time_set_utc_offset(time, off);
2576 return time;
2577 }
2578
2579 return time;
2580 }
2581
2582 static struct timespec
2583 time_timespec(VALUE num, int interval)
2584 {
2585 struct timespec t;
2586 const char *const tstr = interval ? "time interval" : "time";
2587 VALUE i, f, ary;
2588
2589 #ifndef NEGATIVE_TIME_T
2590 # define arg_range_check(v) \
2591 (((v) < 0) ? \
2592 rb_raise(rb_eArgError, "%s must not be negative", tstr) : \
2593 (void)0)
2594 #else
2595 # define arg_range_check(v) \
2596 ((interval && (v) < 0) ? \
2597 rb_raise(rb_eArgError, "time interval must not be negative") : \
2598 (void)0)
2599 #endif
2600
2601 if (FIXNUM_P(num)) {
2602 t.tv_sec = NUM2TIMET(num);
2603 arg_range_check(t.tv_sec);
2604 t.tv_nsec = 0;
2605 }
2606 else if (RB_FLOAT_TYPE_P(num)) {
2607 double x = RFLOAT_VALUE(num);
2608 arg_range_check(x);
2609 {
2610 double f, d;
2611
2612 d = modf(x, &f);
2613 if (d >= 0) {
2614 t.tv_nsec = (int)(d*1e9+0.5);
2615 if (t.tv_nsec >= 1000000000) {
2616 t.tv_nsec -= 1000000000;
2617 f += 1;
2618 }
2619 }
2620 else if ((t.tv_nsec = (int)(-d*1e9+0.5)) > 0) {
2621 t.tv_nsec = 1000000000 - t.tv_nsec;
2622 f -= 1;
2623 }
2624 t.tv_sec = (time_t)f;
2625 if (f != t.tv_sec) {
2626 rb_raise(rb_eRangeError, "%f out of Time range", x);
2627 }
2628 }
2629 }
2630 else if (RB_TYPE_P(num, T_BIGNUM)) {
2631 t.tv_sec = NUM2TIMET(num);
2632 arg_range_check(t.tv_sec);
2633 t.tv_nsec = 0;
2634 }
2635 else {
2636 i = INT2FIX(1);
2637 ary = rb_check_funcall(num, id_divmod, 1, &i);
2638 if (ary != Qundef && !NIL_P(ary = rb_check_array_type(ary))) {
2639 i = rb_ary_entry(ary, 0);
2640 f = rb_ary_entry(ary, 1);
2641 t.tv_sec = NUM2TIMET(i);
2642 arg_range_check(t.tv_sec);
2643 f = rb_funcall(f, '*', 1, INT2FIX(1000000000));
2644 t.tv_nsec = NUM2LONG(f);
2645 }
2646 else {
2647 rb_raise(rb_eTypeError, "can't convert %"PRIsVALUE" into %s",
2648 rb_obj_class(num), tstr);
2649 }
2650 }
2651 return t;
2652 #undef arg_range_check
2653 }
2654
2655 static struct timeval
2656 time_timeval(VALUE num, int interval)
2657 {
2658 struct timespec ts;
2659 struct timeval tv;
2660
2661 ts = time_timespec(num, interval);
2662 tv.tv_sec = (TYPEOF_TIMEVAL_TV_SEC)ts.tv_sec;
2663 tv.tv_usec = (TYPEOF_TIMEVAL_TV_USEC)(ts.tv_nsec / 1000);
2664
2665 return tv;
2666 }
2667
2668 struct timeval
2669 rb_time_interval(VALUE num)
2670 {
2671 return time_timeval(num, TRUE);
2672 }
2673
2674 struct timeval
2675 rb_time_timeval(VALUE time)
2676 {
2677 struct time_object *tobj;
2678 struct timeval t;
2679 struct timespec ts;
2680
2681 if (IsTimeval(time)) {
2682 GetTimeval(time, tobj);
2683 ts = timew2timespec(tobj->timew);
2684 t.tv_sec = (TYPEOF_TIMEVAL_TV_SEC)ts.tv_sec;
2685 t.tv_usec = (TYPEOF_TIMEVAL_TV_USEC)(ts.tv_nsec / 1000);
2686 return t;
2687 }
2688 return time_timeval(time, FALSE);
2689 }
2690
2691 struct timespec
2692 rb_time_timespec(VALUE time)
2693 {
2694 struct time_object *tobj;
2695 struct timespec t;
2696
2697 if (IsTimeval(time)) {
2698 GetTimeval(time, tobj);
2699 t = timew2timespec(tobj->timew);
2700 return t;
2701 }
2702 return time_timespec(time, FALSE);
2703 }
2704
2705 struct timespec
2706 rb_time_timespec_interval(VALUE num)
2707 {
2708 return time_timespec(num, TRUE);
2709 }
2710
2711 enum {
2712 TMOPT_IN,
2713 TMOPT_MAX_
2714 };
2715
2716 static bool
2717 get_tmopt(VALUE opts, VALUE vals[TMOPT_MAX_])
2718 {
2719 ID ids[TMOPT_MAX_];
2720
2721 if (NIL_P(opts)) return false;
2722 CONST_ID(ids[TMOPT_IN], "in");
2723 rb_get_kwargs(opts, ids, 0, TMOPT_MAX_, vals);
2724 return true;
2725 }
2726
2727 /*
2728 * call-seq:
2729 * Time.now -> time
2730 *
2731 * Creates a new Time object for the current time.
2732 * This is same as Time.new without arguments.
2733 *
2734 * Time.now #=> 2009-06-24 12:39:54 +0900
2735 */
2736
2737 static VALUE
2738 time_s_now(int argc, VALUE *argv, VALUE klass)
2739 {
2740 VALUE vals[TMOPT_MAX_], opts, t, zone = Qundef;
2741 rb_scan_args(argc, argv, ":", &opts);
2742 if (get_tmopt(opts, vals)) zone = vals[TMOPT_IN];
2743 t = rb_class_new_instance(0, NULL, klass);
2744 if (zone != Qundef) {
2745 time_zonelocal(t, zone);
2746 }
2747 return t;
2748 }
2749
2750 static int
2751 get_scale(VALUE unit)
2752 {
2753 if (unit == ID2SYM(id_nanosecond) || unit == ID2SYM(id_nsec)) {
2754 return 1000000000;
2755 }
2756 else if (unit == ID2SYM(id_microsecond) || unit == ID2SYM(id_usec)) {
2757 return 1000000;
2758 }
2759 else if (unit == ID2SYM(id_millisecond)) {
2760 return 1000;
2761 }
2762 else {
2763 rb_raise(rb_eArgError, "unexpected unit: %"PRIsVALUE, unit);
2764 }
2765 }
2766
2767 /*
2768 * call-seq:
2769 * Time.at(time) -> time
2770 * Time.at(seconds_with_frac) -> time
2771 * Time.at(seconds, microseconds_with_frac) -> time
2772 * Time.at(seconds, milliseconds, :millisecond) -> time
2773 * Time.at(seconds, microseconds, :usec) -> time
2774 * Time.at(seconds, microseconds, :microsecond) -> time
2775 * Time.at(seconds, nanoseconds, :nsec) -> time
2776 * Time.at(seconds, nanoseconds, :nanosecond) -> time
2777 * Time.at(time, in: tz) -> time
2778 * Time.at(seconds_with_frac, in: tz) -> time
2779 * Time.at(seconds, microseconds_with_frac, in: tz) -> time
2780 * Time.at(seconds, milliseconds, :millisecond, in: tz) -> time
2781 * Time.at(seconds, microseconds, :usec, in: tz) -> time
2782 * Time.at(seconds, microseconds, :microsecond, in: tz) -> time
2783 * Time.at(seconds, nanoseconds, :nsec, in: tz) -> time
2784 * Time.at(seconds, nanoseconds, :nanosecond, in: tz) -> time
2785 *
2786 * Creates a new Time object with the value given by +time+,
2787 * the given number of +seconds_with_frac+, or
2788 * +seconds+ and +microseconds_with_frac+ since the Epoch.
2789 * +seconds_with_frac+ and +microseconds_with_frac+
2790 * can be an Integer, Float, Rational, or other Numeric.
2791 * non-portable feature allows the offset to be negative on some systems.
2792 *
2793 * If +in+ argument is given, the result is in that timezone or UTC offset, or
2794 * if a numeric argument is given, the result is in local time.
2795 *
2796 * Time.at(0) #=> 1969-12-31 18:00:00 -0600
2797 * Time.at(Time.at(0)) #=> 1969-12-31 18:00:00 -0600
2798 * Time.at(946702800) #=> 1999-12-31 23:00:00 -0600
2799 * Time.at(-284061600) #=> 1960-12-31 00:00:00 -0600
2800 * Time.at(946684800.2).usec #=> 200000
2801 * Time.at(946684800, 123456.789).nsec #=> 123456789
2802 * Time.at(946684800, 123456789, :nsec).nsec #=> 123456789
2803 */
2804
2805 static VALUE
2806 time_s_at(int argc, VALUE *argv, VALUE klass)
2807 {
2808 VALUE time, t, unit = Qundef, zone = Qundef, opts;
2809 VALUE vals[TMOPT_MAX_];
2810 wideval_t timew;
2811
2812 argc = rb_scan_args(argc, argv, "12:", &time, &t, &unit, &opts);
2813 if (get_tmopt(opts, vals)) {
2814 zone = vals[0];
2815 }
2816 if (argc >= 2) {
2817 int scale = argc == 3 ? get_scale(unit) : 1000000;
2818 time = num_exact(time);
2819 t = num_exact(t);
2820 timew = wadd(rb_time_magnify(v2w(time)), wmulquoll(v2w(t), TIME_SCALE, scale));
2821 t = time_new_timew(klass, timew);
2822 }
2823 else if (IsTimeval(time)) {
2824 struct time_object *tobj, *tobj2;
2825 GetTimeval(time, tobj);
2826 t = time_new_timew(klass, tobj->timew);
2827 GetTimeval(t, tobj2);
2828 TZMODE_COPY(tobj2, tobj);
2829 }
2830 else {
2831 timew = rb_time_magnify(v2w(num_exact(time)));
2832 t = time_new_timew(klass, timew);
2833 }
2834 if (zone != Qundef) {
2835 time_zonelocal(t, zone);
2836 }
2837
2838 return t;
2839 }
2840
2841 static const char months[][4] = {
2842 "jan", "feb", "mar", "apr", "may", "jun",
2843 "jul", "aug", "sep", "oct", "nov", "dec",
2844 };
2845
2846 static int
2847 obj2int(VALUE obj)
2848 {
2849 if (RB_TYPE_P(obj, T_STRING)) {
2850 obj = rb_str_to_inum(obj, 10, FALSE);
2851 }
2852
2853 return NUM2INT(obj);
2854 }
2855
2856 static uint32_t
2857 obj2ubits(VALUE obj, size_t bits)
2858 {
2859 static const uint32_t u32max = (uint32_t)-1;
2860 const uint32_t usable_mask = ~(u32max << bits);
2861 uint32_t rv;
2862 int tmp = obj2int(obj);
2863
2864 if (tmp < 0)
2865 rb_raise(rb_eArgError, "argument out of range");
2866 rv = tmp;
2867 if ((rv & usable_mask) != rv)
2868 rb_raise(rb_eArgError, "argument out of range");
2869 return rv;
2870 }
2871
2872 static VALUE
2873 obj2vint(VALUE obj)
2874 {
2875 if (RB_TYPE_P(obj, T_STRING)) {
2876 obj = rb_str_to_inum(obj, 10, FALSE);
2877 }
2878 else {
2879 obj = rb_to_int(obj);
2880 }
2881
2882 return obj;
2883 }
2884
2885 static uint32_t
2886 obj2subsecx(VALUE obj, VALUE *subsecx)
2887 {
2888 VALUE subsec;
2889
2890 if (RB_TYPE_P(obj, T_STRING)) {
2891 obj = rb_str_to_inum(obj, 10, FALSE);
2892 *subsecx = INT2FIX(0);
2893 }
2894 else {
2895 divmodv(num_exact(obj), INT2FIX(1), &obj, &subsec);
2896 *subsecx = w2v(rb_time_magnify(v2w(subsec)));
2897 }
2898 return obj2ubits(obj, 6); /* vtm->sec */
2899 }
2900
2901 static VALUE
2902 usec2subsecx(VALUE obj)
2903 {
2904 if (RB_TYPE_P(obj, T_STRING)) {
2905 obj = rb_str_to_inum(obj, 10, FALSE);
2906 }
2907
2908 return mulquov(num_exact(obj), INT2FIX(TIME_SCALE), INT2FIX(1000000));
2909 }
2910
2911 static uint32_t
2912 month_arg(VALUE arg)
2913 {
2914 int i, mon;
2915
2916 VALUE s = rb_check_string_type(arg);
2917 if (!NIL_P(s) && RSTRING_LEN(s) > 0) {
2918 mon = 0;
2919 for (i=0; i<12; i++) {
2920 if (RSTRING_LEN(s) == 3 &&
2921 STRNCASECMP(months[i], RSTRING_PTR(s), 3) == 0) {
2922 mon = i+1;
2923 break;
2924 }
2925 }
2926 if (mon == 0) {
2927 char c = RSTRING_PTR(s)[0];
2928
2929 if ('0' <= c && c <= '9') {
2930 mon = obj2ubits(s, 4);
2931 }
2932 }
2933 }
2934 else {
2935 mon = obj2ubits(arg, 4);
2936 }
2937 return mon;
2938 }
2939
2940 static VALUE
2941 validate_utc_offset(VALUE utc_offset)
2942 {
2943 if (le(utc_offset, INT2FIX(-86400)) || ge(utc_offset, INT2FIX(86400)))
2944 rb_raise(rb_eArgError, "utc_offset out of range");
2945 return utc_offset;
2946 }
2947
2948 static VALUE
2949 validate_zone_name(VALUE zone_name)
2950 {
2951 StringValueCStr(zone_name);
2952 return zone_name;
2953 }
2954
2955 static void
2956 validate_vtm(struct vtm *vtm)
2957 {
2958 #define validate_vtm_range(mem, b, e) \
2959 ((vtm->mem < b || vtm->mem > e) ? \
2960 rb_raise(rb_eArgError, #mem" out of range") : (void)0)
2961 validate_vtm_range(mon, 1, 12);
2962 validate_vtm_range(mday, 1, 31);
2963 validate_vtm_range(hour, 0, 24);
2964 validate_vtm_range(min, 0, (vtm->hour == 24 ? 0 : 59));
2965 validate_vtm_range(sec, 0, (vtm->hour == 24 ? 0 : 60));
2966 if (lt(vtm->subsecx, INT2FIX(0)) || ge(vtm->subsecx, INT2FIX(TIME_SCALE)))
2967 rb_raise(rb_eArgError, "subsecx out of range");
2968 if (!NIL_P(vtm->utc_offset)) validate_utc_offset(vtm->utc_offset);
2969 #undef validate_vtm_range
2970 }
2971
2972 static void
2973 time_arg(int argc, const VALUE *argv, struct vtm *vtm)
2974 {
2975 VALUE v[8];
2976 VALUE subsecx = INT2FIX(0);
2977
2978 vtm->year = INT2FIX(0);
2979 vtm->mon = 0;
2980 vtm->mday = 0;
2981 vtm->hour = 0;
2982 vtm->min = 0;
2983 vtm->sec = 0;
2984 vtm->subsecx = INT2FIX(0);
2985 vtm->utc_offset = Qnil;
2986 vtm->wday = 0;
2987 vtm->yday = 0;
2988 vtm->isdst = 0;
2989 vtm->zone = rb_fstring_lit("");
2990
2991 if (argc == 10) {
2992 v[0] = argv[5];
2993 v[1] = argv[4];
2994 v[2] = argv[3];
2995 v[3] = argv[2];
2996 v[4] = argv[1];
2997 v[5] = argv[0];
2998 v[6] = Qnil;
2999 vtm->isdst = RTEST(argv[8]) ? 1 : 0;
3000 }
3001 else {
3002 rb_scan_args(argc, argv, "17", &v[0],&v[1],&v[2],&v[3],&v[4],&v[5],&v[6],&v[7]);
3003 /* v[6] may be usec or zone (parsedate) */
3004 /* v[7] is wday (parsedate; ignored) */
3005 vtm->wday = VTM_WDAY_INITVAL;
3006 vtm->isdst = VTM_ISDST_INITVAL;
3007 }
3008
3009 vtm->year = obj2vint(v[0]);
3010
3011 if (NIL_P(v[1])) {
3012 vtm->mon = 1;
3013 }
3014 else {
3015 vtm->mon = month_arg(v[1]);
3016 }
3017
3018 if (NIL_P(v[2])) {
3019 vtm->mday = 1;
3020 }
3021 else {
3022 vtm->mday = obj2ubits(v[2], 5);
3023 }
3024
3025 /* normalize month-mday */
3026 switch (vtm->mon) {
3027 case 2:
3028 {
3029 /* this drops higher bits but it's not a problem to calc leap year */
3030 unsigned int mday2 = leap_year_v_p(vtm->year) ? 29 : 28;
3031 if (vtm->mday > mday2) {
3032 vtm->mday -= mday2;
3033 vtm->mon++;
3034 }
3035 }
3036 break;
3037 case 4:
3038 case 6:
3039 case 9:
3040 case 11:
3041 if (vtm->mday == 31) {
3042 vtm->mon++;
3043 vtm->mday = 1;
3044 }
3045 break;
3046 }
3047
3048 vtm->hour = NIL_P(v[3])?0:obj2ubits(v[3], 5);
3049
3050 vtm->min = NIL_P(v[4])?0:obj2ubits(v[4], 6);
3051
3052 if (!NIL_P(v[6]) && argc == 7) {
3053 vtm->sec = NIL_P(v[5])?0:obj2ubits(v[5],6);
3054 subsecx = usec2subsecx(v[6]);
3055 }
3056 else {
3057 /* when argc == 8, v[6] is timezone, but ignored */
3058 if (NIL_P(v[5])) {
3059 vtm->sec = 0;
3060 }
3061 else {
3062 vtm->sec = obj2subsecx(v[5], &subsecx);
3063 }
3064 }
3065 vtm->subsecx = subsecx;
3066
3067 validate_vtm(vtm);
3068 RB_GC_GUARD(subsecx);
3069 }
3070
3071 static int
3072 leap_year_p(long y)
3073 {
3074 /* TODO:
3075 * ensure about negative years in proleptic Gregorian calendar.
3076 */
3077 unsigned long uy = (unsigned long)(LIKELY(y >= 0) ? y : -y);
3078
3079 if (LIKELY(uy % 4 != 0)) return 0;
3080
3081 unsigned long century = uy / 100;
3082 if (LIKELY(uy != century * 100)) return 1;
3083 return century % 4 == 0;
3084 }
3085
3086 static time_t
3087 timegm_noleapsecond(struct tm *tm)
3088 {
3089 long tm_year = tm->tm_year;
3090 int tm_yday = calc_tm_yday(tm->tm_year, tm->tm_mon, tm->tm_mday);
3091
3092 /*
3093 * `Seconds Since the Epoch' in SUSv3:
3094 * tm_sec + tm_min*60 + tm_hour*3600 + tm_yday*86400 +
3095 * (tm_year-70)*31536000 + ((tm_year-69)/4)*86400 -
3096 * ((tm_year-1)/100)*86400 + ((tm_year+299)/400)*86400
3097 */
3098 return tm->tm_sec + tm->tm_min*60 + tm->tm_hour*3600 +
3099 (time_t)(tm_yday +
3100 (tm_year-70)*365 +
3101 DIV(tm_year-69,4) -
3102 DIV(tm_year-1,100) +
3103 DIV(tm_year+299,400))*86400;
3104 }
3105
3106 #if 0
3107 #define DEBUG_FIND_TIME_NUMGUESS
3108 #define DEBUG_GUESSRANGE
3109 #endif
3110
3111 #ifdef DEBUG_GUESSRANGE
3112 #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))
3113 #else
3114 #define DEBUG_REPORT_GUESSRANGE
3115 #endif
3116
3117 #ifdef DEBUG_FIND_TIME_NUMGUESS
3118 #define DEBUG_FIND_TIME_NUMGUESS_INC find_time_numguess++,
3119 static unsigned long long find_time_numguess;
3120
3121 static VALUE find_time_numguess_getter(void)
3122 {
3123 return ULL2NUM(find_time_numguess);
3124 }
3125 #else
3126 #define DEBUG_FIND_TIME_NUMGUESS_INC
3127 #endif
3128
3129 static const char *
3130 find_time_t(struct tm *tptr, int utc_p, time_t *tp)
3131 {
3132 time_t guess, guess0, guess_lo, guess_hi;
3133 struct tm *tm, tm0, tm_lo, tm_hi;
3134 int d;
3135 int find_dst;
3136 struct tm result;
3137 int status;
3138 int tptr_tm_yday;
3139
3140 #define GUESS(p) (DEBUG_FIND_TIME_NUMGUESS_INC (utc_p ? gmtime_with_leapsecond((p), &result) : LOCALTIME((p), result)))
3141
3142 guess_lo = TIMET_MIN;
3143 guess_hi = TIMET_MAX;
3144
3145 find_dst = 0 < tptr->tm_isdst;
3146
3147 /* /etc/localtime might be changed. reload it. */
3148 update_tz();
3149
3150 tm0 = *tptr;
3151 if (tm0.tm_mon < 0) {
3152 tm0.tm_mon = 0;
3153 tm0.tm_mday = 1;
3154 tm0.tm_hour = 0;
3155 tm0.tm_min = 0;
3156 tm0.tm_sec = 0;
3157 }
3158 else if (11 < tm0.tm_mon) {
3159 tm0.tm_mon = 11;
3160 tm0.tm_mday = 31;
3161 tm0.tm_hour = 23;
3162 tm0.tm_min = 59;
3163 tm0.tm_sec = 60;
3164 }
3165 else if (tm0.tm_mday < 1) {
3166 tm0.tm_mday = 1;
3167 tm0.tm_hour = 0;
3168 tm0.tm_min = 0;
3169 tm0.tm_sec = 0;
3170 }
3171 else if ((d = (leap_year_p(1900 + tm0.tm_year) ?
3172 leap_year_days_in_month :
3173 common_year_days_in_month)[tm0.tm_mon]) < tm0.tm_mday) {
3174 tm0.tm_mday = d;
3175 tm0.tm_hour = 23;
3176 tm0.tm_min = 59;
3177 tm0.tm_sec = 60;
3178 }
3179 else if (tm0.tm_hour < 0) {
3180 tm0.tm_hour = 0;
3181 tm0.tm_min = 0;
3182 tm0.tm_sec = 0;
3183 }
3184 else if (23 < tm0.tm_hour) {
3185 tm0.tm_hour = 23;
3186 tm0.tm_min = 59;
3187 tm0.tm_sec = 60;
3188 }
3189 else if (tm0.tm_min < 0) {
3190 tm0.tm_min = 0;
3191 tm0.tm_sec = 0;
3192 }
3193 else if (59 < tm0.tm_min) {
3194 tm0.tm_min = 59;
3195 tm0.tm_sec = 60;
3196 }
3197 else if (tm0.tm_sec < 0) {
3198 tm0.tm_sec = 0;
3199 }
3200 else if (60 < tm0.tm_sec) {
3201 tm0.tm_sec = 60;
3202 }
3203
3204 DEBUG_REPORT_GUESSRANGE;
3205 guess0 = guess = timegm_noleapsecond(&tm0);
3206 tm = GUESS(&guess);
3207 if (tm) {
3208 d = tmcmp(tptr, tm);
3209 if (d == 0) { goto found; }
3210 if (d < 0) {
3211 guess_hi = guess;
3212 guess -= 24 * 60 * 60;
3213 }
3214 else {
3215 guess_lo = guess;
3216 guess += 24 * 60 * 60;
3217 }
3218 DEBUG_REPORT_GUESSRANGE;
3219 if (guess_lo < guess && guess < guess_hi && (tm = GUESS(&guess)) != NULL) {
3220 d = tmcmp(tptr, tm);
3221 if (d == 0) { goto found; }
3222 if (d < 0)
3223 guess_hi = guess;
3224 else
3225 guess_lo = guess;
3226 DEBUG_REPORT_GUESSRANGE;
3227 }
3228 }
3229
3230 tm = GUESS(&guess_lo);
3231 if (!tm) goto error;
3232 d = tmcmp(tptr, tm);
3233 if (d < 0) goto out_of_range;
3234 if (d == 0) { guess = guess_lo; goto found; }
3235 tm_lo = *tm;
3236
3237 tm = GUESS(&guess_hi);
3238 if (!tm) goto error;
3239 d = tmcmp(tptr, tm);
3240 if (d > 0) goto out_of_range;
3241 if (d == 0) { guess = guess_hi; goto found; }
3242 tm_hi = *tm;
3243
3244 DEBUG_REPORT_GUESSRANGE;
3245
3246 status = 1;
3247
3248 while (guess_lo + 1 < guess_hi) {
3249 if (status == 0) {
3250 binsearch:
3251 guess = guess_lo / 2 + guess_hi / 2;
3252 if (guess <= guess_lo)
3253 guess = guess_lo + 1;
3254 else if (guess >= guess_hi)
3255 guess = guess_hi - 1;
3256 status = 1;
3257 }
3258 else {
3259 if (status == 1) {
3260 time_t guess0_hi = timegm_noleapsecond(&tm_hi);
3261 guess = guess_hi - (guess0_hi - guess0);
3262 if (guess == guess_hi) /* hh:mm:60 tends to cause this condition. */
3263 guess--;
3264 status = 2;
3265 }
3266 else if (status == 2) {
3267 time_t guess0_lo = timegm_noleapsecond(&tm_lo);
3268 guess = guess_lo + (guess0 - guess0_lo);
3269 if (guess == guess_lo)
3270 guess++;
3271 status = 0;
3272 }
3273 if (guess <= guess_lo || guess_hi <= guess) {
3274 /* Previous guess is invalid. try binary search. */
3275 #ifdef DEBUG_GUESSRANGE
3276 if (guess <= guess_lo) fprintf(stderr, "too small guess: %ld <= %ld\n", guess, guess_lo);
3277 if (guess_hi <= guess) fprintf(stderr, "too big guess: %ld <= %ld\n", guess_hi, guess);
3278 #endif
3279 goto binsearch;
3280 }
3281 }
3282
3283 tm = GUESS(&guess);
3284 if (!tm) goto error;
3285
3286 d = tmcmp(tptr, tm);
3287
3288 if (d < 0) {
3289 guess_hi = guess;
3290 tm_hi = *tm;
3291 DEBUG_REPORT_GUESSRANGE;
3292 }
3293 else if (d > 0) {
3294 guess_lo = guess;
3295 tm_lo = *tm;
3296 DEBUG_REPORT_GUESSRANGE;
3297 }
3298 else {
3299 found:
3300 if (!utc_p) {
3301 /* If localtime is nonmonotonic, another result may exist. */
3302 time_t guess2;
3303 if (find_dst) {
3304 guess2 = guess - 2 * 60 * 60;
3305 tm = LOCALTIME(&guess2, result);
3306 if (tm) {
3307 if (tptr->tm_hour != (tm->tm_hour + 2) % 24 ||
3308 tptr->tm_min != tm->tm_min ||
3309 tptr->tm_sec != tm->tm_sec) {
3310 guess2 -= (tm->tm_hour - tptr->tm_hour) * 60 * 60 +
3311 (tm->tm_min - tptr->tm_min) * 60 +
3312 (tm->tm_sec - tptr->tm_sec);
3313 if (tptr->tm_mday != tm->tm_mday)
3314 guess2 += 24 * 60 * 60;
3315 if (guess != guess2) {
3316 tm = LOCALTIME(&guess2, result);
3317 if (tm && tmcmp(tptr, tm) == 0) {
3318 if (guess < guess2)
3319 *tp = guess;
3320 else
3321 *tp = guess2;
3322 return NULL;
3323 }
3324 }
3325 }
3326 }
3327 }
3328 else {
3329 guess2 = guess + 2 * 60 * 60;
3330 tm = LOCALTIME(&guess2, result);
3331 if (tm) {
3332 if ((tptr->tm_hour + 2) % 24 != tm->tm_hour ||
3333 tptr->tm_min != tm->tm_min ||
3334 tptr->tm_sec != tm->tm_sec) {
3335 guess2 -= (tm->tm_hour - tptr->tm_hour) * 60 * 60 +
3336 (tm->tm_min - tptr->tm_min) * 60 +
3337 (tm->tm_sec - tptr->tm_sec);
3338 if (tptr->tm_mday != tm->tm_mday)
3339 guess2 -= 24 * 60 * 60;
3340 if (guess != guess2) {
3341 tm = LOCALTIME(&guess2, result);
3342 if (tm && tmcmp(tptr, tm) == 0) {
3343 if (guess < guess2)
3344 *tp = guess2;
3345 else
3346 *tp = guess;
3347 return NULL;
3348 }
3349 }
3350 }
3351 }
3352 }
3353 }
3354 *tp = guess;
3355 return NULL;
3356 }
3357 }
3358
3359 /* Given argument has no corresponding time_t. Let's extrapolate. */
3360 /*
3361 * `Seconds Since the Epoch' in SUSv3:
3362 * tm_sec + tm_min*60 + tm_hour*3600 + tm_yday*86400 +
3363 * (tm_year-70)*31536000 + ((tm_year-69)/4)*86400 -
3364 * ((tm_year-1)/100)*86400 + ((tm_year+299)/400)*86400
3365 */
3366
3367 tptr_tm_yday = calc_tm_yday(tptr->tm_year, tptr->tm_mon, tptr->tm_mday);
3368
3369 *tp = guess_lo +
3370 ((tptr->tm_year - tm_lo.tm_year) * 365 +
3371 DIV((tptr->tm_year-69), 4) -
3372 DIV((tptr->tm_year-1), 100) +
3373 DIV((tptr->tm_year+299), 400) -
3374 DIV((tm_lo.tm_year-69), 4) +
3375 DIV((tm_lo.tm_year-1), 100) -
3376 DIV((tm_lo.tm_year+299), 400) +
3377 tptr_tm_yday -
3378 tm_lo.tm_yday) * 86400 +
3379 (tptr->tm_hour - tm_lo.tm_hour) * 3600 +
3380 (tptr->tm_min - tm_lo.tm_min) * 60 +
3381 (tptr->tm_sec - (tm_lo.tm_sec == 60 ? 59 : tm_lo.tm_sec));
3382
3383 return NULL;
3384
3385 out_of_range:
3386 return "time out of range";
3387
3388 error:
3389 return "gmtime/localtime error";
3390 }
3391
3392 static int
3393 vtmcmp(struct vtm *a, struct vtm *b)
3394 {
3395 if (ne(a->year, b->year))
3396 return lt(a->year, b->year) ? -1 : 1;
3397 else if (a->mon != b->mon)
3398 return a->mon < b->mon ? -1 : 1;
3399 else if (a->mday != b->mday)
3400 return a->mday < b->mday ? -1 : 1;
3401 else if (a->hour != b->hour)
3402 return a->hour < b->hour ? -1 : 1;
3403 else if (a->min != b->min)
3404 return a->min < b->min ? -1 : 1;
3405 else if (a->sec != b->sec)
3406 return a->sec < b->sec ? -1 : 1;
3407 else if (ne(a->subsecx, b->subsecx))
3408 return lt(a->subsecx, b->subsecx) ? -1 : 1;
3409 else
3410 return 0;
3411 }
3412
3413 static int
3414 tmcmp(struct tm *a, struct tm *b)
3415 {
3416 if (a->tm_year != b->tm_year)
3417 return a->tm_year < b->tm_year ? -1 : 1;
3418 else if (a->tm_mon != b->tm_mon)
3419 return a->tm_mon < b->tm_mon ? -1 : 1;
3420 else if (a->tm_mday != b->tm_mday)
3421 return a->tm_mday < b->tm_mday ? -1 : 1;
3422 else if (a->tm_hour != b->tm_hour)
3423 return a->tm_hour < b->tm_hour ? -1 : 1;
3424 else if (a->tm_min != b->tm_min)
3425 return a->tm_min < b->tm_min ? -1 : 1;
3426 else if (a->tm_sec != b->tm_sec)
3427 return a->tm_sec < b->tm_sec ? -1 : 1;
3428 else
3429 return 0;
3430 }
3431
3432 /*
3433 * call-seq:
3434 * Time.utc(year) -> time
3435 * Time.utc(year, month) -> time
3436 * Time.utc(year, month, day) -> time
3437 * Time.utc(year, month, day, hour) -> time
3438 * Time.utc(year, month, day, hour, min) -> time
3439 * Time.utc(year, month, day, hour, min, sec_with_frac) -> time
3440 * Time.utc(year, month, day, hour, min, sec, usec_with_frac) -> time
3441 * Time.utc(sec, min, hour, day, month, year, dummy, dummy, dummy, dummy) -> time
3442 * Time.gm(year) -> time
3443 * Time.gm(year, month) -> time
3444 * Time.gm(year, month, day) -> time
3445 * Time.gm(year, month, day, hour) -> time
3446 * Time.gm(year, month, day, hour, min) -> time
3447 * Time.gm(year, month, day, hour, min, sec_with_frac) -> time
3448 * Time.gm(year, month, day, hour, min, sec, usec_with_frac) -> time
3449 * Time.gm(sec, min, hour, day, month, year, dummy, dummy, dummy, dummy) -> time
3450 *
3451 * Creates a Time object based on given values, interpreted as UTC (GMT). The
3452 * year must be specified. Other values default to the minimum value
3453 * for that field (and may be +nil+ or omitted). Months may
3454 * be specified by numbers from 1 to 12, or by the three-letter English
3455 * month names. Hours are specified on a 24-hour clock (0..23). Raises
3456 * an ArgumentError if any values are out of range. Will
3457 * also accept ten arguments in the order output by Time#to_a.
3458 *
3459 * +sec_with_frac+ and +usec_with_frac+ can have a fractional part.
3460 *
3461 * Time.utc(2000,"jan",1,20,15,1) #=> 2000-01-01 20:15:01 UTC
3462 * Time.gm(2000,"jan",1,20,15,1) #=> 2000-01-01 20:15:01 UTC
3463 */
3464 static VALUE
3465 time_s_mkutc(int argc, VALUE *argv, VALUE klass)
3466 {
3467 struct vtm vtm;
3468
3469 time_arg(argc, argv, &vtm);
3470 return time_gmtime(time_new_timew(klass, timegmw(&vtm)));
3471 }
3472
3473 /*
3474 * call-seq:
3475 * Time.local(year) -> time
3476 * Time.local(year, month) -> time
3477 * Time.local(year, month, day) -> time
3478 * Time.local(year, month, day, hour) -> time
3479 * Time.local(year, month, day, hour, min) -> time
3480 * Time.local(year, month, day, hour, min, sec_with_frac) -> time
3481 * Time.local(year, month, day, hour, min, sec, usec_with_frac) -> time
3482 * Time.local(sec, min, hour, day, month, year, dummy, dummy, isdst, dummy) -> time
3483 * Time.mktime(year) -> time
3484 * Time.mktime(year, month) -> time
3485 * Time.mktime(year, month, day) -> time
3486 * Time.mktime(year, month, day, hour) -> time
3487 * Time.mktime(year, month, day, hour, min) -> time
3488 * Time.mktime(year, month, day, hour, min, sec_with_frac) -> time
3489 * Time.mktime(year, month, day, hour, min, sec, usec_with_frac) -> time
3490 * Time.mktime(sec, min, hour, day, month, year, dummy, dummy, isdst, dummy) -> time
3491 *
3492 * Same as Time::gm, but interprets the values in the
3493 * local time zone.
3494 *
3495 * Time.local(2000,"jan",1,20,15,1) #=> 2000-01-01 20:15:01 -0600
3496 */
3497
3498 static VALUE
3499 time_s_mktime(int argc, VALUE *argv, VALUE klass)
3500 {
3501 struct vtm vtm;
3502
3503 time_arg(argc, argv, &vtm);
3504 return time_localtime(time_new_timew(klass, timelocalw(&vtm)));
3505 }
3506
3507 /*
3508 * call-seq:
3509 * time.to_i -> int
3510 * time.tv_sec -> int
3511 *
3512 * Returns the value of _time_ as an integer number of seconds
3513 * since the Epoch.
3514 *
3515 * t = Time.now
3516 * "%10.5f" % t.to_f #=> "1270968656.89607"
3517 * t.to_i #=> 1270968656
3518 */
3519
3520 static VALUE
3521 time_to_i(VALUE time)
3522 {
3523 struct time_object *tobj;
3524
3525 GetTimeval(time, tobj);
3526 return w2v(wdiv(tobj->timew, WINT2FIXWV(TIME_SCALE)));
3527 }
3528
3529 /*
3530 * call-seq:
3531 * time.to_f -> float
3532 *
3533 * Returns the value of _time_ as a floating point number of
3534 * seconds since the Epoch.
3535 *
3536 * t = Time.now
3537 * "%10.5f" % t.to_f #=> "1270968744.77658"
3538 * t.to_i #=> 1270968744
3539 *
3540 * Note that IEEE 754 double is not accurate enough to represent
3541 * the exact number of nanoseconds since the Epoch.
3542 */
3543
3544 static VALUE
3545 time_to_f(VALUE time)
3546 {
3547 struct time_object *tobj;
3548
3549 GetTimeval(time, tobj);
3550 return rb_Float(rb_time_unmagnify_to_float(tobj->timew));
3551 }
3552
3553 /*
3554 * call-seq:
3555 * time.to_r -> a_rational
3556 *
3557 * Returns the value of _time_ as a rational number of seconds
3558 * since the Epoch.
3559 *
3560 * t = Time.now
3561 * t.to_r #=> (1270968792716287611/1000000000)
3562 *
3563 * This methods is intended to be used to get an accurate value
3564 * representing the nanoseconds since the Epoch. You can use this method
3565 * to convert _time_ to another Epoch.
3566 */
3567
3568 static VALUE
3569 time_to_r(VALUE time)
3570 {
3571 struct time_object *tobj;
3572 VALUE v;
3573
3574 GetTimeval(time, tobj);
3575 v = rb_time_unmagnify_to_rational(tobj->timew);
3576 if (!RB_TYPE_P(v, T_RATIONAL)) {
3577 v = rb_Rational1(v);
3578 }
3579 return v;
3580 }
3581
3582 /*
3583 * call-seq:
3584 * time.usec -> int
3585 * time.tv_usec -> int
3586 *
3587 * Returns the number of microseconds for _time_.
3588 *
3589 * t = Time.now #=> 2007-11-19 08:03:26 -0600
3590 * "%10.6f" % t.to_f #=> "1195481006.775195"
3591 * t.usec #=> 775195
3592 */
3593
3594 static VALUE
3595 time_usec(VALUE time)
3596 {
3597 struct time_object *tobj;
3598 wideval_t w, q, r;
3599
3600 GetTimeval(time, tobj);
3601
3602 w = wmod(tobj->timew, WINT2WV(TIME_SCALE));
3603 wmuldivmod(w, WINT2FIXWV(1000000), WINT2FIXWV(TIME_SCALE), &q, &r);
3604 return rb_to_int(w2v(q));
3605 }
3606
3607 /*
3608 * call-seq:
3609 * time.nsec -> int
3610 * time.tv_nsec -> int
3611 *
3612 * Returns the number of nanoseconds for _time_.
3613 *
3614 * t = Time.now #=> 2007-11-17 15:18:03 +0900
3615 * "%10.9f" % t.to_f #=> "1195280283.536151409"
3616 * t.nsec #=> 536151406
3617 *
3618 * The lowest digits of #to_f and #nsec are different because
3619 * IEEE 754 double is not accurate enough to represent
3620 * the exact number of nanoseconds since the Epoch.
3621 *
3622 * The more accurate value is returned by #nsec.
3623 */
3624
3625 static VALUE
3626 time_nsec(VALUE time)
3627 {
3628 struct time_object *tobj;
3629
3630 GetTimeval(time, tobj);
3631 return rb_to_int(w2v(wmulquoll(wmod(tobj->timew, WINT2WV(TIME_SCALE)), 1000000000, TIME_SCALE)));
3632 }
3633
3634 /*
3635 * call-seq:
3636 * time.subsec -> number
3637 *
3638 * Returns the fraction for _time_.
3639 *
3640 * The return value can be a rational number.
3641 *
3642 * t = Time.now #=> 2009-03-26 22:33:12 +0900
3643 * "%10.9f" % t.to_f #=> "1238074392.940563917"
3644 * t.subsec #=> (94056401/100000000)
3645 *
3646 * The lowest digits of #to_f and #subsec are different because
3647 * IEEE 754 double is not accurate enough to represent
3648 * the rational number.
3649 *
3650 * The more accurate value is returned by #subsec.
3651 */
3652
3653 static VALUE
3654 time_subsec(VALUE time)
3655 {
3656 struct time_object *tobj;
3657
3658 GetTimeval(time, tobj);
3659 return quov(w2v(wmod(tobj->timew, WINT2FIXWV(TIME_SCALE))), INT2FIX(TIME_SCALE));
3660 }
3661
3662 /*
3663 * call-seq:
3664 * time <=> other_time -> -1, 0, +1, or nil
3665 *
3666 * Comparison---Compares +time+ with +other_time+.
3667 *
3668 * -1, 0, +1 or nil depending on whether +time+ is less than, equal to, or
3669 * greater than +other_time+.
3670 *
3671 * +nil+ is returned if the two values are incomparable.
3672 *
3673 * t = Time.now #=> 2007-11-19 08:12:12 -0600
3674 * t2 = t + 2592000 #=> 2007-12-19 08:12:12 -0600
3675 * t <=> t2 #=> -1
3676 * t2 <=> t #=> 1
3677 *
3678 * t = Time.now #=> 2007-11-19 08:13:38 -0600
3679 * t2 = t + 0.1 #=> 2007-11-19 08:13:38 -0600
3680 * t.nsec #=> 98222999
3681 * t2.nsec #=> 198222999
3682 * t <=> t2 #=> -1
3683 * t2 <=> t #=> 1
3684 * t <=> t #=> 0
3685 */
3686
3687 static VALUE
3688 time_cmp(VALUE time1, VALUE time2)
3689 {
3690 struct time_object *tobj1, *tobj2;
3691 int n;
3692
3693 GetTimeval(time1, tobj1);
3694 if (IsTimeval(time2)) {
3695 GetTimeval(time2, tobj2);
3696 n = wcmp(tobj1->timew, tobj2->timew);
3697 }
3698 else {
3699 return rb_invcmp(time1, time2);
3700 }
3701 if (n == 0) return INT2FIX(0);
3702 if (n > 0) return INT2FIX(1);
3703 return INT2FIX(-1);
3704 }
3705
3706 /*
3707 * call-seq:
3708 * time.eql?(other_time)
3709 *
3710 * Returns +true+ if _time_ and +other_time+ are
3711 * both Time objects with the same seconds and fractional seconds.
3712 */
3713
3714 static VALUE
3715 time_eql(VALUE time1, VALUE time2)
3716 {
3717 struct time_object *tobj1, *tobj2;
3718
3719 GetTimeval(time1, tobj1);
3720 if (IsTimeval(time2)) {
3721 GetTimeval(time2, tobj2);
3722 return rb_equal(w2v(tobj1->timew), w2v(tobj2->timew));
3723 }
3724 return Qfalse;
3725 }
3726
3727 /*
3728 * call-seq:
3729 * time.utc? -> true or false
3730 * time.gmt? -> true or false
3731 *
3732 * Returns +true+ if _time_ represents a time in UTC (GMT).
3733 *
3734 * t = Time.now #=> 2007-11-19 08:15:23 -0600
3735 * t.utc? #=> false
3736 * t = Time.gm(2000,"jan",1,20,15,1) #=> 2000-01-01 20:15:01 UTC
3737 * t.utc? #=> true
3738 *
3739 * t = Time.now #=> 2007-11-19 08:16:03 -0600
3740 * t.gmt? #=> false
3741 * t = Time.gm(2000,1,1,20,15,1) #=> 2000-01-01 20:15:01 UTC
3742 * t.gmt? #=> true
3743 */
3744
3745 static VALUE
3746 time_utc_p(VALUE time)
3747 {
3748 struct time_object *tobj;
3749
3750 GetTimeval(time, tobj);
3751 if (TZMODE_UTC_P(tobj)) return Qtrue;
3752 return Qfalse;
3753 }
3754
3755 /*
3756 * call-seq:
3757 * time.hash -> integer
3758 *
3759 * Returns a hash code for this Time object.
3760 *
3761 * See also Object#hash.
3762 */
3763
3764 static VALUE
3765 time_hash(VALUE time)
3766 {
3767 struct time_object *tobj;
3768
3769 GetTimeval(time, tobj);
3770 return rb_hash(w2v(tobj->timew));
3771 }
3772
3773 /* :nodoc: */
3774 static VALUE
3775 time_init_copy(VALUE copy, VALUE time)
3776 {
3777 struct time_object *tobj, *tcopy;
3778
3779 if (!OBJ_INIT_COPY(copy, time)) return copy;
3780 GetTimeval(time, tobj);
3781 GetNewTimeval(copy, tcopy);
3782 MEMCPY(tcopy, tobj, struct time_object, 1);
3783
3784 return copy;
3785 }
3786
3787 static VALUE
3788 time_dup(VALUE time)
3789 {
3790 VALUE dup = time_s_alloc(rb_obj_class(time));
3791 time_init_copy(dup, time);
3792 return dup;
3793 }
3794
3795 static VALUE
3796 time_localtime(VALUE time)
3797 {
3798 struct time_object *tobj;
3799 struct vtm vtm;
3800 VALUE zone;
3801
3802 GetTimeval(time, tobj);
3803 if (TZMODE_LOCALTIME_P(tobj)) {
3804 if (tobj->tm_got)
3805 return time;
3806 }
3807 else {
3808 time_modify(time);
3809 }
3810
3811 zone = tobj->vtm.zone;
3812 if (maybe_tzobj_p(zone) && zone_localtime(zone, time)) {
3813 return time;
3814 }
3815
3816 if (!localtimew(tobj->timew, &vtm))
3817 rb_raise(rb_eArgError, "localtime error");
3818 tobj->vtm = vtm;
3819
3820 tobj->tm_got = 1;
3821 TZMODE_SET_LOCALTIME(tobj);
3822 return time;
3823 }
3824
3825 static VALUE
3826 time_zonelocal(VALUE time, VALUE off)
3827 {
3828 VALUE zone = off;
3829 if (zone_localtime(zone, time)) return time;
3830
3831 if (NIL_P(off = utc_offset_arg(off))) {
3832 if (NIL_P(zone = find_timezone(time, zone))) invalid_utc_offset();
3833 if (!zone_localtime(zone, time)) invalid_utc_offset();
3834 return time;
3835 }
3836 else if (off == UTC_ZONE) {
3837 return time_gmtime(time);
3838 }
3839 validate_utc_offset(off);
3840
3841 time_set_utc_offset(time, off);
3842 return time_fixoff(time);
3843 }
3844
3845 /*
3846 * call-seq:
3847 * time.localtime -> time
3848 * time.localtime(utc_offset) -> time
3849 *
3850 * Converts _time_ to local time (using the local time zone in
3851 * effect at the creation time of _time_) modifying the receiver.
3852 *
3853 * If +utc_offset+ is given, it is used instead of the local time.
3854 *
3855 * t = Time.utc(2000, "jan", 1, 20, 15, 1) #=> 2000-01-01 20:15:01 UTC
3856 * t.utc? #=> true
3857 *
3858 * t.localtime #=> 2000-01-01 14:15:01 -0600
3859 * t.utc? #=> false
3860 *
3861 * t.localtime("+09:00") #=> 2000-01-02 05:15:01 +0900
3862 * t.utc? #=> false
3863 *
3864 * If +utc_offset+ is not given and _time_ is local time, just returns
3865 * the receiver.
3866 */
3867
3868 static VALUE
3869 time_localtime_m(int argc, VALUE *argv, VALUE time)
3870 {
3871 VALUE off;
3872
3873 if (rb_check_arity(argc, 0, 1) && !NIL_P(off = argv[0])) {
3874 return time_zonelocal(time, off);
3875 }
3876
3877 return time_localtime(time);
3878 }
3879
3880 /*
3881 * call-seq:
3882 * time.gmtime -> time
3883 * time.utc -> time
3884 *
3885 * Converts _time_ to UTC (GMT), modifying the receiver.
3886 *
3887 * t = Time.now #=> 2007-11-19 08:18:31 -0600
3888 * t.gmt? #=> false
3889 * t.gmtime #=> 2007-11-19 14:18:31 UTC
3890 * t.gmt? #=> true
3891 *
3892 * t = Time.now #=> 2007-11-19 08:18:51 -0600
3893 * t.utc? #=> false
3894 * t.utc #=> 2007-11-19 14:18:51 UTC
3895 * t.utc? #=> true
3896 */
3897
3898 static VALUE
3899 time_gmtime(VALUE time)
3900 {
3901 struct time_object *tobj;
3902 struct vtm vtm;
3903
3904 GetTimeval(time, tobj);
3905 if (TZMODE_UTC_P(tobj)) {
3906 if (tobj->tm_got)
3907 return time;
3908 }
3909 else {
3910 time_modify(time);
3911 }
3912
3913 vtm.zone = rb_fstring_lit("UTC");
3914 GMTIMEW(tobj->timew, &vtm);
3915 tobj->vtm = vtm;
3916
3917 tobj->tm_got = 1;
3918 TZMODE_SET_UTC(tobj);
3919 return time;
3920 }
3921
3922 static VALUE
3923 time_fixoff(VALUE time)
3924 {
3925 struct time_object *tobj;
3926 struct vtm vtm;
3927 VALUE off, zone;
3928
3929 GetTimeval(time, tobj);
3930 if (TZMODE_FIXOFF_P(tobj)) {
3931 if (tobj->tm_got)
3932 return time;
3933 }
3934 else {
3935 time_modify(time);
3936 }
3937
3938 if (TZMODE_FIXOFF_P(tobj))
3939 off = tobj->vtm.utc_offset;
3940 else
3941 off = INT2FIX(0);
3942
3943 GMTIMEW(tobj->timew, &vtm);
3944
3945 zone = tobj->vtm.zone;
3946 tobj->vtm = vtm;
3947 tobj->vtm.zone = zone;
3948 vtm_add_offset(&tobj->vtm, off, +1);
3949
3950 tobj->tm_got = 1;
3951 TZMODE_SET_FIXOFF(tobj, off);
3952 return time;
3953 }
3954
3955 /*
3956 * call-seq:
3957 * time.getlocal -> new_time
3958 * time.getlocal(utc_offset) -> new_time
3959 * time.getlocal(timezone) -> new_time
3960 *
3961 * Returns a new Time object representing _time_ in
3962 * local time (using the local time zone in effect for this process).
3963 *
3964 * If +utc_offset+ is given, it is used instead of the local time.
3965 * +utc_offset+ can be given as a human-readable string (eg. <code>"+09:00"</code>)
3966 * or as a number of seconds (eg. <code>32400</code>).
3967 *
3968 * t = Time.utc(2000,1,1,20,15,1) #=> 2000-01-01 20:15:01 UTC
3969 * t.utc? #=> true
3970 *
3971 * l = t.getlocal #=> 2000-01-01 14:15:01 -0600
3972 * l.utc? #=> false
3973 * t == l #=> true
3974 *
3975 * j = t.getlocal("+09:00") #=> 2000-01-02 05:15:01 +0900
3976 * j.utc? #=> false
3977 * t == j #=> true
3978 *
3979 * k = t.getlocal(9*60*60) #=> 2000-01-02 05:15:01 +0900
3980 * k.utc? #=> false
3981 * t == k #=> true
3982 */
3983
3984 static VALUE
3985 time_getlocaltime(int argc, VALUE *argv, VALUE time)
3986 {
3987 VALUE off;
3988
3989 if (rb_check_arity(argc, 0, 1) && !NIL_P(off = argv[0])) {
3990 VALUE zone = off;
3991 if (maybe_tzobj_p(zone)) {
3992 VALUE t = time_dup(time);
3993 if (zone_localtime(off, t)) return t;
3994 }
3995
3996 if (NIL_P(off = utc_offset_arg(off))) {
3997 if (NIL_P(zone = find_timezone(time, zone))) invalid_utc_offset();
3998 time = time_dup(time);
3999 if (!zone_localtime(zone, time)) invalid_utc_offset();
4000 return time;
4001 }
4002 else if (off == UTC_ZONE) {
4003 return time_gmtime(time_dup(time));
4004 }
4005 validate_utc_offset(off);
4006
4007 time = time_dup(time);
4008 time_set_utc_offset(time, off);
4009 return time_fixoff(time);
4010 }
4011
4012 return time_localtime(time_dup(time));
4013 }
4014
4015 /*
4016 * call-seq:
4017 * time.getgm -> new_time
4018 * time.getutc -> new_time
4019 *
4020 * Returns a new Time object representing _time_ in UTC.
4021 *
4022 * t = Time.local(2000,1,1,20,15,1) #=> 2000-01-01 20:15:01 -0600
4023 * t.gmt? #=> false
4024 * y = t.getgm #=> 2000-01-02 02:15:01 UTC
4025 * y.gmt? #=> true
4026 * t == y #=> true
4027 */
4028
4029 static VALUE
4030 time_getgmtime(VALUE time)
4031 {
4032 return time_gmtime(time_dup(time));
4033 }
4034
4035 static VALUE
4036 time_get_tm(VALUE time, struct time_object *tobj)
4037 {
4038 if (TZMODE_UTC_P(tobj)) return time_gmtime(time);
4039 if (TZMODE_FIXOFF_P(tobj)) return time_fixoff(time);
4040 return time_localtime(time);
4041 }
4042
4043 static VALUE strftime_cstr(const char *fmt, size_t len, VALUE time, rb_encoding *enc);
4044 #define strftimev(fmt, time, enc) strftime_cstr((fmt), rb_strlen_lit(fmt), (time), (enc))
4045
4046 /*
4047 * call-seq:
4048 * time.asctime -> string
4049 * time.ctime -> string
4050 *
4051 * Returns a canonical string representation of _time_.
4052 *
4053 * Time.now.asctime #=> "Wed Apr 9 08:56:03 2003"
4054 * Time.now.ctime #=> "Wed Apr 9 08:56:03 2003"
4055 */
4056
4057 static VALUE
4058 time_asctime(VALUE time)
4059 {
4060 return strftimev("%a %b %e %T %Y", time, rb_usascii_encoding());
4061 }
4062
4063 /*
4064 * call-seq:
4065 * time.to_s -> string
4066 *
4067 * Returns a string representing _time_. Equivalent to calling
4068 * #strftime with the appropriate format string.
4069 *
4070 * t = Time.now
4071 * t.to_s #=> "2012-11-10 18:16:12 +0100"
4072 * t.strftime "%Y-%m-%d %H:%M:%S %z" #=> "2012-11-10 18:16:12 +0100"
4073 *
4074 * t.utc.to_s #=> "2012-11-10 17:16:12 UTC"
4075 * t.strftime "%Y-%m-%d %H:%M:%S UTC" #=> "2012-11-10 17:16:12 UTC"
4076 */
4077
4078 static VALUE
4079 time_to_s(VALUE time)
4080 {
4081 struct time_object *tobj;
4082
4083 GetTimeval(time, tobj);
4084 if (TZMODE_UTC_P(tobj))
4085 return strftimev("%Y-%m-%d %H:%M:%S UTC", time, rb_usascii_encoding());
4086 else
4087 return strftimev("%Y-%m-%d %H:%M:%S %z", time, rb_usascii_encoding());
4088 }
4089
4090 /*
4091 * call-seq:
4092 * time.inspect -> string
4093 *
4094 * Returns a detailed string representing _time_. Unlike to_s,
4095 * preserves nanoseconds in the representation for easier debugging.
4096 *
4097 * t = Time.now
4098 * t.inspect #=> "2012-11-10 18:16:12.261257655 +0100"
4099 * t.strftime "%Y-%m-%d %H:%M:%S.%N %z" #=> "2012-11-10 18:16:12.261257655 +0100"
4100 *
4101 * t.utc.inspect #=> "2012-11-10 17:16:12.261257655 UTC"
4102 * t.strftime "%Y-%m-%d %H:%M:%S.%N UTC" #=> "2012-11-10 17:16:12.261257655 UTC"
4103 */
4104
4105 static VALUE
4106 time_inspect(VALUE time)
4107 {
4108 struct time_object *tobj;
4109 VALUE str, subsec;
4110
4111 GetTimeval(time, tobj);
4112 str = strftimev("%Y-%m-%d %H:%M:%S", time, rb_usascii_encoding());
4113 subsec = w2v(wmod(tobj->timew, WINT2FIXWV(TIME_SCALE)));
4114 if (FIXNUM_P(subsec) && FIX2LONG(subsec) == 0) {
4115 }
4116 else if (FIXNUM_P(subsec) && FIX2LONG(subsec) < TIME_SCALE) {
4117 long len;
4118 str = rb_enc_sprintf(rb_usascii_encoding(), "%"PRIsVALUE".%09ld", str, FIX2LONG(subsec));
4119 for (len=RSTRING_LEN(str); RSTRING_PTR(str)[len-1] == '0' && len > 0; len--)
4120 ;
4121 rb_str_resize(str, len);
4122 }
4123 else {
4124 rb_str_cat_cstr(str, " ");
4125 subsec = quov(subsec, INT2FIX(TIME_SCALE));
4126 rb_str_concat(str, rb_obj_as_string(subsec));
4127 }
4128 if (TZMODE_UTC_P(tobj)) {
4129 rb_str_cat_cstr(str, " UTC");
4130 }
4131 else {
4132 rb_str_concat(str, strftimev(" %z", time, rb_usascii_encoding()));
4133 }
4134 return str;
4135 }
4136
4137 static VALUE
4138 time_add0(VALUE klass, const struct time_object *tobj, VALUE torig, VALUE offset, int sign)
4139 {
4140 VALUE result;
4141 struct time_object *result_tobj;
4142
4143 offset = num_exact(offset);
4144 if (sign < 0)
4145 result = time_new_timew(klass, wsub(tobj->timew, rb_time_magnify(v2w(offset))));
4146 else
4147 result = time_new_timew(klass, wadd(tobj->timew, rb_time_magnify(v2w(offset))));
4148 GetTimeval(result, result_tobj);
4149 TZMODE_COPY(result_tobj, tobj);
4150
4151 return result;
4152 }
4153
4154 static VALUE
4155 time_add(const struct time_object *tobj, VALUE torig, VALUE offset, int sign)
4156 {
4157 return time_add0(rb_cTime, tobj, torig, offset, sign);
4158 }
4159
4160 /*
4161 * call-seq:
4162 * time + numeric -> time
4163 *
4164 * Addition --- Adds some number of seconds (possibly fractional) to
4165 * _time_ and returns that value as a new Time object.
4166 *
4167 * t = Time.now #=> 2007-11-19 08:22:21 -0600
4168 * t + (60 * 60 * 24) #=> 2007-11-20 08:22:21 -0600
4169 */
4170
4171 static VALUE
4172 time_plus(VALUE time1, VALUE time2)
4173 {
4174 struct time_object *tobj;
4175 GetTimeval(time1, tobj);
4176
4177 if (IsTimeval(time2)) {
4178 rb_raise(rb_eTypeError, "time + time?");
4179 }
4180 return time_add(tobj, time1, time2, 1);
4181 }
4182
4183 /*
4184 * call-seq:
4185 * time - other_time -> float
4186 * time - numeric -> time
4187 *
4188 * Difference --- Returns a difference in seconds as a Float
4189 * between _time_ and +other_time+, or subtracts the given number
4190 * of seconds in +numeric+ from _time_.
4191 *
4192 * t = Time.now #=> 2007-11-19 08:23:10 -0600
4193 * t2 = t + 2592000 #=> 2007-12-19 08:23:10 -0600
4194 * t2 - t #=> 2592000.0
4195 * t2 - 2592000 #=> 2007-11-19 08:23:10 -0600
4196 */
4197
4198 static VALUE
4199 time_minus(VALUE time1, VALUE time2)
4200 {
4201 struct time_object *tobj;
4202
4203 GetTimeval(time1, tobj);
4204 if (IsTimeval(time2)) {
4205 struct time_object *tobj2;
4206
4207 GetTimeval(time2, tobj2);
4208 return rb_Float(rb_time_unmagnify_to_float(wsub(tobj->timew, tobj2->timew)));
4209 }
4210 return time_add(tobj, time1, time2, -1);
4211 }
4212
4213 /*
4214 * call-seq:
4215 * time.succ -> new_time
4216 *
4217 * Returns a new Time object, one second later than _time_.
4218 * Time#succ is obsolete since 1.9.2 for time is not a discrete value.
4219 *
4220 * t = Time.now #=> 2007-11-19 08:23:57 -0600
4221 * t.succ #=> 2007-11-19 08:23:58 -0600
4222 *
4223 * Use instead <code>time + 1</code>
4224 *
4225 * t + 1 #=> 2007-11-19 08:23:58 -0600
4226 */
4227
4228 VALUE
4229 rb_time_succ(VALUE time)
4230 {
4231 struct time_object *tobj;
4232 struct time_object *tobj2;
4233
4234 rb_warn("Time#succ is obsolete; use time + 1");
4235 GetTimeval(time, tobj);
4236 time = time_new_timew(rb_cTime, wadd(tobj->timew, WINT2FIXWV(TIME_SCALE)));
4237 GetTimeval(time, tobj2);
4238 TZMODE_COPY(tobj2, tobj);
4239 if (TZMODE_LOCALTIME_P(tobj2) && maybe_tzobj_p(tobj2->vtm.zone)) {
4240 zone_localtime(tobj2->vtm.zone, time);
4241 }
4242 return time;
4243 }
4244
4245 #define time_succ rb_time_succ
4246
4247 static VALUE
4248 ndigits_denominator(VALUE ndigits)
4249 {
4250 long nd = NUM2LONG(ndigits);
4251
4252 if (nd < 0) {
4253 rb_raise(rb_eArgError, "negative ndigits given");
4254 }
4255 if (nd == 0) {
4256 return INT2FIX(1);
4257 }
4258 return rb_rational_new(INT2FIX(1),
4259 rb_int_positive_pow(10, (unsigned long)nd));
4260 }
4261
4262 /*
4263 * call-seq:
4264 * time.round([ndigits]) -> new_time
4265 *
4266 * Rounds sub seconds to a given precision in decimal digits (0 digits by default).
4267 * It returns a new Time object.
4268 * +ndigits+ should be zero or a positive integer.
4269 *
4270 * require 'time'
4271 *
4272 * t = Time.utc(2010,3,30, 5,43,25.123456789r)
4273 * t.iso8601(10) #=> "2010-03-30T05:43:25.1234567890Z"
4274 * t.round.iso8601(10) #=> "2010-03-30T05:43:25.0000000000Z"
4275 * t.round(0).iso8601(10) #=> "2010-03-30T05:43:25.0000000000Z"
4276 * t.round(1).iso8601(10) #=> "2010-03-30T05:43:25.1000000000Z"
4277 * t.round(2).iso8601(10) #=> "2010-03-30T05:43:25.1200000000Z"
4278 * t.round(3).iso8601(10) #=> "2010-03-30T05:43:25.1230000000Z"
4279 * t.round(4).iso8601(10) #=> "2010-03-30T05:43:25.1235000000Z"
4280 *
4281 * t = Time.utc(1999,12,31, 23,59,59)
4282 * (t + 0.4).round.iso8601(3) #=> "1999-12-31T23:59:59.000Z"
4283 * (t + 0.49).round.iso8601(3) #=> "1999-12-31T23:59:59.000Z"
4284 * (t + 0.5).round.iso8601(3) #=> "2000-01-01T00:00:00.000Z"
4285 * (t + 1.4).round.iso8601(3) #=> "2000-01-01T00:00:00.000Z"
4286 * (t + 1.49).round.iso8601(3) #=> "2000-01-01T00:00:00.000Z"
4287 * (t + 1.5).round.iso8601(3) #=> "2000-01-01T00:00:01.000Z"
4288 *
4289 * t = Time.utc(1999,12,31, 23,59,59)
4290 * (t + 0.123456789).round(4).iso8601(6) #=> "1999-12-31T23:59:59.123500Z"
4291 */
4292
4293 static VALUE
4294 time_round(int argc, VALUE *argv, VALUE time)
4295 {
4296 VALUE ndigits, v, den;
4297 struct time_object *tobj;
4298
4299 if (!rb_check_arity(argc, 0, 1) || NIL_P(ndigits = argv[0]))
4300 den = INT2FIX(1);
4301 else
4302 den = ndigits_denominator(ndigits);
4303
4304 GetTimeval(time, tobj);
4305 v = w2v(rb_time_unmagnify(tobj->timew));
4306
4307 v = modv(v, den);
4308 if (lt(v, quov(den, INT2FIX(2))))
4309 return time_add(tobj, time, v, -1);
4310 else
4311 return time_add(tobj, time, subv(den, v), 1);
4312 }
4313
4314 /*
4315 * call-seq:
4316 * time.floor([ndigits]) -> new_time
4317 *
4318 * Floors sub seconds to a given precision in decimal digits (0 digits by default).
4319 * It returns a new Time object.
4320 * +ndigits+ should be zero or a positive integer.
4321 *
4322 * require 'time'
4323 *
4324 * t = Time.utc(2010,3,30, 5,43,25.123456789r)
4325 * t.iso8601(10) #=> "2010-03-30T05:43:25.1234567890Z"
4326 * t.floor.iso8601(10) #=> "2010-03-30T05:43:25.0000000000Z"
4327 * t.floor(0).iso8601(10) #=> "2010-03-30T05:43:25.0000000000Z"
4328 * t.floor(1).iso8601(10) #=> "2010-03-30T05:43:25.1000000000Z"
4329 * t.floor(2).iso8601(10) #=> "2010-03-30T05:43:25.1200000000Z"
4330 * t.floor(3).iso8601(10) #=> "2010-03-30T05:43:25.1230000000Z"
4331 * t.floor(4).iso8601(10) #=> "2010-03-30T05:43:25.1234000000Z"
4332 *
4333 * t = Time.utc(1999,12,31, 23,59,59)
4334 * (t + 0.4).floor.iso8601(3) #=> "1999-12-31T23:59:59.000Z"
4335 * (t + 0.9).floor.iso8601(3) #=> "1999-12-31T23:59:59.000Z"
4336 * (t + 1.4).floor.iso8601(3) #=> "2000-01-01T00:00:00.000Z"
4337 * (t + 1.9).floor.iso8601(3) #=> "2000-01-01T00:00:00.000Z"
4338 *
4339 * t = Time.utc(1999,12,31, 23,59,59)
4340 * (t + 0.123456789).floor(4).iso8601(6) #=> "1999-12-31T23:59:59.123400Z"
4341 */
4342
4343 static VALUE
4344 time_floor(int argc, VALUE *argv, VALUE time)
4345 {
4346 VALUE ndigits, v, den;
4347 struct time_object *tobj;
4348
4349 if (!rb_check_arity(argc, 0, 1) || NIL_P(ndigits = argv[0]))
4350 den = INT2FIX(1);
4351 else
4352 den = ndigits_denominator(ndigits);
4353
4354 GetTimeval(time, tobj);
4355 v = w2v(rb_time_unmagnify(tobj->timew));
4356
4357 v = modv(v, den);
4358 return time_add(tobj, time, v, -1);
4359 }
4360
4361 /*
4362 * call-seq:
4363 * time.ceil([ndigits]) -> new_time
4364 *
4365 * Ceils sub seconds to a given precision in decimal digits (0 digits by default).
4366 * It returns a new Time object.
4367 * +ndigits+ should be zero or a positive integer.
4368 *
4369 * require 'time'
4370 *
4371 * t = Time.utc(2010,3,30, 5,43,25.0123456789r)
4372 * t.iso8601(10) #=> "2010-03-30T05:43:25.0123456789Z"
4373 * t.ceil.iso8601(10) #=> "2010-03-30T05:43:26.0000000000Z"
4374 * t.ceil(0).iso8601(10) #=> "2010-03-30T05:43:26.0000000000Z"
4375 * t.ceil(1).iso8601(10) #=> "2010-03-30T05:43:25.1000000000Z"
4376 * t.ceil(2).iso8601(10) #=> "2010-03-30T05:43:25.0200000000Z"
4377 * t.ceil(3).iso8601(10) #=> "2010-03-30T05:43:25.0130000000Z"
4378 * t.ceil(4).iso8601(10) #=> "2010-03-30T05:43:25.0124000000Z"
4379 *
4380 * t = Time.utc(1999,12,31, 23,59,59)
4381 * (t + 0.4).ceil.iso8601(3) #=> "2000-01-01T00:00:00.000Z"
4382 * (t + 0.9).ceil.iso8601(3) #=> "2000-01-01T00:00:00.000Z"
4383 * (t + 1.4).ceil.iso8601(3) #=> "2000-01-01T00:00:01.000Z"
4384 * (t + 1.9).ceil.iso8601(3) #=> "2000-01-01T00:00:01.000Z"
4385 *
4386 * t = Time.utc(1999,12,31, 23,59,59)
4387 * (t + 0.123456789).ceil(4).iso8601(6) #=> "1999-12-31T23:59:59.123500Z"
4388 */
4389
4390 static VALUE
4391 time_ceil(int argc, VALUE *argv, VALUE time)
4392 {
4393 VALUE ndigits, v, den;
4394 struct time_object *tobj;
4395
4396 if (!rb_check_arity(argc, 0, 1) || NIL_P(ndigits = argv[0]))
4397 den = INT2FIX(1);
4398 else
4399 den = ndigits_denominator(ndigits);
4400
4401 GetTimeval(time, tobj);
4402 v = w2v(rb_time_unmagnify(tobj->timew));
4403
4404 v = modv(v, den);
4405 return time_add(tobj, time, subv(den, v), 1);
4406 }
4407
4408 /*
4409 * call-seq:
4410 * time.sec -> integer
4411 *
4412 * Returns the second of the minute (0..60) for _time_.
4413 *
4414 * *Note:* Seconds range from zero to 60 to allow the system to inject
4415 * leap seconds. See http://en.wikipedia.org/wiki/Leap_second for further
4416 * details.
4417 *
4418 * t = Time.now #=> 2007-11-19 08:25:02 -0600
4419 * t.sec #=> 2
4420 */
4421
4422 static VALUE
4423 time_sec(VALUE time)
4424 {
4425 struct time_object *tobj;
4426
4427 GetTimeval(time, tobj);
4428 MAKE_TM(time, tobj);
4429 return INT2FIX(tobj->vtm.sec);
4430 }
4431
4432 /*
4433 * call-seq:
4434 * time.min -> integer
4435 *
4436 * Returns the minute of the hour (0..59) for _time_.
4437 *
4438 * t = Time.now #=> 2007-11-19 08:25:51 -0600
4439 * t.min #=> 25
4440 */
4441
4442 static VALUE
4443 time_min(VALUE time)
4444 {
4445 struct time_object *tobj;
4446
4447 GetTimeval(time, tobj);
4448 MAKE_TM(time, tobj);
4449 return INT2FIX(tobj->vtm.min);
4450 }
4451
4452 /*
4453 * call-seq:
4454 * time.hour -> integer
4455 *
4456 * Returns the hour of the day (0..23) for _time_.
4457 *
4458 * t = Time.now #=> 2007-11-19 08:26:20 -0600
4459 * t.hour #=> 8
4460 */
4461
4462 static VALUE
4463 time_hour(VALUE time)
4464 {
4465 struct time_object *tobj;
4466
4467 GetTimeval(time, tobj);
4468 MAKE_TM(time, tobj);
4469 return INT2FIX(tobj->vtm.hour);
4470 }
4471
4472 /*
4473 * call-seq:
4474 * time.day -> integer
4475 * time.mday -> integer
4476 *
4477 * Returns the day of the month (1..n) for _time_.
4478 *
4479 * t = Time.now #=> 2007-11-19 08:27:03 -0600
4480 * t.day #=> 19
4481 * t.mday #=> 19
4482 */
4483
4484 static VALUE
4485 time_mday(VALUE time)
4486 {
4487 struct time_object *tobj;
4488
4489 GetTimeval(time, tobj);
4490 MAKE_TM(time, tobj);
4491 return INT2FIX(tobj->vtm.mday);
4492 }
4493
4494 /*
4495 * call-seq:
4496 * time.mon -> integer
4497 * time.month -> integer
4498 *
4499 * Returns the month of the year (1..12) for _time_.
4500 *
4501 * t = Time.now #=> 2007-11-19 08:27:30 -0600
4502 * t.mon #=> 11
4503 * t.month #=> 11
4504 */
4505
4506 static VALUE
4507 time_mon(VALUE time)
4508 {
4509 struct time_object *tobj;
4510
4511 GetTimeval(time, tobj);
4512 MAKE_TM(time, tobj);
4513 return INT2FIX(tobj->vtm.mon);
4514 }
4515
4516 /*
4517 * call-seq:
4518 * time.year -> integer
4519 *
4520 * Returns the year for _time_ (including the century).
4521 *
4522 * t = Time.now #=> 2007-11-19 08:27:51 -0600
4523 * t.year #=> 2007
4524 */
4525
4526 static VALUE
4527 time_year(VALUE time)
4528 {
4529 struct time_object *tobj;
4530
4531 GetTimeval(time, tobj);
4532 MAKE_TM(time, tobj);
4533 return tobj->vtm.year;
4534 }
4535
4536 /*
4537 * call-seq:
4538 * time.wday -> integer
4539 *
4540 * Returns an integer representing the day of the week, 0..6, with
4541 * Sunday == 0.
4542 *
4543 * t = Time.now #=> 2007-11-20 02:35:35 -0600
4544 * t.wday #=> 2
4545 * t.sunday? #=> false
4546 * t.monday? #=> false
4547 * t.tuesday? #=> true
4548 * t.wednesday? #=> false
4549 * t.thursday? #=> false
4550 * t.friday? #=> false
4551 * t.saturday? #=> false
4552 */
4553
4554 static VALUE
4555 time_wday(VALUE time)
4556 {
4557 struct time_object *tobj;
4558
4559 GetTimeval(time, tobj);
4560 MAKE_TM(time, tobj);
4561 return INT2FIX((int)tobj->vtm.wday);
4562 }
4563
4564 #define wday_p(n) {\
4565 struct time_object *tobj;\
4566 GetTimeval(time, tobj);\
4567 MAKE_TM(time, tobj);\
4568 return (tobj->vtm.wday == (n)) ? Qtrue : Qfalse;\
4569 }
4570
4571 /*
4572 * call-seq:
4573 * time.sunday? -> true or false
4574 *
4575 * Returns +true+ if _time_ represents Sunday.
4576 *
4577 * t = Time.local(1990, 4, 1) #=> 1990-04-01 00:00:00 -0600
4578 * t.sunday? #=> true
4579 */
4580
4581 static VALUE
4582 time_sunday(VALUE time)
4583 {
4584 wday_p(0);
4585 }
4586
4587 /*
4588 * call-seq:
4589 * time.monday? -> true or false
4590 *
4591 * Returns +true+ if _time_ represents Monday.
4592 *
4593 * t = Time.local(2003, 8, 4) #=> 2003-08-04 00:00:00 -0500
4594 * t.monday? #=> true
4595 */
4596
4597 static VALUE
4598 time_monday(VALUE time)
4599 {
4600 wday_p(1);
4601 }
4602
4603 /*
4604 * call-seq:
4605 * time.tuesday? -> true or false
4606 *
4607 * Returns +true+ if _time_ represents Tuesday.
4608 *
4609 * t = Time.local(1991, 2, 19) #=> 1991-02-19 00:00:00 -0600
4610 * t.tuesday? #=> true
4611 */
4612
4613 static VALUE
4614 time_tuesday(VALUE time)
4615 {
4616 wday_p(2);
4617 }
4618
4619 /*
4620 * call-seq:
4621 * time.wednesday? -> true or false
4622 *
4623 * Returns +true+ if _time_ represents Wednesday.
4624 *
4625 * t = Time.local(1993, 2, 24) #=> 1993-02-24 00:00:00 -0600
4626 * t.wednesday? #=> true
4627 */
4628
4629 static VALUE
4630 time_wednesday(VALUE time)
4631 {
4632 wday_p(3);
4633 }
4634
4635 /*
4636 * call-seq:
4637 * time.thursday? -> true or false
4638 *
4639 * Returns +true+ if _time_ represents Thursday.
4640 *
4641 * t = Time.local(1995, 12, 21) #=> 1995-12-21 00:00:00 -0600
4642 * t.thursday? #=> true
4643 */
4644
4645 static VALUE
4646 time_thursday(VALUE time)
4647 {
4648 wday_p(4);
4649 }
4650
4651 /*
4652 * call-seq:
4653 * time.friday? -> true or false
4654 *
4655 * Returns +true+ if _time_ represents Friday.
4656 *
4657 * t = Time.local(1987, 12, 18) #=> 1987-12-18 00:00:00 -0600
4658 * t.friday? #=> true
4659 */
4660
4661 static VALUE
4662 time_friday(VALUE time)
4663 {
4664 wday_p(5);
4665 }
4666
4667 /*
4668 * call-seq:
4669 * time.saturday? -> true or false
4670 *
4671 * Returns +true+ if _time_ represents Saturday.
4672 *
4673 * t = Time.local(2006, 6, 10) #=> 2006-06-10 00:00:00 -0500
4674 * t.saturday? #=> true
4675 */
4676
4677 static VALUE
4678 time_saturday(VALUE time)
4679 {
4680 wday_p(6);
4681 }
4682
4683 /*
4684 * call-seq:
4685 * time.yday -> integer
4686 *
4687 * Returns an integer representing the day of the year, 1..366.
4688 *
4689 * t = Time.now #=> 2007-11-19 08:32:31 -0600
4690 * t.yday #=> 323
4691 */
4692
4693 static VALUE
4694 time_yday(VALUE time)
4695 {
4696 struct time_object *tobj;
4697
4698 GetTimeval(time, tobj);
4699 MAKE_TM(time, tobj);
4700 return INT2FIX(tobj->vtm.yday);
4701 }
4702
4703 /*
4704 * call-seq:
4705 * time.isdst -> true or false
4706 * time.dst? -> true or false
4707 *
4708 * Returns +true+ if _time_ occurs during Daylight
4709 * Saving Time in its time zone.
4710 *
4711 * # CST6CDT:
4712 * Time.local(2000, 1, 1).zone #=> "CST"
4713 * Time.local(2000, 1, 1).isdst #=> false
4714 * Time.local(2000, 1, 1).dst? #=> false
4715 * Time.local(2000, 7, 1).zone #=> "CDT"
4716 * Time.local(2000, 7, 1).isdst #=> true
4717 * Time.local(2000, 7, 1).dst? #=> true
4718 *
4719 * # Asia/Tokyo:
4720 * Time.local(2000, 1, 1).zone #=> "JST"
4721 * Time.local(2000, 1, 1).isdst #=> false
4722 * Time.local(2000, 1, 1).dst? #=> false
4723 * Time.local(2000, 7, 1).zone #=> "JST"
4724 * Time.local(2000, 7, 1).isdst #=> false
4725 * Time.local(2000, 7, 1).dst? #=> false
4726 */
4727
4728 static VALUE
4729 time_isdst(VALUE time)
4730 {
4731 struct time_object *tobj;
4732
4733 GetTimeval(time, tobj);
4734 MAKE_TM(time, tobj);
4735 if (tobj->vtm.isdst == VTM_ISDST_INITVAL) {
4736 rb_raise(rb_eRuntimeError, "isdst is not set yet");
4737 }
4738 return tobj->vtm.isdst ? Qtrue : Qfalse;
4739 }
4740
4741 /*
4742 * call-seq:
4743 * time.zone -> string or timezone
4744 *
4745 * Returns the name of the time zone used for _time_. As of Ruby
4746 * 1.8, returns ``UTC'' rather than ``GMT'' for UTC times.
4747 *
4748 * t = Time.gm(2000, "jan", 1, 20, 15, 1)
4749 * t.zone #=> "UTC"
4750 * t = Time.local(2000, "jan", 1, 20, 15, 1)
4751 * t.zone #=> "CST"
4752 */
4753
4754 static VALUE
4755 time_zone(VALUE time)
4756 {
4757 struct time_object *tobj;
4758 VALUE zone;
4759
4760 GetTimeval(time, tobj);
4761 MAKE_TM(time, tobj);
4762
4763 if (TZMODE_UTC_P(tobj)) {
4764 return rb_usascii_str_new_cstr("UTC");
4765 }
4766 zone = tobj->vtm.zone;
4767 if (NIL_P(zone))
4768 return Qnil;
4769
4770 if (RB_TYPE_P(zone, T_STRING))
4771 zone = rb_str_dup(zone);
4772 return zone;
4773 }
4774
4775 /*
4776 * call-seq:
4777 * time.gmt_offset -> integer
4778 * time.gmtoff -> integer
4779 * time.utc_offset -> integer
4780 *
4781 * Returns the offset in seconds between the timezone of _time_
4782 * and UTC.
4783 *
4784 * t = Time.gm(2000,1,1,20,15,1) #=> 2000-01-01 20:15:01 UTC
4785 * t.gmt_offset #=> 0
4786 * l = t.getlocal #=> 2000-01-01 14:15:01 -0600
4787 * l.gmt_offset #=> -21600
4788 */
4789
4790 VALUE
4791 rb_time_utc_offset(VALUE time)
4792 {
4793 struct time_object *tobj;
4794
4795 GetTimeval(time, tobj);
4796
4797 if (TZMODE_UTC_P(tobj)) {
4798 return INT2FIX(0);
4799 }
4800 else {
4801 MAKE_TM(time, tobj);
4802 return tobj->vtm.utc_offset;
4803 }
4804 }
4805
4806 /*
4807 * call-seq:
4808 * time.to_a -> array
4809 *
4810 * Returns a ten-element _array_ of values for _time_:
4811 *
4812 * [sec, min, hour, day, month, year, wday, yday, isdst, zone]
4813 *
4814 * See the individual methods for an explanation of the
4815 * valid ranges of each value. The ten elements can be passed directly
4816 * to Time::utc or Time::local to create a
4817 * new Time object.
4818 *
4819 * t = Time.now #=> 2007-11-19 08:36:01 -0600
4820 * now = t.to_a #=> [1, 36, 8, 19, 11, 2007, 1, 323, false, "CST"]
4821 */
4822
4823 static VALUE
4824 time_to_a(VALUE time)
4825 {
4826 struct time_object *tobj;
4827
4828 GetTimeval(time, tobj);
4829 MAKE_TM(time, tobj);
4830 return rb_ary_new3(10,
4831 INT2FIX(tobj->vtm.sec),
4832 INT2FIX(tobj->vtm.min),
4833 INT2FIX(tobj->vtm.hour),
4834 INT2FIX(tobj->vtm.mday),
4835 INT2FIX(tobj->vtm.mon),
4836 tobj->vtm.year,
4837 INT2FIX(tobj->vtm.wday),
4838 INT2FIX(tobj->vtm.yday),
4839 tobj->vtm.isdst?Qtrue:Qfalse,
4840 time_zone(time));
4841 }
4842
4843 static VALUE
4844 rb_strftime_alloc(const char *format, size_t format_len, rb_encoding *enc,
4845 VALUE time, struct vtm *vtm, wideval_t timew, int gmt)
4846 {
4847 VALUE timev = Qnil;
4848 struct timespec ts;
4849
4850 if (!timew2timespec_exact(timew, &ts))
4851 timev = w2v(rb_time_unmagnify(timew));
4852
4853 if (NIL_P(timev)) {
4854 return rb_strftime_timespec(format, format_len, enc, time, vtm, &ts, gmt);
4855 }
4856 else {
4857 return rb_strftime(format, format_len, enc, time, vtm, timev, gmt);
4858 }
4859 }
4860
4861 static VALUE
4862 strftime_cstr(const char *fmt, size_t len, VALUE time, rb_encoding *enc)
4863 {
4864 struct time_object *tobj;
4865 VALUE str;
4866
4867 GetTimeval(time, tobj);
4868 MAKE_TM(time, tobj);
4869 str = rb_strftime_alloc(fmt, len, enc, time, &tobj->vtm, tobj->timew, TZMODE_UTC_P(tobj));
4870 if (!str) rb_raise(rb_eArgError, "invalid format: %s", fmt);
4871 return str;
4872 }
4873
4874 /*
4875 * call-seq:
4876 * time.strftime( string ) -> string
4877 *
4878 * Formats _time_ according to the directives in the given format string.
4879 *
4880 * The directives begin with a percent (%) character.
4881 * Any text not listed as a directive will be passed through to the
4882 * output string.
4883 *
4884 * The directive consists of a percent (%) character,
4885 * zero or more flags, optional minimum field width,
4886 * optional modifier and a conversion specifier
4887 * as follows:
4888 *
4889 * %<flags><width><modifier><conversion>
4890 *
4891 * Flags:
4892 * - don't pad a numerical output
4893 * _ use spaces for padding
4894 * 0 use zeros for padding
4895 * ^ upcase the result string
4896 * # change case
4897 * : use colons for %z
4898 *
4899 * The minimum field width specifies the minimum width.
4900 *
4901 * The modifiers are "E" and "O".
4902 * They are ignored.
4903 *
4904 * Format directives:
4905 *
4906 * Date (Year, Month, Day):
4907 * %Y - Year with century if provided, will pad result at least 4 digits.
4908 * -0001, 0000, 1995, 2009, 14292, etc.
4909 * %C - year / 100 (rounded down such as 20 in 2009)
4910 * %y - year % 100 (00..99)
4911 *
4912 * %m - Month of the year, zero-padded (01..12)
4913 * %_m blank-padded ( 1..12)
4914 * %-m no-padded (1..12)
4915 * %B - The full month name (``January'')
4916 * %^B uppercased (``JANUARY'')
4917 * %b - The abbreviated month name (``Jan'')
4918 * %^b uppercased (``JAN'')
4919 * %h - Equivalent to %b
4920 *
4921 * %d - Day of the month, zero-padded (01..31)
4922 * %-d no-padded (1..31)
4923 * %e - Day of the month, blank-padded ( 1..31)
4924 *
4925 * %j - Day of the year (001..366)
4926 *
4927 * Time (Hour, Minute, Second, Subsecond):
4928 * %H - Hour of the day, 24-hour clock, zero-padded (00..23)
4929 * %k - Hour of the day, 24-hour clock, blank-padded ( 0..23)
4930 * %I - Hour of the day, 12-hour clock, zero-padded (01..12)
4931 * %l - Hour of the day, 12-hour clock, blank-padded ( 1..12)
4932 * %P - Meridian indicator, lowercase (``am'' or ``pm'')
4933 * %p - Meridian indicator, uppercase (``AM'' or ``PM'')
4934 *
4935 * %M - Minute of the hour (00..59)
4936 *
4937 * %S - Second of the minute (00..60)
4938 *
4939 * %L - Millisecond of the second (000..999)
4940 * The digits under millisecond are truncated to not produce 1000.
4941 * %N - Fractional seconds digits, default is 9 digits (nanosecond)
4942 * %3N millisecond (3 digits)
4943 * %6N microsecond (6 digits)
4944 * %9N nanosecond (9 digits)
4945 * %12N picosecond (12 digits)
4946 * %15N femtosecond (15 digits)
4947 * %18N attosecond (18 digits)
4948 * %21N zeptosecond (21 digits)
4949 * %24N yoctosecond (24 digits)
4950 * The digits under the specified length are truncated to avoid
4951 * carry up.
4952 *
4953 * Time zone:
4954 * %z - Time zone as hour and minute offset from UTC (e.g. +0900)
4955 * %:z - hour and minute offset from UTC with a colon (e.g. +09:00)
4956 * %::z - hour, minute and second offset from UTC (e.g. +09:00:00)
4957 * %Z - Abbreviated time zone name or similar information. (OS dependent)
4958 *
4959 * Weekday:
4960 * %A - The full weekday name (``Sunday'')
4961 * %^A uppercased (``SUNDAY'')
4962 * %a - The abbreviated name (``Sun'')
4963 * %^a uppercased (``SUN'')
4964 * %u - Day of the week (Monday is 1, 1..7)
4965 * %w - Day of the week (Sunday is 0, 0..6)
4966 *
4967 * ISO 8601 week-based year and week number:
4968 * The first week of YYYY starts with a Monday and includes YYYY-01-04.
4969 * The days in the year before the first week are in the last week of
4970 * the previous year.
4971 * %G - The week-based year
4972 * %g - The last 2 digits of the week-based year (00..99)
4973 * %V - Week number of the week-based year (01..53)
4974 *
4975 * Week number:
4976 * The first week of YYYY that starts with a Sunday or Monday (according to %U
4977 * or %W). The days in the year before the first week are in week 0.
4978 * %U - Week number of the year. The week starts with Sunday. (00..53)
4979 * %W - Week number of the year. The week starts with Monday. (00..53)
4980 *
4981 * Seconds since the Epoch:
4982 * %s - Number of seconds since 1970-01-01 00:00:00 UTC.
4983 *
4984 * Literal string:
4985 * %n - Newline character (\n)
4986 * %t - Tab character (\t)
4987 * %% - Literal ``%'' character
4988 *
4989 * Combination:
4990 * %c - date and time (%a %b %e %T %Y)
4991 * %D - Date (%m/%d/%y)
4992 * %F - The ISO 8601 date format (%Y-%m-%d)
4993 * %v - VMS date (%e-%^b-%4Y)
4994 * %x - Same as %D
4995 * %X - Same as %T
4996 * %r - 12-hour time (%I:%M:%S %p)
4997 * %R - 24-hour time (%H:%M)
4998 * %T - 24-hour time (%H:%M:%S)
4999 *
5000 * This method is similar to strftime() function defined in ISO C and POSIX.
5001 *
5002 * While all directives are locale independent since Ruby 1.9, %Z is platform
5003 * dependent.
5004 * So, the result may differ even if the same format string is used in other
5005 * systems such as C.
5006 *
5007 * %z is recommended over %Z.
5008 * %Z doesn't identify the timezone.
5009 * For example, "CST" is used at America/Chicago (-06:00),
5010 * America/Havana (-05:00), Asia/Harbin (+08:00), Australia/Darwin (+09:30)
5011 * and Australia/Adelaide (+10:30).
5012 * Also, %Z is highly dependent on the operating system.
5013 * For example, it may generate a non ASCII string on Japanese Windows,
5014 * i.e. the result can be different to "JST".
5015 * So the numeric time zone offset, %z, is recommended.
5016 *
5017 * Examples:
5018 *
5019 * t = Time.new(2007,11,19,8,37,48,"-06:00") #=> 2007-11-19 08:37:48 -0600
5020 * t.strftime("Printed on %m/%d/%Y") #=> "Printed on 11/19/2007"
5021 * t.strftime("at %I:%M %p") #=> "at 08:37 AM"
5022 *
5023 * Various ISO 8601 formats:
5024 * %Y%m%d => 20071119 Calendar date (basic)
5025 * %F => 2007-11-19 Calendar date (extended)
5026 * %Y-%m => 2007-11 Calendar date, reduced accuracy, specific month
5027 * %Y => 2007 Calendar date, reduced accuracy, specific year
5028 * %C => 20 Calendar date, reduced accuracy, specific century
5029 * %Y%j => 2007323 Ordinal date (basic)
5030 * %Y-%j => 2007-323 Ordinal date (extended)
5031 * %GW%V%u => 2007W471 Week date (basic)
5032 * %G-W%V-%u => 2007-W47-1 Week date (extended)
5033 * %GW%V => 2007W47 Week date, reduced accuracy, specific week (basic)
5034 * %G-W%V => 2007-W47 Week date, reduced accuracy, specific week (extended)
5035 * %H%M%S => 083748 Local time (basic)
5036 * %T => 08:37:48 Local time (extended)
5037 * %H%M => 0837 Local time, reduced accuracy, specific minute (basic)
5038 * %H:%M => 08:37 Local time, reduced accuracy, specific minute (extended)
5039 * %H => 08 Local time, reduced accuracy, specific hour
5040 * %H%M%S,%L => 083748,000 Local time with decimal fraction, comma as decimal sign (basic)
5041 * %T,%L => 08:37:48,000 Local time with decimal fraction, comma as decimal sign (extended)
5042 * %H%M%S.%L => 083748.000 Local time with decimal fraction, full stop as decimal sign (basic)
5043 * %T.%L => 08:37:48.000 Local time with decimal fraction, full stop as decimal sign (extended)
5044 * %H%M%S%z => 083748-0600 Local time and the difference from UTC (basic)
5045 * %T%:z => 08:37:48-06:00 Local time and the difference from UTC (extended)
5046 * %Y%m%dT%H%M%S%z => 20071119T083748-0600 Date and time of day for calendar date (basic)
5047 * %FT%T%:z => 2007-11-19T08:37:48-06:00 Date and time of day for calendar date (extended)
5048 * %Y%jT%H%M%S%z => 2007323T083748-0600 Date and time of day for ordinal date (basic)
5049 * %Y-%jT%T%:z => 2007-323T08:37:48-06:00 Date and time of day for ordinal date (extended)
5050 * %GW%V%uT%H%M%S%z => 2007W471T083748-0600 Date and time of day for week date (basic)
5051 * %G-W%V-%uT%T%:z => 2007-W47-1T08:37:48-06:00 Date and time of day for week date (extended)
5052 * %Y%m%dT%H%M => 20071119T0837 Calendar date and local time (basic)
5053 * %FT%R => 2007-11-19T08:37 Calendar date and local time (extended)
5054 * %Y%jT%H%MZ => 2007323T0837Z Ordinal date and UTC of day (basic)
5055 * %Y-%jT%RZ => 2007-323T08:37Z Ordinal date and UTC of day (extended)
5056 * %GW%V%uT%H%M%z => 2007W471T0837-0600 Week date and local time and difference from UTC (basic)
5057 * %G-W%V-%uT%R%:z => 2007-W47-1T08:37-06:00 Week date and local time and difference from UTC (extended)
5058 *
5059 */
5060
5061 static VALUE
5062 time_strftime(VALUE time, VALUE format)
5063 {
5064 struct time_object *tobj;
5065 const char *fmt;
5066 long len;
5067 rb_encoding *enc;
5068 VALUE tmp;
5069
5070 GetTimeval(time, tobj);
5071 MAKE_TM(time, tobj);
5072 StringValue(format);
5073 if (!rb_enc_str_asciicompat_p(format)) {
5074 rb_raise(rb_eArgError, "format should have ASCII compatible encoding");
5075 }
5076 tmp = rb_str_tmp_frozen_acquire(format);
5077 fmt = RSTRING_PTR(tmp);
5078 len = RSTRING_LEN(tmp);
5079 enc = rb_enc_get(format);
5080 if (len == 0) {
5081 rb_warning("strftime called with empty format string");
5082 return rb_enc_str_new(0, 0, enc);
5083 }
5084 else {
5085 VALUE str = rb_strftime_alloc(fmt, len, enc, time, &tobj->vtm, tobj->timew,
5086 TZMODE_UTC_P(tobj));
5087 rb_str_tmp_frozen_release(format, tmp);
5088 if (!str) rb_raise(rb_eArgError, "invalid format: %"PRIsVALUE, format);
5089 return str;
5090 }
5091 }
5092
5093 int ruby_marshal_write_long(long x, char *buf);
5094
5095 enum {base_dump_size = 8};
5096
5097 /* :nodoc: */
5098 static VALUE
5099 time_mdump(VALUE time)
5100 {
5101 struct time_object *tobj;
5102 unsigned long p, s;
5103 char buf[base_dump_size + sizeof(long) + 1];
5104 int i;
5105 VALUE str;
5106
5107 struct vtm vtm;
5108 long year;
5109 long usec, nsec;
5110 VALUE subsecx, nano, subnano, v, zone;
5111
5112 VALUE year_extend = Qnil;
5113 const int max_year = 1900+0xffff;
5114
5115 GetTimeval(time, tobj);
5116
5117 gmtimew(tobj->timew, &vtm);
5118
5119 if (FIXNUM_P(vtm.year)) {
5120 year = FIX2LONG(vtm.year);
5121 if (year > max_year) {
5122 year_extend = INT2FIX(year - max_year);
5123 year = max_year;
5124 }
5125 else if (year < 1900) {
5126 year_extend = LONG2NUM(1900 - year);
5127 year = 1900;
5128 }
5129 }
5130 else {
5131 if (rb_int_positive_p(vtm.year)) {
5132 year_extend = rb_int_minus(vtm.year, INT2FIX(max_year));
5133 year = max_year;
5134 }
5135 else {
5136 year_extend = rb_int_minus(INT2FIX(1900), vtm.year);
5137 year = 1900;
5138 }
5139 }
5140
5141 subsecx = vtm.subsecx;
5142
5143 nano = mulquov(subsecx, INT2FIX(1000000000), INT2FIX(TIME_SCALE));
5144 divmodv(nano, INT2FIX(1), &v, &subnano);
5145 nsec = FIX2LONG(v);
5146 usec = nsec / 1000;
5147 nsec = nsec % 1000;
5148
5149 nano = addv(LONG2FIX(nsec), subnano);
5150
5151 p = 0x1UL << 31 | /* 1 */
5152 TZMODE_UTC_P(tobj) << 30 | /* 1 */
5153 (year-1900) << 14 | /* 16 */
5154 (vtm.mon-1) << 10 | /* 4 */
5155 vtm.mday << 5 | /* 5 */
5156 vtm.hour; /* 5 */
5157 s = (unsigned long)vtm.min << 26 | /* 6 */
5158 vtm.sec << 20 | /* 6 */
5159 usec; /* 20 */
5160
5161 for (i=0; i<4; i++) {
5162 buf[i] = (unsigned char)p;
5163 p = RSHIFT(p, 8);
5164 }
5165 for (i=4; i<8; i++) {
5166 buf[i] = (unsigned char)s;
5167 s = RSHIFT(s, 8);
5168 }
5169
5170 if (!NIL_P(year_extend)) {
5171 /*
5172 * Append extended year distance from 1900..(1900+0xffff). In
5173 * each cases, there is no sign as the value is positive. The
5174 * format is length (marshaled long) + little endian packed
5175 * binary (like as Fixnum and Bignum).
5176 */
5177 size_t ysize = rb_absint_size(year_extend, NULL);
5178 char *p, *const buf_year_extend = buf + base_dump_size;
5179 if (ysize > LONG_MAX ||
5180 (i = ruby_marshal_write_long((long)ysize, buf_year_extend)) < 0) {
5181 rb_raise(rb_eArgError, "year too %s to marshal: %"PRIsVALUE" UTC",
5182 (year == 1900 ? "small" : "big"), vtm.year);
5183 }
5184 i += base_dump_size;
5185 str = rb_str_new(NULL, i + ysize);
5186 p = RSTRING_PTR(str);
5187 memcpy(p, buf, i);
5188 p += i;
5189 rb_integer_pack(year_extend, p, ysize, 1, 0, INTEGER_PACK_LITTLE_ENDIAN);
5190 }
5191 else {
5192 str = rb_str_new(buf, base_dump_size);
5193 }
5194 rb_copy_generic_ivar(str, time);
5195 if (!rb_equal(nano, INT2FIX(0))) {
5196 if (RB_TYPE_P(nano, T_RATIONAL)) {
5197 rb_ivar_set(str, id_nano_num, RRATIONAL(nano)->num);
5198 rb_ivar_set(str, id_nano_den, RRATIONAL(nano)->den);
5199 }
5200 else {
5201 rb_ivar_set(str, id_nano_num, nano);
5202 rb_ivar_set(str, id_nano_den, INT2FIX(1));
5203 }
5204 }
5205 if (nsec) { /* submicro is only for Ruby 1.9.1 compatibility */
5206 /*
5207 * submicro is formatted in fixed-point packed BCD (without sign).
5208 * It represent digits under microsecond.
5209 * For nanosecond resolution, 3 digits (2 bytes) are used.
5210 * However it can be longer.
5211 * Extra digits are ignored for loading.
5212 */
5213 char buf[2];
5214 int len = (int)sizeof(buf);
5215 buf[1] = (char)((nsec % 10) << 4);
5216 nsec /= 10;
5217 buf[0] = (char)(nsec % 10);
5218 nsec /= 10;
5219 buf[0] |= (char)((nsec % 10) << 4);
5220 if (buf[1] == 0)
5221 len = 1;
5222 rb_ivar_set(str, id_submicro, rb_str_new(buf, len));
5223 }
5224 if (!TZMODE_UTC_P(tobj)) {
5225 VALUE off = rb_time_utc_offset(time), div, mod;
5226 divmodv(off, INT2FIX(1), &div, &mod);
5227 if (rb_equal(mod, INT2FIX(0)))
5228 off = rb_Integer(div);
5229 rb_ivar_set(str, id_offset, off);
5230 }
5231 zone = tobj->vtm.zone;
5232 if (maybe_tzobj_p(zone)) {
5233 zone = rb_funcallv(zone, id_name, 0, 0);
5234 }
5235 rb_ivar_set(str, id_zone, zone);
5236 return str;
5237 }
5238
5239 /* :nodoc: */
5240 static VALUE
5241 time_dump(int argc, VALUE *argv, VALUE time)
5242 {
5243 VALUE str;
5244
5245 rb_check_arity(argc, 0, 1);
5246 str = time_mdump(time);
5247
5248 return str;
5249 }
5250
5251 static VALUE
5252 mload_findzone(VALUE arg)
5253 {
5254 VALUE *argp = (VALUE *)arg;
5255 VALUE time = argp[0], zone = argp[1];
5256 return find_timezone(time, zone);
5257 }
5258
5259 static VALUE
5260 mload_zone(VALUE time, VALUE zone)
5261 {
5262 VALUE z, args[2];
5263 args[0] = time;
5264 args[1] = zone;
5265 z = rb_rescue(mload_findzone, (VALUE)args, 0, Qnil);
5266 if (NIL_P(z)) return rb_fstring(zone);
5267 if (RB_TYPE_P(z, T_STRING)) return rb_fstring(z);
5268 return z;
5269 }
5270
5271 long ruby_marshal_read_long(const char **buf, long len);
5272
5273 /* :nodoc: */
5274 static VALUE
5275 time_mload(VALUE time, VALUE str)
5276 {
5277 struct time_object *tobj;
5278 unsigned long p, s;
5279 time_t sec;
5280 long usec;
5281 unsigned char *buf;
5282 struct vtm vtm;
5283 int i, gmt;
5284 long nsec;
5285 VALUE submicro, nano_num, nano_den, offset, zone, year;
5286 wideval_t timew;
5287
5288 time_modify(time);
5289
5290 #define get_attr(attr, iffound) \
5291 attr = rb_attr_delete(str, id_##attr); \
5292 if (!NIL_P(attr)) { \
5293 iffound; \
5294 }
5295
5296 get_attr(nano_num, {});
5297 get_attr(nano_den, {});
5298 get_attr(submicro, {});
5299 get_attr(offset, (offset = rb_rescue(validate_utc_offset, offset, NULL, Qnil)));
5300 get_attr(zone, (zone = rb_rescue(validate_zone_name, zone, NULL, Qnil)));
5301 get_attr(year, {});
5302
5303 #undef get_attr
5304
5305 rb_copy_generic_ivar(time, str);
5306
5307 StringValue(str);
5308 buf = (unsigned char *)RSTRING_PTR(str);
5309 if (RSTRING_LEN(str) < base_dump_size) {
5310 invalid_format:
5311 rb_raise(rb_eTypeError, "marshaled time format differ");
5312 }
5313
5314 p = s = 0;
5315 for (i=0; i<4; i++) {
5316 p |= (unsigned long)buf[i]<<(8*i);
5317 }
5318 for (i=4; i<8; i++) {
5319 s |= (unsigned long)buf[i]<<(8*(i-4));
5320 }
5321
5322 if ((p & (1UL<<31)) == 0) {
5323 gmt = 0;
5324 offset = Qnil;
5325 sec = p;
5326 usec = s;
5327 nsec = usec * 1000;
5328 timew = wadd(rb_time_magnify(TIMET2WV(sec)), wmulquoll(WINT2FIXWV(usec), TIME_SCALE, 1000000));
5329 }
5330 else {
5331 p &= ~(1UL<<31);
5332 gmt = (int)((p >> 30) & 0x1);
5333
5334 if (NIL_P(year)) {
5335 year = INT2FIX(((int)(p >> 14) & 0xffff) + 1900);
5336 }
5337 if (RSTRING_LEN(str) > base_dump_size) {
5338 long len = RSTRING_LEN(str) - base_dump_size;
5339 long ysize = 0;
5340 VALUE year_extend;
5341 const char *ybuf = (const char *)(buf += base_dump_size);
5342 ysize = ruby_marshal_read_long(&ybuf, len);
5343 len -= ybuf - (const char *)buf;
5344 if (ysize < 0 || ysize > len) goto invalid_format;
5345 year_extend = rb_integer_unpack(ybuf, ysize, 1, 0, INTEGER_PACK_LITTLE_ENDIAN);
5346 if (year == INT2FIX(1900)) {
5347 year = rb_int_minus(year, year_extend);
5348 }
5349 else {
5350 year = rb_int_plus(year, year_extend);
5351 }
5352 }
5353 vtm.year = year;
5354 vtm.mon = ((int)(p >> 10) & 0xf) + 1;
5355 vtm.mday = (int)(p >> 5) & 0x1f;
5356 vtm.hour = (int) p & 0x1f;
5357 vtm.min = (int)(s >> 26) & 0x3f;
5358 vtm.sec = (int)(s >> 20) & 0x3f;
5359 vtm.utc_offset = INT2FIX(0);
5360 vtm.yday = vtm.wday = 0;
5361 vtm.isdst = 0;
5362 vtm.zone = rb_fstring_lit("");
5363
5364 usec = (long)(s & 0xfffff);
5365 nsec = usec * 1000;
5366
5367
5368 vtm.subsecx = mulquov(LONG2FIX(nsec), INT2FIX(TIME_SCALE), LONG2FIX(1000000000));
5369 if (nano_num != Qnil) {
5370 VALUE nano = quov(num_exact(nano_num), num_exact(nano_den));
5371 vtm.subsecx = addv(vtm.subsecx, mulquov(nano, INT2FIX(TIME_SCALE), LONG2FIX(1000000000)));
5372 }
5373 else if (submicro != Qnil) { /* for Ruby 1.9.1 compatibility */
5374 unsigned char *ptr;
5375 long len;
5376 int digit;
5377 ptr = (unsigned char*)StringValuePtr(submicro);
5378 len = RSTRING_LEN(submicro);
5379 nsec = 0;
5380 if (0 < len) {
5381 if (10 <= (digit = ptr[0] >> 4)) goto end_submicro;
5382 nsec += digit * 100;
5383 if (10 <= (digit = ptr[0] & 0xf)) goto end_submicro;
5384 nsec += digit * 10;
5385 }
5386 if (1 < len) {
5387 if (10 <= (digit = ptr[1] >> 4)) goto end_submicro;
5388 nsec += digit;
5389 }
5390 vtm.subsecx = addv(vtm.subsecx, mulquov(LONG2FIX(nsec), INT2FIX(TIME_SCALE), LONG2FIX(1000000000)));
5391 end_submicro: ;
5392 }
5393 timew = timegmw(&vtm);
5394 }
5395
5396 GetNewTimeval(time, tobj);
5397 tobj->tzmode = TIME_TZMODE_LOCALTIME;
5398 tobj->tm_got = 0;
5399 tobj->timew = timew;
5400 if (gmt) {
5401 TZMODE_SET_UTC(tobj);
5402 }
5403 else if (!NIL_P(offset)) {
5404 time_set_utc_offset(time, offset);
5405 time_fixoff(time);
5406 }
5407 if (!NIL_P(zone)) {
5408 zone = mload_zone(time, zone);
5409 tobj->vtm.zone = zone;
5410 zone_localtime(zone, time);
5411 }
5412
5413 return time;
5414 }
5415
5416 /* :nodoc: */
5417 static VALUE
5418 time_load(VALUE klass, VALUE str)
5419 {
5420 VALUE time = time_s_alloc(klass);
5421
5422 time_mload(time, str);
5423 return time;
5424 }
5425
5426 /* :nodoc:*/
5427 /* Document-class: Time::tm
5428 *
5429 * A container class for timezone conversion.
5430 */
5431
5432 /*
5433 * call-seq:
5434 *
5435 * Time::tm.from_time(t) -> tm
5436 *
5437 * Creates new Time::tm object from a Time object.
5438 */
5439
5440 static VALUE
5441 tm_from_time(VALUE klass, VALUE time)
5442 {
5443 struct time_object *tobj;
5444 struct vtm vtm, *v;
5445 #if TM_IS_TIME
5446 VALUE tm;
5447 struct time_object *ttm;
5448
5449 GetTimeval(time, tobj);
5450 tm = time_s_alloc(klass);
5451 ttm = DATA_PTR(tm);
5452 v = &vtm;
5453 GMTIMEW(ttm->timew = tobj->timew, v);
5454 v->subsecx = INT2FIX(0);
5455 v->zone = Qnil;
5456 ttm->vtm = *v;
5457 ttm->tm_got = 1;
5458 TZMODE_SET_UTC(ttm);
5459 return tm;
5460 #else
5461 VALUE args[8];
5462 int i = 0;
5463
5464 GetTimeval(time, tobj);
5465 if (tobj->tm_got && TZMODE_UTC_P(tobj))
5466 v = &tobj->vtm;
5467 else
5468 GMTIMEW(tobj->timew, v = &vtm);
5469 args[i++] = v->year;
5470 args[i++] = INT2FIX(v->mon);
5471 args[i++] = INT2FIX(v->mday);
5472 args[i++] = INT2FIX(v->hour);
5473 args[i++] = INT2FIX(v->min);
5474 args[i++] = INT2FIX(v->sec);
5475 switch (v->isdst) {
5476 case 0: args[i++] = Qfalse; break;
5477 case 1: args[i++] = Qtrue; break;
5478 default: args[i++] = Qnil; break;
5479 }
5480 args[i++] = w2v(rb_time_unmagnify(tobj->timew));
5481 return rb_class_new_instance(i, args, klass);
5482 #endif
5483 }
5484
5485 /*
5486 * call-seq:
5487 *
5488 * Time::tm.new(year, month=nil, day=nil, hour=nil, min=nil, sec=nil, tz=nil) -> tm
5489 *
5490 * Creates new Time::tm object.
5491 */
5492
5493 static VALUE
5494 tm_initialize(int argc, VALUE *argv, VALUE tm)
5495 {
5496 struct vtm vtm;
5497 wideval_t t;
5498
5499 if (rb_check_arity(argc, 1, 7) > 6) argc = 6;
5500 time_arg(argc, argv, &vtm);
5501 t = timegmw(&vtm);
5502 {
5503 #if TM_IS_TIME
5504 struct time_object *tobj = DATA_PTR(tm);
5505 tobj->tzmode = TIME_TZMODE_UTC;
5506 tobj->timew = t;
5507 tobj->vtm = vtm;
5508 #else
5509 int i = 0;
5510 RSTRUCT_SET(tm, i++, INT2FIX(vtm.sec));
5511 RSTRUCT_SET(tm, i++, INT2FIX(vtm.min));
5512 RSTRUCT_SET(tm, i++, INT2FIX(vtm.hour));
5513 RSTRUCT_SET(tm, i++, INT2FIX(vtm.mday));
5514 RSTRUCT_SET(tm, i++, INT2FIX(vtm.mon));
5515 RSTRUCT_SET(tm, i++, vtm.year);
5516 RSTRUCT_SET(tm, i++, w2v(rb_time_unmagnify(t)));
5517 #endif
5518 }
5519 return tm;
5520 }
5521
5522 /* call-seq:
5523 *
5524 * tm.to_time -> time
5525 *
5526 * Returns a new Time object.
5527 */
5528
5529 static VALUE
5530 tm_to_time(VALUE tm)
5531 {
5532 #if TM_IS_TIME
5533 struct time_object *torig = get_timeval(tm);
5534 VALUE dup = time_s_alloc(rb_cTime);
5535 struct time_object *tobj = DATA_PTR(dup);
5536 *tobj = *torig;
5537 return dup;
5538 #else
5539 VALUE t[6];
5540 const VALUE *p = RSTRUCT_CONST_PTR(tm);
5541 int i;
5542
5543 for (i = 0; i < numberof(t); ++i) {
5544 t[i] = p[numberof(t) - 1 - i];
5545 }
5546 return time_s_mkutc(numberof(t), t, rb_cTime);
5547 #endif
5548 }
5549
5550 #if !TM_IS_TIME
5551 static VALUE
5552 tm_zero(VALUE tm)
5553 {
5554 return INT2FIX(0);
5555 }
5556
5557 #define tm_subsec tm_zero
5558 #define tm_utc_offset tm_zero
5559
5560 static VALUE
5561 tm_isdst(VALUE tm)
5562 {
5563 return Qfalse;
5564 }
5565
5566 static VALUE
5567 tm_to_s(VALUE tm)
5568 {
5569 const VALUE *p = RSTRUCT_CONST_PTR(tm);
5570
5571 return rb_sprintf("%.4"PRIsVALUE"-%.2"PRIsVALUE"-%.2"PRIsVALUE" "
5572 "%.2"PRIsVALUE":%.2"PRIsVALUE":%.2"PRIsVALUE" "
5573 "UTC",
5574 p[5], p[4], p[3], p[2], p[1], p[0]);
5575 }
5576 #else
5577 static VALUE
5578 tm_plus(VALUE tm, VALUE offset)
5579 {
5580 return time_add0(rb_obj_class(tm), get_timeval(tm), tm, offset, +1);
5581 }
5582
5583 static VALUE
5584 tm_minus(VALUE tm, VALUE offset)
5585 {
5586 return time_add0(rb_obj_class(tm), get_timeval(tm), tm, offset, -1);
5587 }
5588 #endif
5589
5590 static VALUE
5591 Init_tm(VALUE outer, const char *name)
5592 {
5593 /* :stopdoc:*/
5594 VALUE tm;
5595 #if TM_IS_TIME
5596 tm = rb_define_class_under(outer, name, rb_cObject);
5597 rb_define_alloc_func(tm, time_s_alloc);
5598 rb_define_method(tm, "sec", time_sec, 0);
5599 rb_define_method(tm, "min", time_min, 0);
5600 rb_define_method(tm, "hour", time_hour, 0);
5601 rb_define_method(tm, "mday", time_mday, 0);
5602 rb_define_method(tm, "day", time_mday, 0);
5603 rb_define_method(tm, "mon", time_mon, 0);
5604 rb_define_method(tm, "month", time_mon, 0);
5605 rb_define_method(tm, "year", time_year, 0);
5606 rb_define_method(tm, "isdst", time_isdst, 0);
5607 rb_define_method(tm, "dst?", time_isdst, 0);
5608 rb_define_method(tm, "zone", time_zone, 0);
5609 rb_define_method(tm, "gmtoff", rb_time_utc_offset, 0);
5610 rb_define_method(tm, "gmt_offset", rb_time_utc_offset, 0);
5611 rb_define_method(tm, "utc_offset", rb_time_utc_offset, 0);
5612 rb_define_method(tm, "utc?", time_utc_p, 0);
5613 rb_define_method(tm, "gmt?", time_utc_p, 0);
5614 rb_define_method(tm, "to_s", time_to_s, 0);
5615 rb_define_method(tm, "inspect", time_inspect, 0);
5616 rb_define_method(tm, "to_a", time_to_a, 0);
5617 rb_define_method(tm, "tv_sec", time_to_i, 0);
5618 rb_define_method(tm, "tv_usec", time_usec, 0);
5619 rb_define_method(tm, "usec", time_usec, 0);
5620 rb_define_method(tm, "tv_nsec", time_nsec, 0);
5621 rb_define_method(tm, "nsec", time_nsec, 0);
5622 rb_define_method(tm, "subsec", time_subsec, 0);
5623 rb_define_method(tm, "to_i", time_to_i, 0);
5624 rb_define_method(tm, "to_f", time_to_f, 0);
5625 rb_define_method(tm, "to_r", time_to_r, 0);
5626 rb_define_method(tm, "+", tm_plus, 1);
5627 rb_define_method(tm, "-", tm_minus, 1);
5628 #else
5629 tm = rb_struct_define_under(outer, "tm",
5630 "sec", "min", "hour",
5631 "mday", "mon", "year",
5632 "to_i", NULL);
5633 rb_define_method(tm, "subsec", tm_subsec, 0);
5634 rb_define_method(tm, "utc_offset", tm_utc_offset, 0);
5635 rb_define_method(tm, "to_s", tm_to_s, 0);
5636 rb_define_method(tm, "inspect", tm_to_s, 0);
5637 rb_define_method(tm, "isdst", tm_isdst, 0);
5638 rb_define_method(tm, "dst?", tm_isdst, 0);
5639 #endif
5640 rb_define_method(tm, "initialize", tm_initialize, -1);
5641 rb_define_method(tm, "utc", tm_to_time, 0);
5642 rb_alias(tm, rb_intern("to_time"), rb_intern("utc"));
5643 rb_define_singleton_method(tm, "from_time", tm_from_time, 1);
5644 /* :startdoc:*/
5645
5646 return tm;
5647 }
5648
5649 VALUE
5650 rb_time_zone_abbreviation(VALUE zone, VALUE time)
5651 {
5652 VALUE tm, abbr, strftime_args[2];
5653
5654 abbr = rb_check_string_type(zone);
5655 if (!NIL_P(abbr)) return abbr;
5656
5657 tm = tm_from_time(rb_cTimeTM, time);
5658 abbr = rb_check_funcall(zone, rb_intern("abbr"), 1, &tm);
5659 if (abbr != Qundef) {
5660 goto found;
5661 }
5662 #ifdef SUPPORT_TZINFO_ZONE_ABBREVIATION
5663 abbr = rb_check_funcall(zone, rb_intern("period_for_utc"), 1, &tm);
5664 if (abbr != Qundef) {
5665 abbr = rb_funcallv(abbr, rb_intern("abbreviation"), 0, 0);
5666 goto found;
5667 }
5668 #endif
5669 strftime_args[0] = rb_fstring_lit("%Z");
5670 strftime_args[1] = tm;
5671 abbr = rb_check_funcall(zone, rb_intern("strftime"), 2, strftime_args);
5672 if (abbr != Qundef) {
5673 goto found;
5674 }
5675 abbr = rb_check_funcall_default(zone, idName, 0, 0, Qnil);
5676 found:
5677 return rb_obj_as_string(abbr);
5678 }
5679
5680 /*
5681 * Time is an abstraction of dates and times. Time is stored internally as
5682 * the number of seconds with fraction since the _Epoch_, January 1, 1970
5683 * 00:00 UTC. Also see the library module Date. The Time class treats GMT
5684 * (Greenwich Mean Time) and UTC (Coordinated Universal Time) as equivalent.
5685 * GMT is the older way of referring to these baseline times but persists in
5686 * the names of calls on POSIX systems.
5687 *
5688 * All times may have fraction. Be aware of this fact when comparing times
5689 * with each other -- times that are apparently equal when displayed may be
5690 * different when compared.
5691 *
5692 * Since Ruby 1.9.2, Time implementation uses a signed 63 bit integer,
5693 * Bignum or Rational.
5694 * The integer is a number of nanoseconds since the _Epoch_ which can
5695 * represent 1823-11-12 to 2116-02-20.
5696 * When Bignum or Rational is used (before 1823, after 2116, under
5697 * nanosecond), Time works slower as when integer is used.
5698 *
5699 * = Examples
5700 *
5701 * All of these examples were done using the EST timezone which is GMT-5.
5702 *
5703 * == Creating a new Time instance
5704 *
5705 * You can create a new instance of Time with Time::new. This will use the
5706 * current system time. Time::now is an alias for this. You can also
5707 * pass parts of the time to Time::new such as year, month, minute, etc. When
5708 * you want to construct a time this way you must pass at least a year. If you
5709 * pass the year with nothing else time will default to January 1 of that year
5710 * at 00:00:00 with the current system timezone. Here are some examples:
5711 *
5712 * Time.new(2002) #=> 2002-01-01 00:00:00 -0500
5713 * Time.new(2002, 10) #=> 2002-10-01 00:00:00 -0500
5714 * Time.new(2002, 10, 31) #=> 2002-10-31 00:00:00 -0500
5715 *
5716 * You can pass a UTC offset:
5717 *
5718 * Time.new(2002, 10, 31, 2, 2, 2, "+02:00") #=> 2002-10-31 02:02:02 +0200
5719 *
5720 * Or a timezone object:
5721 *
5722 * tz = timezone("Europe/Athens") # Eastern European Time, UTC+2
5723 * Time.new(2002, 10, 31, 2, 2, 2, tz) #=> 2002-10-31 02:02:02 +0200
5724 *
5725 * You can also use Time::gm, Time::local and Time::utc to infer GMT,
5726 * local and UTC timezones instead of using the current system
5727 * setting.
5728 *
5729 * You can also create a new time using Time::at which takes the number of
5730 * seconds (or fraction of seconds) since the {Unix
5731 * Epoch}[http://en.wikipedia.org/wiki/Unix_time].
5732 *
5733 * Time.at(628232400) #=> 1989-11-28 00:00:00 -0500
5734 *
5735 * == Working with an instance of Time
5736 *
5737 * Once you have an instance of Time there is a multitude of things you can
5738 * do with it. Below are some examples. For all of the following examples, we
5739 * will work on the assumption that you have done the following:
5740 *
5741 * t = Time.new(1993, 02, 24, 12, 0, 0, "+09:00")
5742 *
5743 * Was that a monday?
5744 *
5745 * t.monday? #=> false
5746 *
5747 * What year was that again?
5748 *
5749 * t.year #=> 1993
5750 *
5751 * Was it daylight savings at the time?
5752 *
5753 * t.dst? #=> false
5754 *
5755 * What's the day a year later?
5756 *
5757 * t + (60*60*24*365) #=> 1994-02-24 12:00:00 +0900
5758 *
5759 * How many seconds was that since the Unix Epoch?
5760 *
5761 * t.to_i #=> 730522800
5762 *
5763 * You can also do standard functions like compare two times.
5764 *
5765 * t1 = Time.new(2010)
5766 * t2 = Time.new(2011)
5767 *
5768 * t1 == t2 #=> false
5769 * t1 == t1 #=> true
5770 * t1 < t2 #=> true
5771 * t1 > t2 #=> false
5772 *
5773 * Time.new(2010,10,31).between?(t1, t2) #=> true
5774 *
5775 * == Timezone argument
5776 *
5777 * A timezone argument must have +local_to_utc+ and +utc_to_local+
5778 * methods, and may have +name+, +abbr+, and +dst?+ methods.
5779 *
5780 * The +local_to_utc+ method should convert a Time-like object from
5781 * the timezone to UTC, and +utc_to_local+ is the opposite. The
5782 * result also should be a Time or Time-like object (not necessary to
5783 * be the same class). The #zone of the result is just ignored.
5784 * Time-like argument to these methods is similar to a Time object in
5785 * UTC without sub-second; it has attribute readers for the parts,
5786 * e.g. #year, #month, and so on, and epoch time readers, #to_i. The
5787 * sub-second attributes are fixed as 0, and #utc_offset, #zone,
5788 * #isdst, and their aliases are same as a Time object in UTC.
5789 * Also #to_time, #+, and #- methods are defined.
5790 *
5791 * The +name+ method is used for marshaling. If this method is not
5792 * defined on a timezone object, Time objects using that timezone
5793 * object can not be dumped by Marshal.
5794 *
5795 * The +abbr+ method is used by '%Z' in #strftime.
5796 *
5797 * The +dst?+ method is called with a +Time+ value and should return whether
5798 * the +Time+ value is in daylight savings time in the zone.
5799 *
5800 * === Auto conversion to Timezone
5801 *
5802 * At loading marshaled data, a timezone name will be converted to a timezone
5803 * object by +find_timezone+ class method, if the method is defined.
5804 *
5805 * Similarly, that class method will be called when a timezone argument does
5806 * not have the necessary methods mentioned above.
5807 */
5808
5809 void
5810 Init_Time(void)
5811 {
5812 #undef rb_intern
5813 #define rb_intern(str) rb_intern_const(str)
5814
5815 id_submicro = rb_intern("submicro");
5816 id_nano_num = rb_intern("nano_num");
5817 id_nano_den = rb_intern("nano_den");
5818 id_offset = rb_intern("offset");
5819 id_zone = rb_intern("zone");
5820 id_nanosecond = rb_intern("nanosecond");
5821 id_microsecond = rb_intern("microsecond");
5822 id_millisecond = rb_intern("millisecond");
5823 id_nsec = rb_intern("nsec");
5824 id_usec = rb_intern("usec");
5825 id_local_to_utc = rb_intern("local_to_utc");
5826 id_utc_to_local = rb_intern("utc_to_local");
5827 id_year = rb_intern("year");
5828 id_mon = rb_intern("mon");
5829 id_mday = rb_intern("mday");
5830 id_hour = rb_intern("hour");
5831 id_min = rb_intern("min");
5832 id_sec = rb_intern("sec");
5833 id_isdst = rb_intern("isdst");
5834 id_find_timezone = rb_intern("find_timezone");
5835
5836 rb_cTime = rb_define_class("Time", rb_cObject);
5837 rb_include_module(rb_cTime, rb_mComparable);
5838
5839 rb_define_alloc_func(rb_cTime, time_s_alloc);
5840 rb_define_singleton_method(rb_cTime, "now", time_s_now, -1);
5841 rb_define_singleton_method(rb_cTime, "at", time_s_at, -1);
5842 rb_define_singleton_method(rb_cTime, "utc", time_s_mkutc, -1);
5843 rb_define_singleton_method(rb_cTime, "gm", time_s_mkutc, -1);
5844 rb_define_singleton_method(rb_cTime, "local", time_s_mktime, -1);
5845 rb_define_singleton_method(rb_cTime, "mktime", time_s_mktime, -1);
5846
5847 rb_define_method(rb_cTime, "to_i", time_to_i, 0);
5848 rb_define_method(rb_cTime, "to_f", time_to_f, 0);
5849 rb_define_method(rb_cTime, "to_r", time_to_r, 0);
5850 rb_define_method(rb_cTime, "<=>", time_cmp, 1);
5851 rb_define_method(rb_cTime, "eql?", time_eql, 1);
5852 rb_define_method(rb_cTime, "hash", time_hash, 0);
5853 rb_define_method(rb_cTime, "initialize", time_init, -1);
5854 rb_define_method(rb_cTime, "initialize_copy", time_init_copy, 1);
5855
5856 rb_define_method(rb_cTime, "localtime", time_localtime_m, -1);
5857 rb_define_method(rb_cTime, "gmtime", time_gmtime, 0);
5858 rb_define_method(rb_cTime, "utc", time_gmtime, 0);
5859 rb_define_method(rb_cTime, "getlocal", time_getlocaltime, -1);
5860 rb_define_method(rb_cTime, "getgm", time_getgmtime, 0);
5861 rb_define_method(rb_cTime, "getutc", time_getgmtime, 0);
5862
5863 rb_define_method(rb_cTime, "ctime", time_asctime, 0);
5864 rb_define_method(rb_cTime, "asctime", time_asctime, 0);
5865 rb_define_method(rb_cTime, "to_s", time_to_s, 0);
5866 rb_define_method(rb_cTime, "inspect", time_inspect, 0);
5867 rb_define_method(rb_cTime, "to_a", time_to_a, 0);
5868
5869 rb_define_method(rb_cTime, "+", time_plus, 1);
5870 rb_define_method(rb_cTime, "-", time_minus, 1);
5871
5872 rb_define_method(rb_cTime, "succ", time_succ, 0);
5873 rb_define_method(rb_cTime, "round", time_round, -1);
5874 rb_define_method(rb_cTime, "floor", time_floor, -1);
5875 rb_define_method(rb_cTime, "ceil", time_ceil, -1);
5876
5877 rb_define_method(rb_cTime, "sec", time_sec, 0);
5878 rb_define_method(rb_cTime, "min", time_min, 0);
5879 rb_define_method(rb_cTime, "hour", time_hour, 0);
5880 rb_define_method(rb_cTime, "mday", time_mday, 0);
5881 rb_define_method(rb_cTime, "day", time_mday, 0);
5882 rb_define_method(rb_cTime, "mon", time_mon, 0);
5883 rb_define_method(rb_cTime, "month", time_mon, 0);
5884 rb_define_method(rb_cTime, "year", time_year, 0);
5885 rb_define_method(rb_cTime, "wday", time_wday, 0);
5886 rb_define_method(rb_cTime, "yday", time_yday, 0);
5887 rb_define_method(rb_cTime, "isdst", time_isdst, 0);
5888 rb_define_method(rb_cTime, "dst?", time_isdst, 0);
5889 rb_define_method(rb_cTime, "zone", time_zone, 0);
5890 rb_define_method(rb_cTime, "gmtoff", rb_time_utc_offset, 0);
5891 rb_define_method(rb_cTime, "gmt_offset", rb_time_utc_offset, 0);
5892 rb_define_method(rb_cTime, "utc_offset", rb_time_utc_offset, 0);
5893
5894 rb_define_method(rb_cTime, "utc?", time_utc_p, 0);
5895 rb_define_method(rb_cTime, "gmt?", time_utc_p, 0);
5896
5897 rb_define_method(rb_cTime, "sunday?", time_sunday, 0);
5898 rb_define_method(rb_cTime, "monday?", time_monday, 0);
5899 rb_define_method(rb_cTime, "tuesday?", time_tuesday, 0);
5900 rb_define_method(rb_cTime, "wednesday?", time_wednesday, 0);
5901 rb_define_method(rb_cTime, "thursday?", time_thursday, 0);
5902 rb_define_method(rb_cTime, "friday?", time_friday, 0);
5903 rb_define_method(rb_cTime, "saturday?", time_saturday, 0);
5904
5905 rb_define_method(rb_cTime, "tv_sec", time_to_i, 0);
5906 rb_define_method(rb_cTime, "tv_usec", time_usec, 0);
5907 rb_define_method(rb_cTime, "usec", time_usec, 0);
5908 rb_define_method(rb_cTime, "tv_nsec", time_nsec, 0);
5909 rb_define_method(rb_cTime, "nsec", time_nsec, 0);
5910 rb_define_method(rb_cTime, "subsec", time_subsec, 0);
5911
5912 rb_define_method(rb_cTime, "strftime", time_strftime, 1);
5913
5914 /* methods for marshaling */
5915 rb_define_private_method(rb_cTime, "_dump", time_dump, -1);
5916 rb_define_private_method(rb_singleton_class(rb_cTime), "_load", time_load, 1);
5917 #if 0
5918 /* Time will support marshal_dump and marshal_load in the future (1.9 maybe) */
5919 rb_define_private_method(rb_cTime, "marshal_dump", time_mdump, 0);
5920 rb_define_private_method(rb_cTime, "marshal_load", time_mload, 1);
5921 #endif
5922
5923 #ifdef DEBUG_FIND_TIME_NUMGUESS
5924 rb_define_virtual_variable("$find_time_numguess", find_time_numguess_getter, NULL);
5925 #endif
5926
5927 rb_cTimeTM = Init_tm(rb_cTime, "tm");
5928 }
idTo_int
@ idTo_int
Definition: rb_mjit_min_header-2.7.0.h:8713
rb_get_kwargs
int rb_get_kwargs(VALUE keyword_hash, const ID *table, int required, int optional, VALUE *values)
Definition: class.c:1886
ISASCII
#define ISASCII(c)
Definition: ruby.h:2304
ID
unsigned long ID
Definition: ruby.h:103
rb_check_funcall
VALUE rb_check_funcall(VALUE, ID, int, const VALUE *)
Definition: vm_eval.c:505
TMOPT_MAX_
@ TMOPT_MAX_
Definition: time.c:2713
rb_str_concat
VALUE rb_str_concat(VALUE, VALUE)
Definition: string.c:3065
rb_define_class
VALUE rb_define_class(const char *name, VALUE super)
Defines a top-level class.
Definition: class.c:649
FIXWV_MAX
#define FIXWV_MAX
Definition: time.c:227
TIMET2NUM
#define TIMET2NUM(v)
Definition: rb_mjit_min_header-2.7.0.h:119
M28
#define M28(m)
Definition: time.c:776
rb_fstring
VALUE rb_fstring(VALUE)
Definition: string.c:312
TypedData_Make_Struct
#define TypedData_Make_Struct(klass, type, data_type, sval)
Definition: ruby.h:1244
idEq
@ idEq
Definition: id.h:96
tzset
void tzset(void)
TRUE
#define TRUE
Definition: nkf.h:175
rb_big_modulo
VALUE rb_big_modulo(VALUE x, VALUE y)
Definition: bignum.c:6103
rb_struct_define_under
VALUE rb_struct_define_under(VALUE, const char *,...)
Definition: struct.c:449
rb_time_timespec
struct timespec rb_time_timespec(VALUE time)
Definition: time.c:2692
rb_time_nano_new
VALUE rb_time_nano_new(time_t sec, long nsec)
Definition: time.c:2519
time_t
long time_t
Definition: rb_mjit_min_header-2.7.0.h:1236
rb_include_module
void rb_include_module(VALUE klass, VALUE module)
Definition: class.c:869
LONG_MAX
#define LONG_MAX
Definition: ruby.h:220
rb_exc_new_str
VALUE rb_exc_new_str(VALUE etype, VALUE str)
Definition: error.c:972
FIX2INT
#define FIX2INT(x)
Definition: ruby.h:717
rb_gc_register_mark_object
void rb_gc_register_mark_object(VALUE obj)
Definition: gc.c:7063
GMTIME
#define GMTIME(tm, result)
Definition: time.c:735
tm::tm_min
int tm_min
Definition: rb_mjit_min_header-2.7.0.h:1935
rb_warn
void rb_warn(const char *fmt,...)
Definition: error.c:313
GetNewTimeval
#define GetNewTimeval(obj, tobj)
Definition: time.c:1732
ISDIGIT
#define ISDIGIT(c)
Definition: ruby.h:2312
rb_warning
void rb_warning(const char *fmt,...)
Definition: error.c:334
TZMODE_SET_UTC
#define TZMODE_SET_UTC(tobj)
Definition: time.c:1738
rb_scan_args
#define rb_scan_args(argc, argvp, fmt,...)
Definition: rb_mjit_min_header-2.7.0.h:6372
wlt
#define wlt(x, y)
Definition: time.c:356
INT2FIX
#define INT2FIX(i)
Definition: ruby.h:263
localtime
struct tm * localtime(const time_t *_timer)
RSTRING_PTR
#define RSTRING_PTR(str)
Definition: ruby.h:1009
time
time_t time(time_t *_timer)
div
void div_t div(int __numer, int __denom)
NUM2LONG
#define NUM2LONG(x)
Definition: ruby.h:679
TZMODE_UTC_P
#define TZMODE_UTC_P(tobj)
Definition: time.c:1737
MOD
#define MOD(n, d)
Definition: time.c:55
GMTIMEW
#define GMTIMEW(w, v)
Definition: time.c:1292
ne
#define ne(x, y)
Definition: time.c:82
rb_equal
VALUE rb_equal(VALUE, VALUE)
Same as Object#===, case equality.
Definition: object.c:124
rb_locale_encoding
rb_encoding * rb_locale_encoding(void)
Definition: encoding.c:1372
rb_int_positive_pow
RUBY_EXTERN VALUE rb_int_positive_pow(long x, unsigned long y)
Definition: numeric.c:4033
rb_enc_sprintf
VALUE rb_enc_sprintf(rb_encoding *enc, const char *format,...)
Definition: sprintf.c:1178
tm::tm_wday
int tm_wday
Definition: rb_mjit_min_header-2.7.0.h:1940
VALUE
unsigned long VALUE
Definition: ruby.h:102
rb_obj_as_string
VALUE rb_obj_as_string(VALUE)
Definition: string.c:1440
rb_eArgError
VALUE rb_eArgError
Definition: error.c:923
LOCALTIME
#define LOCALTIME(tm, result)
Definition: time.c:716
MAKE_TM
#define MAKE_TM(time, tobj)
Definition: time.c:1754
encoding.h
WINT2FIXWV
#define WINT2FIXWV(i)
Definition: time.c:230
RB_TYPE_P
#define RB_TYPE_P(obj, type)
Definition: ruby.h:560
TZMODE_COPY
#define TZMODE_COPY(tobj1, tobj2)
Definition: time.c:1748
WV2TIMET
#define WV2TIMET(t)
Definition: time.c:643
rb_cTime
VALUE rb_cTime
Definition: time.c:645
rb_enc_get
rb_encoding * rb_enc_get(VALUE obj)
Definition: encoding.c:872
TZMODE_LOCALTIME_P
#define TZMODE_LOCALTIME_P(tobj)
Definition: time.c:1740
int
__inline__ int
Definition: rb_mjit_min_header-2.7.0.h:2839
rb_invcmp
VALUE rb_invcmp(VALUE x, VALUE y)
Definition: compar.c:47
CLOCK_REALTIME
#define CLOCK_REALTIME
Definition: win32.h:133
zone
Definition: zonetab.h:35
id.h
TIME_TZMODE_UTC
#define TIME_TZMODE_UTC
Definition: time.c:1720
SIGNED_VALUE
#define SIGNED_VALUE
Definition: ruby.h:104
uwideint_t
unsigned long uwideint_t
Definition: time.c:218
INT_MIN
#define INT_MIN
Definition: rb_mjit_min_header-2.7.0.h:4050
INTEGER_PACK_LITTLE_ENDIAN
#define INTEGER_PACK_LITTLE_ENDIAN
Definition: intern.h:162
ruby_reset_leap_second_info
void ruby_reset_leap_second_info(void)
Definition: time.c:1193
uint64_t
unsigned long long uint64_t
Definition: sha2.h:102
StringValue
use StringValue() instead")))
TIME_TZMODE_LOCALTIME
#define TIME_TZMODE_LOCALTIME
Definition: time.c:1719
rb_str_dup
VALUE rb_str_dup(VALUE)
Definition: string.c:1516
RRATIONAL
#define RRATIONAL(obj)
Definition: internal.h:794
rb_check_string_type
VALUE rb_check_string_type(VALUE)
Definition: string.c:2314
rb_Float
VALUE rb_Float(VALUE)
Equivalent to Kernel#Float in Ruby.
Definition: object.c:3493
Qundef
#define Qundef
Definition: ruby.h:470
TMOPT_IN
@ TMOPT_IN
Definition: time.c:2712
rb_define_singleton_method
void rb_define_singleton_method(VALUE obj, const char *name, VALUE(*func)(ANYARGS), int argc)
Defines a singleton method for obj.
Definition: class.c:1755
T_RATIONAL
#define T_RATIONAL
Definition: ruby.h:541
tm::tm_sec
int tm_sec
Definition: rb_mjit_min_header-2.7.0.h:1934
rb_define_method
void rb_define_method(VALUE klass, const char *name, VALUE(*func)(ANYARGS), int argc)
Definition: class.c:1551
rb_long2int
#define rb_long2int(n)
Definition: ruby.h:350
rb_time_new
VALUE rb_time_new(time_t sec, long usec)
Definition: time.c:2492
INT2NUM
#define INT2NUM(x)
Definition: ruby.h:1609
ptr
struct RIMemo * ptr
Definition: debug.c:74
rb_time_timeval
struct timeval rb_time_timeval(VALUE time)
Definition: time.c:2675
rb_Integer
VALUE rb_Integer(VALUE)
Equivalent to Kernel#Integer in Ruby.
Definition: object.c:3106
Qfalse
#define Qfalse
Definition: ruby.h:467
DBL2NUM
#define DBL2NUM(dbl)
Definition: ruby.h:967
rb_numeric_quo
VALUE rb_numeric_quo(VALUE x, VALUE y)
Definition: rational.c:2014
rb_ary_new3
#define rb_ary_new3
Definition: intern.h:104
VTM_WDAY_INITVAL
#define VTM_WDAY_INITVAL
Definition: time.c:56
NULL
#define NULL
Definition: _sdbm.c:101
tm::tm_mon
int tm_mon
Definition: rb_mjit_min_header-2.7.0.h:1938
uint32_t
unsigned int uint32_t
Definition: sha2.h:101
rb_cmpint
#define rb_cmpint(cmp, a, b)
tm::tm_mday
int tm_mday
Definition: rb_mjit_min_header-2.7.0.h:1937
PRIsVALUE
#define PRIsVALUE
Definition: ruby.h:166
int64_t
__int64_t int64_t
Definition: rb_mjit_min_header-2.7.0.h:1180
WIDEVALUE
VALUE WIDEVALUE
Definition: time.c:220
tzname
#define tzname
Definition: rb_mjit_min_header-2.7.0.h:1995
TIMET2WV
#define TIMET2WV(t)
Definition: time.c:620
id_divmod
#define id_divmod
Definition: time.c:44
FIX2LONG
#define FIX2LONG(x)
Definition: ruby.h:394
ID2SYM
#define ID2SYM(x)
Definition: ruby.h:414
rb_intern
#define rb_intern(str)
error
const rb_iseq_t const char * error
Definition: rb_mjit_min_header-2.7.0.h:13506
strlen
size_t strlen(const char *)
tm::tm_hour
int tm_hour
Definition: rb_mjit_min_header-2.7.0.h:1936
rb_respond_to
int rb_respond_to(VALUE, ID)
Definition: vm_method.c:2190
gmtime
struct tm * gmtime(const time_t *_timer)
rb_check_arity
#define rb_check_arity
Definition: intern.h:347
wideint_t
long wideint_t
Definition: time.c:219
timespec::tv_nsec
long tv_nsec
Definition: missing.h:62
ULL2NUM
#define ULL2NUM(v)
Definition: rb_mjit_min_header-2.7.0.h:4242
NDIV
#define NDIV(x, y)
Definition: time.c:52
v
int VALUE v
Definition: rb_mjit_min_header-2.7.0.h:12332
rb_usascii_str_new_static
VALUE rb_usascii_str_new_static(const char *, long)
Definition: string.c:878
rb_str_resize
VALUE rb_str_resize(VALUE, long)
Definition: string.c:2709
strftimev
#define strftimev(fmt, time, enc)
Definition: time.c:4044
rb_funcallv
#define rb_funcallv(recv, mid, argc, argv)
Definition: rb_mjit_min_header-2.7.0.h:7899
GetTimeval
#define GetTimeval(obj, tobj)
Definition: time.c:1731
RUBY_TYPED_DEFAULT_FREE
#define RUBY_TYPED_DEFAULT_FREE
Definition: ruby.h:1203
rb_raise
void rb_raise(VALUE exc, const char *fmt,...)
Definition: error.c:2669
MUL_OVERFLOW_FIXWV_P
#define MUL_OVERFLOW_FIXWV_P(a, b)
Definition: time.c:237
rb_str_tmp_frozen_acquire
VALUE rb_str_tmp_frozen_acquire(VALUE str)
Definition: string.c:1210
rb_ary_entry
VALUE rb_ary_entry(VALUE ary, long offset)
Definition: array.c:1512
rb_eRangeError
VALUE rb_eRangeError
Definition: error.c:926
rb_str_to_inum
VALUE rb_str_to_inum(VALUE str, int base, int badcheck)
Definition: bignum.c:4268
wday_p
#define wday_p(n)
Definition: time.c:4564
timev.h
LONG2NUM
#define LONG2NUM(x)
Definition: ruby.h:1644
obj
const VALUE VALUE obj
Definition: rb_mjit_min_header-2.7.0.h:5742
TIME_TZMODE_UNINITIALIZED
#define TIME_TZMODE_UNINITIALIZED
Definition: time.c:1722
ge
#define ge(x, y)
Definition: time.c:86
gt
#define gt(x, y)
Definition: time.c:84
rb_obj_class
VALUE rb_obj_class(VALUE)
Equivalent to Object#class in Ruby.
Definition: object.c:217
rb_integer_pack
int rb_integer_pack(VALUE val, void *words, size_t numwords, size_t wordsize, size_t nails, int flags)
Definition: bignum.c:3547
rb_alias
void rb_alias(VALUE, ID, ID)
Definition: vm_method.c:1581
memcpy
void * memcpy(void *__restrict, const void *__restrict, size_t)
rb_time_interval
struct timeval rb_time_interval(VALUE num)
Definition: time.c:2669
tm::tm_isdst
int tm_isdst
Definition: rb_mjit_min_header-2.7.0.h:1942
ruby_marshal_read_long
long ruby_marshal_read_long(const char **buf, long len)
Definition: marshal.c:1266
DATA_PTR
#define DATA_PTR(dta)
Definition: ruby.h:1175
fprintf
int fprintf(FILE *__restrict, const char *__restrict,...) __attribute__((__format__(__printf__
LIKELY
#define LIKELY(x)
Definition: ffi_common.h:125
timespec::tv_sec
time_t tv_sec
Definition: missing.h:61
rb_encoding
const typedef OnigEncodingType rb_encoding
Definition: encoding.h:115
rb_check_frozen
#define rb_check_frozen(obj)
Definition: intern.h:319
rb_Rational1
#define rb_Rational1(x)
Definition: intern.h:182
strftime
size_t strftime(char *__restrict _s, size_t _maxsize, const char *__restrict _fmt, const struct tm *__restrict _t)
mulquov
#define mulquov(x, y, z)
Definition: time.c:171
rb_big_cmp
VALUE rb_big_cmp(VALUE x, VALUE y)
Definition: bignum.c:5419
rb_time_succ
VALUE rb_time_succ(VALUE time)
Definition: time.c:4229
WINT2WV
#define WINT2WV(wi)
Definition: time.c:264
i
uint32_t i
Definition: rb_mjit_min_header-2.7.0.h:5464
IsTimeval
#define IsTimeval(obj)
Definition: time.c:1734
INT_MAX
#define INT_MAX
Definition: rb_mjit_min_header-2.7.0.h:4052
long
#define long
Definition: rb_mjit_min_header-2.7.0.h:2880
base_dump_size
@ base_dump_size
Definition: time.c:5095
leap_year_v_p
#define leap_year_v_p(y)
Definition: time.c:670
TYPEOF_TIMEVAL_TV_SEC
#define TYPEOF_TIMEVAL_TV_SEC
Definition: timev.h:25
rb_time_timespec_new
VALUE rb_time_timespec_new(const struct timespec *ts, int offset)
Returns a time object with UTC/localtime/fixed offset.
Definition: time.c:2530
FIXWV2WINT
#define FIXWV2WINT(w)
Definition: time.c:231
rb_time_utc_offset
VALUE rb_time_utc_offset(VALUE time)
Definition: time.c:4791
EXTRACT_TIME
#define EXTRACT_TIME()
stderr
#define stderr
Definition: rb_mjit_min_header-2.7.0.h:1485
rb_sys_fail
void rb_sys_fail(const char *mesg)
Definition: error.c:2793
rb_eTypeError
VALUE rb_eTypeError
Definition: error.c:922
rb_copy_generic_ivar
void rb_copy_generic_ivar(VALUE, VALUE)
Definition: variable.c:1447
validate_vtm_range
#define validate_vtm_range(mem, b, e)
ruby_marshal_write_long
int ruby_marshal_write_long(long x, char *buf)
Definition: marshal.c:307
NORETURN
NORETURN(static void invalid_utc_offset(void))
rb_integer_unpack
VALUE rb_integer_unpack(const void *words, size_t numwords, size_t wordsize, size_t nails, int flags)
Definition: bignum.c:3633
INTEGER_PACK_NATIVE_BYTE_ORDER
#define INTEGER_PACK_NATIVE_BYTE_ORDER
Definition: intern.h:155
rb_eRuntimeError
VALUE rb_eRuntimeError
Definition: error.c:920
RSHIFT
#define RSHIFT(x, y)
Definition: rb_mjit_min_header-2.7.0.h:414
digit
#define digit(x)
Definition: langinfo.c:56
mod
#define mod(x, y)
Definition: date_strftime.c:28
SIGNED_WIDEVALUE
SIGNED_VALUE SIGNED_WIDEVALUE
Definition: time.c:221
STRNCASECMP
#define STRNCASECMP(s1, s2, n)
Definition: ruby.h:2324
StringValuePtr
#define StringValuePtr(v)
Definition: ruby.h:603
rb_big_minus
VALUE rb_big_minus(VALUE x, VALUE y)
Definition: bignum.c:5853
uint8_t
unsigned char uint8_t
Definition: sha2.h:100
FALSE
#define FALSE
Definition: nkf.h:174
DEBUG_REPORT_GUESSRANGE
#define DEBUG_REPORT_GUESSRANGE
Definition: time.c:3114
FIXNUM_P
#define FIXNUM_P(f)
Definition: ruby.h:396
rb_usascii_str_new_cstr
#define rb_usascii_str_new_cstr(str)
Definition: rb_mjit_min_header-2.7.0.h:6121
rb_to_int
VALUE rb_to_int(VALUE)
Converts val into Integer.
Definition: object.c:3021
localtime_r
struct tm * localtime_r(const time_t *, struct tm *)
Definition: win32.c:7862
WIDEVAL_WRAP
#define WIDEVAL_WRAP(v)
Definition: time.c:249
rb_check_to_int
VALUE rb_check_to_int(VALUE)
Tries to convert val into Integer.
Definition: object.c:3036
CONST_ID
#define CONST_ID(var, str)
Definition: ruby.h:1841
StringValueCStr
#define StringValueCStr(v)
Definition: ruby.h:604
rb_check_array_type
VALUE rb_check_array_type(VALUE ary)
Definition: array.c:909
RSTRUCT_SET
#define RSTRUCT_SET(st, idx, v)
Definition: ruby.h:1257
D28
#define D28
Definition: time.c:807
D30
#define D30
Definition: time.c:815
le
#define le(x, y)
Definition: time.c:85
id_name
#define id_name
Definition: time.c:45
rb_typeddata_is_kind_of
int rb_typeddata_is_kind_of(VALUE obj, const rb_data_type_t *data_type)
Definition: error.c:872
timeval::tv_sec
time_t tv_sec
Definition: missing.h:54
CLASS_OF
#define CLASS_OF(v)
Definition: ruby.h:484
fmt
const VALUE int int int int int int VALUE char * fmt
Definition: rb_mjit_min_header-2.7.0.h:6462
rb_num_zerodiv
void rb_num_zerodiv(void)
Definition: numeric.c:194
id_to_i
#define id_to_i
Definition: complex.c:40
rb_rational_new
VALUE rb_rational_new(VALUE, VALUE)
Definition: rational.c:1945
D31
#define D31
Definition: time.c:819
neg
#define neg(x)
Definition: time.c:141
char
#define char
Definition: rb_mjit_min_header-2.7.0.h:2876
rb_cObject
RUBY_EXTERN VALUE rb_cObject
Definition: ruby.h:2010
get_attr
#define get_attr(attr, iffound)
buf
unsigned char buf[MIME_BUF_SIZE]
Definition: nkf.c:4322
n
const char size_t n
Definition: rb_mjit_min_header-2.7.0.h:5456
rb_str_cat_cstr
#define rb_str_cat_cstr(str, ptr)
Definition: rb_mjit_min_header-2.7.0.h:6126
rb_exc_raise
void rb_exc_raise(VALUE mesg)
Raises an exception in the current thread.
Definition: eval.c:667
T_BIGNUM
#define T_BIGNUM
Definition: ruby.h:533
TypedData_Get_Struct
#define TypedData_Get_Struct(obj, type, data_type, sval)
Definition: ruby.h:1252
rb_funcall
#define rb_funcall(recv, mid, argc,...)
Definition: rb_mjit_min_header-2.7.0.h:6585
internal.h
OBJ_INIT_COPY
#define OBJ_INIT_COPY(obj, orig)
Definition: intern.h:331
UTC_ZONE
#define UTC_ZONE
Definition: time.c:46
timeval::tv_usec
long tv_usec
Definition: missing.h:55
arg
VALUE arg
Definition: rb_mjit_min_header-2.7.0.h:5601
argv
char ** argv
Definition: ruby.c:223
f
#define f
idTo_i
@ idTo_i
Definition: rb_mjit_min_header-2.7.0.h:8722
NUM2TIMET
#define NUM2TIMET(v)
Definition: rb_mjit_min_header-2.7.0.h:120
TIME_SCALE
#define TIME_SCALE
Definition: timev.h:22
rb_sprintf
VALUE rb_sprintf(const char *format,...)
Definition: sprintf.c:1197
rb_strftime
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:919
klass
VALUE klass
Definition: rb_mjit_min_header-2.7.0.h:13254
rb_int_positive_p
int rb_int_positive_p(VALUE num)
Definition: numeric.c:301
EXTRACT_VTM
#define EXTRACT_VTM()
M31
#define M31(m)
Definition: time.c:788
timeval
Definition: missing.h:53
rb_hash
VALUE rb_hash(VALUE obj)
Definition: hash.c:129
ruby_tz_uptodate_p
bool ruby_tz_uptodate_p
Definition: time.c:674
TZMODE_SET_LOCALTIME
#define TZMODE_SET_LOCALTIME(tobj)
Definition: time.c:1741
str
char str[HTML_ESCAPE_MAX_LEN+1]
Definition: escape.c:18
lt
#define lt(x, y)
Definition: time.c:83
modf
double modf(double, double *)
FIXNUM_MIN
#define FIXNUM_MIN
Definition: ruby.h:260
rb_usascii_str_new
#define rb_usascii_str_new(str, len)
Definition: rb_mjit_min_header-2.7.0.h:6118
rb_mComparable
VALUE rb_mComparable
Definition: compar.c:16
gmtime_r
struct tm * gmtime_r(const time_t *, struct tm *)
Definition: win32.c:7838
RUBY_TYPED_FREE_IMMEDIATELY
#define RUBY_TYPED_FREE_IMMEDIATELY
Definition: ruby.h:1207
rb_big_mul
VALUE rb_big_mul(VALUE x, VALUE y)
Definition: bignum.c:5933
MEMCPY
#define MEMCPY(p1, p2, type, n)
Definition: ruby.h:1753
rb_time_num_new
VALUE rb_time_num_new(VALUE timev, VALUE off)
Definition: time.c:2553
rb_time_zone_abbreviation
VALUE rb_time_zone_abbreviation(VALUE zone, VALUE time)
Definition: time.c:5650
M29
#define M29(m)
Definition: time.c:780
wideval_t
WIDEVALUE wideval_t
Definition: time.c:248
TZMODE_SET_FIXOFF
#define TZMODE_SET_FIXOFF(tobj, off)
Definition: time.c:1744
FIXWV_P
#define FIXWV_P(w)
Definition: time.c:236
rb_enc_str_asciicompat_p
#define rb_enc_str_asciicompat_p(str)
Definition: encoding.h:257
clock_gettime
int clock_gettime(clockid_t, struct timespec *)
Definition: win32.c:4612
NIL_P
#define NIL_P(v)
Definition: ruby.h:482
rb_str_tmp_frozen_release
void rb_str_tmp_frozen_release(VALUE str, VALUE tmp)
Definition: string.c:1217
argc
int argc
Definition: ruby.c:222
rb_big_plus
VALUE rb_big_plus(VALUE x, VALUE y)
Definition: bignum.c:5824
rb_singleton_class
VALUE rb_singleton_class(VALUE obj)
Returns the singleton class of obj.
Definition: class.c:1725
rb_check_funcall_default
VALUE rb_check_funcall_default(VALUE, ID, int, const VALUE *, VALUE)
Definition: vm_eval.c:533
TIME_INIT_P
#define TIME_INIT_P(tobj)
Definition: time.c:1735
rb_strftime_timespec
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:929
tm
Definition: rb_mjit_min_header-2.7.0.h:1932
rb_data_type_struct
Definition: ruby.h:1148
tm::tm_year
int tm_year
Definition: rb_mjit_min_header-2.7.0.h:1939
RFLOAT_VALUE
#define RFLOAT_VALUE(v)
Definition: ruby.h:966
GUESS
#define GUESS(p)
wmulquoll
#define wmulquoll(x, y, z)
Definition: time.c:416
rb_timespec_now
void rb_timespec_now(struct timespec *ts)
Definition: time.c:1873
rb_str_new
#define rb_str_new(str, len)
Definition: rb_mjit_min_header-2.7.0.h:6116
rb_gc_mark
void rb_gc_mark(VALUE ptr)
Definition: gc.c:5212
rb_time_timespec_interval
struct timespec rb_time_timespec_interval(VALUE num)
Definition: time.c:2706
Qtrue
#define Qtrue
Definition: ruby.h:468
Init_Time
void Init_Time(void)
Definition: time.c:5810
rb_big_div
VALUE rb_big_div(VALUE x, VALUE y)
Definition: bignum.c:6091
FIXWVABLE
#define FIXWVABLE(i)
Definition: time.c:229
len
uint8_t len
Definition: escape.c:17
idCmp
@ idCmp
Definition: id.h:84
M30
#define M30(m)
Definition: time.c:784
tm::tm_yday
int tm_yday
Definition: rb_mjit_min_header-2.7.0.h:1941
rb_class_new_instance
VALUE rb_class_new_instance(int, const VALUE *, VALUE)
Allocates and initializes an instance of klass.
Definition: object.c:1955
timespec
Definition: missing.h:60
id_div
#define id_div
Definition: time.c:43
LONG2FIX
#define LONG2FIX(i)
Definition: ruby.h:265
rb_ivar_set
VALUE rb_ivar_set(VALUE, ID, VALUE)
Definition: variable.c:1300
T_STRING
#define T_STRING
Definition: ruby.h:528
time_succ
#define time_succ
Definition: time.c:4245
rb_define_class_under
VALUE rb_define_class_under(VALUE outer, const char *name, VALUE super)
Defines a class under the namespace of outer.
Definition: class.c:698
mktime
time_t mktime(struct tm *_timeptr)
FIXWV_MIN
#define FIXWV_MIN
Definition: time.c:228
RB_INTEGER_TYPE_P
#define RB_INTEGER_TYPE_P(obj)
Definition: ruby_missing.h:15
TIMET_MIN
#define TIMET_MIN
Definition: rb_mjit_min_header-2.7.0.h:6641
ruby::backward::cxxanyargs::rb_define_virtual_variable
void rb_define_virtual_variable(const char *q, type *w, void_type *e)
Define a function-backended global variable.
Definition: cxxanyargs.hpp:59
VTM_ISDST_INITVAL
#define VTM_ISDST_INITVAL
Definition: time.c:57
idName
@ idName
Definition: rb_mjit_min_header-2.7.0.h:8738
NUM2INT
#define NUM2INT(x)
Definition: ruby.h:715
T_STRUCT
#define T_STRUCT
Definition: ruby.h:532
Qnil
#define Qnil
Definition: ruby.h:469
rb_fstring_lit
#define rb_fstring_lit(str)
Definition: internal.h:2123
DIV
#define DIV(n, d)
Definition: time.c:54
arg_range_check
#define arg_range_check(v)
rb_absint_size
size_t rb_absint_size(VALUE val, int *nlz_bits_ret)
Definition: bignum.c:3247
tm::tm_gmtoff
long tm_gmtoff
Definition: rb_mjit_min_header-2.7.0.h:1943
rb_int_plus
VALUE rb_int_plus(VALUE x, VALUE y)
Definition: numeric.c:3610
RB_GC_GUARD
#define RB_GC_GUARD(v)
Definition: ruby.h:585
WIDEVAL_GET
#define WIDEVAL_GET(w)
Definition: time.c:250
numberof
#define numberof(array)
Definition: etc.c:618
rb_rescue
VALUE rb_rescue(VALUE(*b_proc)(VALUE), VALUE data1, VALUE(*r_proc)(VALUE, VALUE), VALUE data2)
An equivalent of rescue clause.
Definition: eval.c:1046
gettimeofday
int gettimeofday(struct timeval *, struct timezone *)
Definition: win32.c:4598
RSTRING_LEN
#define RSTRING_LEN(str)
Definition: ruby.h:1005
PACKED_STRUCT_UNALIGNED
PACKED_STRUCT_UNALIGNED(struct time_object { wideval_t timew;struct vtm vtm;unsigned int tzmode:3;unsigned int tm_got:1;})
D29
#define D29
Definition: time.c:811
rb_define_private_method
void rb_define_private_method(VALUE klass, const char *name, VALUE(*func)(ANYARGS), int argc)
Definition: class.c:1569
tm::tm_zone
const char * tm_zone
Definition: rb_mjit_min_header-2.7.0.h:1944
rb_enc_str_new
VALUE rb_enc_str_new(const char *, long, rb_encoding *)
Definition: string.c:796
idTo_r
@ idTo_r
Definition: rb_mjit_min_header-2.7.0.h:8724
rb_define_alloc_func
void rb_define_alloc_func(VALUE, rb_alloc_func_t)
RTEST
#define RTEST(v)
Definition: ruby.h:481
dup
int dup(int __fildes)
TIMET_MAX
#define TIMET_MAX
Definition: rb_mjit_min_header-2.7.0.h:6640
RSTRUCT_CONST_PTR
#define RSTRUCT_CONST_PTR(st)
Definition: internal.h:962
RB_FLOAT_TYPE_P
#define RB_FLOAT_TYPE_P(obj)
Definition: ruby.h:556
TYPEOF_TIMEVAL_TV_USEC
#define TYPEOF_TIMEVAL_TV_USEC
Definition: timev.h:31
rb_usascii_encoding
rb_encoding * rb_usascii_encoding(void)
Definition: encoding.c:1340
rb_int_minus
VALUE rb_int_minus(VALUE x, VALUE y)
Definition: numeric.c:3649
TZMODE_FIXOFF_P
#define TZMODE_FIXOFF_P(tobj)
Definition: time.c:1743
name
const char * name
Definition: nkf.c:208