4 ** See Copyright Notice in mruby.h
7 #ifndef MRB_WITHOUT_FLOAT
12 #include <mruby/class.h>
13 #include <mruby/data.h>
14 #include <mruby/numeric.h>
15 #include <mruby/time.h>
16 #include <mruby/string.h>
18 #ifdef MRB_DISABLE_STDIO
24 #define NDIV(x,y) (-(-((x)+1)/(y))-1)
25 #define TO_S_FMT "%Y-%m-%d %H:%M:%S "
27 #if defined(_MSC_VER) && _MSC_VER < 1800
28 double round(double x) {
29 return floor(x + 0.5);
33 #ifndef MRB_WITHOUT_FLOAT
34 # if !defined(__MINGW64__) && defined(_WIN32)
35 # define llround(x) round(x)
39 #if defined(__MINGW64__) || defined(__MINGW32__)
40 # include <sys/time.h>
43 /** Time class configuration */
46 /* C99 does not have gettimeofday that is required to retrieve microseconds */
47 /* uncomment following macro on platforms without gettimeofday(2) */
48 /* #define NO_GETTIMEOFDAY */
51 /* C99 does not have reentrant gmtime_r() so it might cause troubles under */
52 /* multi-threading environment. undef following macro on platforms that */
53 /* does not have gmtime_r() and localtime_r(). */
54 /* #define NO_GMTIME_R */
58 /* Win32 platform do not provide gmtime_r/localtime_r; emulate them using gmtime_s/localtime_s */
59 #define gmtime_r(tp, tm) ((gmtime_s((tm), (tp)) == 0) ? (tm) : NULL)
60 #define localtime_r(tp, tm) ((localtime_s((tm), (tp)) == 0) ? (tm) : NULL)
67 /* mruby usually use its own implementation of struct tm to string conversion */
68 /* except when DISABLE_STDIO is set. In that case, it uses asctime() or asctime_r(). */
69 /* By default mruby tries to use asctime_r() which is reentrant. */
70 /* Undef following macro on platforms that does not have asctime_r(). */
71 /* #define NO_ASCTIME_R */
74 /* mktime() creates tm structure for localtime; timegm() is for UTC time */
75 /* define following macro to use probably faster timegm() on the platform */
76 /* #define USE_SYSTEM_TIMEGM */
78 /** end of Time class configuration */
80 #ifndef NO_GETTIMEOFDAY
82 # define WIN32_LEAN_AND_MEAN /* don't include winsock.h */
84 # define gettimeofday my_gettimeofday
87 # define UI64(x) x##ui64
89 # define UI64(x) x##ull
92 typedef long suseconds_t;
94 # if (!defined __MINGW64__) && (!defined __MINGW32__)
102 gettimeofday(struct timeval *tv, void *tz)
105 mrb_assert(0); /* timezone is not supported */
110 unsigned __int64 u64;
112 GetSystemTimeAsFileTime(&t.ft); /* 100 ns intervals since Windows epoch */
113 t.u64 -= UI64(116444736000000000); /* Unix epoch bias */
114 t.u64 /= 10; /* to microseconds */
115 tv->tv_sec = (time_t)(t.u64 / (1000 * 1000));
116 tv->tv_usec = t.u64 % (1000 * 1000);
121 # include <sys/time.h>
125 #define gmtime_r(t,r) gmtime(t)
126 #define localtime_r(t,r) localtime(t)
129 #ifndef USE_SYSTEM_TIMEGM
130 #define timegm my_timgm
133 is_leapyear(unsigned int y)
135 return (y % 4) == 0 && ((y % 100) != 0 || (y % 400) == 0);
139 timegm(struct tm *tm)
141 static const unsigned int ndays[2][12] = {
142 {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
143 {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
147 unsigned int *nday = (unsigned int*) ndays[is_leapyear(tm->tm_year+1900)];
149 static const int epoch_year = 70;
150 if(tm->tm_year >= epoch_year) {
151 for (i = epoch_year; i < tm->tm_year; ++i)
152 r += is_leapyear(i+1900) ? 366*24*60*60 : 365*24*60*60;
154 for (i = tm->tm_year; i < epoch_year; ++i)
155 r -= is_leapyear(i+1900) ? 366*24*60*60 : 365*24*60*60;
157 for (i = 0; i < tm->tm_mon; ++i)
158 r += nday[i] * 24 * 60 * 60;
159 r += (tm->tm_mday - 1) * 24 * 60 * 60;
160 r += tm->tm_hour * 60 * 60;
161 r += tm->tm_min * 60;
167 /* Since we are limited to using ISO C99, this implementation is based
168 * on time_t. That means the resolution of time is only precise to the
169 * second level. Also, there are only 2 timezones, namely UTC and LOCAL.
172 typedef struct mrb_timezone_name {
177 static const mrb_timezone_name timezone_names[] = {
178 { "none", sizeof("none") - 1 },
179 { "UTC", sizeof("UTC") - 1 },
180 { "LOCAL", sizeof("LOCAL") - 1 },
183 #ifndef MRB_DISABLE_STDIO
184 static const char mon_names[12][4] = {
185 "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
188 static const char wday_names[7][4] = {
189 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat",
196 enum mrb_timezone timezone;
200 static const struct mrb_data_type mrb_time_type = { "Time", mrb_free };
202 #ifndef MRB_WITHOUT_FLOAT
203 void mrb_check_num_exact(mrb_state *mrb, mrb_float num);
204 typedef mrb_float mrb_sec;
205 #define mrb_sec_value(mrb, sec) mrb_float_value(mrb, sec)
207 typedef mrb_int mrb_sec;
208 #define mrb_sec_value(mrb, sec) mrb_fixnum_value(sec)
211 #define MRB_TIME_T_UINT (~(time_t)0 > 0)
212 #define MRB_TIME_MIN ( \
213 MRB_TIME_T_UINT ? 0 : \
214 (sizeof(time_t) <= 4 ? INT32_MIN : INT64_MIN) \
216 #define MRB_TIME_MAX ( \
217 MRB_TIME_T_UINT ? (sizeof(time_t) <= 4 ? UINT32_MAX : UINT64_MAX) : \
218 (sizeof(time_t) <= 4 ? INT32_MAX : INT64_MAX) \
222 fixable_time_t_p(time_t v)
224 if (MRB_INT_MIN <= MRB_TIME_MIN && MRB_TIME_MAX <= MRB_INT_MAX) return TRUE;
229 mrb_to_time_t(mrb_state *mrb, mrb_value obj, time_t *usec)
233 switch (mrb_type(obj)) {
234 #ifndef MRB_WITHOUT_FLOAT
237 mrb_float f = mrb_float(obj);
239 mrb_check_num_exact(mrb, f);
240 if (f >= ((mrb_float)MRB_TIME_MAX-1.0) || f < ((mrb_float)MRB_TIME_MIN+1.0)) {
246 *usec = (time_t)llround((f - t) * 1.0e+6);
249 t = (time_t)llround(f);
253 #endif /* MRB_WITHOUT_FLOAT */
257 mrb_int i = mrb_int(mrb, obj);
259 if ((MRB_INT_MAX > MRB_TIME_MAX && i > 0 && i > (mrb_int)MRB_TIME_MAX) ||
260 (MRB_TIME_MIN > MRB_INT_MIN && MRB_TIME_MIN > i)) {
265 if (usec) { *usec = 0; }
273 mrb_raisef(mrb, E_ARGUMENT_ERROR, "%v out of Time range", obj);
276 if (usec) { *usec = 0; }
280 /** Updates the datetime of a mrb_time based on it's timezone and
281 seconds setting. Returns self on success, NULL of failure.
282 if `dealloc` is set `true`, it frees `self` on error. */
283 static struct mrb_time*
284 time_update_datetime(mrb_state *mrb, struct mrb_time *self, int dealloc)
287 time_t t = self->sec;
289 if (self->timezone == MRB_TIMEZONE_UTC) {
290 aid = gmtime_r(&t, &self->datetime);
293 aid = localtime_r(&t, &self->datetime);
296 mrb_sec sec = (mrb_sec)t;
298 if (dealloc) mrb_free(mrb, self);
299 mrb_raisef(mrb, E_ARGUMENT_ERROR, "%v out of Time range", mrb_sec_value(mrb, sec));
304 self->datetime = *aid; /* copy data */
311 mrb_time_wrap(mrb_state *mrb, struct RClass *tc, struct mrb_time *tm)
313 return mrb_obj_value(Data_Wrap_Struct(mrb, tc, &mrb_time_type, tm));
316 /* Allocates a mrb_time object and initializes it. */
317 static struct mrb_time*
318 time_alloc_time(mrb_state *mrb, time_t sec, time_t usec, enum mrb_timezone timezone)
322 tm = (struct mrb_time *)mrb_malloc(mrb, sizeof(struct mrb_time));
326 long sec2 = (long)NDIV(tm->usec,1000000); /* negative div */
327 tm->usec -= sec2 * 1000000;
330 else if (tm->usec >= 1000000) {
331 long sec2 = (long)(tm->usec / 1000000);
332 tm->usec -= sec2 * 1000000;
335 tm->timezone = timezone;
336 time_update_datetime(mrb, tm, TRUE);
341 static struct mrb_time*
342 time_alloc(mrb_state *mrb, mrb_value sec, mrb_value usec, enum mrb_timezone timezone)
346 tsec = mrb_to_time_t(mrb, sec, &tusec);
347 tusec += mrb_to_time_t(mrb, usec, NULL);
349 return time_alloc_time(mrb, tsec, tusec, timezone);
353 mrb_time_make_time(mrb_state *mrb, struct RClass *c, time_t sec, time_t usec, enum mrb_timezone timezone)
355 return mrb_time_wrap(mrb, c, time_alloc_time(mrb, sec, usec, timezone));
359 mrb_time_make(mrb_state *mrb, struct RClass *c, mrb_value sec, mrb_value usec, enum mrb_timezone timezone)
361 return mrb_time_wrap(mrb, c, time_alloc(mrb, sec, usec, timezone));
364 static struct mrb_time*
365 current_mrb_time(mrb_state *mrb)
367 struct mrb_time tmzero = {0};
371 #if defined(TIME_UTC) && !defined(__ANDROID__)
374 if (timespec_get(&ts, TIME_UTC) == 0) {
375 mrb_raise(mrb, E_RUNTIME_ERROR, "timespec_get() failed for unknown reasons");
378 usec = ts.tv_nsec / 1000;
380 #elif defined(NO_GETTIMEOFDAY)
382 static time_t last_sec = 0, last_usec = 0;
385 if (sec != last_sec) {
390 /* add 1 usec to differentiate two times */
399 gettimeofday(&tv, NULL);
404 tm = (struct mrb_time *)mrb_malloc(mrb, sizeof(*tm));
406 tm->sec = sec; tm->usec = usec;
407 tm->timezone = MRB_TIMEZONE_LOCAL;
408 time_update_datetime(mrb, tm, TRUE);
413 /* Allocates a new Time object with given millis value. */
415 mrb_time_now(mrb_state *mrb, mrb_value self)
417 return mrb_time_wrap(mrb, mrb_class_ptr(self), current_mrb_time(mrb));
421 mrb_time_at(mrb_state *mrb, time_t sec, time_t usec, enum mrb_timezone zone)
423 return mrb_time_make_time(mrb, mrb_class_get(mrb, "Time"), sec, usec, zone);
427 /* Creates an instance of time at the given time in seconds, etc. */
429 mrb_time_at_m(mrb_state *mrb, mrb_value self)
432 mrb_value usec = mrb_fixnum_value(0);
434 mrb_get_args(mrb, "o|o", &sec, &usec);
436 return mrb_time_make(mrb, mrb_class_ptr(self), sec, usec, MRB_TIMEZONE_LOCAL);
439 static struct mrb_time*
440 time_mktime(mrb_state *mrb, mrb_int ayear, mrb_int amonth, mrb_int aday,
441 mrb_int ahour, mrb_int amin, mrb_int asec, mrb_int ausec,
442 enum mrb_timezone timezone)
445 struct tm nowtime = { 0 };
447 nowtime.tm_year = (int)ayear - 1900;
448 nowtime.tm_mon = (int)amonth - 1;
449 nowtime.tm_mday = (int)aday;
450 nowtime.tm_hour = (int)ahour;
451 nowtime.tm_min = (int)amin;
452 nowtime.tm_sec = (int)asec;
453 nowtime.tm_isdst = -1;
455 if (nowtime.tm_mon < 0 || nowtime.tm_mon > 11
456 || nowtime.tm_mday < 1 || nowtime.tm_mday > 31
457 || nowtime.tm_hour < 0 || nowtime.tm_hour > 24
458 || (nowtime.tm_hour == 24 && (nowtime.tm_min > 0 || nowtime.tm_sec > 0))
459 || nowtime.tm_min < 0 || nowtime.tm_min > 59
460 || nowtime.tm_sec < 0 || nowtime.tm_sec > 60)
461 mrb_raise(mrb, E_RUNTIME_ERROR, "argument out of range");
463 if (timezone == MRB_TIMEZONE_UTC) {
464 nowsecs = timegm(&nowtime);
467 nowsecs = mktime(&nowtime);
469 if (nowsecs == (time_t)-1) {
470 mrb_raise(mrb, E_ARGUMENT_ERROR, "Not a valid time.");
473 return time_alloc_time(mrb, nowsecs, ausec, timezone);
477 /* Creates an instance of time at the given time in UTC. */
479 mrb_time_gm(mrb_state *mrb, mrb_value self)
481 mrb_int ayear = 0, amonth = 1, aday = 1, ahour = 0, amin = 0, asec = 0, ausec = 0;
483 mrb_get_args(mrb, "i|iiiiii",
484 &ayear, &amonth, &aday, &ahour, &amin, &asec, &ausec);
485 return mrb_time_wrap(mrb, mrb_class_ptr(self),
486 time_mktime(mrb, ayear, amonth, aday, ahour, amin, asec, ausec, MRB_TIMEZONE_UTC));
491 /* Creates an instance of time at the given time in local time zone. */
493 mrb_time_local(mrb_state *mrb, mrb_value self)
495 mrb_int ayear = 0, amonth = 1, aday = 1, ahour = 0, amin = 0, asec = 0, ausec = 0;
497 mrb_get_args(mrb, "i|iiiiii",
498 &ayear, &amonth, &aday, &ahour, &amin, &asec, &ausec);
499 return mrb_time_wrap(mrb, mrb_class_ptr(self),
500 time_mktime(mrb, ayear, amonth, aday, ahour, amin, asec, ausec, MRB_TIMEZONE_LOCAL));
503 static struct mrb_time*
504 time_get_ptr(mrb_state *mrb, mrb_value time)
508 tm = DATA_GET_PTR(mrb, time, &mrb_time_type, struct mrb_time);
510 mrb_raise(mrb, E_ARGUMENT_ERROR, "uninitialized time");
516 mrb_time_eq(mrb_state *mrb, mrb_value self)
518 mrb_value other = mrb_get_arg1(mrb);
519 struct mrb_time *tm1, *tm2;
522 tm1 = DATA_GET_PTR(mrb, self, &mrb_time_type, struct mrb_time);
523 tm2 = DATA_CHECK_GET_PTR(mrb, other, &mrb_time_type, struct mrb_time);
524 eq_p = tm1 && tm2 && tm1->sec == tm2->sec && tm1->usec == tm2->usec;
526 return mrb_bool_value(eq_p);
530 mrb_time_cmp(mrb_state *mrb, mrb_value self)
532 mrb_value other = mrb_get_arg1(mrb);
533 struct mrb_time *tm1, *tm2;
535 tm1 = DATA_GET_PTR(mrb, self, &mrb_time_type, struct mrb_time);
536 tm2 = DATA_CHECK_GET_PTR(mrb, other, &mrb_time_type, struct mrb_time);
537 if (!tm1 || !tm2) return mrb_nil_value();
538 if (tm1->sec > tm2->sec) {
539 return mrb_fixnum_value(1);
541 else if (tm1->sec < tm2->sec) {
542 return mrb_fixnum_value(-1);
544 /* tm1->sec == tm2->sec */
545 if (tm1->usec > tm2->usec) {
546 return mrb_fixnum_value(1);
548 else if (tm1->usec < tm2->usec) {
549 return mrb_fixnum_value(-1);
551 return mrb_fixnum_value(0);
555 mrb_time_plus(mrb_state *mrb, mrb_value self)
557 mrb_value o = mrb_get_arg1(mrb);
561 tm = time_get_ptr(mrb, self);
562 sec = mrb_to_time_t(mrb, o, &usec);
563 return mrb_time_make_time(mrb, mrb_obj_class(mrb, self), tm->sec+sec, tm->usec+usec, tm->timezone);
567 mrb_time_minus(mrb_state *mrb, mrb_value self)
569 mrb_value other = mrb_get_arg1(mrb);
570 struct mrb_time *tm, *tm2;
572 tm = time_get_ptr(mrb, self);
573 tm2 = DATA_CHECK_GET_PTR(mrb, other, &mrb_time_type, struct mrb_time);
575 #ifndef MRB_WITHOUT_FLOAT
577 f = (mrb_sec)(tm->sec - tm2->sec)
578 + (mrb_sec)(tm->usec - tm2->usec) / 1.0e6;
579 return mrb_float_value(mrb, f);
582 f = tm->sec - tm2->sec;
583 if (tm->usec < tm2->usec) f--;
584 return mrb_fixnum_value(f);
589 sec = mrb_to_time_t(mrb, other, &usec);
590 return mrb_time_make_time(mrb, mrb_obj_class(mrb, self), tm->sec-sec, tm->usec-usec, tm->timezone);
595 /* Returns week day number of time. */
597 mrb_time_wday(mrb_state *mrb, mrb_value self)
601 tm = time_get_ptr(mrb, self);
602 return mrb_fixnum_value(tm->datetime.tm_wday);
606 /* Returns year day number of time. */
608 mrb_time_yday(mrb_state *mrb, mrb_value self)
612 tm = time_get_ptr(mrb, self);
613 return mrb_fixnum_value(tm->datetime.tm_yday + 1);
617 /* Returns year of time. */
619 mrb_time_year(mrb_state *mrb, mrb_value self)
623 tm = time_get_ptr(mrb, self);
624 return mrb_fixnum_value(tm->datetime.tm_year + 1900);
628 /* Returns name of time's timezone. */
630 mrb_time_zone(mrb_state *mrb, mrb_value self)
634 tm = time_get_ptr(mrb, self);
635 if (tm->timezone <= MRB_TIMEZONE_NONE) return mrb_nil_value();
636 if (tm->timezone >= MRB_TIMEZONE_LAST) return mrb_nil_value();
637 return mrb_str_new_static(mrb,
638 timezone_names[tm->timezone].name,
639 timezone_names[tm->timezone].len);
643 /* Returns a string that describes the time. */
645 mrb_time_asctime(mrb_state *mrb, mrb_value self)
647 struct mrb_time *tm = time_get_ptr(mrb, self);
648 struct tm *d = &tm->datetime;
651 #if defined(MRB_DISABLE_STDIO)
657 s = asctime_r(d, buf);
659 len = strlen(s)-1; /* truncate the last newline */
663 len = snprintf(buf, sizeof(buf), "%s %s %2d %02d:%02d:%02d %.4d",
664 wday_names[d->tm_wday], mon_names[d->tm_mon], d->tm_mday,
665 d->tm_hour, d->tm_min, d->tm_sec,
668 return mrb_str_new(mrb, buf, len);
672 /* Returns the day in the month of the time. */
674 mrb_time_day(mrb_state *mrb, mrb_value self)
678 tm = time_get_ptr(mrb, self);
679 return mrb_fixnum_value(tm->datetime.tm_mday);
684 /* Returns true if daylight saving was applied for this time. */
686 mrb_time_dst_p(mrb_state *mrb, mrb_value self)
690 tm = time_get_ptr(mrb, self);
691 return mrb_bool_value(tm->datetime.tm_isdst);
696 /* Returns the Time object of the UTC(GMT) timezone. */
698 mrb_time_getutc(mrb_state *mrb, mrb_value self)
700 struct mrb_time *tm, *tm2;
702 tm = time_get_ptr(mrb, self);
703 tm2 = (struct mrb_time *)mrb_malloc(mrb, sizeof(*tm));
705 tm2->timezone = MRB_TIMEZONE_UTC;
706 time_update_datetime(mrb, tm2, TRUE);
707 return mrb_time_wrap(mrb, mrb_obj_class(mrb, self), tm2);
711 /* Returns the Time object of the LOCAL timezone. */
713 mrb_time_getlocal(mrb_state *mrb, mrb_value self)
715 struct mrb_time *tm, *tm2;
717 tm = time_get_ptr(mrb, self);
718 tm2 = (struct mrb_time *)mrb_malloc(mrb, sizeof(*tm));
720 tm2->timezone = MRB_TIMEZONE_LOCAL;
721 time_update_datetime(mrb, tm2, TRUE);
722 return mrb_time_wrap(mrb, mrb_obj_class(mrb, self), tm2);
726 /* Returns hour of time. */
728 mrb_time_hour(mrb_state *mrb, mrb_value self)
732 tm = time_get_ptr(mrb, self);
733 return mrb_fixnum_value(tm->datetime.tm_hour);
737 /* Initializes a time by setting the amount of milliseconds since the epoch.*/
739 mrb_time_initialize(mrb_state *mrb, mrb_value self)
741 mrb_int ayear = 0, amonth = 1, aday = 1, ahour = 0,
742 amin = 0, asec = 0, ausec = 0;
746 n = mrb_get_args(mrb, "|iiiiiii",
747 &ayear, &amonth, &aday, &ahour, &amin, &asec, &ausec);
748 tm = (struct mrb_time*)DATA_PTR(self);
752 mrb_data_init(self, NULL, &mrb_time_type);
755 tm = current_mrb_time(mrb);
758 tm = time_mktime(mrb, ayear, amonth, aday, ahour, amin, asec, ausec, MRB_TIMEZONE_LOCAL);
760 mrb_data_init(self, tm, &mrb_time_type);
764 /* 15.2.19.7.17(x) */
765 /* Initializes a copy of this time object. */
767 mrb_time_initialize_copy(mrb_state *mrb, mrb_value copy)
769 mrb_value src = mrb_get_arg1(mrb);
770 struct mrb_time *t1, *t2;
772 if (mrb_obj_equal(mrb, copy, src)) return copy;
773 if (!mrb_obj_is_instance_of(mrb, src, mrb_obj_class(mrb, copy))) {
774 mrb_raise(mrb, E_TYPE_ERROR, "wrong argument class");
776 t1 = (struct mrb_time *)DATA_PTR(copy);
777 t2 = (struct mrb_time *)DATA_PTR(src);
779 mrb_raise(mrb, E_ARGUMENT_ERROR, "uninitialized time");
782 t1 = (struct mrb_time *)mrb_malloc(mrb, sizeof(struct mrb_time));
783 mrb_data_init(copy, t1, &mrb_time_type);
790 /* Sets the timezone attribute of the Time object to LOCAL. */
792 mrb_time_localtime(mrb_state *mrb, mrb_value self)
796 tm = time_get_ptr(mrb, self);
797 tm->timezone = MRB_TIMEZONE_LOCAL;
798 time_update_datetime(mrb, tm, FALSE);
803 /* Returns day of month of time. */
805 mrb_time_mday(mrb_state *mrb, mrb_value self)
809 tm = time_get_ptr(mrb, self);
810 return mrb_fixnum_value(tm->datetime.tm_mday);
814 /* Returns minutes of time. */
816 mrb_time_min(mrb_state *mrb, mrb_value self)
820 tm = time_get_ptr(mrb, self);
821 return mrb_fixnum_value(tm->datetime.tm_min);
824 /* 15.2.19.7.21 and 15.2.19.7.22 */
825 /* Returns month of time. */
827 mrb_time_mon(mrb_state *mrb, mrb_value self)
831 tm = time_get_ptr(mrb, self);
832 return mrb_fixnum_value(tm->datetime.tm_mon + 1);
836 /* Returns seconds in minute of time. */
838 mrb_time_sec(mrb_state *mrb, mrb_value self)
842 tm = time_get_ptr(mrb, self);
843 return mrb_fixnum_value(tm->datetime.tm_sec);
846 #ifndef MRB_WITHOUT_FLOAT
848 /* Returns a Float with the time since the epoch in seconds. */
850 mrb_time_to_f(mrb_state *mrb, mrb_value self)
854 tm = time_get_ptr(mrb, self);
855 return mrb_float_value(mrb, (mrb_float)tm->sec + (mrb_float)tm->usec/1.0e6);
860 /* Returns an Integer with the time since the epoch in seconds. */
862 mrb_time_to_i(mrb_state *mrb, mrb_value self)
866 tm = time_get_ptr(mrb, self);
867 #ifndef MRB_WITHOUT_FLOAT
868 if (!fixable_time_t_p(tm->sec)) {
869 return mrb_float_value(mrb, (mrb_float)tm->sec);
872 return mrb_fixnum_value((mrb_int)tm->sec);
876 /* Returns an Integer with the time since the epoch in microseconds. */
878 mrb_time_usec(mrb_state *mrb, mrb_value self)
882 tm = time_get_ptr(mrb, self);
883 #ifndef MRB_WITHOUT_FLOAT
884 if (!fixable_time_t_p(tm->usec)) {
885 return mrb_float_value(mrb, (mrb_float)tm->usec);
888 return mrb_fixnum_value((mrb_int)tm->usec);
892 /* Sets the timezone attribute of the Time object to UTC. */
894 mrb_time_utc(mrb_state *mrb, mrb_value self)
898 tm = time_get_ptr(mrb, self);
899 tm->timezone = MRB_TIMEZONE_UTC;
900 time_update_datetime(mrb, tm, FALSE);
905 /* Returns true if this time is in the UTC timezone false if not. */
907 mrb_time_utc_p(mrb_state *mrb, mrb_value self)
911 tm = time_get_ptr(mrb, self);
912 return mrb_bool_value(tm->timezone == MRB_TIMEZONE_UTC);
916 time_to_s_utc(mrb_state *mrb, struct mrb_time *tm, char *buf, size_t buf_len)
918 return strftime(buf, buf_len, TO_S_FMT "UTC", &tm->datetime);
922 time_to_s_local(mrb_state *mrb, struct mrb_time *tm, char *buf, size_t buf_len)
924 #if defined(_MSC_VER) && _MSC_VER < 1900 || defined(__MINGW64__) || defined(__MINGW32__)
925 struct tm datetime = {0};
926 time_t utc_sec = timegm(&tm->datetime);
930 if (utc_sec == (time_t)-1) {
931 mrb_raise(mrb, E_ARGUMENT_ERROR, "Not a valid time.");
933 offset = abs((int)(utc_sec - tm->sec) / 60);
934 datetime.tm_year = 100;
935 datetime.tm_hour = offset / 60;
936 datetime.tm_min = offset % 60;
937 len = strftime(buf, buf_len, TO_S_FMT, &tm->datetime);
938 buf[len++] = utc_sec < tm->sec ? '-' : '+';
940 return len + strftime(buf + len, buf_len - len, "%H%M", &datetime);
942 return strftime(buf, buf_len, TO_S_FMT "%z", &tm->datetime);
947 mrb_time_to_s(mrb_state *mrb, mrb_value self)
950 struct mrb_time *tm = time_get_ptr(mrb, self);
951 mrb_bool utc = tm->timezone == MRB_TIMEZONE_UTC;
952 size_t len = (utc ? time_to_s_utc : time_to_s_local)(mrb, tm, buf, sizeof(buf));
953 mrb_value str = mrb_str_new(mrb, buf, len);
954 RSTR_SET_ASCII_FLAG(mrb_str_ptr(str));
959 mrb_mruby_time_gem_init(mrb_state* mrb)
963 tc = mrb_define_class(mrb, "Time", mrb->object_class);
964 MRB_SET_INSTANCE_TT(tc, MRB_TT_DATA);
965 mrb_include_module(mrb, tc, mrb_module_get(mrb, "Comparable"));
966 mrb_define_class_method(mrb, tc, "at", mrb_time_at_m, MRB_ARGS_ARG(1, 1)); /* 15.2.19.6.1 */
967 mrb_define_class_method(mrb, tc, "gm", mrb_time_gm, MRB_ARGS_ARG(1,6)); /* 15.2.19.6.2 */
968 mrb_define_class_method(mrb, tc, "local", mrb_time_local, MRB_ARGS_ARG(1,6)); /* 15.2.19.6.3 */
969 mrb_define_class_method(mrb, tc, "mktime", mrb_time_local, MRB_ARGS_ARG(1,6));/* 15.2.19.6.4 */
970 mrb_define_class_method(mrb, tc, "now", mrb_time_now, MRB_ARGS_NONE()); /* 15.2.19.6.5 */
971 mrb_define_class_method(mrb, tc, "utc", mrb_time_gm, MRB_ARGS_ARG(1,6)); /* 15.2.19.6.6 */
973 mrb_define_method(mrb, tc, "==" , mrb_time_eq , MRB_ARGS_REQ(1));
974 mrb_define_method(mrb, tc, "<=>" , mrb_time_cmp , MRB_ARGS_REQ(1)); /* 15.2.19.7.1 */
975 mrb_define_method(mrb, tc, "+" , mrb_time_plus , MRB_ARGS_REQ(1)); /* 15.2.19.7.2 */
976 mrb_define_method(mrb, tc, "-" , mrb_time_minus , MRB_ARGS_REQ(1)); /* 15.2.19.7.3 */
977 mrb_define_method(mrb, tc, "to_s" , mrb_time_to_s , MRB_ARGS_NONE());
978 mrb_define_method(mrb, tc, "inspect", mrb_time_to_s , MRB_ARGS_NONE());
979 mrb_define_method(mrb, tc, "asctime", mrb_time_asctime, MRB_ARGS_NONE()); /* 15.2.19.7.4 */
980 mrb_define_method(mrb, tc, "ctime" , mrb_time_asctime, MRB_ARGS_NONE()); /* 15.2.19.7.5 */
981 mrb_define_method(mrb, tc, "day" , mrb_time_day , MRB_ARGS_NONE()); /* 15.2.19.7.6 */
982 mrb_define_method(mrb, tc, "dst?" , mrb_time_dst_p , MRB_ARGS_NONE()); /* 15.2.19.7.7 */
983 mrb_define_method(mrb, tc, "getgm" , mrb_time_getutc , MRB_ARGS_NONE()); /* 15.2.19.7.8 */
984 mrb_define_method(mrb, tc, "getlocal",mrb_time_getlocal,MRB_ARGS_NONE()); /* 15.2.19.7.9 */
985 mrb_define_method(mrb, tc, "getutc" , mrb_time_getutc , MRB_ARGS_NONE()); /* 15.2.19.7.10 */
986 mrb_define_method(mrb, tc, "gmt?" , mrb_time_utc_p , MRB_ARGS_NONE()); /* 15.2.19.7.11 */
987 mrb_define_method(mrb, tc, "gmtime" , mrb_time_utc , MRB_ARGS_NONE()); /* 15.2.19.7.13 */
988 mrb_define_method(mrb, tc, "hour" , mrb_time_hour, MRB_ARGS_NONE()); /* 15.2.19.7.15 */
989 mrb_define_method(mrb, tc, "localtime", mrb_time_localtime, MRB_ARGS_NONE()); /* 15.2.19.7.18 */
990 mrb_define_method(mrb, tc, "mday" , mrb_time_mday, MRB_ARGS_NONE()); /* 15.2.19.7.19 */
991 mrb_define_method(mrb, tc, "min" , mrb_time_min, MRB_ARGS_NONE()); /* 15.2.19.7.20 */
993 mrb_define_method(mrb, tc, "mon" , mrb_time_mon, MRB_ARGS_NONE()); /* 15.2.19.7.21 */
994 mrb_define_method(mrb, tc, "month", mrb_time_mon, MRB_ARGS_NONE()); /* 15.2.19.7.22 */
996 mrb_define_method(mrb, tc, "sec" , mrb_time_sec, MRB_ARGS_NONE()); /* 15.2.19.7.23 */
997 mrb_define_method(mrb, tc, "to_i", mrb_time_to_i, MRB_ARGS_NONE()); /* 15.2.19.7.25 */
998 #ifndef MRB_WITHOUT_FLOAT
999 mrb_define_method(mrb, tc, "to_f", mrb_time_to_f, MRB_ARGS_NONE()); /* 15.2.19.7.24 */
1001 mrb_define_method(mrb, tc, "usec", mrb_time_usec, MRB_ARGS_NONE()); /* 15.2.19.7.26 */
1002 mrb_define_method(mrb, tc, "utc" , mrb_time_utc, MRB_ARGS_NONE()); /* 15.2.19.7.27 */
1003 mrb_define_method(mrb, tc, "utc?", mrb_time_utc_p,MRB_ARGS_NONE()); /* 15.2.19.7.28 */
1004 mrb_define_method(mrb, tc, "wday", mrb_time_wday, MRB_ARGS_NONE()); /* 15.2.19.7.30 */
1005 mrb_define_method(mrb, tc, "yday", mrb_time_yday, MRB_ARGS_NONE()); /* 15.2.19.7.31 */
1006 mrb_define_method(mrb, tc, "year", mrb_time_year, MRB_ARGS_NONE()); /* 15.2.19.7.32 */
1007 mrb_define_method(mrb, tc, "zone", mrb_time_zone, MRB_ARGS_NONE()); /* 15.2.19.7.33 */
1009 mrb_define_method(mrb, tc, "initialize", mrb_time_initialize, MRB_ARGS_REQ(1)); /* 15.2.19.7.16 */
1010 mrb_define_method(mrb, tc, "initialize_copy", mrb_time_initialize_copy, MRB_ARGS_REQ(1)); /* 15.2.19.7.17 */
1013 methods not available:
1014 gmt_offset(15.2.19.7.12)
1015 gmtoff(15.2.19.7.14)
1016 utc_offset(15.2.19.7.29)
1021 mrb_mruby_time_gem_final(mrb_state* mrb)