* transcode.c (trans_open_i): check the result of rb_transcoding_open.
[ruby-svn.git] / time.c
blobfdf465ceb85247e8ff5d39d562f7def1bba2c742
1 /**********************************************************************
3 time.c -
5 $Author$
6 created at: Tue Dec 28 14:31:59 JST 1993
8 Copyright (C) 1993-2007 Yukihiro Matsumoto
10 **********************************************************************/
12 #include "ruby/ruby.h"
13 #include <sys/types.h>
14 #include <time.h>
15 #include <errno.h>
16 #include "ruby/encoding.h"
18 #ifdef HAVE_UNISTD_H
19 #include <unistd.h>
20 #endif
22 #include <math.h>
24 VALUE rb_cTime;
25 static VALUE time_utc_offset _((VALUE));
27 static ID id_divmod, id_mul, id_submicro;
29 struct time_object {
30 struct timespec ts;
31 struct tm tm;
32 int gmt;
33 int tm_got;
36 #define GetTimeval(obj, tobj) \
37 Data_Get_Struct(obj, struct time_object, tobj)
39 static void
40 time_free(void *tobj)
42 if (tobj) xfree(tobj);
45 static VALUE
46 time_s_alloc(VALUE klass)
48 VALUE obj;
49 struct time_object *tobj;
51 obj = Data_Make_Struct(klass, struct time_object, 0, time_free, tobj);
52 tobj->tm_got=0;
53 tobj->ts.tv_sec = 0;
54 tobj->ts.tv_nsec = 0;
56 return obj;
59 static void
60 time_modify(VALUE time)
62 rb_check_frozen(time);
63 if (!OBJ_TAINTED(time) && rb_safe_level() >= 4)
64 rb_raise(rb_eSecurityError, "Insecure: can't modify Time");
68 * Document-method: now
70 * Synonym for <code>Time.new</code>. Returns a +Time+ object
71 * initialized to the current system time.
75 * call-seq:
76 * Time.new -> time
78 * Returns a <code>Time</code> object initialized to the current system
79 * time. <b>Note:</b> The object created will be created using the
80 * resolution available on your system clock, and so may include
81 * fractional seconds.
83 * a = Time.new #=> 2007-11-19 07:50:02 -0600
84 * b = Time.new #=> 2007-11-19 07:50:02 -0600
85 * a == b #=> false
86 * "%.6f" % a.to_f #=> "1195480202.282373"
87 * "%.6f" % b.to_f #=> "1195480202.283415"
91 static VALUE
92 time_init(VALUE time)
94 struct time_object *tobj;
96 time_modify(time);
97 GetTimeval(time, tobj);
98 tobj->tm_got=0;
99 tobj->ts.tv_sec = 0;
100 tobj->ts.tv_nsec = 0;
101 #ifdef HAVE_CLOCK_GETTIME
102 if (clock_gettime(CLOCK_REALTIME, &tobj->ts) == -1) {
103 rb_sys_fail("clock_gettime");
105 #else
107 struct timeval tv;
108 if (gettimeofday(&tv, 0) < 0) {
109 rb_sys_fail("gettimeofday");
111 tobj->ts.tv_sec = tv.tv_sec;
112 tobj->ts.tv_nsec = tv.tv_usec * 1000;
114 #endif
116 return time;
119 #define NDIV(x,y) (-(-((x)+1)/(y))-1)
120 #define NMOD(x,y) ((y)-(-((x)+1)%(y))-1)
122 static void
123 time_overflow_p(time_t *secp, long *nsecp)
125 time_t tmp, sec = *secp;
126 long nsec = *nsecp;
128 if (nsec >= 1000000000) { /* nsec positive overflow */
129 tmp = sec + nsec / 1000000000;
130 nsec %= 1000000000;
131 if (sec > 0 && tmp < 0) {
132 rb_raise(rb_eRangeError, "out of Time range");
134 sec = tmp;
136 if (nsec < 0) { /* nsec negative overflow */
137 tmp = sec + NDIV(nsec,1000000000); /* negative div */
138 nsec = NMOD(nsec,1000000000); /* negative mod */
139 if (sec < 0 && tmp > 0) {
140 rb_raise(rb_eRangeError, "out of Time range");
142 sec = tmp;
144 #ifndef NEGATIVE_TIME_T
145 if (sec < 0)
146 rb_raise(rb_eArgError, "time must be positive");
147 #endif
148 *secp = sec;
149 *nsecp = nsec;
152 static VALUE
153 time_new_internal(VALUE klass, time_t sec, long nsec)
155 VALUE time = time_s_alloc(klass);
156 struct time_object *tobj;
158 GetTimeval(time, tobj);
159 time_overflow_p(&sec, &nsec);
160 tobj->ts.tv_sec = sec;
161 tobj->ts.tv_nsec = nsec;
163 return time;
166 VALUE
167 rb_time_new(time_t sec, long usec)
169 return time_new_internal(rb_cTime, sec, usec * 1000);
172 VALUE
173 rb_time_nano_new(time_t sec, long nsec)
175 return time_new_internal(rb_cTime, sec, nsec);
178 static struct timespec
179 time_timespec(VALUE num, int interval)
181 struct timespec t;
182 const char *tstr = interval ? "time interval" : "time";
183 VALUE i, f, ary;
185 #ifndef NEGATIVE_TIME_T
186 interval = 1;
187 #endif
189 switch (TYPE(num)) {
190 case T_FIXNUM:
191 t.tv_sec = FIX2LONG(num);
192 if (interval && t.tv_sec < 0)
193 rb_raise(rb_eArgError, "%s must be positive", tstr);
194 t.tv_nsec = 0;
195 break;
197 case T_FLOAT:
198 if (interval && RFLOAT_VALUE(num) < 0.0)
199 rb_raise(rb_eArgError, "%s must be positive", tstr);
200 else {
201 double f, d;
203 d = modf(RFLOAT_VALUE(num), &f);
204 if (d < 0) {
205 d += 1;
206 f -= 1;
208 t.tv_sec = (time_t)f;
209 if (f != t.tv_sec) {
210 rb_raise(rb_eRangeError, "%f out of Time range", RFLOAT_VALUE(num));
212 t.tv_nsec = (long)(d*1e9+0.5);
214 break;
216 case T_BIGNUM:
217 t.tv_sec = NUM2LONG(num);
218 if (interval && t.tv_sec < 0)
219 rb_raise(rb_eArgError, "%s must be positive", tstr);
220 t.tv_nsec = 0;
221 break;
223 default:
224 if (rb_respond_to(num, id_divmod)) {
225 ary = rb_check_array_type(rb_funcall(num, id_divmod, 1, INT2FIX(1)));
226 if (NIL_P(ary)) {
227 goto typeerror;
229 i = rb_ary_entry(ary, 0);
230 f = rb_ary_entry(ary, 1);
231 t.tv_sec = NUM2LONG(i);
232 if (interval && t.tv_sec < 0)
233 rb_raise(rb_eArgError, "%s must be positive", tstr);
234 f = rb_funcall(f, id_mul, 1, INT2FIX(1000000000));
235 t.tv_nsec = NUM2LONG(f);
237 else {
238 typeerror:
239 rb_raise(rb_eTypeError, "can't convert %s into %s",
240 rb_obj_classname(num), tstr);
242 break;
244 return t;
247 static struct timeval
248 time_timeval(VALUE num, int interval)
250 struct timespec ts;
251 struct timeval tv;
253 ts = time_timespec(num, interval);
254 tv.tv_sec = ts.tv_sec;
255 tv.tv_usec = ts.tv_nsec / 1000;
257 return tv;
260 struct timeval
261 rb_time_interval(VALUE num)
263 return time_timeval(num, Qtrue);
266 struct timeval
267 rb_time_timeval(VALUE time)
269 struct time_object *tobj;
270 struct timeval t;
272 if (TYPE(time) == T_DATA && RDATA(time)->dfree == time_free) {
273 GetTimeval(time, tobj);
274 t.tv_sec = tobj->ts.tv_sec;
275 t.tv_usec = tobj->ts.tv_nsec / 1000;
276 return t;
278 return time_timeval(time, Qfalse);
281 struct timespec
282 rb_time_timespec(VALUE time)
284 struct time_object *tobj;
285 struct timespec t;
287 if (TYPE(time) == T_DATA && RDATA(time)->dfree == time_free) {
288 GetTimeval(time, tobj);
289 t = tobj->ts;
290 return t;
292 return time_timespec(time, Qfalse);
296 * call-seq:
297 * Time.at(time) => time
298 * Time.at(seconds_with_frac) => time
299 * Time.at(seconds, microseconds_with_frac) => time
301 * Creates a new time object with the value given by <i>time</i>,
302 * the given number of <i>seconds_with_frac</i>, or
303 * <i>seconds</i> and <i>microseconds_with_frac</i> from the Epoch.
304 * <i>seconds_with_frac</i> and <i>microseconds_with_frac</i>
305 * can be Integer, Float, Rational, or other Numeric.
306 * non-portable feature allows the offset to be negative on some systems.
308 * Time.at(0) #=> 1969-12-31 18:00:00 -0600
309 * Time.at(Time.at(0)) #=> 1969-12-31 18:00:00 -0600
310 * Time.at(946702800) #=> 1999-12-31 23:00:00 -0600
311 * Time.at(-284061600) #=> 1960-12-31 00:00:00 -0600
312 * Time.at(946684800.2).usec #=> 200000
313 * Time.at(946684800, 123456.789).nsec #=> 123456789
316 static VALUE
317 time_s_at(int argc, VALUE *argv, VALUE klass)
319 struct timespec ts;
320 VALUE time, t;
322 if (rb_scan_args(argc, argv, "11", &time, &t) == 2) {
323 ts.tv_sec = NUM2LONG(time);
324 ts.tv_nsec = NUM2LONG(rb_funcall(t, id_mul, 1, INT2FIX(1000)));
326 else {
327 ts = rb_time_timespec(time);
329 t = time_new_internal(klass, ts.tv_sec, ts.tv_nsec);
330 if (TYPE(time) == T_DATA && RDATA(time)->dfree == time_free) {
331 struct time_object *tobj, *tobj2;
333 GetTimeval(time, tobj);
334 GetTimeval(t, tobj2);
335 tobj2->gmt = tobj->gmt;
337 return t;
340 static const char months[][4] = {
341 "jan", "feb", "mar", "apr", "may", "jun",
342 "jul", "aug", "sep", "oct", "nov", "dec",
345 static long
346 obj2long(VALUE obj)
348 if (TYPE(obj) == T_STRING) {
349 obj = rb_str_to_inum(obj, 10, Qfalse);
352 return NUM2LONG(obj);
355 static long
356 obj2nsec(VALUE obj, long *nsec)
358 struct timespec ts;
360 if (TYPE(obj) == T_STRING) {
361 obj = rb_str_to_inum(obj, 10, Qfalse);
362 *nsec = 0;
363 return NUM2LONG(obj);
366 ts = time_timespec(obj, 1);
367 *nsec = ts.tv_nsec;
368 return ts.tv_sec;
371 static long
372 obj2long1000(VALUE obj)
374 if (TYPE(obj) == T_STRING) {
375 obj = rb_str_to_inum(obj, 10, Qfalse);
376 return NUM2LONG(obj) * 1000;
379 return NUM2LONG(rb_funcall(obj, id_mul, 1, INT2FIX(1000)));
382 static void
383 time_arg(int argc, VALUE *argv, struct tm *tm, long *nsec)
385 VALUE v[8];
386 int i;
387 long year;
389 MEMZERO(tm, struct tm, 1);
390 *nsec = 0;
391 if (argc == 10) {
392 v[0] = argv[5];
393 v[1] = argv[4];
394 v[2] = argv[3];
395 v[3] = argv[2];
396 v[4] = argv[1];
397 v[5] = argv[0];
398 v[6] = Qnil;
399 tm->tm_isdst = RTEST(argv[8]) ? 1 : 0;
401 else {
402 rb_scan_args(argc, argv, "17", &v[0],&v[1],&v[2],&v[3],&v[4],&v[5],&v[6],&v[7]);
403 /* v[6] may be usec or zone (parsedate) */
404 /* v[7] is wday (parsedate; ignored) */
405 tm->tm_wday = -1;
406 tm->tm_isdst = -1;
409 year = obj2long(v[0]);
411 if (0 <= year && year < 39) {
412 rb_warning("2 digits year is used: %ld", year);
413 year += 100;
415 else if (69 <= year && year < 139) {
416 rb_warning("2 or 3 digits year is used: %ld", year);
418 else {
419 year -= 1900;
422 tm->tm_year = year;
424 if (NIL_P(v[1])) {
425 tm->tm_mon = 0;
427 else {
428 VALUE s = rb_check_string_type(v[1]);
429 if (!NIL_P(s)) {
430 tm->tm_mon = -1;
431 for (i=0; i<12; i++) {
432 if (RSTRING_LEN(s) == 3 &&
433 STRCASECMP(months[i], RSTRING_PTR(s)) == 0) {
434 tm->tm_mon = i;
435 break;
438 if (tm->tm_mon == -1) {
439 char c = RSTRING_PTR(s)[0];
441 if ('0' <= c && c <= '9') {
442 tm->tm_mon = obj2long(s)-1;
446 else {
447 tm->tm_mon = obj2long(v[1])-1;
450 if (NIL_P(v[2])) {
451 tm->tm_mday = 1;
453 else {
454 tm->tm_mday = obj2long(v[2]);
456 tm->tm_hour = NIL_P(v[3])?0:obj2long(v[3]);
457 tm->tm_min = NIL_P(v[4])?0:obj2long(v[4]);
458 if (!NIL_P(v[6]) && argc == 7) {
459 tm->tm_sec = NIL_P(v[5])?0:obj2long(v[5]);
460 *nsec = obj2long1000(v[6]);
462 else {
463 /* when argc == 8, v[6] is timezone, but ignored */
464 tm->tm_sec = NIL_P(v[5])?0:obj2nsec(v[5], nsec);
467 /* value validation */
468 if (
469 tm->tm_year != year ||
470 #ifndef NEGATIVE_TIME_T
471 tm->tm_year < 69 ||
472 #endif
473 tm->tm_mon < 0 || tm->tm_mon > 11
474 || tm->tm_mday < 1 || tm->tm_mday > 31
475 || tm->tm_hour < 0 || tm->tm_hour > 24
476 || (tm->tm_hour == 24 && (tm->tm_min > 0 || tm->tm_sec > 0))
477 || tm->tm_min < 0 || tm->tm_min > 59
478 || tm->tm_sec < 0 || tm->tm_sec > 60)
479 rb_raise(rb_eArgError, "argument out of range");
482 static VALUE time_gmtime(VALUE);
483 static VALUE time_localtime(VALUE);
484 static VALUE time_get_tm(VALUE, int);
486 static int
487 leap_year_p(long y)
489 return ((y % 4 == 0) && (y % 100 != 0)) || (y % 400 == 0);
492 #define DIV(n,d) ((n)<0 ? NDIV((n),(d)) : (n)/(d))
494 static time_t
495 timegm_noleapsecond(struct tm *tm)
497 static const int common_year_yday_offset[] = {
499 -1 + 31,
500 -1 + 31 + 28,
501 -1 + 31 + 28 + 31,
502 -1 + 31 + 28 + 31 + 30,
503 -1 + 31 + 28 + 31 + 30 + 31,
504 -1 + 31 + 28 + 31 + 30 + 31 + 30,
505 -1 + 31 + 28 + 31 + 30 + 31 + 30 + 31,
506 -1 + 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31,
507 -1 + 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30,
508 -1 + 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31,
509 -1 + 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30
510 /* 1 2 3 4 5 6 7 8 9 10 11 */
512 static const int leap_year_yday_offset[] = {
514 -1 + 31,
515 -1 + 31 + 29,
516 -1 + 31 + 29 + 31,
517 -1 + 31 + 29 + 31 + 30,
518 -1 + 31 + 29 + 31 + 30 + 31,
519 -1 + 31 + 29 + 31 + 30 + 31 + 30,
520 -1 + 31 + 29 + 31 + 30 + 31 + 30 + 31,
521 -1 + 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31,
522 -1 + 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30,
523 -1 + 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31,
524 -1 + 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30
525 /* 1 2 3 4 5 6 7 8 9 10 11 */
528 long tm_year = tm->tm_year;
529 int tm_yday = tm->tm_mday;
530 if (leap_year_p(tm_year + 1900))
531 tm_yday += leap_year_yday_offset[tm->tm_mon];
532 else
533 tm_yday += common_year_yday_offset[tm->tm_mon];
536 * `Seconds Since the Epoch' in SUSv3:
537 * tm_sec + tm_min*60 + tm_hour*3600 + tm_yday*86400 +
538 * (tm_year-70)*31536000 + ((tm_year-69)/4)*86400 -
539 * ((tm_year-1)/100)*86400 + ((tm_year+299)/400)*86400
541 return tm->tm_sec + tm->tm_min*60 + tm->tm_hour*3600 +
542 (time_t)(tm_yday +
543 (tm_year-70)*365 +
544 DIV(tm_year-69,4) -
545 DIV(tm_year-1,100) +
546 DIV(tm_year+299,400))*86400;
549 static int
550 tmcmp(struct tm *a, struct tm *b)
552 if (a->tm_year != b->tm_year)
553 return a->tm_year < b->tm_year ? -1 : 1;
554 else if (a->tm_mon != b->tm_mon)
555 return a->tm_mon < b->tm_mon ? -1 : 1;
556 else if (a->tm_mday != b->tm_mday)
557 return a->tm_mday < b->tm_mday ? -1 : 1;
558 else if (a->tm_hour != b->tm_hour)
559 return a->tm_hour < b->tm_hour ? -1 : 1;
560 else if (a->tm_min != b->tm_min)
561 return a->tm_min < b->tm_min ? -1 : 1;
562 else if (a->tm_sec != b->tm_sec)
563 return a->tm_sec < b->tm_sec ? -1 : 1;
564 else
565 return 0;
568 #if SIZEOF_TIME_T == SIZEOF_LONG
569 typedef unsigned long unsigned_time_t;
570 #elif SIZEOF_TIME_T == SIZEOF_INT
571 typedef unsigned int unsigned_time_t;
572 #elif SIZEOF_TIME_T == SIZEOF_LONG_LONG
573 typedef unsigned LONG_LONG unsigned_time_t;
574 #else
575 # error cannot find integer type which size is same as time_t.
576 #endif
578 static time_t
579 search_time_t(struct tm *tptr, int utc_p)
581 time_t guess, guess_lo, guess_hi;
582 struct tm *tm, tm_lo, tm_hi;
583 int d, have_guess;
584 int find_dst;
586 find_dst = 0 < tptr->tm_isdst;
588 #ifdef NEGATIVE_TIME_T
589 guess_lo = (time_t)~((unsigned_time_t)~(time_t)0 >> 1);
590 #else
591 guess_lo = 0;
592 #endif
593 guess_hi = ((time_t)-1) < ((time_t)0) ?
594 (time_t)((unsigned_time_t)~(time_t)0 >> 1) :
595 ~(time_t)0;
597 guess = timegm_noleapsecond(tptr);
598 tm = (utc_p ? gmtime : localtime)(&guess);
599 if (tm) {
600 d = tmcmp(tptr, tm);
601 if (d == 0) return guess;
602 if (d < 0) {
603 guess_hi = guess;
604 guess -= 24 * 60 * 60;
606 else {
607 guess_lo = guess;
608 guess += 24 * 60 * 60;
610 if (guess_lo < guess && guess < guess_hi &&
611 (tm = (utc_p ? gmtime : localtime)(&guess)) != NULL) {
612 d = tmcmp(tptr, tm);
613 if (d == 0) return guess;
614 if (d < 0)
615 guess_hi = guess;
616 else
617 guess_lo = guess;
621 tm = (utc_p ? gmtime : localtime)(&guess_lo);
622 if (!tm) goto error;
623 d = tmcmp(tptr, tm);
624 if (d < 0) goto out_of_range;
625 if (d == 0) return guess_lo;
626 tm_lo = *tm;
628 tm = (utc_p ? gmtime : localtime)(&guess_hi);
629 if (!tm) goto error;
630 d = tmcmp(tptr, tm);
631 if (d > 0) goto out_of_range;
632 if (d == 0) return guess_hi;
633 tm_hi = *tm;
635 have_guess = 0;
637 while (guess_lo + 1 < guess_hi) {
638 /* there is a gap between guess_lo and guess_hi. */
639 unsigned long range = 0;
640 if (!have_guess) {
641 int a, b;
643 Try precious guess by a linear interpolation at first.
644 `a' and `b' is a coefficient of guess_lo and guess_hi as:
646 guess = (guess_lo * a + guess_hi * b) / (a + b)
648 However this causes overflow in most cases, following assignment
649 is used instead:
651 guess = guess_lo / d * a + (guess_lo % d) * a / d
652 + guess_hi / d * b + (guess_hi % d) * b / d
653 where d = a + b
655 To avoid overflow in this assignment, `d' is restricted to less than
656 sqrt(2**31). By this restriction and other reasons, the guess is
657 not accurate and some error is expected. `range' approximates
658 the maximum error.
660 When these parameters are not suitable, i.e. guess is not within
661 guess_lo and guess_hi, simple guess by binary search is used.
663 range = 366 * 24 * 60 * 60;
664 a = (tm_hi.tm_year - tptr->tm_year);
665 b = (tptr->tm_year - tm_lo.tm_year);
666 /* 46000 is selected as `some big number less than sqrt(2**31)'. */
667 if (a + b <= 46000 / 12) {
668 range = 31 * 24 * 60 * 60;
669 a *= 12;
670 b *= 12;
671 a += tm_hi.tm_mon - tptr->tm_mon;
672 b += tptr->tm_mon - tm_lo.tm_mon;
673 if (a + b <= 46000 / 31) {
674 range = 24 * 60 * 60;
675 a *= 31;
676 b *= 31;
677 a += tm_hi.tm_mday - tptr->tm_mday;
678 b += tptr->tm_mday - tm_lo.tm_mday;
679 if (a + b <= 46000 / 24) {
680 range = 60 * 60;
681 a *= 24;
682 b *= 24;
683 a += tm_hi.tm_hour - tptr->tm_hour;
684 b += tptr->tm_hour - tm_lo.tm_hour;
685 if (a + b <= 46000 / 60) {
686 range = 60;
687 a *= 60;
688 b *= 60;
689 a += tm_hi.tm_min - tptr->tm_min;
690 b += tptr->tm_min - tm_lo.tm_min;
691 if (a + b <= 46000 / 60) {
692 range = 1;
693 a *= 60;
694 b *= 60;
695 a += tm_hi.tm_sec - tptr->tm_sec;
696 b += tptr->tm_sec - tm_lo.tm_sec;
702 if (a <= 0) a = 1;
703 if (b <= 0) b = 1;
704 d = a + b;
706 Although `/' and `%' may produce unexpected result with negative
707 argument, it doesn't cause serious problem because there is a
708 fail safe.
710 guess = guess_lo / d * a + (guess_lo % d) * a / d
711 + guess_hi / d * b + (guess_hi % d) * b / d;
712 have_guess = 1;
715 if (guess <= guess_lo || guess_hi <= guess) {
716 /* Precious guess is invalid. try binary search. */
717 guess = guess_lo / 2 + guess_hi / 2;
718 if (guess <= guess_lo)
719 guess = guess_lo + 1;
720 else if (guess >= guess_hi)
721 guess = guess_hi - 1;
722 range = 0;
725 tm = (utc_p ? gmtime : localtime)(&guess);
726 if (!tm) goto error;
727 have_guess = 0;
729 d = tmcmp(tptr, tm);
730 if (d < 0) {
731 guess_hi = guess;
732 tm_hi = *tm;
733 if (range) {
734 guess = guess - range;
735 range = 0;
736 if (guess_lo < guess && guess < guess_hi)
737 have_guess = 1;
740 else if (d > 0) {
741 guess_lo = guess;
742 tm_lo = *tm;
743 if (range) {
744 guess = guess + range;
745 range = 0;
746 if (guess_lo < guess && guess < guess_hi)
747 have_guess = 1;
750 else {
751 if (!utc_p) {
752 /* If localtime is nonmonotonic, another result may exist. */
753 time_t guess2;
754 if (find_dst) {
755 guess2 = guess - 2 * 60 * 60;
756 tm = localtime(&guess2);
757 if (tm) {
758 if (tptr->tm_hour != (tm->tm_hour + 2) % 24 ||
759 tptr->tm_min != tm->tm_min ||
760 tptr->tm_sec != tm->tm_sec
762 guess2 -= (tm->tm_hour - tptr->tm_hour) * 60 * 60 +
763 (tm->tm_min - tptr->tm_min) * 60 +
764 (tm->tm_sec - tptr->tm_sec);
765 if (tptr->tm_mday != tm->tm_mday)
766 guess2 += 24 * 60 * 60;
767 if (guess != guess2) {
768 tm = localtime(&guess2);
769 if (tmcmp(tptr, tm) == 0) {
770 if (guess < guess2)
771 return guess;
772 else
773 return guess2;
779 else {
780 guess2 = guess + 2 * 60 * 60;
781 tm = localtime(&guess2);
782 if (tm) {
783 if ((tptr->tm_hour + 2) % 24 != tm->tm_hour ||
784 tptr->tm_min != tm->tm_min ||
785 tptr->tm_sec != tm->tm_sec
787 guess2 -= (tm->tm_hour - tptr->tm_hour) * 60 * 60 +
788 (tm->tm_min - tptr->tm_min) * 60 +
789 (tm->tm_sec - tptr->tm_sec);
790 if (tptr->tm_mday != tm->tm_mday)
791 guess2 -= 24 * 60 * 60;
792 if (guess != guess2) {
793 tm = localtime(&guess2);
794 if (tmcmp(tptr, tm) == 0) {
795 if (guess < guess2)
796 return guess2;
797 else
798 return guess;
805 return guess;
808 /* Given argument has no corresponding time_t. Let's outerpolation. */
809 if (tm_lo.tm_year == tptr->tm_year && tm_lo.tm_mon == tptr->tm_mon) {
810 return guess_lo +
811 (tptr->tm_mday - tm_lo.tm_mday) * 24 * 60 * 60 +
812 (tptr->tm_hour - tm_lo.tm_hour) * 60 * 60 +
813 (tptr->tm_min - tm_lo.tm_min) * 60 +
814 (tptr->tm_sec - tm_lo.tm_sec);
816 else if (tm_hi.tm_year == tptr->tm_year && tm_hi.tm_mon == tptr->tm_mon) {
817 return guess_hi +
818 (tptr->tm_mday - tm_hi.tm_mday) * 24 * 60 * 60 +
819 (tptr->tm_hour - tm_hi.tm_hour) * 60 * 60 +
820 (tptr->tm_min - tm_hi.tm_min) * 60 +
821 (tptr->tm_sec - tm_hi.tm_sec);
824 out_of_range:
825 rb_raise(rb_eArgError, "time out of range");
827 error:
828 rb_raise(rb_eArgError, "gmtime/localtime error");
829 return 0; /* not reached */
832 static time_t
833 make_time_t(struct tm *tptr, int utc_p)
835 time_t t;
836 #ifdef NEGATIVE_TIME_T
837 struct tm *tmp;
838 #endif
839 struct tm buf;
840 buf = *tptr;
841 if (utc_p) {
842 #if defined(HAVE_TIMEGM)
843 if ((t = timegm(&buf)) != -1)
844 return t;
845 #ifdef NEGATIVE_TIME_T
846 if ((tmp = gmtime(&t)) &&
847 tptr->tm_year == tmp->tm_year &&
848 tptr->tm_mon == tmp->tm_mon &&
849 tptr->tm_mday == tmp->tm_mday &&
850 tptr->tm_hour == tmp->tm_hour &&
851 tptr->tm_min == tmp->tm_min &&
852 tptr->tm_sec == tmp->tm_sec
854 return t;
855 #endif
856 #endif
857 return search_time_t(&buf, utc_p);
859 else {
860 #if defined(HAVE_MKTIME)
861 if ((t = mktime(&buf)) != -1)
862 return t;
863 #ifdef NEGATIVE_TIME_T
864 if ((tmp = localtime(&t)) &&
865 tptr->tm_year == tmp->tm_year &&
866 tptr->tm_mon == tmp->tm_mon &&
867 tptr->tm_mday == tmp->tm_mday &&
868 tptr->tm_hour == tmp->tm_hour &&
869 tptr->tm_min == tmp->tm_min &&
870 tptr->tm_sec == tmp->tm_sec
872 return t;
873 #endif
874 #endif
875 return search_time_t(&buf, utc_p);
879 static VALUE
880 time_utc_or_local(int argc, VALUE *argv, int utc_p, VALUE klass)
882 struct tm tm;
883 VALUE time;
884 long nsec;
886 time_arg(argc, argv, &tm, &nsec);
887 time = time_new_internal(klass, make_time_t(&tm, utc_p), nsec);
888 if (utc_p) return time_gmtime(time);
889 return time_localtime(time);
893 * call-seq:
894 * Time.utc(year) => time
895 * Time.utc(year, month) => time
896 * Time.utc(year, month, day) => time
897 * Time.utc(year, month, day, hour) => time
898 * Time.utc(year, month, day, hour, min) => time
899 * Time.utc(year, month, day, hour, min, sec_with_frac) => time
900 * Time.utc(year, month, day, hour, min, sec, usec_with_frac) => time
901 * Time.utc(sec, min, hour, day, month, year, wday, yday, isdst, tz) => time
902 * Time.gm(year) => time
903 * Time.gm(year, month) => time
904 * Time.gm(year, month, day) => time
905 * Time.gm(year, month, day, hour) => time
906 * Time.gm(year, month, day, hour, min) => time
907 * Time.gm(year, month, day, hour, min, sec_with_frac) => time
908 * Time.gm(year, month, day, hour, min, sec, usec_with_frac) => time
909 * Time.gm(sec, min, hour, day, month, year, wday, yday, isdst, tz) => time
911 * Creates a time based on given values, interpreted as UTC (GMT). The
912 * year must be specified. Other values default to the minimum value
913 * for that field (and may be <code>nil</code> or omitted). Months may
914 * be specified by numbers from 1 to 12, or by the three-letter English
915 * month names. Hours are specified on a 24-hour clock (0..23). Raises
916 * an <code>ArgumentError</code> if any values are out of range. Will
917 * also accept ten arguments in the order output by
918 * <code>Time#to_a</code>.
919 * <i>sec_with_frac</i> and <i>usec_with_frac</i> can have a fractional part.
921 * Time.utc(2000,"jan",1,20,15,1) #=> 2000-01-01 20:15:01 UTC
922 * Time.gm(2000,"jan",1,20,15,1) #=> 2000-01-01 20:15:01 UTC
924 static VALUE
925 time_s_mkutc(int argc, VALUE *argv, VALUE klass)
927 return time_utc_or_local(argc, argv, Qtrue, klass);
931 * call-seq:
932 * Time.local(year) => time
933 * Time.local(year, month) => time
934 * Time.local(year, month, day) => time
935 * Time.local(year, month, day, hour) => time
936 * Time.local(year, month, day, hour, min) => time
937 * Time.local(year, month, day, hour, min, sec_with_frac) => time
938 * Time.local(year, month, day, hour, min, sec, usec_with_frac) => time
939 * Time.local(sec, min, hour, day, month, year, wday, yday, isdst, tz) => time
940 * Time.mktime(year) => time
941 * Time.mktime(year, month) => time
942 * Time.mktime(year, month, day) => time
943 * Time.mktime(year, month, day, hour) => time
944 * Time.mktime(year, month, day, hour, min) => time
945 * Time.mktime(year, month, day, hour, min, sec_with_frac) => time
946 * Time.mktime(year, month, day, hour, min, sec, usec_with_frac) => time
947 * Time.mktime(sec, min, hour, day, month, year, wday, yday, isdst, tz) => time
949 * Same as <code>Time::gm</code>, but interprets the values in the
950 * local time zone.
952 * Time.local(2000,"jan",1,20,15,1) #=> 2000-01-01 20:15:01 -0600
955 static VALUE
956 time_s_mktime(int argc, VALUE *argv, VALUE klass)
958 return time_utc_or_local(argc, argv, Qfalse, klass);
962 * call-seq:
963 * time.to_i => int
964 * time.tv_sec => int
966 * Returns the value of <i>time</i> as an integer number of seconds
967 * since the Epoch.
969 * t = Time.now
970 * "%10.5f" % t.to_f #=> "1049896564.17839"
971 * t.to_i #=> 1049896564
974 static VALUE
975 time_to_i(VALUE time)
977 struct time_object *tobj;
979 GetTimeval(time, tobj);
980 return LONG2NUM(tobj->ts.tv_sec);
984 * call-seq:
985 * time.to_f => float
987 * Returns the value of <i>time</i> as a floating point number of
988 * seconds since the Epoch.
990 * t = Time.now
991 * "%10.5f" % t.to_f #=> "1049896564.13654"
992 * t.to_i #=> 1049896564
994 * Note that IEEE 754 double is not accurate enough to represent
995 * nanoseconds from the Epoch.
998 static VALUE
999 time_to_f(VALUE time)
1001 struct time_object *tobj;
1003 GetTimeval(time, tobj);
1004 return DOUBLE2NUM((double)tobj->ts.tv_sec+(double)tobj->ts.tv_nsec/1e9);
1008 * call-seq:
1009 * time.usec => int
1010 * time.tv_usec => int
1012 * Returns just the number of microseconds for <i>time</i>.
1014 * t = Time.now #=> 2007-11-19 08:03:26 -0600
1015 * "%10.6f" % t.to_f #=> "1195481006.775195"
1016 * t.usec #=> 775195
1019 static VALUE
1020 time_usec(VALUE time)
1022 struct time_object *tobj;
1024 GetTimeval(time, tobj);
1025 return LONG2NUM(tobj->ts.tv_nsec/1000);
1029 * call-seq:
1030 * time.nsec => int
1031 * time.tv_nsec => int
1033 * Returns just the number of nanoseconds for <i>time</i>.
1035 * t = Time.now #=> 2007-11-17 15:18:03 +0900
1036 * "%10.9f" % t.to_f #=> "1195280283.536151409"
1037 * t.nsec #=> 536151406
1039 * The lowest digit of to_f and nsec is different because
1040 * IEEE 754 double is not accurate enough to represent
1041 * nanoseconds from the Epoch.
1042 * The accurate value is returned by nsec.
1045 static VALUE
1046 time_nsec(VALUE time)
1048 struct time_object *tobj;
1050 GetTimeval(time, tobj);
1051 return LONG2NUM(tobj->ts.tv_nsec);
1055 * call-seq:
1056 * time <=> other_time => -1, 0, +1
1058 * Comparison---Compares <i>time</i> with <i>other_time</i>.
1060 * t = Time.now #=> 2007-11-19 08:12:12 -0600
1061 * t2 = t + 2592000 #=> 2007-12-19 08:12:12 -0600
1062 * t <=> t2 #=> -1
1063 * t2 <=> t #=> 1
1065 * t = Time.now #=> 2007-11-19 08:13:38 -0600
1066 * t2 = t + 0.1 #=> 2007-11-19 08:13:38 -0600
1067 * t.nsec #=> 98222999
1068 * t2.nsec #=> 198222999
1069 * t <=> t2 #=> -1
1070 * t2 <=> t #=> 1
1071 * t <=> t #=> 0
1074 static VALUE
1075 time_cmp(VALUE time1, VALUE time2)
1077 struct time_object *tobj1, *tobj2;
1079 GetTimeval(time1, tobj1);
1080 if (TYPE(time2) == T_DATA && RDATA(time2)->dfree == time_free) {
1081 GetTimeval(time2, tobj2);
1082 if (tobj1->ts.tv_sec == tobj2->ts.tv_sec) {
1083 if (tobj1->ts.tv_nsec == tobj2->ts.tv_nsec) return INT2FIX(0);
1084 if (tobj1->ts.tv_nsec > tobj2->ts.tv_nsec) return INT2FIX(1);
1085 return INT2FIX(-1);
1087 if (tobj1->ts.tv_sec > tobj2->ts.tv_sec) return INT2FIX(1);
1088 return INT2FIX(-1);
1091 return Qnil;
1095 * call-seq:
1096 * time.eql?(other_time)
1098 * Return <code>true</code> if <i>time</i> and <i>other_time</i> are
1099 * both <code>Time</code> objects with the same seconds and fractional
1100 * seconds.
1103 static VALUE
1104 time_eql(VALUE time1, VALUE time2)
1106 struct time_object *tobj1, *tobj2;
1108 GetTimeval(time1, tobj1);
1109 if (TYPE(time2) == T_DATA && RDATA(time2)->dfree == time_free) {
1110 GetTimeval(time2, tobj2);
1111 if (tobj1->ts.tv_sec == tobj2->ts.tv_sec) {
1112 if (tobj1->ts.tv_nsec == tobj2->ts.tv_nsec) return Qtrue;
1115 return Qfalse;
1119 * call-seq:
1120 * time.utc? => true or false
1121 * time.gmt? => true or false
1123 * Returns <code>true</code> if <i>time</i> represents a time in UTC
1124 * (GMT).
1126 * t = Time.now #=> 2007-11-19 08:15:23 -0600
1127 * t.utc? #=> false
1128 * t = Time.gm(2000,"jan",1,20,15,1) #=> 2000-01-01 20:15:01 UTC
1129 * t.utc? #=> true
1131 * t = Time.now #=> 2007-11-19 08:16:03 -0600
1132 * t.gmt? #=> false
1133 * t = Time.gm(2000,1,1,20,15,1) #=> 2000-01-01 20:15:01 UTC
1134 * t.gmt? #=> true
1137 static VALUE
1138 time_utc_p(VALUE time)
1140 struct time_object *tobj;
1142 GetTimeval(time, tobj);
1143 if (tobj->gmt) return Qtrue;
1144 return Qfalse;
1148 * call-seq:
1149 * time.hash => fixnum
1151 * Return a hash code for this time object.
1154 static VALUE
1155 time_hash(VALUE time)
1157 struct time_object *tobj;
1158 long hash;
1160 GetTimeval(time, tobj);
1161 hash = tobj->ts.tv_sec ^ tobj->ts.tv_nsec;
1162 return LONG2FIX(hash);
1165 /* :nodoc: */
1166 static VALUE
1167 time_init_copy(VALUE copy, VALUE time)
1169 struct time_object *tobj, *tcopy;
1171 if (copy == time) return copy;
1172 time_modify(copy);
1173 if (TYPE(time) != T_DATA || RDATA(time)->dfree != time_free) {
1174 rb_raise(rb_eTypeError, "wrong argument type");
1176 GetTimeval(time, tobj);
1177 GetTimeval(copy, tcopy);
1178 MEMCPY(tcopy, tobj, struct time_object, 1);
1180 return copy;
1183 static VALUE
1184 time_dup(VALUE time)
1186 VALUE dup = time_s_alloc(CLASS_OF(time));
1187 time_init_copy(dup, time);
1188 return dup;
1192 * call-seq:
1193 * time.localtime => time
1195 * Converts <i>time</i> to local time (using the local time zone in
1196 * effect for this process) modifying the receiver.
1198 * t = Time.gm(2000, "jan", 1, 20, 15, 1) #=> 2000-01-01 20:15:01 UTC
1199 * t.gmt? #=> true
1200 * t.localtime #=> 2000-01-01 14:15:01 -0600
1201 * t.gmt? #=> false
1204 static VALUE
1205 time_localtime(VALUE time)
1207 struct time_object *tobj;
1208 struct tm *tm_tmp;
1209 time_t t;
1211 GetTimeval(time, tobj);
1212 if (!tobj->gmt) {
1213 if (tobj->tm_got)
1214 return time;
1216 else {
1217 time_modify(time);
1219 t = tobj->ts.tv_sec;
1220 tm_tmp = localtime(&t);
1221 if (!tm_tmp)
1222 rb_raise(rb_eArgError, "localtime error");
1223 tobj->tm = *tm_tmp;
1224 tobj->tm_got = 1;
1225 tobj->gmt = 0;
1226 return time;
1230 * call-seq:
1231 * time.gmtime => time
1232 * time.utc => time
1234 * Converts <i>time</i> to UTC (GMT), modifying the receiver.
1236 * t = Time.now #=> 2007-11-19 08:18:31 -0600
1237 * t.gmt? #=> false
1238 * t.gmtime #=> 2007-11-19 14:18:31 UTC
1239 * t.gmt? #=> true
1241 * t = Time.now #=> 2007-11-19 08:18:51 -0600
1242 * t.utc? #=> false
1243 * t.utc #=> 2007-11-19 14:18:51 UTC
1244 * t.utc? #=> true
1247 static VALUE
1248 time_gmtime(VALUE time)
1250 struct time_object *tobj;
1251 struct tm *tm_tmp;
1252 time_t t;
1254 GetTimeval(time, tobj);
1255 if (tobj->gmt) {
1256 if (tobj->tm_got)
1257 return time;
1259 else {
1260 time_modify(time);
1262 t = tobj->ts.tv_sec;
1263 tm_tmp = gmtime(&t);
1264 if (!tm_tmp)
1265 rb_raise(rb_eArgError, "gmtime error");
1266 tobj->tm = *tm_tmp;
1267 tobj->tm_got = 1;
1268 tobj->gmt = 1;
1269 return time;
1273 * call-seq:
1274 * time.getlocal => new_time
1276 * Returns a new <code>new_time</code> object representing <i>time</i> in
1277 * local time (using the local time zone in effect for this process).
1279 * t = Time.gm(2000,1,1,20,15,1) #=> 2000-01-01 20:15:01 UTC
1280 * t.gmt? #=> true
1281 * l = t.getlocal #=> 2000-01-01 14:15:01 -0600
1282 * l.gmt? #=> false
1283 * t == l #=> true
1286 static VALUE
1287 time_getlocaltime(VALUE time)
1289 return time_localtime(time_dup(time));
1293 * call-seq:
1294 * time.getgm => new_time
1295 * time.getutc => new_time
1297 * Returns a new <code>new_time</code> object representing <i>time</i> in
1298 * UTC.
1300 * t = Time.local(2000,1,1,20,15,1) #=> 2000-01-01 20:15:01 -0600
1301 * t.gmt? #=> false
1302 * y = t.getgm #=> 2000-01-02 02:15:01 UTC
1303 * y.gmt? #=> true
1304 * t == y #=> true
1307 static VALUE
1308 time_getgmtime(VALUE time)
1310 return time_gmtime(time_dup(time));
1313 static VALUE
1314 time_get_tm(VALUE time, int gmt)
1316 if (gmt) return time_gmtime(time);
1317 return time_localtime(time);
1321 * call-seq:
1322 * time.asctime => string
1323 * time.ctime => string
1325 * Returns a canonical string representation of <i>time</i>.
1327 * Time.now.asctime #=> "Wed Apr 9 08:56:03 2003"
1330 static VALUE
1331 time_asctime(VALUE time)
1333 struct time_object *tobj;
1334 char *s;
1336 GetTimeval(time, tobj);
1337 if (tobj->tm_got == 0) {
1338 time_get_tm(time, tobj->gmt);
1340 s = asctime(&tobj->tm);
1341 if (s[24] == '\n') s[24] = '\0';
1343 return rb_str_new2(s);
1347 * call-seq:
1348 * time.inspect => string
1349 * time.to_s => string
1351 * Returns a string representing <i>time</i>. Equivalent to calling
1352 * <code>Time#strftime</code> with a format string of
1353 * ``<code>%Y-%m-%d</code> <code>%H:%M:%S</code> <code>%z</code>''
1354 * for a local time and
1355 * ``<code>%Y-%m-%d</code> <code>%H:%M:%S</code> <code>UTC</code>''
1356 * for a UTC time.
1358 * Time.now.to_s #=> "2007-10-05 16:09:51 +0900"
1359 * Time.now.utc.to_s #=> "2007-10-05 07:09:51 UTC"
1362 static VALUE
1363 time_to_s(VALUE time)
1365 struct time_object *tobj;
1366 char buf[128];
1367 int len;
1369 GetTimeval(time, tobj);
1370 if (tobj->tm_got == 0) {
1371 time_get_tm(time, tobj->gmt);
1373 if (tobj->gmt == 1) {
1374 len = strftime(buf, 128, "%Y-%m-%d %H:%M:%S UTC", &tobj->tm);
1376 else {
1377 long off;
1378 char sign = '+';
1379 #if defined(HAVE_STRUCT_TM_TM_GMTOFF)
1380 off = tobj->tm.tm_gmtoff;
1381 #else
1382 VALUE tmp = time_utc_offset(time);
1383 off = NUM2INT(tmp);
1384 #endif
1385 if (off < 0) {
1386 sign = '-';
1387 off = -off;
1389 len = strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S ", &tobj->tm);
1390 len += snprintf(buf+len, sizeof(buf)-len, "%c%02d%02d", sign,
1391 (int)(off/3600), (int)(off%3600/60));
1393 return rb_str_new(buf, len);
1396 static VALUE
1397 time_add(struct time_object *tobj, VALUE offset, int sign)
1399 double v = NUM2DBL(offset);
1400 double f, d;
1401 unsigned_time_t sec_off;
1402 time_t sec;
1403 long nsec_off, nsec;
1404 VALUE result;
1406 if (v < 0) {
1407 v = -v;
1408 sign = -sign;
1410 d = modf(v, &f);
1411 sec_off = (unsigned_time_t)f;
1412 if (f != (double)sec_off)
1413 rb_raise(rb_eRangeError, "time %s %f out of Time range",
1414 sign < 0 ? "-" : "+", v);
1415 nsec_off = (long)(d*1e9+0.5);
1417 if (sign < 0) {
1418 sec = tobj->ts.tv_sec - sec_off;
1419 nsec = tobj->ts.tv_nsec - nsec_off;
1420 if (sec > tobj->ts.tv_sec)
1421 rb_raise(rb_eRangeError, "time - %f out of Time range", v);
1423 else {
1424 sec = tobj->ts.tv_sec + sec_off;
1425 nsec = tobj->ts.tv_nsec + nsec_off;
1426 if (sec < tobj->ts.tv_sec)
1427 rb_raise(rb_eRangeError, "time + %f out of Time range", v);
1429 result = rb_time_nano_new(sec, nsec);
1430 if (tobj->gmt) {
1431 GetTimeval(result, tobj);
1432 tobj->gmt = 1;
1434 return result;
1438 * call-seq:
1439 * time + numeric => time
1441 * Addition---Adds some number of seconds (possibly fractional) to
1442 * <i>time</i> and returns that value as a new time.
1444 * t = Time.now #=> 2007-11-19 08:22:21 -0600
1445 * t + (60 * 60 * 24) #=> 2007-11-20 08:22:21 -0600
1448 static VALUE
1449 time_plus(VALUE time1, VALUE time2)
1451 struct time_object *tobj;
1452 GetTimeval(time1, tobj);
1454 if (TYPE(time2) == T_DATA && RDATA(time2)->dfree == time_free) {
1455 rb_raise(rb_eTypeError, "time + time?");
1457 return time_add(tobj, time2, 1);
1461 * call-seq:
1462 * time - other_time => float
1463 * time - numeric => time
1465 * Difference---Returns a new time that represents the difference
1466 * between two times, or subtracts the given number of seconds in
1467 * <i>numeric</i> from <i>time</i>.
1469 * t = Time.now #=> 2007-11-19 08:23:10 -0600
1470 * t2 = t + 2592000 #=> 2007-12-19 08:23:10 -0600
1471 * t2 - t #=> 2592000.0
1472 * t2 - 2592000 #=> 2007-11-19 08:23:10 -0600
1475 static VALUE
1476 time_minus(VALUE time1, VALUE time2)
1478 struct time_object *tobj;
1480 GetTimeval(time1, tobj);
1481 if (TYPE(time2) == T_DATA && RDATA(time2)->dfree == time_free) {
1482 struct time_object *tobj2;
1483 double f;
1485 GetTimeval(time2, tobj2);
1486 if (tobj->ts.tv_sec < tobj2->ts.tv_sec)
1487 f = -(double)(unsigned_time_t)(tobj2->ts.tv_sec - tobj->ts.tv_sec);
1488 else
1489 f = (double)(unsigned_time_t)(tobj->ts.tv_sec - tobj2->ts.tv_sec);
1490 f += ((double)tobj->ts.tv_nsec - (double)tobj2->ts.tv_nsec)*1e-9;
1492 return DOUBLE2NUM(f);
1494 return time_add(tobj, time2, -1);
1498 * call-seq:
1499 * time.succ => new_time
1501 * Return a new time object, one second later than <code>time</code>.
1503 * t = Time.now #=> 2007-11-19 08:23:57 -0600
1504 * t.succ #=> 2007-11-19 08:23:58 -0600
1507 static VALUE
1508 time_succ(VALUE time)
1510 struct time_object *tobj;
1511 int gmt;
1513 GetTimeval(time, tobj);
1514 gmt = tobj->gmt;
1515 time = rb_time_nano_new(tobj->ts.tv_sec + 1, tobj->ts.tv_nsec);
1516 GetTimeval(time, tobj);
1517 tobj->gmt = gmt;
1518 return time;
1521 VALUE
1522 rb_time_succ(VALUE time)
1524 return time_succ(time);
1528 * call-seq:
1529 * time.sec => fixnum
1531 * Returns the second of the minute (0..60)<em>[Yes, seconds really can
1532 * range from zero to 60. This allows the system to inject leap seconds
1533 * every now and then to correct for the fact that years are not really
1534 * a convenient number of hours long.]</em> for <i>time</i>.
1536 * t = Time.now #=> 2007-11-19 08:25:02 -0600
1537 * t.sec #=> 2
1540 static VALUE
1541 time_sec(VALUE time)
1543 struct time_object *tobj;
1545 GetTimeval(time, tobj);
1546 if (tobj->tm_got == 0) {
1547 time_get_tm(time, tobj->gmt);
1549 return INT2FIX(tobj->tm.tm_sec);
1553 * call-seq:
1554 * time.min => fixnum
1556 * Returns the minute of the hour (0..59) for <i>time</i>.
1558 * t = Time.now #=> 2007-11-19 08:25:51 -0600
1559 * t.min #=> 25
1562 static VALUE
1563 time_min(VALUE time)
1565 struct time_object *tobj;
1567 GetTimeval(time, tobj);
1568 if (tobj->tm_got == 0) {
1569 time_get_tm(time, tobj->gmt);
1571 return INT2FIX(tobj->tm.tm_min);
1575 * call-seq:
1576 * time.hour => fixnum
1578 * Returns the hour of the day (0..23) for <i>time</i>.
1580 * t = Time.now #=> 2007-11-19 08:26:20 -0600
1581 * t.hour #=> 8
1584 static VALUE
1585 time_hour(VALUE time)
1587 struct time_object *tobj;
1589 GetTimeval(time, tobj);
1590 if (tobj->tm_got == 0) {
1591 time_get_tm(time, tobj->gmt);
1593 return INT2FIX(tobj->tm.tm_hour);
1597 * call-seq:
1598 * time.day => fixnum
1599 * time.mday => fixnum
1601 * Returns the day of the month (1..n) for <i>time</i>.
1603 * t = Time.now #=> 2007-11-19 08:27:03 -0600
1604 * t.day #=> 19
1605 * t.mday #=> 19
1608 static VALUE
1609 time_mday(VALUE time)
1611 struct time_object *tobj;
1613 GetTimeval(time, tobj);
1614 if (tobj->tm_got == 0) {
1615 time_get_tm(time, tobj->gmt);
1617 return INT2FIX(tobj->tm.tm_mday);
1621 * call-seq:
1622 * time.mon => fixnum
1623 * time.month => fixnum
1625 * Returns the month of the year (1..12) for <i>time</i>.
1627 * t = Time.now #=> 2007-11-19 08:27:30 -0600
1628 * t.mon #=> 11
1629 * t.month #=> 11
1632 static VALUE
1633 time_mon(VALUE time)
1635 struct time_object *tobj;
1637 GetTimeval(time, tobj);
1638 if (tobj->tm_got == 0) {
1639 time_get_tm(time, tobj->gmt);
1641 return INT2FIX(tobj->tm.tm_mon+1);
1645 * call-seq:
1646 * time.year => fixnum
1648 * Returns the year for <i>time</i> (including the century).
1650 * t = Time.now #=> 2007-11-19 08:27:51 -0600
1651 * t.year #=> 2007
1654 static VALUE
1655 time_year(VALUE time)
1657 struct time_object *tobj;
1659 GetTimeval(time, tobj);
1660 if (tobj->tm_got == 0) {
1661 time_get_tm(time, tobj->gmt);
1663 return LONG2NUM((long)tobj->tm.tm_year+1900);
1667 * call-seq:
1668 * time.wday => fixnum
1670 * Returns an integer representing the day of the week, 0..6, with
1671 * Sunday == 0.
1673 * t = Time.now #=> 2007-11-20 02:35:35 -0600
1674 * t.wday #=> 2
1675 * t.sunday? #=> false
1676 * t.monday? #=> false
1677 * t.tuesday? #=> true
1678 * t.wednesday? #=> false
1679 * t.thursday? #=> false
1680 * t.friday? #=> false
1681 * t.saturday? #=> false
1684 static VALUE
1685 time_wday(VALUE time)
1687 struct time_object *tobj;
1689 GetTimeval(time, tobj);
1690 if (tobj->tm_got == 0) {
1691 time_get_tm(time, tobj->gmt);
1693 return INT2FIX(tobj->tm.tm_wday);
1696 #define wday_p(n) {\
1697 struct time_object *tobj;\
1698 GetTimeval(time, tobj);\
1699 if (tobj->tm_got == 0) {\
1700 time_get_tm(time, tobj->gmt);\
1702 return (tobj->tm.tm_wday == (n)) ? Qtrue : Qfalse;\
1706 * call-seq:
1707 * time.sunday? => true or false
1709 * Returns <code>true</code> if <i>time</i> represents Sunday.
1711 * t = Time.local(1990, 4, 1) #=> 1990-04-01 00:00:00 -0600
1712 * t.sunday? #=> true
1715 static VALUE
1716 time_sunday(VALUE time)
1718 wday_p(0);
1722 * call-seq:
1723 * time.monday? => true or false
1725 * Returns <code>true</code> if <i>time</i> represents Monday.
1727 * t = Time.local(2003, 8, 4) #=> 2003-08-04 00:00:00 -0500
1728 * p t.monday? #=> true
1731 static VALUE
1732 time_monday(VALUE time)
1734 wday_p(1);
1738 * call-seq:
1739 * time.tuesday? => true or false
1741 * Returns <code>true</code> if <i>time</i> represents Tuesday.
1743 * t = Time.local(1991, 2, 19) #=> 1991-02-19 00:00:00 -0600
1744 * p t.tuesday? #=> true
1747 static VALUE
1748 time_tuesday(VALUE time)
1750 wday_p(2);
1754 * call-seq:
1755 * time.wednesday? => true or false
1757 * Returns <code>true</code> if <i>time</i> represents Wednesday.
1759 * t = Time.local(1993, 2, 24) #=> 1993-02-24 00:00:00 -0600
1760 * p t.wednesday? #=> true
1763 static VALUE
1764 time_wednesday(VALUE time)
1766 wday_p(3);
1770 * call-seq:
1771 * time.thursday? => true or false
1773 * Returns <code>true</code> if <i>time</i> represents Thursday.
1775 * t = Time.local(1995, 12, 21) #=> 1995-12-21 00:00:00 -0600
1776 * p t.thursday? #=> true
1779 static VALUE
1780 time_thursday(VALUE time)
1782 wday_p(4);
1786 * call-seq:
1787 * time.friday? => true or false
1789 * Returns <code>true</code> if <i>time</i> represents Friday.
1791 * t = Time.local(1987, 12, 18) #=> 1987-12-18 00:00:00 -0600
1792 * t.friday? #=> true
1795 static VALUE
1796 time_friday(VALUE time)
1798 wday_p(5);
1802 * call-seq:
1803 * time.saturday? => true or false
1805 * Returns <code>true</code> if <i>time</i> represents Saturday.
1807 * t = Time.local(2006, 6, 10) #=> 2006-06-10 00:00:00 -0500
1808 * t.saturday? #=> true
1811 static VALUE
1812 time_saturday(VALUE time)
1814 wday_p(6);
1818 * call-seq:
1819 * time.yday => fixnum
1821 * Returns an integer representing the day of the year, 1..366.
1823 * t = Time.now #=> 2007-11-19 08:32:31 -0600
1824 * t.yday #=> 323
1827 static VALUE
1828 time_yday(VALUE time)
1830 struct time_object *tobj;
1832 GetTimeval(time, tobj);
1833 if (tobj->tm_got == 0) {
1834 time_get_tm(time, tobj->gmt);
1836 return INT2FIX(tobj->tm.tm_yday+1);
1840 * call-seq:
1841 * time.isdst => true or false
1842 * time.dst? => true or false
1844 * Returns <code>true</code> if <i>time</i> occurs during Daylight
1845 * Saving Time in its time zone.
1847 * # CST6CDT:
1848 * Time.local(2000, 1, 1).zone #=> "CST"
1849 * Time.local(2000, 1, 1).isdst #=> false
1850 * Time.local(2000, 1, 1).dst? #=> false
1851 * Time.local(2000, 7, 1).zone #=> "CDT"
1852 * Time.local(2000, 7, 1).isdst #=> true
1853 * Time.local(2000, 7, 1).dst? #=> true
1855 * # Asia/Tokyo:
1856 * Time.local(2000, 1, 1).zone #=> "JST"
1857 * Time.local(2000, 1, 1).isdst #=> false
1858 * Time.local(2000, 1, 1).dst? #=> false
1859 * Time.local(2000, 7, 1).zone #=> "JST"
1860 * Time.local(2000, 7, 1).isdst #=> false
1861 * Time.local(2000, 7, 1).dst? #=> false
1864 static VALUE
1865 time_isdst(VALUE time)
1867 struct time_object *tobj;
1869 GetTimeval(time, tobj);
1870 if (tobj->tm_got == 0) {
1871 time_get_tm(time, tobj->gmt);
1873 return tobj->tm.tm_isdst?Qtrue:Qfalse;
1877 * call-seq:
1878 * time.zone => string
1880 * Returns the name of the time zone used for <i>time</i>. As of Ruby
1881 * 1.8, returns ``UTC'' rather than ``GMT'' for UTC times.
1883 * t = Time.gm(2000, "jan", 1, 20, 15, 1)
1884 * t.zone #=> "UTC"
1885 * t = Time.local(2000, "jan", 1, 20, 15, 1)
1886 * t.zone #=> "CST"
1889 static VALUE
1890 time_zone(VALUE time)
1892 struct time_object *tobj;
1893 #if !defined(HAVE_TM_ZONE) && (!defined(HAVE_TZNAME) || !defined(HAVE_DAYLIGHT))
1894 char buf[64];
1895 int len;
1896 #endif
1898 GetTimeval(time, tobj);
1899 if (tobj->tm_got == 0) {
1900 time_get_tm(time, tobj->gmt);
1903 if (tobj->gmt == 1) {
1904 return rb_str_new2("UTC");
1906 #if defined(HAVE_TM_ZONE)
1907 return rb_str_new2(tobj->tm.tm_zone);
1908 #elif defined(HAVE_TZNAME) && defined(HAVE_DAYLIGHT)
1909 return rb_str_new2(tzname[daylight && tobj->tm.tm_isdst]);
1910 #else
1911 len = strftime(buf, 64, "%Z", &tobj->tm);
1912 return rb_str_new(buf, len);
1913 #endif
1917 * call-seq:
1918 * time.gmt_offset => fixnum
1919 * time.gmtoff => fixnum
1920 * time.utc_offset => fixnum
1922 * Returns the offset in seconds between the timezone of <i>time</i>
1923 * and UTC.
1925 * t = Time.gm(2000,1,1,20,15,1) #=> 2000-01-01 20:15:01 UTC
1926 * t.gmt_offset #=> 0
1927 * l = t.getlocal #=> 2000-01-01 14:15:01 -0600
1928 * l.gmt_offset #=> -21600
1931 static VALUE
1932 time_utc_offset(VALUE time)
1934 struct time_object *tobj;
1936 GetTimeval(time, tobj);
1937 if (tobj->tm_got == 0) {
1938 time_get_tm(time, tobj->gmt);
1941 if (tobj->gmt == 1) {
1942 return INT2FIX(0);
1944 else {
1945 #if defined(HAVE_STRUCT_TM_TM_GMTOFF)
1946 return INT2NUM(tobj->tm.tm_gmtoff);
1947 #else
1948 struct tm *u, *l;
1949 time_t t;
1950 long off;
1951 l = &tobj->tm;
1952 t = tobj->ts.tv_sec;
1953 u = gmtime(&t);
1954 if (!u)
1955 rb_raise(rb_eArgError, "gmtime error");
1956 if (l->tm_year != u->tm_year)
1957 off = l->tm_year < u->tm_year ? -1 : 1;
1958 else if (l->tm_mon != u->tm_mon)
1959 off = l->tm_mon < u->tm_mon ? -1 : 1;
1960 else if (l->tm_mday != u->tm_mday)
1961 off = l->tm_mday < u->tm_mday ? -1 : 1;
1962 else
1963 off = 0;
1964 off = off * 24 + l->tm_hour - u->tm_hour;
1965 off = off * 60 + l->tm_min - u->tm_min;
1966 off = off * 60 + l->tm_sec - u->tm_sec;
1967 return LONG2FIX(off);
1968 #endif
1973 * call-seq:
1974 * time.to_a => array
1976 * Returns a ten-element <i>array</i> of values for <i>time</i>:
1977 * {<code>[ sec, min, hour, day, month, year, wday, yday, isdst, zone
1978 * ]</code>}. See the individual methods for an explanation of the
1979 * valid ranges of each value. The ten elements can be passed directly
1980 * to <code>Time::utc</code> or <code>Time::local</code> to create a
1981 * new <code>Time</code>.
1983 * t = Time.now #=> 2007-11-19 08:36:01 -0600
1984 * now = t.to_a #=> [1, 36, 8, 19, 11, 2007, 1, 323, false, "CST"]
1987 static VALUE
1988 time_to_a(VALUE time)
1990 struct time_object *tobj;
1992 GetTimeval(time, tobj);
1993 if (tobj->tm_got == 0) {
1994 time_get_tm(time, tobj->gmt);
1996 return rb_ary_new3(10,
1997 INT2FIX(tobj->tm.tm_sec),
1998 INT2FIX(tobj->tm.tm_min),
1999 INT2FIX(tobj->tm.tm_hour),
2000 INT2FIX(tobj->tm.tm_mday),
2001 INT2FIX(tobj->tm.tm_mon+1),
2002 LONG2NUM((long)tobj->tm.tm_year+1900),
2003 INT2FIX(tobj->tm.tm_wday),
2004 INT2FIX(tobj->tm.tm_yday+1),
2005 tobj->tm.tm_isdst?Qtrue:Qfalse,
2006 time_zone(time));
2009 #define SMALLBUF 100
2010 static int
2011 rb_strftime(char **buf, const char *format, struct tm *time)
2013 int size, len, flen;
2015 (*buf)[0] = '\0';
2016 flen = strlen(format);
2017 if (flen == 0) {
2018 return 0;
2020 errno = 0;
2021 len = strftime(*buf, SMALLBUF, format, time);
2022 if (len != 0 || (**buf == '\0' && errno != ERANGE)) return len;
2023 for (size=1024; ; size*=2) {
2024 *buf = xmalloc(size);
2025 (*buf)[0] = '\0';
2026 len = strftime(*buf, size, format, time);
2028 * buflen can be zero EITHER because there's not enough
2029 * room in the string, or because the control command
2030 * goes to the empty string. Make a reasonable guess that
2031 * if the buffer is 1024 times bigger than the length of the
2032 * format string, it's not failing for lack of room.
2034 if (len > 0 || size >= 1024 * flen) return len;
2035 xfree(*buf);
2037 /* not reached */
2041 * call-seq:
2042 * time.strftime( string ) => string
2044 * Formats <i>time</i> according to the directives in the given format
2045 * string. Any text not listed as a directive will be passed through
2046 * to the output string.
2048 * Format meaning:
2049 * %a - The abbreviated weekday name (``Sun'')
2050 * %A - The full weekday name (``Sunday'')
2051 * %b - The abbreviated month name (``Jan'')
2052 * %B - The full month name (``January'')
2053 * %c - The preferred local date and time representation
2054 * %d - Day of the month (01..31)
2055 * %H - Hour of the day, 24-hour clock (00..23)
2056 * %I - Hour of the day, 12-hour clock (01..12)
2057 * %j - Day of the year (001..366)
2058 * %m - Month of the year (01..12)
2059 * %M - Minute of the hour (00..59)
2060 * %p - Meridian indicator (``AM'' or ``PM'')
2061 * %S - Second of the minute (00..60)
2062 * %U - Week number of the current year,
2063 * starting with the first Sunday as the first
2064 * day of the first week (00..53)
2065 * %W - Week number of the current year,
2066 * starting with the first Monday as the first
2067 * day of the first week (00..53)
2068 * %w - Day of the week (Sunday is 0, 0..6)
2069 * %x - Preferred representation for the date alone, no time
2070 * %X - Preferred representation for the time alone, no date
2071 * %y - Year without a century (00..99)
2072 * %Y - Year with century
2073 * %Z - Time zone name
2074 * %% - Literal ``%'' character
2076 * t = Time.now #=> 2007-11-19 08:37:48 -0600
2077 * t.strftime("Printed on %m/%d/%Y") #=> "Printed on 11/19/2007"
2078 * t.strftime("at %I:%M%p") #=> "at 08:37AM"
2081 static VALUE
2082 time_strftime(VALUE time, VALUE format)
2084 void rb_enc_copy(VALUE, VALUE);
2085 struct time_object *tobj;
2086 char buffer[SMALLBUF], *buf = buffer;
2087 const char *fmt;
2088 long len;
2089 VALUE str;
2091 GetTimeval(time, tobj);
2092 if (tobj->tm_got == 0) {
2093 time_get_tm(time, tobj->gmt);
2095 StringValue(format);
2096 if (!rb_enc_str_asciicompat_p(format)) {
2097 rb_raise(rb_eArgError, "format should have ASCII compatible encoding");
2099 format = rb_str_new4(format);
2100 fmt = RSTRING_PTR(format);
2101 len = RSTRING_LEN(format);
2102 if (len == 0) {
2103 rb_warning("strftime called with empty format string");
2105 else if (strlen(fmt) < len) {
2106 /* Ruby string may contain \0's. */
2107 const char *p = fmt, *pe = fmt + len;
2109 str = rb_str_new(0, 0);
2110 while (p < pe) {
2111 len = rb_strftime(&buf, p, &tobj->tm);
2112 rb_str_cat(str, buf, len);
2113 p += strlen(p);
2114 if (buf != buffer) {
2115 xfree(buf);
2116 buf = buffer;
2118 for (fmt = p; p < pe && !*p; ++p);
2119 if (p > fmt) rb_str_cat(str, fmt, p - fmt);
2121 return str;
2123 else {
2124 len = rb_strftime(&buf, RSTRING_PTR(format), &tobj->tm);
2126 str = rb_str_new(buf, len);
2127 if (buf != buffer) xfree(buf);
2128 rb_enc_copy(str, format);
2129 return str;
2133 * undocumented
2136 static VALUE
2137 time_mdump(VALUE time)
2139 struct time_object *tobj;
2140 struct tm *tm;
2141 unsigned long p, s;
2142 char buf[8];
2143 time_t t;
2144 int nsec;
2145 int i;
2146 VALUE str;
2148 GetTimeval(time, tobj);
2150 t = tobj->ts.tv_sec;
2151 tm = gmtime(&t);
2153 if ((tm->tm_year & 0xffff) != tm->tm_year)
2154 rb_raise(rb_eArgError, "year too big to marshal: %ld", (long)tm->tm_year);
2156 p = 0x1UL << 31 | /* 1 */
2157 tobj->gmt << 30 | /* 1 */
2158 tm->tm_year << 14 | /* 16 */
2159 tm->tm_mon << 10 | /* 4 */
2160 tm->tm_mday << 5 | /* 5 */
2161 tm->tm_hour; /* 5 */
2162 s = tm->tm_min << 26 | /* 6 */
2163 tm->tm_sec << 20 | /* 6 */
2164 tobj->ts.tv_nsec / 1000; /* 20 */
2165 nsec = tobj->ts.tv_nsec % 1000;
2167 for (i=0; i<4; i++) {
2168 buf[i] = p & 0xff;
2169 p = RSHIFT(p, 8);
2171 for (i=4; i<8; i++) {
2172 buf[i] = s & 0xff;
2173 s = RSHIFT(s, 8);
2176 str = rb_str_new(buf, 8);
2177 rb_copy_generic_ivar(str, time);
2178 if (nsec) {
2180 * submicro is formatted in fixed-point packed BCD (without sign).
2181 * It represent digits under microsecond.
2182 * For nanosecond resolution, 3 digits (2 bytes) are used.
2183 * However it can be longer.
2184 * Extra digits are ignored for loading.
2186 unsigned char buf[2];
2187 int len = sizeof(buf);
2188 buf[1] = (nsec % 10) << 4;
2189 nsec /= 10;
2190 buf[0] = nsec % 10;
2191 nsec /= 10;
2192 buf[0] |= (nsec % 10) << 4;
2193 if (buf[1] == 0)
2194 len = 1;
2195 rb_ivar_set(str, id_submicro, rb_str_new((char *)buf, len));
2197 return str;
2201 * call-seq:
2202 * time._dump => string
2204 * Dump _time_ for marshaling.
2207 static VALUE
2208 time_dump(int argc, VALUE *argv, VALUE time)
2210 VALUE str;
2212 rb_scan_args(argc, argv, "01", 0);
2213 str = time_mdump(time);
2215 return str;
2219 * undocumented
2222 static VALUE
2223 time_mload(VALUE time, VALUE str)
2225 struct time_object *tobj;
2226 unsigned long p, s;
2227 time_t sec;
2228 long usec;
2229 unsigned char *buf;
2230 struct tm tm;
2231 int i, gmt;
2232 long nsec;
2233 VALUE submicro;
2235 time_modify(time);
2237 submicro = rb_attr_get(str, id_submicro);
2238 if (submicro != Qnil) {
2239 st_delete(rb_generic_ivar_table(str), (st_data_t*)&id_submicro, 0);
2241 rb_copy_generic_ivar(time, str);
2243 StringValue(str);
2244 buf = (unsigned char *)RSTRING_PTR(str);
2245 if (RSTRING_LEN(str) != 8) {
2246 rb_raise(rb_eTypeError, "marshaled time format differ");
2249 p = s = 0;
2250 for (i=0; i<4; i++) {
2251 p |= buf[i]<<(8*i);
2253 for (i=4; i<8; i++) {
2254 s |= buf[i]<<(8*(i-4));
2257 if ((p & (1UL<<31)) == 0) {
2258 gmt = 0;
2259 sec = p;
2260 usec = s;
2261 nsec = usec * 1000;
2263 else {
2264 p &= ~(1UL<<31);
2265 gmt = (p >> 30) & 0x1;
2266 tm.tm_year = (p >> 14) & 0xffff;
2267 tm.tm_mon = (p >> 10) & 0xf;
2268 tm.tm_mday = (p >> 5) & 0x1f;
2269 tm.tm_hour = p & 0x1f;
2270 tm.tm_min = (s >> 26) & 0x3f;
2271 tm.tm_sec = (s >> 20) & 0x3f;
2272 tm.tm_isdst = 0;
2274 sec = make_time_t(&tm, Qtrue);
2275 usec = (long)(s & 0xfffff);
2276 nsec = usec * 1000;
2278 if (submicro != Qnil) {
2279 unsigned char *ptr;
2280 long len;
2281 int digit;
2282 ptr = (unsigned char*)StringValuePtr(submicro);
2283 len = RSTRING_LEN(submicro);
2284 if (0 < len) {
2285 if (10 <= (digit = ptr[0] >> 4)) goto end_submicro;
2286 nsec += digit * 100;
2287 if (10 <= (digit = ptr[0] & 0xf)) goto end_submicro;
2288 nsec += digit * 10;
2290 if (1 < len) {
2291 if (10 <= (digit = ptr[1] >> 4)) goto end_submicro;
2292 nsec += digit;
2294 end_submicro: ;
2297 time_overflow_p(&sec, &nsec);
2299 GetTimeval(time, tobj);
2300 tobj->tm_got = 0;
2301 tobj->gmt = gmt;
2302 tobj->ts.tv_sec = sec;
2303 tobj->ts.tv_nsec = nsec;
2305 return time;
2309 * call-seq:
2310 * Time._load(string) => time
2312 * Unmarshal a dumped +Time+ object.
2315 static VALUE
2316 time_load(VALUE klass, VALUE str)
2318 VALUE time = time_s_alloc(klass);
2320 time_mload(time, str);
2321 return time;
2325 * <code>Time</code> is an abstraction of dates and times. Time is
2326 * stored internally as the number of seconds and nanoseconds since
2327 * the <em>Epoch</em>, January 1, 1970 00:00 UTC. On some operating
2328 * systems, this offset is allowed to be negative. Also see the
2329 * library modules <code>Date</code>. The
2330 * <code>Time</code> class treats GMT (Greenwich Mean Time) and UTC
2331 * (Coordinated Universal Time)<em>[Yes, UTC really does stand for
2332 * Coordinated Universal Time. There was a committee involved.]</em>
2333 * as equivalent. GMT is the older way of referring to these
2334 * baseline times but persists in the names of calls on POSIX
2335 * systems.
2337 * All times are stored with some number of nanoseconds. Be aware of
2338 * this fact when comparing times with each other---times that are
2339 * apparently equal when displayed may be different when compared.
2342 void
2343 Init_Time(void)
2345 #undef rb_intern
2347 id_divmod = rb_intern("divmod");
2348 id_mul = rb_intern("*");
2349 id_submicro = rb_intern("submicro");
2351 rb_cTime = rb_define_class("Time", rb_cObject);
2352 rb_include_module(rb_cTime, rb_mComparable);
2354 rb_define_alloc_func(rb_cTime, time_s_alloc);
2355 rb_define_singleton_method(rb_cTime, "now", rb_class_new_instance, -1);
2356 rb_define_singleton_method(rb_cTime, "at", time_s_at, -1);
2357 rb_define_singleton_method(rb_cTime, "utc", time_s_mkutc, -1);
2358 rb_define_singleton_method(rb_cTime, "gm", time_s_mkutc, -1);
2359 rb_define_singleton_method(rb_cTime, "local", time_s_mktime, -1);
2360 rb_define_singleton_method(rb_cTime, "mktime", time_s_mktime, -1);
2362 rb_define_method(rb_cTime, "to_i", time_to_i, 0);
2363 rb_define_method(rb_cTime, "to_f", time_to_f, 0);
2364 rb_define_method(rb_cTime, "<=>", time_cmp, 1);
2365 rb_define_method(rb_cTime, "eql?", time_eql, 1);
2366 rb_define_method(rb_cTime, "hash", time_hash, 0);
2367 rb_define_method(rb_cTime, "initialize", time_init, 0);
2368 rb_define_method(rb_cTime, "initialize_copy", time_init_copy, 1);
2370 rb_define_method(rb_cTime, "localtime", time_localtime, 0);
2371 rb_define_method(rb_cTime, "gmtime", time_gmtime, 0);
2372 rb_define_method(rb_cTime, "utc", time_gmtime, 0);
2373 rb_define_method(rb_cTime, "getlocal", time_getlocaltime, 0);
2374 rb_define_method(rb_cTime, "getgm", time_getgmtime, 0);
2375 rb_define_method(rb_cTime, "getutc", time_getgmtime, 0);
2377 rb_define_method(rb_cTime, "ctime", time_asctime, 0);
2378 rb_define_method(rb_cTime, "asctime", time_asctime, 0);
2379 rb_define_method(rb_cTime, "to_s", time_to_s, 0);
2380 rb_define_method(rb_cTime, "inspect", time_to_s, 0);
2381 rb_define_method(rb_cTime, "to_a", time_to_a, 0);
2383 rb_define_method(rb_cTime, "+", time_plus, 1);
2384 rb_define_method(rb_cTime, "-", time_minus, 1);
2386 rb_define_method(rb_cTime, "succ", time_succ, 0);
2387 rb_define_method(rb_cTime, "sec", time_sec, 0);
2388 rb_define_method(rb_cTime, "min", time_min, 0);
2389 rb_define_method(rb_cTime, "hour", time_hour, 0);
2390 rb_define_method(rb_cTime, "mday", time_mday, 0);
2391 rb_define_method(rb_cTime, "day", time_mday, 0);
2392 rb_define_method(rb_cTime, "mon", time_mon, 0);
2393 rb_define_method(rb_cTime, "month", time_mon, 0);
2394 rb_define_method(rb_cTime, "year", time_year, 0);
2395 rb_define_method(rb_cTime, "wday", time_wday, 0);
2396 rb_define_method(rb_cTime, "yday", time_yday, 0);
2397 rb_define_method(rb_cTime, "isdst", time_isdst, 0);
2398 rb_define_method(rb_cTime, "dst?", time_isdst, 0);
2399 rb_define_method(rb_cTime, "zone", time_zone, 0);
2400 rb_define_method(rb_cTime, "gmtoff", time_utc_offset, 0);
2401 rb_define_method(rb_cTime, "gmt_offset", time_utc_offset, 0);
2402 rb_define_method(rb_cTime, "utc_offset", time_utc_offset, 0);
2404 rb_define_method(rb_cTime, "utc?", time_utc_p, 0);
2405 rb_define_method(rb_cTime, "gmt?", time_utc_p, 0);
2407 rb_define_method(rb_cTime, "sunday?", time_sunday, 0);
2408 rb_define_method(rb_cTime, "monday?", time_monday, 0);
2409 rb_define_method(rb_cTime, "tuesday?", time_tuesday, 0);
2410 rb_define_method(rb_cTime, "wednesday?", time_wednesday, 0);
2411 rb_define_method(rb_cTime, "thursday?", time_thursday, 0);
2412 rb_define_method(rb_cTime, "friday?", time_friday, 0);
2413 rb_define_method(rb_cTime, "saturday?", time_saturday, 0);
2415 rb_define_method(rb_cTime, "tv_sec", time_to_i, 0);
2416 rb_define_method(rb_cTime, "tv_usec", time_usec, 0);
2417 rb_define_method(rb_cTime, "usec", time_usec, 0);
2418 rb_define_method(rb_cTime, "tv_nsec", time_nsec, 0);
2419 rb_define_method(rb_cTime, "nsec", time_nsec, 0);
2421 rb_define_method(rb_cTime, "strftime", time_strftime, 1);
2423 /* methods for marshaling */
2424 rb_define_method(rb_cTime, "_dump", time_dump, -1);
2425 rb_define_singleton_method(rb_cTime, "_load", time_load, 1);
2426 #if 0
2427 /* Time will support marshal_dump and marshal_load in the future (1.9 maybe) */
2428 rb_define_method(rb_cTime, "marshal_dump", time_mdump, 0);
2429 rb_define_method(rb_cTime, "marshal_load", time_mload, 1);
2430 #endif