Ruby 2.0.0p353(2013-11-22revision43784)

- Main Page
- Modules
- Data Structures
- Files

- File List
- Globals

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