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