1/**********************************************************************
2
3  time.c -
4
5  $Author: usa $
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
36static ID id_divmod, id_mul, id_submicro, id_nano_num, id_nano_den, id_offset, id_zone;
37static 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
44static int
45eq(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
53static int
54cmp(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
72static VALUE
73add(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
84static VALUE
85sub(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)
97static int
98long_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
140static VALUE
141mul(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
162static VALUE
163mod(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
174static VALUE
175quo(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
198static void
199divmodv(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
286static inline VALUE
287w2v(wideval_t w)
288{
289#if WIDEVALUE_IS_WIDER
290    if (FIXWV_P(w))
291        return INT64toNUM(FIXWV2WINT(w));
292    return (VALUE)WIDEVAL_GET(w);
293#else
294    return WIDEVAL_GET(w);
295#endif
296}
297
298#if WIDEVALUE_IS_WIDER
299static int
300bdigit_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
326static VALUE
327rb_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
343static VALUE
344rb_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
362static wideval_t
363v2w_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
391static inline wideval_t
392v2w(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
406static int
407weq(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
419static int
420wcmp(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
446static wideval_t
447wadd(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
462static wideval_t
463wsub(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
478static int
479wi_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
520static wideval_t
521wmul(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
540static wideval_t
541wquo(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
569static void
570wdivmod(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
638static void
639wmuldivmod(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
649static wideval_t
650wdiv(wideval_t wx, wideval_t wy)
651{
652    wideval_t q, r;
653    wdivmod(wx, wy, &q, &r);
654    return q;
655}
656
657static wideval_t
658wmod(wideval_t wx, wideval_t wy)
659{
660    wideval_t q, r;
661    wdivmod(wx, wy, &q, &r);
662    return r;
663}
664
665static VALUE
666num_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
732typedef unsigned long unsigned_time_t;
733#elif SIZEOF_TIME_T == SIZEOF_INT
734typedef unsigned int unsigned_time_t;
735#elif SIZEOF_TIME_T == SIZEOF_LONG_LONG
736typedef 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
744static wideval_t
745rb_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
755static wideval_t
756rb_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
772static VALUE
773rb_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
793static void
794split_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
802static wideval_t
803timet2wv(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
823static time_t
824wv2timet(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
846VALUE rb_cTime;
847static VALUE time_utc_offset _((VALUE));
848
849static int obj2int(VALUE obj);
850static VALUE obj2vint(VALUE obj);
851static int month_arg(VALUE arg);
852static VALUE validate_utc_offset(VALUE utc_offset);
853static VALUE validate_zone_name(VALUE zone_name);
854static void validate_vtm(struct vtm *vtm);
855static int obj2subsecx(VALUE obj, VALUE *subsecx);
856
857static VALUE time_gmtime(VALUE);
858static VALUE time_localtime(VALUE);
859static VALUE time_fixoff(VALUE);
860
861static time_t timegm_noleapsecond(struct tm *tm);
862static int tmcmp(struct tm *a, struct tm *b);
863static int vtmcmp(struct vtm *a, struct vtm *b);
864static const char *find_time_t(struct tm *tptr, int utc_p, time_t *tp);
865
866static struct vtm *localtimew(wideval_t timew, struct vtm *result);
867
868static 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
875static inline struct tm *
876rb_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
883static inline struct tm *
884rb_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
892static struct tm *
893rb_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)
921static struct tm *
922rb_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
938static 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};
953static 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
969static const int common_year_days_in_month[] = {
970    31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
971};
972static const int leap_year_days_in_month[] = {
973    31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
974};
975
976static int
977calc_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
990static wideval_t
991timegmw_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
1031static st_table *zone_table;
1032
1033static int
1034zone_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
1048static const char *
1049zone_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
1058static void
1059gmtimew_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
1164static struct tm *
1165gmtime_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
1288static long this_year = 0;
1289static time_t known_leap_seconds_limit;
1290static int number_of_leap_seconds_known;
1291
1292static void
1293init_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
1334static wideval_t
1335timegmw(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
1369static struct vtm *
1370gmtimew(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
1412static 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 */
1447static 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 */
1488static 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
1493static int
1494calc_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
1507static VALUE
1508guess_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
1593static VALUE
1594small_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
1611static wideval_t
1612timelocalw(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
1693static struct tm *
1694localtime_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
1744static int
1745timew_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
1777static struct vtm *
1778localtimew(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
1826struct 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
1856static 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
1864static void
1865time_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
1876static void
1877time_free(void *tobj)
1878{
1879    if (tobj) xfree(tobj);
1880}
1881
1882static size_t
1883time_memsize(const void *tobj)
1884{
1885    return tobj ? sizeof(struct time_object) : 0;
1886}
1887
1888static const rb_data_type_t time_data_type = {
1889    "time",
1890    {time_mark, time_free, time_memsize,},
1891};
1892
1893static VALUE
1894time_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
1907static struct time_object *
1908get_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, rb_obj_class(obj));
1914    }
1915    return tobj;
1916}
1917
1918static struct time_object *
1919get_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, rb_obj_class(obj));
1925    }
1926    return tobj;
1927}
1928
1929static void
1930time_modify(VALUE time)
1931{
1932    rb_check_frozen(time);
1933    rb_check_trusted(time);
1934}
1935
1936static wideval_t
1937timespec2timew(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
1947static struct timespec
1948timew2timespec(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
1962static struct timespec *
1963timew2timespec_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
1987static VALUE
1988time_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
2017static VALUE
2018time_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
2032static void
2033vtm_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
2160static VALUE
2161utc_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
2196static VALUE
2197time_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
2301static VALUE
2302time_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
2310static void
2311time_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
2340static wideval_t
2341nsec2timew(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
2350static VALUE
2351time_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
2363VALUE
2364rb_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
2389VALUE
2390rb_time_nano_new(time_t sec, long nsec)
2391{
2392    return time_new_timew(rb_cTime, nsec2timew(sec, nsec));
2393}
2394
2395VALUE
2396rb_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
2410static struct timespec
2411time_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
2478static struct timeval
2479time_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
2491struct timeval
2492rb_time_interval(VALUE num)
2493{
2494    return time_timeval(num, TRUE);
2495}
2496
2497struct timeval
2498rb_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
2514struct timespec
2515rb_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
2537static VALUE
2538time_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
2566static VALUE
2567time_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
2593static const char months[][4] = {
2594    "jan", "feb", "mar", "apr", "may", "jun",
2595    "jul", "aug", "sep", "oct", "nov", "dec",
2596};
2597
2598static int
2599obj2int(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
2608static VALUE
2609obj2vint(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
2621static int
2622obj2subsecx(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
2637static long
2638usec2subsecx(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
2647static int
2648month_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
2676static VALUE
2677validate_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
2684static VALUE
2685validate_zone_name(VALUE zone_name)
2686{
2687    StringValueCStr(zone_name);
2688    return zone_name;
2689}
2690
2691static void
2692validate_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
2705static void
2706time_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
2773static int
2774leap_year_p(long y)
2775{
2776    return ((y % 4 == 0) && (y % 100 != 0)) || (y % 400 == 0);
2777}
2778
2779static time_t
2780timegm_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++,
2816static unsigned long long find_time_numguess;
2817
2818static 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
2826static const char *
2827find_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
3096static int
3097vtmcmp(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
3117static int
3118tmcmp(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
3136static VALUE
3137time_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 */
3183static VALUE
3184time_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
3214static VALUE
3215time_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
3233static VALUE
3234time_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
3257static VALUE
3258time_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
3281static VALUE
3282time_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
3307static VALUE
3308time_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
3338static VALUE
3339time_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
3366static VALUE
3367time_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
3400static VALUE
3401time_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
3427static VALUE
3428time_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
3458static VALUE
3459time_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
3475static VALUE
3476time_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: */
3485static VALUE
3486time_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
3498static VALUE
3499time_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
3506static VALUE
3507time_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
3550static VALUE
3551time_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
3585static VALUE
3586time_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
3609static VALUE
3610time_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
3663static VALUE
3664time_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
3695static VALUE
3696time_getgmtime(VALUE time)
3697{
3698    return time_gmtime(time_dup(time));
3699}
3700
3701static VALUE
3702time_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
3709static 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
3721static VALUE
3722time_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
3743static VALUE
3744time_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
3755static VALUE
3756time_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
3787static VALUE
3788time_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
3814static VALUE
3815time_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
3844VALUE
3845rb_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
3897static VALUE
3898time_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
3948static VALUE
3949time_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
3968static VALUE
3969time_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
3988static VALUE
3989time_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
4010static VALUE
4011time_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
4032static VALUE
4033time_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
4052static VALUE
4053time_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
4080static VALUE
4081time_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
4107static VALUE
4108time_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
4123static VALUE
4124time_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
4139static VALUE
4140time_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
4155static VALUE
4156time_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
4171static VALUE
4172time_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
4187static VALUE
4188time_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
4203static VALUE
4204time_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
4219static VALUE
4220time_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
4254static VALUE
4255time_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
4277static VALUE
4278time_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
4308static VALUE
4309time_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
4341static VALUE
4342time_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
4362static size_t
4363rb_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
4409static VALUE
4410strftimev(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
4609static VALUE
4610time_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: */
4661static VALUE
4662time_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: */
4762static VALUE
4763time_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: */
4774static VALUE
4775time_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)));
4873end_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	zone = rb_str_new_frozen(zone);
4891	tobj->vtm.zone = RSTRING_PTR(zone);
4892	rb_ivar_set(time, id_zone, zone);
4893    }
4894
4895    return time;
4896}
4897
4898/* :nodoc: */
4899static VALUE
4900time_load(VALUE klass, VALUE str)
4901{
4902    VALUE time = time_s_alloc(klass);
4903
4904    time_mload(time, str);
4905    return time;
4906}
4907
4908/*
4909 *  Time is an abstraction of dates and times. Time is stored internally as
4910 *  the number of seconds with fraction since the _Epoch_, January 1, 1970
4911 *  00:00 UTC. Also see the library module Date. The Time class treats GMT
4912 *  (Greenwich Mean Time) and UTC (Coordinated Universal Time) as equivalent.
4913 *  GMT is the older way of referring to these baseline times but persists in
4914 *  the names of calls on POSIX systems.
4915 *
4916 *  All times may have fraction. Be aware of this fact when comparing times
4917 *  with each other -- times that are apparently equal when displayed may be
4918 *  different when compared.
4919 *
4920 *  Since Ruby 1.9.2, Time implementation uses a signed 63 bit integer,
4921 *  Bignum or Rational.
4922 *  The integer is a number of nanoseconds since the _Epoch_ which can
4923 *  represent 1823-11-12 to 2116-02-20.
4924 *  When Bignum or Rational is used (before 1823, after 2116, under
4925 *  nanosecond), Time works slower as when integer is used.
4926 *
4927 *  = Examples
4928 *
4929 *  All of these examples were done using the EST timezone which is GMT-5.
4930 *
4931 *  == Creating a new Time instance
4932 *
4933 *  You can create a new instance of Time with Time::new. This will use the
4934 *  current system time. Time::now is an alias for this. You can also
4935 *  pass parts of the time to Time::new such as year, month, minute, etc. When
4936 *  you want to construct a time this way you must pass at least a year. If you
4937 *  pass the year with nothing else time will default to January 1 of that year
4938 *  at 00:00:00 with the current system timezone. Here are some examples:
4939 *
4940 *    Time.new(2002)         #=> 2002-01-01 00:00:00 -0500
4941 *    Time.new(2002, 10)     #=> 2002-10-01 00:00:00 -0500
4942 *    Time.new(2002, 10, 31) #=> 2002-10-31 00:00:00 -0500
4943 *    Time.new(2002, 10, 31, 2, 2, 2, "+02:00") #=> 2002-10-31 02:02:02 -0200
4944 *
4945 *  You can also use #gm, #local and
4946 *  #utc to infer GMT, local and UTC timezones instead of using
4947 *  the current system setting.
4948 *
4949 *  You can also create a new time using Time::at which takes the number of
4950 *  seconds (or fraction of seconds) since the {Unix
4951 *  Epoch}[http://en.wikipedia.org/wiki/Unix_time].
4952 *
4953 *    Time.at(628232400) #=> 1989-11-28 00:00:00 -0500
4954 *
4955 *  == Working with an instance of Time
4956 *
4957 *  Once you have an instance of Time there is a multitude of things you can
4958 *  do with it. Below are some examples. For all of the following examples, we
4959 *  will work on the assumption that you have done the following:
4960 *
4961 *    t = Time.new(1993, 02, 24, 12, 0, 0, "+09:00")
4962 *
4963 *  Was that a monday?
4964 *
4965 *    t.monday? #=> false
4966 *
4967 *  What year was that again?
4968 *
4969 *    t.year #=> 1993
4970 *
4971 *  Was is daylight savings at the time?
4972 *
4973 *    t.dst? #=> false
4974 *
4975 *  What's the day a year later?
4976 *
4977 *    t + (60*60*24*365) #=> 1994-02-24 12:00:00 +0900
4978 *
4979 *  How many seconds was that since the Unix Epoch?
4980 *
4981 *    t.to_i #=> 730522800
4982 *
4983 *  You can also do standard functions like compare two times.
4984 *
4985 *    t1 = Time.new(2010)
4986 *    t2 = Time.new(2011)
4987 *
4988 *    t1 == t2 #=> false
4989 *    t1 == t1 #=> true
4990 *    t1 <  t2 #=> true
4991 *    t1 >  t2 #=> false
4992 *
4993 *    Time.new(2010,10,31).between?(t1, t2) #=> true
4994 */
4995
4996void
4997Init_Time(void)
4998{
4999#undef rb_intern
5000#define rb_intern(str) rb_intern_const(str)
5001
5002    id_eq = rb_intern("==");
5003    id_ne = rb_intern("!=");
5004    id_quo = rb_intern("quo");
5005    id_div = rb_intern("div");
5006    id_cmp = rb_intern("<=>");
5007    id_lshift = rb_intern("<<");
5008    id_divmod = rb_intern("divmod");
5009    id_mul = rb_intern("*");
5010    id_submicro = rb_intern("submicro");
5011    id_nano_num = rb_intern("nano_num");
5012    id_nano_den = rb_intern("nano_den");
5013    id_offset = rb_intern("offset");
5014    id_zone = rb_intern("zone");
5015
5016    rb_cTime = rb_define_class("Time", rb_cObject);
5017    rb_include_module(rb_cTime, rb_mComparable);
5018
5019    rb_define_alloc_func(rb_cTime, time_s_alloc);
5020    rb_define_singleton_method(rb_cTime, "now", time_s_now, 0);
5021    rb_define_singleton_method(rb_cTime, "at", time_s_at, -1);
5022    rb_define_singleton_method(rb_cTime, "utc", time_s_mkutc, -1);
5023    rb_define_singleton_method(rb_cTime, "gm", time_s_mkutc, -1);
5024    rb_define_singleton_method(rb_cTime, "local", time_s_mktime, -1);
5025    rb_define_singleton_method(rb_cTime, "mktime", time_s_mktime, -1);
5026
5027    rb_define_method(rb_cTime, "to_i", time_to_i, 0);
5028    rb_define_method(rb_cTime, "to_f", time_to_f, 0);
5029    rb_define_method(rb_cTime, "to_r", time_to_r, 0);
5030    rb_define_method(rb_cTime, "<=>", time_cmp, 1);
5031    rb_define_method(rb_cTime, "eql?", time_eql, 1);
5032    rb_define_method(rb_cTime, "hash", time_hash, 0);
5033    rb_define_method(rb_cTime, "initialize", time_init, -1);
5034    rb_define_method(rb_cTime, "initialize_copy", time_init_copy, 1);
5035
5036    rb_define_method(rb_cTime, "localtime", time_localtime_m, -1);
5037    rb_define_method(rb_cTime, "gmtime", time_gmtime, 0);
5038    rb_define_method(rb_cTime, "utc", time_gmtime, 0);
5039    rb_define_method(rb_cTime, "getlocal", time_getlocaltime, -1);
5040    rb_define_method(rb_cTime, "getgm", time_getgmtime, 0);
5041    rb_define_method(rb_cTime, "getutc", time_getgmtime, 0);
5042
5043    rb_define_method(rb_cTime, "ctime", time_asctime, 0);
5044    rb_define_method(rb_cTime, "asctime", time_asctime, 0);
5045    rb_define_method(rb_cTime, "to_s", time_to_s, 0);
5046    rb_define_method(rb_cTime, "inspect", time_to_s, 0);
5047    rb_define_method(rb_cTime, "to_a", time_to_a, 0);
5048
5049    rb_define_method(rb_cTime, "+", time_plus, 1);
5050    rb_define_method(rb_cTime, "-", time_minus, 1);
5051
5052    rb_define_method(rb_cTime, "succ", time_succ, 0);
5053    rb_define_method(rb_cTime, "round", time_round, -1);
5054
5055    rb_define_method(rb_cTime, "sec", time_sec, 0);
5056    rb_define_method(rb_cTime, "min", time_min, 0);
5057    rb_define_method(rb_cTime, "hour", time_hour, 0);
5058    rb_define_method(rb_cTime, "mday", time_mday, 0);
5059    rb_define_method(rb_cTime, "day", time_mday, 0);
5060    rb_define_method(rb_cTime, "mon", time_mon, 0);
5061    rb_define_method(rb_cTime, "month", time_mon, 0);
5062    rb_define_method(rb_cTime, "year", time_year, 0);
5063    rb_define_method(rb_cTime, "wday", time_wday, 0);
5064    rb_define_method(rb_cTime, "yday", time_yday, 0);
5065    rb_define_method(rb_cTime, "isdst", time_isdst, 0);
5066    rb_define_method(rb_cTime, "dst?", time_isdst, 0);
5067    rb_define_method(rb_cTime, "zone", time_zone, 0);
5068    rb_define_method(rb_cTime, "gmtoff", time_utc_offset, 0);
5069    rb_define_method(rb_cTime, "gmt_offset", time_utc_offset, 0);
5070    rb_define_method(rb_cTime, "utc_offset", time_utc_offset, 0);
5071
5072    rb_define_method(rb_cTime, "utc?", time_utc_p, 0);
5073    rb_define_method(rb_cTime, "gmt?", time_utc_p, 0);
5074
5075    rb_define_method(rb_cTime, "sunday?", time_sunday, 0);
5076    rb_define_method(rb_cTime, "monday?", time_monday, 0);
5077    rb_define_method(rb_cTime, "tuesday?", time_tuesday, 0);
5078    rb_define_method(rb_cTime, "wednesday?", time_wednesday, 0);
5079    rb_define_method(rb_cTime, "thursday?", time_thursday, 0);
5080    rb_define_method(rb_cTime, "friday?", time_friday, 0);
5081    rb_define_method(rb_cTime, "saturday?", time_saturday, 0);
5082
5083    rb_define_method(rb_cTime, "tv_sec", time_to_i, 0);
5084    rb_define_method(rb_cTime, "tv_usec", time_usec, 0);
5085    rb_define_method(rb_cTime, "usec", time_usec, 0);
5086    rb_define_method(rb_cTime, "tv_nsec", time_nsec, 0);
5087    rb_define_method(rb_cTime, "nsec", time_nsec, 0);
5088    rb_define_method(rb_cTime, "subsec", time_subsec, 0);
5089
5090    rb_define_method(rb_cTime, "strftime", time_strftime, 1);
5091
5092    /* methods for marshaling */
5093    rb_define_private_method(rb_cTime, "_dump", time_dump, -1);
5094    rb_define_private_method(rb_singleton_class(rb_cTime), "_load", time_load, 1);
5095#if 0
5096    /* Time will support marshal_dump and marshal_load in the future (1.9 maybe) */
5097    rb_define_private_method(rb_cTime, "marshal_dump", time_mdump, 0);
5098    rb_define_private_method(rb_cTime, "marshal_load", time_mload, 1);
5099#endif
5100
5101#ifdef DEBUG_FIND_TIME_NUMGUESS
5102    rb_define_virtual_variable("$find_time_numguess", find_time_numguess_getter, NULL);
5103#endif
5104}
5105