* process.c (rb_spawn_internal): new function to specify
[ruby-svn.git] / time.c
bloba84c6f3d907896c6fcafd4f653b41b28a280af7b
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) free(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 t.tv_sec = (time_t)f;
205 if (f != t.tv_sec) {
206 rb_raise(rb_eRangeError, "%f out of Time range", RFLOAT_VALUE(num));
208 t.tv_nsec = (long)(d*1e9+0.5);
210 break;
212 case T_BIGNUM:
213 t.tv_sec = NUM2LONG(num);
214 if (interval && t.tv_sec < 0)
215 rb_raise(rb_eArgError, "%s must be positive", tstr);
216 t.tv_nsec = 0;
217 break;
219 default:
220 ary = rb_check_array_type(rb_funcall(num, id_divmod, 1, INT2FIX(1)));
221 if (NIL_P(ary)) {
222 rb_raise(rb_eTypeError, "can't convert %s into %s",
223 rb_obj_classname(num), tstr);
225 i = rb_ary_entry(ary, 0);
226 f = rb_ary_entry(ary, 1);
227 t.tv_sec = NUM2LONG(i);
228 if (interval && t.tv_sec < 0)
229 rb_raise(rb_eArgError, "%s must be positive", tstr);
230 f = rb_funcall(f, id_mul, 1, INT2FIX(1000000000));
231 t.tv_nsec = NUM2LONG(f);
232 break;
234 return t;
237 static struct timeval
238 time_timeval(VALUE num, int interval)
240 struct timespec ts;
241 struct timeval tv;
243 ts = time_timespec(num, interval);
244 tv.tv_sec = ts.tv_sec;
245 tv.tv_usec = ts.tv_nsec / 1000;
247 return tv;
250 struct timeval
251 rb_time_interval(VALUE num)
253 return time_timeval(num, Qtrue);
256 struct timeval
257 rb_time_timeval(VALUE time)
259 struct time_object *tobj;
260 struct timeval t;
262 if (TYPE(time) == T_DATA && RDATA(time)->dfree == time_free) {
263 GetTimeval(time, tobj);
264 t.tv_sec = tobj->ts.tv_sec;
265 t.tv_usec = tobj->ts.tv_nsec / 1000;
266 return t;
268 return time_timeval(time, Qfalse);
271 struct timespec
272 rb_time_timespec(VALUE time)
274 struct time_object *tobj;
275 struct timespec t;
277 if (TYPE(time) == T_DATA && RDATA(time)->dfree == time_free) {
278 GetTimeval(time, tobj);
279 t = tobj->ts;
280 return t;
282 return time_timespec(time, Qfalse);
286 * call-seq:
287 * Time.at(time) => time
288 * Time.at(seconds_with_frac) => time
289 * Time.at(seconds, microseconds_with_frac) => time
291 * Creates a new time object with the value given by <i>time</i>,
292 * the given number of <i>seconds_with_frac</i>, or
293 * <i>seconds</i> and <i>microseconds_with_frac</i> from the Epoch.
294 * <i>seconds_with_frac</i> and <i>microseconds_with_frac</i>
295 * can be Integer, Float, Rational, or other Numeric.
296 * non-portable feature allows the offset to be negative on some systems.
298 * Time.at(0) #=> 1969-12-31 18:00:00 -0600
299 * Time.at(Time.at(0)) #=> 1969-12-31 18:00:00 -0600
300 * Time.at(946702800) #=> 1999-12-31 23:00:00 -0600
301 * Time.at(-284061600) #=> 1960-12-31 00:00:00 -0600
302 * Time.at(946684800.2).usec #=> 200000
303 * Time.at(946684800, 123456.789).nsec #=> 123456789
306 static VALUE
307 time_s_at(int argc, VALUE *argv, VALUE klass)
309 struct timespec ts;
310 VALUE time, t;
312 if (rb_scan_args(argc, argv, "11", &time, &t) == 2) {
313 ts.tv_sec = NUM2LONG(time);
314 ts.tv_nsec = NUM2LONG(rb_funcall(t, id_mul, 1, INT2FIX(1000)));
316 else {
317 ts = rb_time_timespec(time);
319 t = time_new_internal(klass, ts.tv_sec, ts.tv_nsec);
320 if (TYPE(time) == T_DATA && RDATA(time)->dfree == time_free) {
321 struct time_object *tobj, *tobj2;
323 GetTimeval(time, tobj);
324 GetTimeval(t, tobj2);
325 tobj2->gmt = tobj->gmt;
327 return t;
330 static const char *months[] = {
331 "jan", "feb", "mar", "apr", "may", "jun",
332 "jul", "aug", "sep", "oct", "nov", "dec",
335 static long
336 obj2long(VALUE obj)
338 if (TYPE(obj) == T_STRING) {
339 obj = rb_str_to_inum(obj, 10, Qfalse);
342 return NUM2LONG(obj);
345 static long
346 obj2nsec(VALUE obj, long *nsec)
348 struct timespec ts;
350 if (TYPE(obj) == T_STRING) {
351 obj = rb_str_to_inum(obj, 10, Qfalse);
352 *nsec = 0;
353 return NUM2LONG(obj) * 1000;
356 ts = time_timespec(obj, 1);
357 *nsec = ts.tv_nsec;
358 return ts.tv_sec;
361 static long
362 obj2long1000(VALUE obj)
364 if (TYPE(obj) == T_STRING) {
365 obj = rb_str_to_inum(obj, 10, Qfalse);
366 return NUM2LONG(obj) * 1000;
369 return NUM2LONG(rb_funcall(obj, id_mul, 1, INT2FIX(1000)));
372 static void
373 time_arg(int argc, VALUE *argv, struct tm *tm, long *nsec)
375 VALUE v[8];
376 int i;
377 long year;
379 MEMZERO(tm, struct tm, 1);
380 *nsec = 0;
381 if (argc == 10) {
382 v[0] = argv[5];
383 v[1] = argv[4];
384 v[2] = argv[3];
385 v[3] = argv[2];
386 v[4] = argv[1];
387 v[5] = argv[0];
388 v[6] = Qnil;
389 tm->tm_isdst = RTEST(argv[8]) ? 1 : 0;
391 else {
392 rb_scan_args(argc, argv, "17", &v[0],&v[1],&v[2],&v[3],&v[4],&v[5],&v[6],&v[7]);
393 /* v[6] may be usec or zone (parsedate) */
394 /* v[7] is wday (parsedate; ignored) */
395 tm->tm_wday = -1;
396 tm->tm_isdst = -1;
399 year = obj2long(v[0]);
401 if (0 <= year && year < 39) {
402 rb_warning("2 digits year is used: %ld", year);
403 year += 100;
405 else if (69 <= year && year < 139) {
406 rb_warning("2 or 3 digits year is used: %ld", year);
408 else {
409 year -= 1900;
412 tm->tm_year = year;
414 if (NIL_P(v[1])) {
415 tm->tm_mon = 0;
417 else {
418 VALUE s = rb_check_string_type(v[1]);
419 if (!NIL_P(s)) {
420 tm->tm_mon = -1;
421 for (i=0; i<12; i++) {
422 if (RSTRING_LEN(s) == 3 &&
423 STRCASECMP(months[i], RSTRING_PTR(s)) == 0) {
424 tm->tm_mon = i;
425 break;
428 if (tm->tm_mon == -1) {
429 char c = RSTRING_PTR(s)[0];
431 if ('0' <= c && c <= '9') {
432 tm->tm_mon = obj2long(s)-1;
436 else {
437 tm->tm_mon = obj2long(v[1])-1;
440 if (NIL_P(v[2])) {
441 tm->tm_mday = 1;
443 else {
444 tm->tm_mday = obj2long(v[2]);
446 tm->tm_hour = NIL_P(v[3])?0:obj2long(v[3]);
447 tm->tm_min = NIL_P(v[4])?0:obj2long(v[4]);
448 if (!NIL_P(v[6]) && argc == 7) {
449 tm->tm_sec = NIL_P(v[5])?0:obj2long(v[5]);
450 *nsec = obj2long1000(v[6]);
452 else {
453 /* when argc == 8, v[6] is timezone, but ignored */
454 tm->tm_sec = NIL_P(v[5])?0:obj2nsec(v[5], nsec);
457 /* value validation */
458 if (
459 tm->tm_year != year ||
460 #ifndef NEGATIVE_TIME_T
461 tm->tm_year < 69 ||
462 #endif
463 tm->tm_mon < 0 || tm->tm_mon > 11
464 || tm->tm_mday < 1 || tm->tm_mday > 31
465 || tm->tm_hour < 0 || tm->tm_hour > 24
466 || (tm->tm_hour == 24 && (tm->tm_min > 0 || tm->tm_sec > 0))
467 || tm->tm_min < 0 || tm->tm_min > 59
468 || tm->tm_sec < 0 || tm->tm_sec > 60)
469 rb_raise(rb_eArgError, "argument out of range");
472 static VALUE time_gmtime(VALUE);
473 static VALUE time_localtime(VALUE);
474 static VALUE time_get_tm(VALUE, int);
476 static int
477 leap_year_p(long y)
479 return ((y % 4 == 0) && (y % 100 != 0)) || (y % 400 == 0);
482 #define DIV(n,d) ((n)<0 ? NDIV((n),(d)) : (n)/(d))
484 static time_t
485 timegm_noleapsecond(struct tm *tm)
487 static int common_year_yday_offset[] = {
489 -1 + 31,
490 -1 + 31 + 28,
491 -1 + 31 + 28 + 31,
492 -1 + 31 + 28 + 31 + 30,
493 -1 + 31 + 28 + 31 + 30 + 31,
494 -1 + 31 + 28 + 31 + 30 + 31 + 30,
495 -1 + 31 + 28 + 31 + 30 + 31 + 30 + 31,
496 -1 + 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31,
497 -1 + 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30,
498 -1 + 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31,
499 -1 + 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30
500 /* 1 2 3 4 5 6 7 8 9 10 11 */
502 static int leap_year_yday_offset[] = {
504 -1 + 31,
505 -1 + 31 + 29,
506 -1 + 31 + 29 + 31,
507 -1 + 31 + 29 + 31 + 30,
508 -1 + 31 + 29 + 31 + 30 + 31,
509 -1 + 31 + 29 + 31 + 30 + 31 + 30,
510 -1 + 31 + 29 + 31 + 30 + 31 + 30 + 31,
511 -1 + 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31,
512 -1 + 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30,
513 -1 + 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31,
514 -1 + 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30
515 /* 1 2 3 4 5 6 7 8 9 10 11 */
518 long tm_year = tm->tm_year;
519 int tm_yday = tm->tm_mday;
520 if (leap_year_p(tm_year + 1900))
521 tm_yday += leap_year_yday_offset[tm->tm_mon];
522 else
523 tm_yday += common_year_yday_offset[tm->tm_mon];
526 * `Seconds Since the Epoch' in SUSv3:
527 * tm_sec + tm_min*60 + tm_hour*3600 + tm_yday*86400 +
528 * (tm_year-70)*31536000 + ((tm_year-69)/4)*86400 -
529 * ((tm_year-1)/100)*86400 + ((tm_year+299)/400)*86400
531 return tm->tm_sec + tm->tm_min*60 + tm->tm_hour*3600 +
532 (time_t)(tm_yday +
533 (tm_year-70)*365 +
534 DIV(tm_year-69,4) -
535 DIV(tm_year-1,100) +
536 DIV(tm_year+299,400))*86400;
539 static int
540 tmcmp(struct tm *a, struct tm *b)
542 if (a->tm_year != b->tm_year)
543 return a->tm_year < b->tm_year ? -1 : 1;
544 else if (a->tm_mon != b->tm_mon)
545 return a->tm_mon < b->tm_mon ? -1 : 1;
546 else if (a->tm_mday != b->tm_mday)
547 return a->tm_mday < b->tm_mday ? -1 : 1;
548 else if (a->tm_hour != b->tm_hour)
549 return a->tm_hour < b->tm_hour ? -1 : 1;
550 else if (a->tm_min != b->tm_min)
551 return a->tm_min < b->tm_min ? -1 : 1;
552 else if (a->tm_sec != b->tm_sec)
553 return a->tm_sec < b->tm_sec ? -1 : 1;
554 else
555 return 0;
558 #if SIZEOF_TIME_T == SIZEOF_LONG
559 typedef unsigned long unsigned_time_t;
560 #elif SIZEOF_TIME_T == SIZEOF_INT
561 typedef unsigned int unsigned_time_t;
562 #elif SIZEOF_TIME_T == SIZEOF_LONG_LONG
563 typedef unsigned LONG_LONG unsigned_time_t;
564 #else
565 # error cannot find integer type which size is same as time_t.
566 #endif
568 static time_t
569 search_time_t(struct tm *tptr, int utc_p)
571 time_t guess, guess_lo, guess_hi;
572 struct tm *tm, tm_lo, tm_hi;
573 int d, have_guess;
574 int find_dst;
576 find_dst = 0 < tptr->tm_isdst;
578 #ifdef NEGATIVE_TIME_T
579 guess_lo = (time_t)~((unsigned_time_t)~(time_t)0 >> 1);
580 #else
581 guess_lo = 0;
582 #endif
583 guess_hi = ((time_t)-1) < ((time_t)0) ?
584 (time_t)((unsigned_time_t)~(time_t)0 >> 1) :
585 ~(time_t)0;
587 guess = timegm_noleapsecond(tptr);
588 tm = (utc_p ? gmtime : localtime)(&guess);
589 if (tm) {
590 d = tmcmp(tptr, tm);
591 if (d == 0) return guess;
592 if (d < 0) {
593 guess_hi = guess;
594 guess -= 24 * 60 * 60;
596 else {
597 guess_lo = guess;
598 guess += 24 * 60 * 60;
600 if (guess_lo < guess && guess < guess_hi &&
601 (tm = (utc_p ? gmtime : localtime)(&guess)) != NULL) {
602 d = tmcmp(tptr, tm);
603 if (d == 0) return guess;
604 if (d < 0)
605 guess_hi = guess;
606 else
607 guess_lo = guess;
611 tm = (utc_p ? gmtime : localtime)(&guess_lo);
612 if (!tm) goto error;
613 d = tmcmp(tptr, tm);
614 if (d < 0) goto out_of_range;
615 if (d == 0) return guess_lo;
616 tm_lo = *tm;
618 tm = (utc_p ? gmtime : localtime)(&guess_hi);
619 if (!tm) goto error;
620 d = tmcmp(tptr, tm);
621 if (d > 0) goto out_of_range;
622 if (d == 0) return guess_hi;
623 tm_hi = *tm;
625 have_guess = 0;
627 while (guess_lo + 1 < guess_hi) {
628 /* there is a gap between guess_lo and guess_hi. */
629 unsigned long range = 0;
630 if (!have_guess) {
631 int a, b;
633 Try precious guess by a linear interpolation at first.
634 `a' and `b' is a coefficient of guess_lo and guess_hi as:
636 guess = (guess_lo * a + guess_hi * b) / (a + b)
638 However this causes overflow in most cases, following assignment
639 is used instead:
641 guess = guess_lo / d * a + (guess_lo % d) * a / d
642 + guess_hi / d * b + (guess_hi % d) * b / d
643 where d = a + b
645 To avoid overflow in this assignment, `d' is restricted to less than
646 sqrt(2**31). By this restriction and other reasons, the guess is
647 not accurate and some error is expected. `range' approximates
648 the maximum error.
650 When these parameters are not suitable, i.e. guess is not within
651 guess_lo and guess_hi, simple guess by binary search is used.
653 range = 366 * 24 * 60 * 60;
654 a = (tm_hi.tm_year - tptr->tm_year);
655 b = (tptr->tm_year - tm_lo.tm_year);
656 /* 46000 is selected as `some big number less than sqrt(2**31)'. */
657 if (a + b <= 46000 / 12) {
658 range = 31 * 24 * 60 * 60;
659 a *= 12;
660 b *= 12;
661 a += tm_hi.tm_mon - tptr->tm_mon;
662 b += tptr->tm_mon - tm_lo.tm_mon;
663 if (a + b <= 46000 / 31) {
664 range = 24 * 60 * 60;
665 a *= 31;
666 b *= 31;
667 a += tm_hi.tm_mday - tptr->tm_mday;
668 b += tptr->tm_mday - tm_lo.tm_mday;
669 if (a + b <= 46000 / 24) {
670 range = 60 * 60;
671 a *= 24;
672 b *= 24;
673 a += tm_hi.tm_hour - tptr->tm_hour;
674 b += tptr->tm_hour - tm_lo.tm_hour;
675 if (a + b <= 46000 / 60) {
676 range = 60;
677 a *= 60;
678 b *= 60;
679 a += tm_hi.tm_min - tptr->tm_min;
680 b += tptr->tm_min - tm_lo.tm_min;
681 if (a + b <= 46000 / 60) {
682 range = 1;
683 a *= 60;
684 b *= 60;
685 a += tm_hi.tm_sec - tptr->tm_sec;
686 b += tptr->tm_sec - tm_lo.tm_sec;
692 if (a <= 0) a = 1;
693 if (b <= 0) b = 1;
694 d = a + b;
696 Although `/' and `%' may produce unexpected result with negative
697 argument, it doesn't cause serious problem because there is a
698 fail safe.
700 guess = guess_lo / d * a + (guess_lo % d) * a / d
701 + guess_hi / d * b + (guess_hi % d) * b / d;
702 have_guess = 1;
705 if (guess <= guess_lo || guess_hi <= guess) {
706 /* Precious guess is invalid. try binary search. */
707 guess = guess_lo / 2 + guess_hi / 2;
708 if (guess <= guess_lo)
709 guess = guess_lo + 1;
710 else if (guess >= guess_hi)
711 guess = guess_hi - 1;
712 range = 0;
715 tm = (utc_p ? gmtime : localtime)(&guess);
716 if (!tm) goto error;
717 have_guess = 0;
719 d = tmcmp(tptr, tm);
720 if (d < 0) {
721 guess_hi = guess;
722 tm_hi = *tm;
723 if (range) {
724 guess = guess - range;
725 range = 0;
726 if (guess_lo < guess && guess < guess_hi)
727 have_guess = 1;
730 else if (d > 0) {
731 guess_lo = guess;
732 tm_lo = *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 {
741 if (!utc_p) {
742 /* If localtime is nonmonotonic, another result may exist. */
743 time_t guess2;
744 if (find_dst) {
745 guess2 = guess - 2 * 60 * 60;
746 tm = localtime(&guess2);
747 if (tm) {
748 if (tptr->tm_hour != (tm->tm_hour + 2) % 24 ||
749 tptr->tm_min != tm->tm_min ||
750 tptr->tm_sec != tm->tm_sec
752 guess2 -= (tm->tm_hour - tptr->tm_hour) * 60 * 60 +
753 (tm->tm_min - tptr->tm_min) * 60 +
754 (tm->tm_sec - tptr->tm_sec);
755 if (tptr->tm_mday != tm->tm_mday)
756 guess2 += 24 * 60 * 60;
757 if (guess != guess2) {
758 tm = localtime(&guess2);
759 if (tmcmp(tptr, tm) == 0) {
760 if (guess < guess2)
761 return guess;
762 else
763 return guess2;
769 else {
770 guess2 = guess + 2 * 60 * 60;
771 tm = localtime(&guess2);
772 if (tm) {
773 if ((tptr->tm_hour + 2) % 24 != tm->tm_hour ||
774 tptr->tm_min != tm->tm_min ||
775 tptr->tm_sec != tm->tm_sec
777 guess2 -= (tm->tm_hour - tptr->tm_hour) * 60 * 60 +
778 (tm->tm_min - tptr->tm_min) * 60 +
779 (tm->tm_sec - tptr->tm_sec);
780 if (tptr->tm_mday != tm->tm_mday)
781 guess2 -= 24 * 60 * 60;
782 if (guess != guess2) {
783 tm = localtime(&guess2);
784 if (tmcmp(tptr, tm) == 0) {
785 if (guess < guess2)
786 return guess2;
787 else
788 return guess;
795 return guess;
798 /* Given argument has no corresponding time_t. Let's outerpolation. */
799 if (tm_lo.tm_year == tptr->tm_year && tm_lo.tm_mon == tptr->tm_mon) {
800 return guess_lo +
801 (tptr->tm_mday - tm_lo.tm_mday) * 24 * 60 * 60 +
802 (tptr->tm_hour - tm_lo.tm_hour) * 60 * 60 +
803 (tptr->tm_min - tm_lo.tm_min) * 60 +
804 (tptr->tm_sec - tm_lo.tm_sec);
806 else if (tm_hi.tm_year == tptr->tm_year && tm_hi.tm_mon == tptr->tm_mon) {
807 return guess_hi +
808 (tptr->tm_mday - tm_hi.tm_mday) * 24 * 60 * 60 +
809 (tptr->tm_hour - tm_hi.tm_hour) * 60 * 60 +
810 (tptr->tm_min - tm_hi.tm_min) * 60 +
811 (tptr->tm_sec - tm_hi.tm_sec);
814 out_of_range:
815 rb_raise(rb_eArgError, "time out of range");
817 error:
818 rb_raise(rb_eArgError, "gmtime/localtime error");
819 return 0; /* not reached */
822 static time_t
823 make_time_t(struct tm *tptr, int utc_p)
825 time_t t;
826 struct tm *tmp, buf;
827 buf = *tptr;
828 if (utc_p) {
829 #if defined(HAVE_TIMEGM)
830 if ((t = timegm(&buf)) != -1)
831 return t;
832 #ifdef NEGATIVE_TIME_T
833 if ((tmp = gmtime(&t)) &&
834 tptr->tm_year == tmp->tm_year &&
835 tptr->tm_mon == tmp->tm_mon &&
836 tptr->tm_mday == tmp->tm_mday &&
837 tptr->tm_hour == tmp->tm_hour &&
838 tptr->tm_min == tmp->tm_min &&
839 tptr->tm_sec == tmp->tm_sec
841 return t;
842 #endif
843 #endif
844 return search_time_t(&buf, utc_p);
846 else {
847 #if defined(HAVE_MKTIME)
848 if ((t = mktime(&buf)) != -1)
849 return t;
850 #ifdef NEGATIVE_TIME_T
851 if ((tmp = localtime(&t)) &&
852 tptr->tm_year == tmp->tm_year &&
853 tptr->tm_mon == tmp->tm_mon &&
854 tptr->tm_mday == tmp->tm_mday &&
855 tptr->tm_hour == tmp->tm_hour &&
856 tptr->tm_min == tmp->tm_min &&
857 tptr->tm_sec == tmp->tm_sec
859 return t;
860 #endif
861 #endif
862 return search_time_t(&buf, utc_p);
866 static VALUE
867 time_utc_or_local(int argc, VALUE *argv, int utc_p, VALUE klass)
869 struct tm tm;
870 VALUE time;
871 long nsec;
873 time_arg(argc, argv, &tm, &nsec);
874 time = time_new_internal(klass, make_time_t(&tm, utc_p), nsec);
875 if (utc_p) return time_gmtime(time);
876 return time_localtime(time);
880 * call-seq:
881 * Time.utc(year) => time
882 * Time.utc(year, month) => time
883 * Time.utc(year, month, day) => time
884 * Time.utc(year, month, day, hour) => time
885 * Time.utc(year, month, day, hour, min) => time
886 * Time.utc(year, month, day, hour, min, sec_with_frac) => time
887 * Time.utc(year, month, day, hour, min, sec, usec_with_frac) => time
888 * Time.utc(sec, min, hour, day, month, year, wday, yday, isdst, tz) => time
889 * Time.gm(year) => time
890 * Time.gm(year, month) => time
891 * Time.gm(year, month, day) => time
892 * Time.gm(year, month, day, hour) => time
893 * Time.gm(year, month, day, hour, min) => time
894 * Time.gm(year, month, day, hour, min, sec_with_frac) => time
895 * Time.gm(year, month, day, hour, min, sec, usec_with_frac) => time
896 * Time.gm(sec, min, hour, day, month, year, wday, yday, isdst, tz) => time
898 * Creates a time based on given values, interpreted as UTC (GMT). The
899 * year must be specified. Other values default to the minimum value
900 * for that field (and may be <code>nil</code> or omitted). Months may
901 * be specified by numbers from 1 to 12, or by the three-letter English
902 * month names. Hours are specified on a 24-hour clock (0..23). Raises
903 * an <code>ArgumentError</code> if any values are out of range. Will
904 * also accept ten arguments in the order output by
905 * <code>Time#to_a</code>.
906 * <i>sec_with_frac</i> and <i>usec_with_frac</i> can have a fractional part.
908 * Time.utc(2000,"jan",1,20,15,1) #=> 2000-01-01 20:15:01 UTC
909 * Time.gm(2000,"jan",1,20,15,1) #=> 2000-01-01 20:15:01 UTC
911 static VALUE
912 time_s_mkutc(int argc, VALUE *argv, VALUE klass)
914 return time_utc_or_local(argc, argv, Qtrue, klass);
918 * call-seq:
919 * Time.local(year) => time
920 * Time.local(year, month) => time
921 * Time.local(year, month, day) => time
922 * Time.local(year, month, day, hour) => time
923 * Time.local(year, month, day, hour, min) => time
924 * Time.local(year, month, day, hour, min, sec_with_frac) => time
925 * Time.local(year, month, day, hour, min, sec, usec_with_frac) => time
926 * Time.local(sec, min, hour, day, month, year, wday, yday, isdst, tz) => time
927 * Time.mktime(year) => time
928 * Time.mktime(year, month) => time
929 * Time.mktime(year, month, day) => time
930 * Time.mktime(year, month, day, hour) => time
931 * Time.mktime(year, month, day, hour, min) => time
932 * Time.mktime(year, month, day, hour, min, sec_with_frac) => time
933 * Time.mktime(year, month, day, hour, min, sec, usec_with_frac) => time
934 * Time.mktime(sec, min, hour, day, month, year, wday, yday, isdst, tz) => time
936 * Same as <code>Time::gm</code>, but interprets the values in the
937 * local time zone.
939 * Time.local(2000,"jan",1,20,15,1) #=> 2000-01-01 20:15:01 -0600
942 static VALUE
943 time_s_mktime(int argc, VALUE *argv, VALUE klass)
945 return time_utc_or_local(argc, argv, Qfalse, klass);
949 * call-seq:
950 * time.to_i => int
951 * time.tv_sec => int
953 * Returns the value of <i>time</i> as an integer number of seconds
954 * since the Epoch.
956 * t = Time.now
957 * "%10.5f" % t.to_f #=> "1049896564.17839"
958 * t.to_i #=> 1049896564
961 static VALUE
962 time_to_i(VALUE time)
964 struct time_object *tobj;
966 GetTimeval(time, tobj);
967 return LONG2NUM(tobj->ts.tv_sec);
971 * call-seq:
972 * time.to_f => float
974 * Returns the value of <i>time</i> as a floating point number of
975 * seconds since the Epoch.
977 * t = Time.now
978 * "%10.5f" % t.to_f #=> "1049896564.13654"
979 * t.to_i #=> 1049896564
981 * Note that IEEE 754 double is not accurate enough to represent
982 * nanoseconds from the Epoch.
985 static VALUE
986 time_to_f(VALUE time)
988 struct time_object *tobj;
990 GetTimeval(time, tobj);
991 return DOUBLE2NUM((double)tobj->ts.tv_sec+(double)tobj->ts.tv_nsec/1e9);
995 * call-seq:
996 * time.usec => int
997 * time.tv_usec => int
999 * Returns just the number of microseconds for <i>time</i>.
1001 * t = Time.now #=> 2007-11-19 08:03:26 -0600
1002 * "%10.6f" % t.to_f #=> "1195481006.775195"
1003 * t.usec #=> 775195
1006 static VALUE
1007 time_usec(VALUE time)
1009 struct time_object *tobj;
1011 GetTimeval(time, tobj);
1012 return LONG2NUM(tobj->ts.tv_nsec/1000);
1016 * call-seq:
1017 * time.nsec => int
1018 * time.tv_nsec => int
1020 * Returns just the number of nanoseconds for <i>time</i>.
1022 * t = Time.now #=> 2007-11-17 15:18:03 +0900
1023 * "%10.9f" % t.to_f #=> "1195280283.536151409"
1024 * t.nsec #=> 536151406
1026 * The lowest digit of to_f and nsec is different because
1027 * IEEE 754 double is not accurate enough to represent
1028 * nanoseconds from the Epoch.
1029 * The accurate value is returned by nsec.
1032 static VALUE
1033 time_nsec(VALUE time)
1035 struct time_object *tobj;
1037 GetTimeval(time, tobj);
1038 return LONG2NUM(tobj->ts.tv_nsec);
1042 * call-seq:
1043 * time <=> other_time => -1, 0, +1
1045 * Comparison---Compares <i>time</i> with <i>other_time</i>.
1047 * t = Time.now #=> 2007-11-19 08:12:12 -0600
1048 * t2 = t + 2592000 #=> 2007-12-19 08:12:12 -0600
1049 * t <=> t2 #=> -1
1050 * t2 <=> t #=> 1
1052 * t = Time.now #=> 2007-11-19 08:13:38 -0600
1053 * t2 = t + 0.1 #=> 2007-11-19 08:13:38 -0600
1054 * t.nsec #=> 98222999
1055 * t2.nsec #=> 198222999
1056 * t <=> t2 #=> -1
1057 * t2 <=> t #=> 1
1058 * t <=> t #=> 0
1061 static VALUE
1062 time_cmp(VALUE time1, VALUE time2)
1064 struct time_object *tobj1, *tobj2;
1066 GetTimeval(time1, tobj1);
1067 if (TYPE(time2) == T_DATA && RDATA(time2)->dfree == time_free) {
1068 GetTimeval(time2, tobj2);
1069 if (tobj1->ts.tv_sec == tobj2->ts.tv_sec) {
1070 if (tobj1->ts.tv_nsec == tobj2->ts.tv_nsec) return INT2FIX(0);
1071 if (tobj1->ts.tv_nsec > tobj2->ts.tv_nsec) return INT2FIX(1);
1072 return INT2FIX(-1);
1074 if (tobj1->ts.tv_sec > tobj2->ts.tv_sec) return INT2FIX(1);
1075 return INT2FIX(-1);
1078 return Qnil;
1082 * call-seq:
1083 * time.eql?(other_time)
1085 * Return <code>true</code> if <i>time</i> and <i>other_time</i> are
1086 * both <code>Time</code> objects with the same seconds and fractional
1087 * seconds.
1090 static VALUE
1091 time_eql(VALUE time1, VALUE time2)
1093 struct time_object *tobj1, *tobj2;
1095 GetTimeval(time1, tobj1);
1096 if (TYPE(time2) == T_DATA && RDATA(time2)->dfree == time_free) {
1097 GetTimeval(time2, tobj2);
1098 if (tobj1->ts.tv_sec == tobj2->ts.tv_sec) {
1099 if (tobj1->ts.tv_nsec == tobj2->ts.tv_nsec) return Qtrue;
1102 return Qfalse;
1106 * call-seq:
1107 * time.utc? => true or false
1108 * time.gmt? => true or false
1110 * Returns <code>true</code> if <i>time</i> represents a time in UTC
1111 * (GMT).
1113 * t = Time.now #=> 2007-11-19 08:15:23 -0600
1114 * t.utc? #=> false
1115 * t = Time.gm(2000,"jan",1,20,15,1) #=> 2000-01-01 20:15:01 UTC
1116 * t.utc? #=> true
1118 * t = Time.now #=> 2007-11-19 08:16:03 -0600
1119 * t.gmt? #=> false
1120 * t = Time.gm(2000,1,1,20,15,1) #=> 2000-01-01 20:15:01 UTC
1121 * t.gmt? #=> true
1124 static VALUE
1125 time_utc_p(VALUE time)
1127 struct time_object *tobj;
1129 GetTimeval(time, tobj);
1130 if (tobj->gmt) return Qtrue;
1131 return Qfalse;
1135 * call-seq:
1136 * time.hash => fixnum
1138 * Return a hash code for this time object.
1141 static VALUE
1142 time_hash(VALUE time)
1144 struct time_object *tobj;
1145 long hash;
1147 GetTimeval(time, tobj);
1148 hash = tobj->ts.tv_sec ^ tobj->ts.tv_nsec;
1149 return LONG2FIX(hash);
1152 /* :nodoc: */
1153 static VALUE
1154 time_init_copy(VALUE copy, VALUE time)
1156 struct time_object *tobj, *tcopy;
1158 if (copy == time) return copy;
1159 time_modify(copy);
1160 if (TYPE(time) != T_DATA || RDATA(time)->dfree != time_free) {
1161 rb_raise(rb_eTypeError, "wrong argument type");
1163 GetTimeval(time, tobj);
1164 GetTimeval(copy, tcopy);
1165 MEMCPY(tcopy, tobj, struct time_object, 1);
1167 return copy;
1170 static VALUE
1171 time_dup(VALUE time)
1173 VALUE dup = time_s_alloc(CLASS_OF(time));
1174 time_init_copy(dup, time);
1175 return dup;
1179 * call-seq:
1180 * time.localtime => time
1182 * Converts <i>time</i> to local time (using the local time zone in
1183 * effect for this process) modifying the receiver.
1185 * t = Time.gm(2000, "jan", 1, 20, 15, 1) #=> 2000-01-01 20:15:01 UTC
1186 * t.gmt? #=> true
1187 * t.localtime #=> 2000-01-01 14:15:01 -0600
1188 * t.gmt? #=> false
1191 static VALUE
1192 time_localtime(VALUE time)
1194 struct time_object *tobj;
1195 struct tm *tm_tmp;
1196 time_t t;
1198 GetTimeval(time, tobj);
1199 if (!tobj->gmt) {
1200 if (tobj->tm_got)
1201 return time;
1203 else {
1204 time_modify(time);
1206 t = tobj->ts.tv_sec;
1207 tm_tmp = localtime(&t);
1208 if (!tm_tmp)
1209 rb_raise(rb_eArgError, "localtime error");
1210 tobj->tm = *tm_tmp;
1211 tobj->tm_got = 1;
1212 tobj->gmt = 0;
1213 return time;
1217 * call-seq:
1218 * time.gmtime => time
1219 * time.utc => time
1221 * Converts <i>time</i> to UTC (GMT), modifying the receiver.
1223 * t = Time.now #=> 2007-11-19 08:18:31 -0600
1224 * t.gmt? #=> false
1225 * t.gmtime #=> 2007-11-19 14:18:31 UTC
1226 * t.gmt? #=> true
1228 * t = Time.now #=> 2007-11-19 08:18:51 -0600
1229 * t.utc? #=> false
1230 * t.utc #=> 2007-11-19 14:18:51 UTC
1231 * t.utc? #=> true
1234 static VALUE
1235 time_gmtime(VALUE time)
1237 struct time_object *tobj;
1238 struct tm *tm_tmp;
1239 time_t t;
1241 GetTimeval(time, tobj);
1242 if (tobj->gmt) {
1243 if (tobj->tm_got)
1244 return time;
1246 else {
1247 time_modify(time);
1249 t = tobj->ts.tv_sec;
1250 tm_tmp = gmtime(&t);
1251 if (!tm_tmp)
1252 rb_raise(rb_eArgError, "gmtime error");
1253 tobj->tm = *tm_tmp;
1254 tobj->tm_got = 1;
1255 tobj->gmt = 1;
1256 return time;
1260 * call-seq:
1261 * time.getlocal => new_time
1263 * Returns a new <code>new_time</code> object representing <i>time</i> in
1264 * local time (using the local time zone in effect for this process).
1266 * t = Time.gm(2000,1,1,20,15,1) #=> 2000-01-01 20:15:01 UTC
1267 * t.gmt? #=> true
1268 * l = t.getlocal #=> 2000-01-01 14:15:01 -0600
1269 * l.gmt? #=> false
1270 * t == l #=> true
1273 static VALUE
1274 time_getlocaltime(VALUE time)
1276 return time_localtime(time_dup(time));
1280 * call-seq:
1281 * time.getgm => new_time
1282 * time.getutc => new_time
1284 * Returns a new <code>new_time</code> object representing <i>time</i> in
1285 * UTC.
1287 * t = Time.local(2000,1,1,20,15,1) #=> 2000-01-01 20:15:01 -0600
1288 * t.gmt? #=> false
1289 * y = t.getgm #=> 2000-01-02 02:15:01 UTC
1290 * y.gmt? #=> true
1291 * t == y #=> true
1294 static VALUE
1295 time_getgmtime(VALUE time)
1297 return time_gmtime(time_dup(time));
1300 static VALUE
1301 time_get_tm(VALUE time, int gmt)
1303 if (gmt) return time_gmtime(time);
1304 return time_localtime(time);
1308 * call-seq:
1309 * time.asctime => string
1310 * time.ctime => string
1312 * Returns a canonical string representation of <i>time</i>.
1314 * Time.now.asctime #=> "Wed Apr 9 08:56:03 2003"
1317 static VALUE
1318 time_asctime(VALUE time)
1320 struct time_object *tobj;
1321 char *s;
1323 GetTimeval(time, tobj);
1324 if (tobj->tm_got == 0) {
1325 time_get_tm(time, tobj->gmt);
1327 s = asctime(&tobj->tm);
1328 if (s[24] == '\n') s[24] = '\0';
1330 return rb_str_new2(s);
1334 * call-seq:
1335 * time.inspect => string
1336 * time.to_s => string
1338 * Returns a string representing <i>time</i>. Equivalent to calling
1339 * <code>Time#strftime</code> with a format string of
1340 * ``<code>%Y-%m-%d</code> <code>%H:%M:%S</code> <code>%z</code>''
1341 * for a local time and
1342 * ``<code>%Y-%m-%d</code> <code>%H:%M:%S</code> <code>UTC</code>''
1343 * for a UTC time.
1345 * Time.now.to_s #=> "2007-10-05 16:09:51 +0900"
1346 * Time.now.utc.to_s #=> "2007-10-05 07:09:51 UTC"
1349 static VALUE
1350 time_to_s(VALUE time)
1352 struct time_object *tobj;
1353 char buf[128];
1354 int len;
1356 GetTimeval(time, tobj);
1357 if (tobj->tm_got == 0) {
1358 time_get_tm(time, tobj->gmt);
1360 if (tobj->gmt == 1) {
1361 len = strftime(buf, 128, "%Y-%m-%d %H:%M:%S UTC", &tobj->tm);
1363 else {
1364 long off;
1365 char sign = '+';
1366 #if defined(HAVE_STRUCT_TM_TM_GMTOFF)
1367 off = tobj->tm.tm_gmtoff;
1368 #else
1369 VALUE tmp = time_utc_offset(time);
1370 off = NUM2INT(tmp);
1371 #endif
1372 if (off < 0) {
1373 sign = '-';
1374 off = -off;
1376 len = strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S ", &tobj->tm);
1377 len += snprintf(buf+len, sizeof(buf)-len, "%c%02d%02d", sign,
1378 (int)(off/3600), (int)(off%3600/60));
1380 return rb_str_new(buf, len);
1383 static VALUE
1384 time_add(struct time_object *tobj, VALUE offset, int sign)
1386 double v = NUM2DBL(offset);
1387 double f, d;
1388 unsigned_time_t sec_off;
1389 time_t sec;
1390 long nsec_off, nsec;
1391 VALUE result;
1393 if (v < 0) {
1394 v = -v;
1395 sign = -sign;
1397 d = modf(v, &f);
1398 sec_off = (unsigned_time_t)f;
1399 if (f != (double)sec_off)
1400 rb_raise(rb_eRangeError, "time %s %f out of Time range",
1401 sign < 0 ? "-" : "+", v);
1402 nsec_off = (long)(d*1e9+0.5);
1404 if (sign < 0) {
1405 sec = tobj->ts.tv_sec - sec_off;
1406 nsec = tobj->ts.tv_nsec - nsec_off;
1407 if (sec > tobj->ts.tv_sec)
1408 rb_raise(rb_eRangeError, "time - %f out of Time range", v);
1410 else {
1411 sec = tobj->ts.tv_sec + sec_off;
1412 nsec = tobj->ts.tv_nsec + nsec_off;
1413 if (sec < tobj->ts.tv_sec)
1414 rb_raise(rb_eRangeError, "time + %f out of Time range", v);
1416 result = rb_time_nano_new(sec, nsec);
1417 if (tobj->gmt) {
1418 GetTimeval(result, tobj);
1419 tobj->gmt = 1;
1421 return result;
1425 * call-seq:
1426 * time + numeric => time
1428 * Addition---Adds some number of seconds (possibly fractional) to
1429 * <i>time</i> and returns that value as a new time.
1431 * t = Time.now #=> 2007-11-19 08:22:21 -0600
1432 * t + (60 * 60 * 24) #=> 2007-11-20 08:22:21 -0600
1435 static VALUE
1436 time_plus(VALUE time1, VALUE time2)
1438 struct time_object *tobj;
1439 GetTimeval(time1, tobj);
1441 if (TYPE(time2) == T_DATA && RDATA(time2)->dfree == time_free) {
1442 rb_raise(rb_eTypeError, "time + time?");
1444 return time_add(tobj, time2, 1);
1448 * call-seq:
1449 * time - other_time => float
1450 * time - numeric => time
1452 * Difference---Returns a new time that represents the difference
1453 * between two times, or subtracts the given number of seconds in
1454 * <i>numeric</i> from <i>time</i>.
1456 * t = Time.now #=> 2007-11-19 08:23:10 -0600
1457 * t2 = t + 2592000 #=> 2007-12-19 08:23:10 -0600
1458 * t2 - t #=> 2592000.0
1459 * t2 - 2592000 #=> 2007-11-19 08:23:10 -0600
1462 static VALUE
1463 time_minus(VALUE time1, VALUE time2)
1465 struct time_object *tobj;
1467 GetTimeval(time1, tobj);
1468 if (TYPE(time2) == T_DATA && RDATA(time2)->dfree == time_free) {
1469 struct time_object *tobj2;
1470 double f;
1472 GetTimeval(time2, tobj2);
1473 if (tobj->ts.tv_sec < tobj2->ts.tv_sec)
1474 f = -(double)(unsigned_time_t)(tobj2->ts.tv_sec - tobj->ts.tv_sec);
1475 else
1476 f = (double)(unsigned_time_t)(tobj->ts.tv_sec - tobj2->ts.tv_sec);
1477 f += ((double)tobj->ts.tv_nsec - (double)tobj2->ts.tv_nsec)*1e-9;
1479 return DOUBLE2NUM(f);
1481 return time_add(tobj, time2, -1);
1485 * call-seq:
1486 * time.succ => new_time
1488 * Return a new time object, one second later than <code>time</code>.
1490 * t = Time.now #=> 2007-11-19 08:23:57 -0600
1491 * t.succ #=> 2007-11-19 08:23:58 -0600
1494 static VALUE
1495 time_succ(VALUE time)
1497 struct time_object *tobj;
1498 int gmt;
1500 GetTimeval(time, tobj);
1501 gmt = tobj->gmt;
1502 time = rb_time_nano_new(tobj->ts.tv_sec + 1, tobj->ts.tv_nsec);
1503 GetTimeval(time, tobj);
1504 tobj->gmt = gmt;
1505 return time;
1508 VALUE
1509 rb_time_succ(VALUE time)
1511 return time_succ(time);
1515 * call-seq:
1516 * time.sec => fixnum
1518 * Returns the second of the minute (0..60)<em>[Yes, seconds really can
1519 * range from zero to 60. This allows the system to inject leap seconds
1520 * every now and then to correct for the fact that years are not really
1521 * a convenient number of hours long.]</em> for <i>time</i>.
1523 * t = Time.now #=> 2007-11-19 08:25:02 -0600
1524 * t.sec #=> 2
1527 static VALUE
1528 time_sec(VALUE time)
1530 struct time_object *tobj;
1532 GetTimeval(time, tobj);
1533 if (tobj->tm_got == 0) {
1534 time_get_tm(time, tobj->gmt);
1536 return INT2FIX(tobj->tm.tm_sec);
1540 * call-seq:
1541 * time.min => fixnum
1543 * Returns the minute of the hour (0..59) for <i>time</i>.
1545 * t = Time.now #=> 2007-11-19 08:25:51 -0600
1546 * t.min #=> 25
1549 static VALUE
1550 time_min(VALUE time)
1552 struct time_object *tobj;
1554 GetTimeval(time, tobj);
1555 if (tobj->tm_got == 0) {
1556 time_get_tm(time, tobj->gmt);
1558 return INT2FIX(tobj->tm.tm_min);
1562 * call-seq:
1563 * time.hour => fixnum
1565 * Returns the hour of the day (0..23) for <i>time</i>.
1567 * t = Time.now #=> 2007-11-19 08:26:20 -0600
1568 * t.hour #=> 8
1571 static VALUE
1572 time_hour(VALUE time)
1574 struct time_object *tobj;
1576 GetTimeval(time, tobj);
1577 if (tobj->tm_got == 0) {
1578 time_get_tm(time, tobj->gmt);
1580 return INT2FIX(tobj->tm.tm_hour);
1584 * call-seq:
1585 * time.day => fixnum
1586 * time.mday => fixnum
1588 * Returns the day of the month (1..n) for <i>time</i>.
1590 * t = Time.now #=> 2007-11-19 08:27:03 -0600
1591 * t.day #=> 19
1592 * t.mday #=> 19
1595 static VALUE
1596 time_mday(VALUE time)
1598 struct time_object *tobj;
1600 GetTimeval(time, tobj);
1601 if (tobj->tm_got == 0) {
1602 time_get_tm(time, tobj->gmt);
1604 return INT2FIX(tobj->tm.tm_mday);
1608 * call-seq:
1609 * time.mon => fixnum
1610 * time.month => fixnum
1612 * Returns the month of the year (1..12) for <i>time</i>.
1614 * t = Time.now #=> 2007-11-19 08:27:30 -0600
1615 * t.mon #=> 11
1616 * t.month #=> 11
1619 static VALUE
1620 time_mon(VALUE time)
1622 struct time_object *tobj;
1624 GetTimeval(time, tobj);
1625 if (tobj->tm_got == 0) {
1626 time_get_tm(time, tobj->gmt);
1628 return INT2FIX(tobj->tm.tm_mon+1);
1632 * call-seq:
1633 * time.year => fixnum
1635 * Returns the year for <i>time</i> (including the century).
1637 * t = Time.now #=> 2007-11-19 08:27:51 -0600
1638 * t.year #=> 2007
1641 static VALUE
1642 time_year(VALUE time)
1644 struct time_object *tobj;
1646 GetTimeval(time, tobj);
1647 if (tobj->tm_got == 0) {
1648 time_get_tm(time, tobj->gmt);
1650 return LONG2NUM((long)tobj->tm.tm_year+1900);
1654 * call-seq:
1655 * time.wday => fixnum
1657 * Returns an integer representing the day of the week, 0..6, with
1658 * Sunday == 0.
1660 * t = Time.now #=> 2007-11-20 02:35:35 -0600
1661 * t.wday #=> 2
1662 * t.sunday? #=> false
1663 * t.monday? #=> false
1664 * t.tuesday? #=> true
1665 * t.wednesday? #=> false
1666 * t.thursday? #=> false
1667 * t.friday? #=> false
1668 * t.saturday? #=> false
1671 static VALUE
1672 time_wday(VALUE time)
1674 struct time_object *tobj;
1676 GetTimeval(time, tobj);
1677 if (tobj->tm_got == 0) {
1678 time_get_tm(time, tobj->gmt);
1680 return INT2FIX(tobj->tm.tm_wday);
1683 #define wday_p(n) {\
1684 struct time_object *tobj;\
1685 GetTimeval(time, tobj);\
1686 if (tobj->tm_got == 0) {\
1687 time_get_tm(time, tobj->gmt);\
1689 return (tobj->tm.tm_wday == (n)) ? Qtrue : Qfalse;\
1693 * call-seq:
1694 * time.sunday? => true or false
1696 * Returns <code>true</code> if <i>time</i> represents Sunday.
1698 * t = Time.local(1990, 4, 1) #=> 1990-04-01 00:00:00 -0600
1699 * t.sunday? #=> true
1702 static VALUE
1703 time_sunday(VALUE time)
1705 wday_p(0);
1709 * call-seq:
1710 * time.monday? => true or false
1712 * Returns <code>true</code> if <i>time</i> represents Monday.
1714 * t = Time.local(2003, 8, 4) #=> 2003-08-04 00:00:00 -0500
1715 * p t.monday? #=> true
1718 static VALUE
1719 time_monday(VALUE time)
1721 wday_p(1);
1725 * call-seq:
1726 * time.tuesday? => true or false
1728 * Returns <code>true</code> if <i>time</i> represents Tuesday.
1730 * t = Time.local(1991, 2, 19) #=> 1991-02-19 00:00:00 -0600
1731 * p t.tuesday? #=> true
1734 static VALUE
1735 time_tuesday(VALUE time)
1737 wday_p(2);
1741 * call-seq:
1742 * time.wednesday? => true or false
1744 * Returns <code>true</code> if <i>time</i> represents Wednesday.
1746 * t = Time.local(1993, 2, 24) #=> 1993-02-24 00:00:00 -0600
1747 * p t.wednesday? #=> true
1750 static VALUE
1751 time_wednesday(VALUE time)
1753 wday_p(3);
1757 * call-seq:
1758 * time.thursday? => true or false
1760 * Returns <code>true</code> if <i>time</i> represents Thursday.
1762 * t = Time.local(1995, 12, 21) #=> 1995-12-21 00:00:00 -0600
1763 * p t.thursday? #=> true
1766 static VALUE
1767 time_thursday(VALUE time)
1769 wday_p(4);
1773 * call-seq:
1774 * time.friday? => true or false
1776 * Returns <code>true</code> if <i>time</i> represents Friday.
1778 * t = Time.local(1987, 12, 18) #=> 1987-12-18 00:00:00 -0600
1779 * t.friday? #=> true
1782 static VALUE
1783 time_friday(VALUE time)
1785 wday_p(5);
1789 * call-seq:
1790 * time.saturday? => true or false
1792 * Returns <code>true</code> if <i>time</i> represents Saturday.
1794 * t = Time.local(2006, 6, 10) #=> 2006-06-10 00:00:00 -0500
1795 * t.saturday? #=> true
1798 static VALUE
1799 time_saturday(VALUE time)
1801 wday_p(6);
1805 * call-seq:
1806 * time.yday => fixnum
1808 * Returns an integer representing the day of the year, 1..366.
1810 * t = Time.now #=> 2007-11-19 08:32:31 -0600
1811 * t.yday #=> 323
1814 static VALUE
1815 time_yday(VALUE time)
1817 struct time_object *tobj;
1819 GetTimeval(time, tobj);
1820 if (tobj->tm_got == 0) {
1821 time_get_tm(time, tobj->gmt);
1823 return INT2FIX(tobj->tm.tm_yday+1);
1827 * call-seq:
1828 * time.isdst => true or false
1829 * time.dst? => true or false
1831 * Returns <code>true</code> if <i>time</i> occurs during Daylight
1832 * Saving Time in its time zone.
1834 * # CST6CDT:
1835 * Time.local(2000, 1, 1).zone #=> "CST"
1836 * Time.local(2000, 1, 1).isdst #=> false
1837 * Time.local(2000, 1, 1).dst? #=> false
1838 * Time.local(2000, 7, 1).zone #=> "CDT"
1839 * Time.local(2000, 7, 1).isdst #=> true
1840 * Time.local(2000, 7, 1).dst? #=> true
1842 * # Asia/Tokyo:
1843 * Time.local(2000, 1, 1).zone #=> "JST"
1844 * Time.local(2000, 1, 1).isdst #=> false
1845 * Time.local(2000, 1, 1).dst? #=> false
1846 * Time.local(2000, 7, 1).zone #=> "JST"
1847 * Time.local(2000, 7, 1).isdst #=> false
1848 * Time.local(2000, 7, 1).dst? #=> false
1851 static VALUE
1852 time_isdst(VALUE time)
1854 struct time_object *tobj;
1856 GetTimeval(time, tobj);
1857 if (tobj->tm_got == 0) {
1858 time_get_tm(time, tobj->gmt);
1860 return tobj->tm.tm_isdst?Qtrue:Qfalse;
1864 * call-seq:
1865 * time.zone => string
1867 * Returns the name of the time zone used for <i>time</i>. As of Ruby
1868 * 1.8, returns ``UTC'' rather than ``GMT'' for UTC times.
1870 * t = Time.gm(2000, "jan", 1, 20, 15, 1)
1871 * t.zone #=> "UTC"
1872 * t = Time.local(2000, "jan", 1, 20, 15, 1)
1873 * t.zone #=> "CST"
1876 static VALUE
1877 time_zone(VALUE time)
1879 struct time_object *tobj;
1880 #if !defined(HAVE_TM_ZONE) && (!defined(HAVE_TZNAME) || !defined(HAVE_DAYLIGHT))
1881 char buf[64];
1882 int len;
1883 #endif
1885 GetTimeval(time, tobj);
1886 if (tobj->tm_got == 0) {
1887 time_get_tm(time, tobj->gmt);
1890 if (tobj->gmt == 1) {
1891 return rb_str_new2("UTC");
1893 #if defined(HAVE_TM_ZONE)
1894 return rb_str_new2(tobj->tm.tm_zone);
1895 #elif defined(HAVE_TZNAME) && defined(HAVE_DAYLIGHT)
1896 return rb_str_new2(tzname[daylight && tobj->tm.tm_isdst]);
1897 #else
1898 len = strftime(buf, 64, "%Z", &tobj->tm);
1899 return rb_str_new(buf, len);
1900 #endif
1904 * call-seq:
1905 * time.gmt_offset => fixnum
1906 * time.gmtoff => fixnum
1907 * time.utc_offset => fixnum
1909 * Returns the offset in seconds between the timezone of <i>time</i>
1910 * and UTC.
1912 * t = Time.gm(2000,1,1,20,15,1) #=> 2000-01-01 20:15:01 UTC
1913 * t.gmt_offset #=> 0
1914 * l = t.getlocal #=> 2000-01-01 14:15:01 -0600
1915 * l.gmt_offset #=> -21600
1918 static VALUE
1919 time_utc_offset(VALUE time)
1921 struct time_object *tobj;
1923 GetTimeval(time, tobj);
1924 if (tobj->tm_got == 0) {
1925 time_get_tm(time, tobj->gmt);
1928 if (tobj->gmt == 1) {
1929 return INT2FIX(0);
1931 else {
1932 #if defined(HAVE_STRUCT_TM_TM_GMTOFF)
1933 return INT2NUM(tobj->tm.tm_gmtoff);
1934 #else
1935 struct tm *u, *l;
1936 time_t t;
1937 long off;
1938 l = &tobj->tm;
1939 t = tobj->ts.tv_sec;
1940 u = gmtime(&t);
1941 if (!u)
1942 rb_raise(rb_eArgError, "gmtime error");
1943 if (l->tm_year != u->tm_year)
1944 off = l->tm_year < u->tm_year ? -1 : 1;
1945 else if (l->tm_mon != u->tm_mon)
1946 off = l->tm_mon < u->tm_mon ? -1 : 1;
1947 else if (l->tm_mday != u->tm_mday)
1948 off = l->tm_mday < u->tm_mday ? -1 : 1;
1949 else
1950 off = 0;
1951 off = off * 24 + l->tm_hour - u->tm_hour;
1952 off = off * 60 + l->tm_min - u->tm_min;
1953 off = off * 60 + l->tm_sec - u->tm_sec;
1954 return LONG2FIX(off);
1955 #endif
1960 * call-seq:
1961 * time.to_a => array
1963 * Returns a ten-element <i>array</i> of values for <i>time</i>:
1964 * {<code>[ sec, min, hour, day, month, year, wday, yday, isdst, zone
1965 * ]</code>}. See the individual methods for an explanation of the
1966 * valid ranges of each value. The ten elements can be passed directly
1967 * to <code>Time::utc</code> or <code>Time::local</code> to create a
1968 * new <code>Time</code>.
1970 * t = Time.now #=> 2007-11-19 08:36:01 -0600
1971 * now = t.to_a #=> [1, 36, 8, 19, 11, 2007, 1, 323, false, "CST"]
1974 static VALUE
1975 time_to_a(VALUE time)
1977 struct time_object *tobj;
1979 GetTimeval(time, tobj);
1980 if (tobj->tm_got == 0) {
1981 time_get_tm(time, tobj->gmt);
1983 return rb_ary_new3(10,
1984 INT2FIX(tobj->tm.tm_sec),
1985 INT2FIX(tobj->tm.tm_min),
1986 INT2FIX(tobj->tm.tm_hour),
1987 INT2FIX(tobj->tm.tm_mday),
1988 INT2FIX(tobj->tm.tm_mon+1),
1989 LONG2NUM((long)tobj->tm.tm_year+1900),
1990 INT2FIX(tobj->tm.tm_wday),
1991 INT2FIX(tobj->tm.tm_yday+1),
1992 tobj->tm.tm_isdst?Qtrue:Qfalse,
1993 time_zone(time));
1996 #define SMALLBUF 100
1997 static int
1998 rb_strftime(char **buf, const char *format, struct tm *time)
2000 int size, len, flen;
2002 (*buf)[0] = '\0';
2003 flen = strlen(format);
2004 if (flen == 0) {
2005 return 0;
2007 errno = 0;
2008 len = strftime(*buf, SMALLBUF, format, time);
2009 if (len != 0 || (**buf == '\0' && errno != ERANGE)) return len;
2010 for (size=1024; ; size*=2) {
2011 *buf = xmalloc(size);
2012 (*buf)[0] = '\0';
2013 len = strftime(*buf, size, format, time);
2015 * buflen can be zero EITHER because there's not enough
2016 * room in the string, or because the control command
2017 * goes to the empty string. Make a reasonable guess that
2018 * if the buffer is 1024 times bigger than the length of the
2019 * format string, it's not failing for lack of room.
2021 if (len > 0 || size >= 1024 * flen) return len;
2022 free(*buf);
2024 /* not reached */
2028 * call-seq:
2029 * time.strftime( string ) => string
2031 * Formats <i>time</i> according to the directives in the given format
2032 * string. Any text not listed as a directive will be passed through
2033 * to the output string.
2035 * Format meaning:
2036 * %a - The abbreviated weekday name (``Sun'')
2037 * %A - The full weekday name (``Sunday'')
2038 * %b - The abbreviated month name (``Jan'')
2039 * %B - The full month name (``January'')
2040 * %c - The preferred local date and time representation
2041 * %d - Day of the month (01..31)
2042 * %H - Hour of the day, 24-hour clock (00..23)
2043 * %I - Hour of the day, 12-hour clock (01..12)
2044 * %j - Day of the year (001..366)
2045 * %m - Month of the year (01..12)
2046 * %M - Minute of the hour (00..59)
2047 * %p - Meridian indicator (``AM'' or ``PM'')
2048 * %S - Second of the minute (00..60)
2049 * %U - Week number of the current year,
2050 * starting with the first Sunday as the first
2051 * day of the first week (00..53)
2052 * %W - Week number of the current year,
2053 * starting with the first Monday as the first
2054 * day of the first week (00..53)
2055 * %w - Day of the week (Sunday is 0, 0..6)
2056 * %x - Preferred representation for the date alone, no time
2057 * %X - Preferred representation for the time alone, no date
2058 * %y - Year without a century (00..99)
2059 * %Y - Year with century
2060 * %Z - Time zone name
2061 * %% - Literal ``%'' character
2063 * t = Time.now #=> 2007-11-19 08:37:48 -0600
2064 * t.strftime("Printed on %m/%d/%Y") #=> "Printed on 11/19/2007"
2065 * t.strftime("at %I:%M%p") #=> "at 08:37AM"
2068 static VALUE
2069 time_strftime(VALUE time, VALUE format)
2071 void rb_enc_copy(VALUE, VALUE);
2072 struct time_object *tobj;
2073 char buffer[SMALLBUF], *buf = buffer;
2074 const char *fmt;
2075 long len;
2076 VALUE str;
2078 GetTimeval(time, tobj);
2079 if (tobj->tm_got == 0) {
2080 time_get_tm(time, tobj->gmt);
2082 StringValue(format);
2083 if (!rb_enc_str_asciicompat_p(format)) {
2084 rb_raise(rb_eArgError, "format should have ASCII compatible encoding");
2086 format = rb_str_new4(format);
2087 fmt = RSTRING_PTR(format);
2088 len = RSTRING_LEN(format);
2089 if (len == 0) {
2090 rb_warning("strftime called with empty format string");
2092 else if (strlen(fmt) < len) {
2093 /* Ruby string may contain \0's. */
2094 const char *p = fmt, *pe = fmt + len;
2096 str = rb_str_new(0, 0);
2097 while (p < pe) {
2098 len = rb_strftime(&buf, p, &tobj->tm);
2099 rb_str_cat(str, buf, len);
2100 p += strlen(p);
2101 if (buf != buffer) {
2102 free(buf);
2103 buf = buffer;
2105 for (fmt = p; p < pe && !*p; ++p);
2106 if (p > fmt) rb_str_cat(str, fmt, p - fmt);
2108 return str;
2110 else {
2111 len = rb_strftime(&buf, RSTRING_PTR(format), &tobj->tm);
2113 str = rb_str_new(buf, len);
2114 if (buf != buffer) free(buf);
2115 rb_enc_copy(str, format);
2116 return str;
2120 * undocumented
2123 static VALUE
2124 time_mdump(VALUE time)
2126 struct time_object *tobj;
2127 struct tm *tm;
2128 unsigned long p, s;
2129 char buf[8];
2130 time_t t;
2131 int nsec;
2132 int i;
2133 VALUE str;
2135 GetTimeval(time, tobj);
2137 t = tobj->ts.tv_sec;
2138 tm = gmtime(&t);
2140 if ((tm->tm_year & 0xffff) != tm->tm_year)
2141 rb_raise(rb_eArgError, "year too big to marshal: %ld", (long)tm->tm_year);
2143 p = 0x1UL << 31 | /* 1 */
2144 tobj->gmt << 30 | /* 1 */
2145 tm->tm_year << 14 | /* 16 */
2146 tm->tm_mon << 10 | /* 4 */
2147 tm->tm_mday << 5 | /* 5 */
2148 tm->tm_hour; /* 5 */
2149 s = tm->tm_min << 26 | /* 6 */
2150 tm->tm_sec << 20 | /* 6 */
2151 tobj->ts.tv_nsec / 1000; /* 20 */
2152 nsec = tobj->ts.tv_nsec % 1000;
2154 for (i=0; i<4; i++) {
2155 buf[i] = p & 0xff;
2156 p = RSHIFT(p, 8);
2158 for (i=4; i<8; i++) {
2159 buf[i] = s & 0xff;
2160 s = RSHIFT(s, 8);
2163 str = rb_str_new(buf, 8);
2164 rb_copy_generic_ivar(str, time);
2165 if (nsec) {
2167 * submicro is formatted in fixed-point packed BCD (without sign).
2168 * It represent digits under microsecond.
2169 * For nanosecond resolution, 3 digits (2 bytes) are used.
2170 * However it can be longer.
2171 * Extra digits are ignored for loading.
2173 unsigned char buf[2];
2174 int len = sizeof(buf);
2175 buf[1] = (nsec % 10) << 4;
2176 nsec /= 10;
2177 buf[0] = nsec % 10;
2178 nsec /= 10;
2179 buf[0] |= (nsec % 10) << 4;
2180 if (buf[1] == 0)
2181 len = 1;
2182 rb_ivar_set(str, id_submicro, rb_str_new((char *)buf, len));
2184 return str;
2188 * call-seq:
2189 * time._dump => string
2191 * Dump _time_ for marshaling.
2194 static VALUE
2195 time_dump(int argc, VALUE *argv, VALUE time)
2197 VALUE str;
2199 rb_scan_args(argc, argv, "01", 0);
2200 str = time_mdump(time);
2202 return str;
2206 * undocumented
2209 static VALUE
2210 time_mload(VALUE time, VALUE str)
2212 struct time_object *tobj;
2213 unsigned long p, s;
2214 time_t sec;
2215 long usec;
2216 unsigned char *buf;
2217 struct tm tm;
2218 int i, gmt;
2219 long nsec;
2220 VALUE submicro;
2222 time_modify(time);
2224 submicro = rb_attr_get(str, id_submicro);
2225 if (submicro != Qnil) {
2226 st_delete(rb_generic_ivar_table(str), (st_data_t*)&id_submicro, 0);
2228 rb_copy_generic_ivar(time, str);
2230 StringValue(str);
2231 buf = (unsigned char *)RSTRING_PTR(str);
2232 if (RSTRING_LEN(str) != 8) {
2233 rb_raise(rb_eTypeError, "marshaled time format differ");
2236 p = s = 0;
2237 for (i=0; i<4; i++) {
2238 p |= buf[i]<<(8*i);
2240 for (i=4; i<8; i++) {
2241 s |= buf[i]<<(8*(i-4));
2244 if ((p & (1UL<<31)) == 0) {
2245 gmt = 0;
2246 sec = p;
2247 usec = s;
2248 nsec = usec * 1000;
2250 else {
2251 p &= ~(1UL<<31);
2252 gmt = (p >> 30) & 0x1;
2253 tm.tm_year = (p >> 14) & 0xffff;
2254 tm.tm_mon = (p >> 10) & 0xf;
2255 tm.tm_mday = (p >> 5) & 0x1f;
2256 tm.tm_hour = p & 0x1f;
2257 tm.tm_min = (s >> 26) & 0x3f;
2258 tm.tm_sec = (s >> 20) & 0x3f;
2259 tm.tm_isdst = 0;
2261 sec = make_time_t(&tm, Qtrue);
2262 usec = (long)(s & 0xfffff);
2263 nsec = usec * 1000;
2265 if (submicro != Qnil) {
2266 unsigned char *ptr;
2267 long len;
2268 int digit;
2269 ptr = (unsigned char*)StringValuePtr(submicro);
2270 len = RSTRING_LEN(submicro);
2271 if (0 < len) {
2272 if (10 <= (digit = ptr[0] >> 4)) goto end_submicro;
2273 nsec += digit * 100;
2274 if (10 <= (digit = ptr[0] & 0xf)) goto end_submicro;
2275 nsec += digit * 10;
2277 if (1 < len) {
2278 if (10 <= (digit = ptr[1] >> 4)) goto end_submicro;
2279 nsec += digit;
2281 end_submicro: ;
2284 time_overflow_p(&sec, &nsec);
2286 GetTimeval(time, tobj);
2287 tobj->tm_got = 0;
2288 tobj->gmt = gmt;
2289 tobj->ts.tv_sec = sec;
2290 tobj->ts.tv_nsec = nsec;
2292 return time;
2296 * call-seq:
2297 * Time._load(string) => time
2299 * Unmarshal a dumped +Time+ object.
2302 static VALUE
2303 time_load(VALUE klass, VALUE str)
2305 VALUE time = time_s_alloc(klass);
2307 time_mload(time, str);
2308 return time;
2312 * <code>Time</code> is an abstraction of dates and times. Time is
2313 * stored internally as the number of seconds and nanoseconds since
2314 * the <em>Epoch</em>, January 1, 1970 00:00 UTC. On some operating
2315 * systems, this offset is allowed to be negative. Also see the
2316 * library modules <code>Date</code>. The
2317 * <code>Time</code> class treats GMT (Greenwich Mean Time) and UTC
2318 * (Coordinated Universal Time)<em>[Yes, UTC really does stand for
2319 * Coordinated Universal Time. There was a committee involved.]</em>
2320 * as equivalent. GMT is the older way of referring to these
2321 * baseline times but persists in the names of calls on POSIX
2322 * systems.
2324 * All times are stored with some number of nanoseconds. Be aware of
2325 * this fact when comparing times with each other---times that are
2326 * apparently equal when displayed may be different when compared.
2329 void
2330 Init_Time(void)
2332 id_divmod = rb_intern("divmod");
2333 id_mul = rb_intern("*");
2334 id_submicro = rb_intern("submicro");
2336 rb_cTime = rb_define_class("Time", rb_cObject);
2337 rb_include_module(rb_cTime, rb_mComparable);
2339 rb_define_alloc_func(rb_cTime, time_s_alloc);
2340 rb_define_singleton_method(rb_cTime, "now", rb_class_new_instance, -1);
2341 rb_define_singleton_method(rb_cTime, "at", time_s_at, -1);
2342 rb_define_singleton_method(rb_cTime, "utc", time_s_mkutc, -1);
2343 rb_define_singleton_method(rb_cTime, "gm", time_s_mkutc, -1);
2344 rb_define_singleton_method(rb_cTime, "local", time_s_mktime, -1);
2345 rb_define_singleton_method(rb_cTime, "mktime", time_s_mktime, -1);
2347 rb_define_method(rb_cTime, "to_i", time_to_i, 0);
2348 rb_define_method(rb_cTime, "to_f", time_to_f, 0);
2349 rb_define_method(rb_cTime, "<=>", time_cmp, 1);
2350 rb_define_method(rb_cTime, "eql?", time_eql, 1);
2351 rb_define_method(rb_cTime, "hash", time_hash, 0);
2352 rb_define_method(rb_cTime, "initialize", time_init, 0);
2353 rb_define_method(rb_cTime, "initialize_copy", time_init_copy, 1);
2355 rb_define_method(rb_cTime, "localtime", time_localtime, 0);
2356 rb_define_method(rb_cTime, "gmtime", time_gmtime, 0);
2357 rb_define_method(rb_cTime, "utc", time_gmtime, 0);
2358 rb_define_method(rb_cTime, "getlocal", time_getlocaltime, 0);
2359 rb_define_method(rb_cTime, "getgm", time_getgmtime, 0);
2360 rb_define_method(rb_cTime, "getutc", time_getgmtime, 0);
2362 rb_define_method(rb_cTime, "ctime", time_asctime, 0);
2363 rb_define_method(rb_cTime, "asctime", time_asctime, 0);
2364 rb_define_method(rb_cTime, "to_s", time_to_s, 0);
2365 rb_define_method(rb_cTime, "inspect", time_to_s, 0);
2366 rb_define_method(rb_cTime, "to_a", time_to_a, 0);
2368 rb_define_method(rb_cTime, "+", time_plus, 1);
2369 rb_define_method(rb_cTime, "-", time_minus, 1);
2371 rb_define_method(rb_cTime, "succ", time_succ, 0);
2372 rb_define_method(rb_cTime, "sec", time_sec, 0);
2373 rb_define_method(rb_cTime, "min", time_min, 0);
2374 rb_define_method(rb_cTime, "hour", time_hour, 0);
2375 rb_define_method(rb_cTime, "mday", time_mday, 0);
2376 rb_define_method(rb_cTime, "day", time_mday, 0);
2377 rb_define_method(rb_cTime, "mon", time_mon, 0);
2378 rb_define_method(rb_cTime, "month", time_mon, 0);
2379 rb_define_method(rb_cTime, "year", time_year, 0);
2380 rb_define_method(rb_cTime, "wday", time_wday, 0);
2381 rb_define_method(rb_cTime, "yday", time_yday, 0);
2382 rb_define_method(rb_cTime, "isdst", time_isdst, 0);
2383 rb_define_method(rb_cTime, "dst?", time_isdst, 0);
2384 rb_define_method(rb_cTime, "zone", time_zone, 0);
2385 rb_define_method(rb_cTime, "gmtoff", time_utc_offset, 0);
2386 rb_define_method(rb_cTime, "gmt_offset", time_utc_offset, 0);
2387 rb_define_method(rb_cTime, "utc_offset", time_utc_offset, 0);
2389 rb_define_method(rb_cTime, "utc?", time_utc_p, 0);
2390 rb_define_method(rb_cTime, "gmt?", time_utc_p, 0);
2392 rb_define_method(rb_cTime, "sunday?", time_sunday, 0);
2393 rb_define_method(rb_cTime, "monday?", time_monday, 0);
2394 rb_define_method(rb_cTime, "tuesday?", time_tuesday, 0);
2395 rb_define_method(rb_cTime, "wednesday?", time_wednesday, 0);
2396 rb_define_method(rb_cTime, "thursday?", time_thursday, 0);
2397 rb_define_method(rb_cTime, "friday?", time_friday, 0);
2398 rb_define_method(rb_cTime, "saturday?", time_saturday, 0);
2400 rb_define_method(rb_cTime, "tv_sec", time_to_i, 0);
2401 rb_define_method(rb_cTime, "tv_usec", time_usec, 0);
2402 rb_define_method(rb_cTime, "usec", time_usec, 0);
2403 rb_define_method(rb_cTime, "tv_nsec", time_nsec, 0);
2404 rb_define_method(rb_cTime, "nsec", time_nsec, 0);
2406 rb_define_method(rb_cTime, "strftime", time_strftime, 1);
2408 /* methods for marshaling */
2409 rb_define_method(rb_cTime, "_dump", time_dump, -1);
2410 rb_define_singleton_method(rb_cTime, "_load", time_load, 1);
2411 #if 0
2412 /* Time will support marshal_dump and marshal_load in the future (1.9 maybe) */
2413 rb_define_method(rb_cTime, "marshal_dump", time_mdump, 0);
2414 rb_define_method(rb_cTime, "marshal_load", time_mload, 1);
2415 #endif