1 /**********************************************************************
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>
16 #include "ruby/encoding.h"
25 static VALUE time_utc_offset
_((VALUE
));
27 static ID id_divmod
, id_mul
, id_submicro
;
36 #define GetTimeval(obj, tobj) \
37 Data_Get_Struct(obj, struct time_object, tobj)
46 time_s_alloc(VALUE klass
)
49 struct time_object
*tobj
;
51 obj
= Data_Make_Struct(klass
, struct time_object
, 0, time_free
, tobj
);
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.
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
83 * a = Time.new #=> 2007-11-19 07:50:02 -0600
84 * b = Time.new #=> 2007-11-19 07:50:02 -0600
86 * "%.6f" % a.to_f #=> "1195480202.282373"
87 * "%.6f" % b.to_f #=> "1195480202.283415"
94 struct time_object
*tobj
;
97 GetTimeval(time
, tobj
);
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");
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;
119 #define NDIV(x,y) (-(-((x)+1)/(y))-1)
120 #define NMOD(x,y) ((y)-(-((x)+1)%(y))-1)
123 time_overflow_p(time_t *secp
, long *nsecp
)
125 time_t tmp
, sec
= *secp
;
128 if (nsec
>= 1000000000) { /* nsec positive overflow */
129 tmp
= sec
+ nsec
/ 1000000000;
131 if (sec
> 0 && tmp
< 0) {
132 rb_raise(rb_eRangeError
, "out of Time range");
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");
144 #ifndef NEGATIVE_TIME_T
146 rb_raise(rb_eArgError
, "time must be positive");
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
;
167 rb_time_new(time_t sec
, long usec
)
169 return time_new_internal(rb_cTime
, sec
, usec
* 1000);
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
)
182 const char *tstr
= interval
? "time interval" : "time";
185 #ifndef NEGATIVE_TIME_T
191 t
.tv_sec
= FIX2LONG(num
);
192 if (interval
&& t
.tv_sec
< 0)
193 rb_raise(rb_eArgError
, "%s must be positive", tstr
);
198 if (interval
&& RFLOAT_VALUE(num
) < 0.0)
199 rb_raise(rb_eArgError
, "%s must be positive", tstr
);
203 d
= modf(RFLOAT_VALUE(num
), &f
);
204 t
.tv_sec
= (time_t)f
;
206 rb_raise(rb_eRangeError
, "%f out of Time range", RFLOAT_VALUE(num
));
208 t
.tv_nsec
= (long)(d
*1e9
+0.5);
213 t
.tv_sec
= NUM2LONG(num
);
214 if (interval
&& t
.tv_sec
< 0)
215 rb_raise(rb_eArgError
, "%s must be positive", tstr
);
220 ary
= rb_check_array_type(rb_funcall(num
, id_divmod
, 1, INT2FIX(1)));
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
);
237 static struct timeval
238 time_timeval(VALUE num
, int interval
)
243 ts
= time_timespec(num
, interval
);
244 tv
.tv_sec
= ts
.tv_sec
;
245 tv
.tv_usec
= ts
.tv_nsec
/ 1000;
251 rb_time_interval(VALUE num
)
253 return time_timeval(num
, Qtrue
);
257 rb_time_timeval(VALUE time
)
259 struct time_object
*tobj
;
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;
268 return time_timeval(time
, Qfalse
);
272 rb_time_timespec(VALUE time
)
274 struct time_object
*tobj
;
277 if (TYPE(time
) == T_DATA
&& RDATA(time
)->dfree
== time_free
) {
278 GetTimeval(time
, tobj
);
282 return time_timespec(time
, Qfalse
);
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
307 time_s_at(int argc
, VALUE
*argv
, VALUE klass
)
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)));
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
;
330 static const char *months
[] = {
331 "jan", "feb", "mar", "apr", "may", "jun",
332 "jul", "aug", "sep", "oct", "nov", "dec",
338 if (TYPE(obj
) == T_STRING
) {
339 obj
= rb_str_to_inum(obj
, 10, Qfalse
);
342 return NUM2LONG(obj
);
346 obj2nsec(VALUE obj
, long *nsec
)
350 if (TYPE(obj
) == T_STRING
) {
351 obj
= rb_str_to_inum(obj
, 10, Qfalse
);
353 return NUM2LONG(obj
) * 1000;
356 ts
= time_timespec(obj
, 1);
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)));
373 time_arg(int argc
, VALUE
*argv
, struct tm
*tm
, long *nsec
)
379 MEMZERO(tm
, struct tm
, 1);
389 tm
->tm_isdst
= RTEST(argv
[8]) ? 1 : 0;
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) */
399 year
= obj2long(v
[0]);
401 if (0 <= year
&& year
< 39) {
402 rb_warning("2 digits year is used: %ld", year
);
405 else if (69 <= year
&& year
< 139) {
406 rb_warning("2 or 3 digits year is used: %ld", year
);
418 VALUE s
= rb_check_string_type(v
[1]);
421 for (i
=0; i
<12; i
++) {
422 if (RSTRING_LEN(s
) == 3 &&
423 STRCASECMP(months
[i
], RSTRING_PTR(s
)) == 0) {
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;
437 tm
->tm_mon
= obj2long(v
[1])-1;
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]);
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 */
459 tm
->tm_year
!= year
||
460 #ifndef NEGATIVE_TIME_T
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);
479 return ((y
% 4 == 0) && (y
% 100 != 0)) || (y
% 400 == 0);
482 #define DIV(n,d) ((n)<0 ? NDIV((n),(d)) : (n)/(d))
485 timegm_noleapsecond(struct tm
*tm
)
487 static int common_year_yday_offset
[] = {
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
[] = {
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
];
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 +
536 DIV(tm_year
+299,400))*86400;
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;
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
;
565 # error cannot find integer type which size is same as 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
;
576 find_dst
= 0 < tptr
->tm_isdst
;
578 #ifdef NEGATIVE_TIME_T
579 guess_lo
= (time_t)~((unsigned_time_t
)~(time_t)0 >> 1);
583 guess_hi
= ((time_t)-1) < ((time_t)0) ?
584 (time_t)((unsigned_time_t
)~(time_t)0 >> 1) :
587 guess
= timegm_noleapsecond(tptr
);
588 tm
= (utc_p
? gmtime
: localtime
)(&guess
);
591 if (d
== 0) return guess
;
594 guess
-= 24 * 60 * 60;
598 guess
+= 24 * 60 * 60;
600 if (guess_lo
< guess
&& guess
< guess_hi
&&
601 (tm
= (utc_p
? gmtime
: localtime
)(&guess
)) != NULL
) {
603 if (d
== 0) return guess
;
611 tm
= (utc_p
? gmtime
: localtime
)(&guess_lo
);
614 if (d
< 0) goto out_of_range
;
615 if (d
== 0) return guess_lo
;
618 tm
= (utc_p
? gmtime
: localtime
)(&guess_hi
);
621 if (d
> 0) goto out_of_range
;
622 if (d
== 0) return guess_hi
;
627 while (guess_lo
+ 1 < guess_hi
) {
628 /* there is a gap between guess_lo and guess_hi. */
629 unsigned long range
= 0;
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
641 guess = guess_lo / d * a + (guess_lo % d) * a / d
642 + guess_hi / d * b + (guess_hi % d) * b / d
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
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;
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;
667 a
+= tm_hi
.tm_mday
- tptr
->tm_mday
;
668 b
+= tptr
->tm_mday
- tm_lo
.tm_mday
;
669 if (a
+ b
<= 46000 / 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) {
679 a
+= tm_hi
.tm_min
- tptr
->tm_min
;
680 b
+= tptr
->tm_min
- tm_lo
.tm_min
;
681 if (a
+ b
<= 46000 / 60) {
685 a
+= tm_hi
.tm_sec
- tptr
->tm_sec
;
686 b
+= tptr
->tm_sec
- tm_lo
.tm_sec
;
696 Although `/' and `%' may produce unexpected result with negative
697 argument, it doesn't cause serious problem because there is a
700 guess
= guess_lo
/ d
* a
+ (guess_lo
% d
) * a
/ d
701 + guess_hi
/ d
* b
+ (guess_hi
% d
) * b
/ d
;
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;
715 tm
= (utc_p
? gmtime
: localtime
)(&guess
);
724 guess
= guess
- range
;
726 if (guess_lo
< guess
&& guess
< guess_hi
)
734 guess
= guess
+ range
;
736 if (guess_lo
< guess
&& guess
< guess_hi
)
742 /* If localtime is nonmonotonic, another result may exist. */
745 guess2
= guess
- 2 * 60 * 60;
746 tm
= localtime(&guess2
);
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) {
770 guess2
= guess
+ 2 * 60 * 60;
771 tm
= localtime(&guess2
);
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) {
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
) {
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
) {
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
);
815 rb_raise(rb_eArgError
, "time out of range");
818 rb_raise(rb_eArgError
, "gmtime/localtime error");
819 return 0; /* not reached */
823 make_time_t(struct tm
*tptr
, int utc_p
)
829 #if defined(HAVE_TIMEGM)
830 if ((t
= timegm(&buf
)) != -1)
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
844 return search_time_t(&buf
, utc_p
);
847 #if defined(HAVE_MKTIME)
848 if ((t
= mktime(&buf
)) != -1)
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
862 return search_time_t(&buf
, utc_p
);
867 time_utc_or_local(int argc
, VALUE
*argv
, int utc_p
, VALUE klass
)
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
);
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
912 time_s_mkutc(int argc
, VALUE
*argv
, VALUE klass
)
914 return time_utc_or_local(argc
, argv
, Qtrue
, klass
);
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
939 * Time.local(2000,"jan",1,20,15,1) #=> 2000-01-01 20:15:01 -0600
943 time_s_mktime(int argc
, VALUE
*argv
, VALUE klass
)
945 return time_utc_or_local(argc
, argv
, Qfalse
, klass
);
953 * Returns the value of <i>time</i> as an integer number of seconds
957 * "%10.5f" % t.to_f #=> "1049896564.17839"
958 * t.to_i #=> 1049896564
962 time_to_i(VALUE time
)
964 struct time_object
*tobj
;
966 GetTimeval(time
, tobj
);
967 return LONG2NUM(tobj
->ts
.tv_sec
);
974 * Returns the value of <i>time</i> as a floating point number of
975 * seconds since the Epoch.
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.
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
);
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"
1007 time_usec(VALUE time
)
1009 struct time_object
*tobj
;
1011 GetTimeval(time
, tobj
);
1012 return LONG2NUM(tobj
->ts
.tv_nsec
/1000);
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.
1033 time_nsec(VALUE time
)
1035 struct time_object
*tobj
;
1037 GetTimeval(time
, tobj
);
1038 return LONG2NUM(tobj
->ts
.tv_nsec
);
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
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
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);
1074 if (tobj1
->ts
.tv_sec
> tobj2
->ts
.tv_sec
) return INT2FIX(1);
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
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
;
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
1113 * t = Time.now #=> 2007-11-19 08:15:23 -0600
1115 * t = Time.gm(2000,"jan",1,20,15,1) #=> 2000-01-01 20:15:01 UTC
1118 * t = Time.now #=> 2007-11-19 08:16:03 -0600
1120 * t = Time.gm(2000,1,1,20,15,1) #=> 2000-01-01 20:15:01 UTC
1125 time_utc_p(VALUE time
)
1127 struct time_object
*tobj
;
1129 GetTimeval(time
, tobj
);
1130 if (tobj
->gmt
) return Qtrue
;
1136 * time.hash => fixnum
1138 * Return a hash code for this time object.
1142 time_hash(VALUE time
)
1144 struct time_object
*tobj
;
1147 GetTimeval(time
, tobj
);
1148 hash
= tobj
->ts
.tv_sec
^ tobj
->ts
.tv_nsec
;
1149 return LONG2FIX(hash
);
1154 time_init_copy(VALUE copy
, VALUE time
)
1156 struct time_object
*tobj
, *tcopy
;
1158 if (copy
== time
) return 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);
1171 time_dup(VALUE time
)
1173 VALUE dup
= time_s_alloc(CLASS_OF(time
));
1174 time_init_copy(dup
, time
);
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
1187 * t.localtime #=> 2000-01-01 14:15:01 -0600
1192 time_localtime(VALUE time
)
1194 struct time_object
*tobj
;
1198 GetTimeval(time
, tobj
);
1206 t
= tobj
->ts
.tv_sec
;
1207 tm_tmp
= localtime(&t
);
1209 rb_raise(rb_eArgError
, "localtime error");
1218 * time.gmtime => time
1221 * Converts <i>time</i> to UTC (GMT), modifying the receiver.
1223 * t = Time.now #=> 2007-11-19 08:18:31 -0600
1225 * t.gmtime #=> 2007-11-19 14:18:31 UTC
1228 * t = Time.now #=> 2007-11-19 08:18:51 -0600
1230 * t.utc #=> 2007-11-19 14:18:51 UTC
1235 time_gmtime(VALUE time
)
1237 struct time_object
*tobj
;
1241 GetTimeval(time
, tobj
);
1249 t
= tobj
->ts
.tv_sec
;
1250 tm_tmp
= gmtime(&t
);
1252 rb_raise(rb_eArgError
, "gmtime error");
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
1268 * l = t.getlocal #=> 2000-01-01 14:15:01 -0600
1274 time_getlocaltime(VALUE time
)
1276 return time_localtime(time_dup(time
));
1281 * time.getgm => new_time
1282 * time.getutc => new_time
1284 * Returns a new <code>new_time</code> object representing <i>time</i> in
1287 * t = Time.local(2000,1,1,20,15,1) #=> 2000-01-01 20:15:01 -0600
1289 * y = t.getgm #=> 2000-01-02 02:15:01 UTC
1295 time_getgmtime(VALUE time
)
1297 return time_gmtime(time_dup(time
));
1301 time_get_tm(VALUE time
, int gmt
)
1303 if (gmt
) return time_gmtime(time
);
1304 return time_localtime(time
);
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"
1318 time_asctime(VALUE time
)
1320 struct time_object
*tobj
;
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
);
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>''
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"
1350 time_to_s(VALUE time
)
1352 struct time_object
*tobj
;
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
);
1366 #if defined(HAVE_STRUCT_TM_TM_GMTOFF)
1367 off
= tobj
->tm
.tm_gmtoff
;
1369 VALUE tmp
= time_utc_offset(time
);
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
);
1384 time_add(struct time_object
*tobj
, VALUE offset
, int sign
)
1386 double v
= NUM2DBL(offset
);
1388 unsigned_time_t sec_off
;
1390 long nsec_off
, nsec
;
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);
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
);
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
);
1418 GetTimeval(result
, tobj
);
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
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);
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
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
;
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
);
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);
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
1495 time_succ(VALUE time
)
1497 struct time_object
*tobj
;
1500 GetTimeval(time
, tobj
);
1502 time
= rb_time_nano_new(tobj
->ts
.tv_sec
+ 1, tobj
->ts
.tv_nsec
);
1503 GetTimeval(time
, tobj
);
1509 rb_time_succ(VALUE time
)
1511 return time_succ(time
);
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
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
);
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
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
);
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
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
);
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
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
);
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
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);
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
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);
1655 * time.wday => fixnum
1657 * Returns an integer representing the day of the week, 0..6, with
1660 * t = Time.now #=> 2007-11-20 02:35:35 -0600
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
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;\
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
1703 time_sunday(VALUE time
)
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
1719 time_monday(VALUE time
)
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
1735 time_tuesday(VALUE time
)
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
1751 time_wednesday(VALUE time
)
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
1767 time_thursday(VALUE time
)
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
1783 time_friday(VALUE time
)
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
1799 time_saturday(VALUE time
)
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
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);
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.
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
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
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
;
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)
1872 * t = Time.local(2000, "jan", 1, 20, 15, 1)
1877 time_zone(VALUE time
)
1879 struct time_object
*tobj
;
1880 #if !defined(HAVE_TM_ZONE) && (!defined(HAVE_TZNAME) || !defined(HAVE_DAYLIGHT))
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
]);
1898 len
= strftime(buf
, 64, "%Z", &tobj
->tm
);
1899 return rb_str_new(buf
, len
);
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>
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
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) {
1932 #if defined(HAVE_STRUCT_TM_TM_GMTOFF)
1933 return INT2NUM(tobj
->tm
.tm_gmtoff
);
1939 t
= tobj
->ts
.tv_sec
;
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;
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
);
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"]
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
,
1996 #define SMALLBUF 100
1998 rb_strftime(char **buf
, const char *format
, struct tm
*time
)
2000 int size
, len
, flen
;
2003 flen
= strlen(format
);
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
);
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
;
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.
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"
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
;
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
);
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);
2098 len
= rb_strftime(&buf
, p
, &tobj
->tm
);
2099 rb_str_cat(str
, buf
, len
);
2101 if (buf
!= buffer
) {
2105 for (fmt
= p
; p
< pe
&& !*p
; ++p
);
2106 if (p
> fmt
) rb_str_cat(str
, fmt
, p
- fmt
);
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
);
2124 time_mdump(VALUE time
)
2126 struct time_object
*tobj
;
2135 GetTimeval(time
, tobj
);
2137 t
= tobj
->ts
.tv_sec
;
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
++) {
2158 for (i
=4; i
<8; i
++) {
2163 str
= rb_str_new(buf
, 8);
2164 rb_copy_generic_ivar(str
, time
);
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;
2179 buf
[0] |= (nsec
% 10) << 4;
2182 rb_ivar_set(str
, id_submicro
, rb_str_new((char *)buf
, len
));
2189 * time._dump => string
2191 * Dump _time_ for marshaling.
2195 time_dump(int argc
, VALUE
*argv
, VALUE time
)
2199 rb_scan_args(argc
, argv
, "01", 0);
2200 str
= time_mdump(time
);
2210 time_mload(VALUE time
, VALUE str
)
2212 struct time_object
*tobj
;
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
);
2231 buf
= (unsigned char *)RSTRING_PTR(str
);
2232 if (RSTRING_LEN(str
) != 8) {
2233 rb_raise(rb_eTypeError
, "marshaled time format differ");
2237 for (i
=0; i
<4; i
++) {
2240 for (i
=4; i
<8; i
++) {
2241 s
|= buf
[i
]<<(8*(i
-4));
2244 if ((p
& (1UL<<31)) == 0) {
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;
2261 sec
= make_time_t(&tm
, Qtrue
);
2262 usec
= (long)(s
& 0xfffff);
2265 if (submicro
!= Qnil
) {
2269 ptr
= (unsigned char*)StringValuePtr(submicro
);
2270 len
= RSTRING_LEN(submicro
);
2272 if (10 <= (digit
= ptr
[0] >> 4)) goto end_submicro
;
2273 nsec
+= digit
* 100;
2274 if (10 <= (digit
= ptr
[0] & 0xf)) goto end_submicro
;
2278 if (10 <= (digit
= ptr
[1] >> 4)) goto end_submicro
;
2284 time_overflow_p(&sec
, &nsec
);
2286 GetTimeval(time
, tobj
);
2289 tobj
->ts
.tv_sec
= sec
;
2290 tobj
->ts
.tv_nsec
= nsec
;
2297 * Time._load(string) => time
2299 * Unmarshal a dumped +Time+ object.
2303 time_load(VALUE klass
, VALUE str
)
2305 VALUE time
= time_s_alloc(klass
);
2307 time_mload(time
, str
);
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
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.
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);
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);