Groonga 3.0.9 Source Code Document
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
time.c
Go to the documentation of this file.
1 /*
2 ** time.c - Time class
3 **
4 ** See Copyright Notice in mruby.h
5 */
6 
7 
8 #include "mruby.h"
9 #include <stdio.h>
10 #include <time.h>
11 #include "mruby/class.h"
12 #include "mruby/data.h"
13 
16 /* gettimeofday(2) */
17 /* C99 does not have gettimeofday that is required to retrieve microseconds */
18 /* uncomment following macro on platforms without gettimeofday(2) */
19 /* #define NO_GETTIMEOFDAY */
20 
21 /* gmtime(3) */
22 /* C99 does not have reentrant gmtime_r() so it might cause troubles under */
23 /* multi-threading environment. undef following macro on platforms that */
24 /* does not have gmtime_r() and localtime_r(). */
25 /* #define NO_GMTIME_R */
26 
27 #ifdef _WIN32
28 #if _MSC_VER
29 /* Win32 platform do not provide gmtime_r/localtime_r; emulate them using gmtime_s/localtime_s */
30 #define gmtime_r(tp, tm) ((gmtime_s((tm), (tp)) == 0) ? (tm) : NULL)
31 #define localtime_r(tp, tm) ((localtime_s((tm), (tp)) == 0) ? (tm) : NULL)
32 #else
33 #define NO_GMTIME_R
34 #endif
35 #endif
36 
37 /* timegm(3) */
38 /* mktime() creates tm structure for localtime; timegm() is for UTF time */
39 /* define following macro to use probably faster timegm() on the platform */
40 /* #define USE_SYSTEM_TIMEGM */
41 
44 #ifndef NO_GETTIMEOFDAY
45 #include <sys/time.h>
46 #endif
47 #ifdef NO_GMTIME_R
48 #define gmtime_r(t,r) gmtime(t)
49 #define localtime_r(t,r) (tzset(),localtime(t))
50 #endif
51 
52 #ifndef USE_SYSTEM_TIMEGM
53 #define timegm my_timgm
54 
55 static unsigned int
56 is_leapyear(unsigned int y)
57 {
58  return (y % 4) == 0 && ((y % 100) != 0 || (y % 400) == 0);
59 }
60 
61 static time_t
62 timegm(struct tm *tm)
63 {
64  static const unsigned int ndays[2][12] = {
65  {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
66  {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
67  };
68  time_t r = 0;
69  int i;
70  unsigned int *nday = (unsigned int*) ndays[is_leapyear(tm->tm_year+1900)];
71 
72  for (i = 70; i < tm->tm_year; ++i)
73  r += is_leapyear(i+1900) ? 366*24*60*60 : 365*24*60*60;
74  for (i = 0; i < tm->tm_mon; ++i)
75  r += nday[i] * 24 * 60 * 60;
76  r += (tm->tm_mday - 1) * 24 * 60 * 60;
77  r += tm->tm_hour * 60 * 60;
78  r += tm->tm_min * 60;
79  r += tm->tm_sec;
80  return r;
81 }
82 #endif
83 
84 /* Since we are limited to using ISO C89, this implementation is based
85 * on time_t. That means the resolution of time is only precise to the
86 * second level. Also, there are only 2 timezones, namely UTC and LOCAL.
87 */
88 
94 };
95 
96 static const char *timezone_names[] = {
97  "none",
98  "UTC",
99  "LOCAL",
100  NULL
101 };
102 
103 static const char *mon_names[] = {
104  "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
105 };
106 
107 static const char *wday_names[] = {
108  "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat",
109 };
110 
111 struct mrb_time {
112  time_t sec;
113  time_t usec;
115  struct tm datetime;
116 };
117 
118 static struct mrb_data_type mrb_time_type = { "Time", mrb_free };
119 
122 static struct mrb_time*
123 mrb_time_update_datetime(struct mrb_time *self)
124 {
125  struct tm *aid;
126 
127  if (self->timezone == MRB_TIMEZONE_UTC) {
128  aid = gmtime_r(&self->sec, &self->datetime);
129  }
130  else {
131  aid = localtime_r(&self->sec, &self->datetime);
132  }
133  if (!aid) return NULL;
134 #ifdef NO_GMTIME_R
135  self->datetime = *aid; // copy data
136 #endif
137 
138  return self;
139 }
140 
141 static mrb_value
142 mrb_time_wrap(mrb_state *mrb, struct RClass *tc, struct mrb_time *tm)
143 {
144  return mrb_obj_value(Data_Wrap_Struct(mrb, tc, &mrb_time_type, tm));
145 }
146 
147 
148 /* Allocates a mrb_time object and initializes it. */
149 static struct mrb_time*
150 time_alloc(mrb_state *mrb, double sec, double usec, enum mrb_timezone timezone)
151 {
152  struct mrb_time *tm;
153 
154  tm = (struct mrb_time *)mrb_malloc(mrb, sizeof(struct mrb_time));
155  tm->sec = (time_t)sec;
156  tm->usec = (sec - tm->sec) * 1.0e6 + usec;
157  while (tm->usec < 0) {
158  tm->sec--;
159  tm->usec += 1.0e6;
160  }
161  while (tm->usec > 1.0e6) {
162  tm->sec++;
163  tm->usec -= 1.0e6;
164  }
165  tm->timezone = timezone;
166  mrb_time_update_datetime(tm);
167 
168  return tm;
169 }
170 
171 static mrb_value
172 mrb_time_make(mrb_state *mrb, struct RClass *c, double sec, double usec, enum mrb_timezone timezone)
173 {
174  return mrb_time_wrap(mrb, c, time_alloc(mrb, sec, usec, timezone));
175 }
176 
177 static struct mrb_time*
178 current_mrb_time(mrb_state *mrb)
179 {
180  struct mrb_time *tm;
181 
182  tm = (struct mrb_time *)mrb_malloc(mrb, sizeof(*tm));
183 #ifdef NO_GETTIMEOFDAY
184  {
185  static time_t last_sec = 0, last_usec = 0;
186 
187  tm->sec = time(NULL);
188  if (tm->sec != last_sec) {
189  last_sec = tm->sec;
190  last_usec = 0;
191  }
192  else {
193  /* add 1 usec to differentiate two times */
194  last_usec += 1;
195  }
196  tm->usec = last_usec;
197  }
198 #else
199  {
200  struct timeval tv;
201 
202  gettimeofday(&tv, NULL);
203  tm->sec = tv.tv_sec;
204  tm->usec = tv.tv_usec;
205  }
206 #endif
208  mrb_time_update_datetime(tm);
209 
210  return tm;
211 }
212 
213 /* Allocates a new Time object with given millis value. */
214 static mrb_value
215 mrb_time_now(mrb_state *mrb, mrb_value self)
216 {
217  return mrb_time_wrap(mrb, mrb_class_ptr(self), current_mrb_time(mrb));
218 }
219 
220 /* 15.2.19.6.1 */
221 /* Creates an instance of time at the given time in seconds, etc. */
222 static mrb_value
223 mrb_time_at(mrb_state *mrb, mrb_value self)
224 {
225  mrb_float f, f2 = 0;
226 
227  mrb_get_args(mrb, "f|f", &f, &f2);
228  return mrb_time_make(mrb, mrb_class_ptr(self), f, f2, MRB_TIMEZONE_LOCAL);
229 }
230 
231 static struct mrb_time*
232 time_mktime(mrb_state *mrb, mrb_int ayear, mrb_int amonth, mrb_int aday,
233  mrb_int ahour, mrb_int amin, mrb_int asec, mrb_int ausec,
234  enum mrb_timezone timezone)
235 {
236  time_t nowsecs;
237  struct tm nowtime = { 0 };
238 
239  nowtime.tm_year = (int)ayear - 1900;
240  nowtime.tm_mon = (int)amonth - 1;
241  nowtime.tm_mday = (int)aday;
242  nowtime.tm_hour = (int)ahour;
243  nowtime.tm_min = (int)amin;
244  nowtime.tm_sec = (int)asec;
245  nowtime.tm_isdst = -1;
246  if (timezone == MRB_TIMEZONE_UTC) {
247  nowsecs = timegm(&nowtime);
248  }
249  else {
250  nowsecs = mktime(&nowtime);
251  }
252  if (nowsecs < 0) {
253  mrb_raise(mrb, E_ARGUMENT_ERROR, "Not a valid time.");
254  }
255 
256  return time_alloc(mrb, nowsecs, ausec, timezone);
257 }
258 
259 /* 15.2.19.6.2 */
260 /* Creates an instance of time at the given time in UTC. */
261 static mrb_value
262 mrb_time_gm(mrb_state *mrb, mrb_value self)
263 {
264  mrb_int ayear = 0, amonth = 1, aday = 1, ahour = 0, amin = 0, asec = 0, ausec = 0;
265 
266  mrb_get_args(mrb, "i|iiiiii",
267  &ayear, &amonth, &aday, &ahour, &amin, &asec, &ausec);
268  return mrb_time_wrap(mrb, mrb_class_ptr(self),
269  time_mktime(mrb, ayear, amonth, aday, ahour, amin, asec, ausec, MRB_TIMEZONE_UTC));
270 }
271 
272 
273 /* 15.2.19.6.3 */
274 /* Creates an instance of time at the given time in local time zone. */
275 static mrb_value
276 mrb_time_local(mrb_state *mrb, mrb_value self)
277 {
278  mrb_int ayear = 0, amonth = 1, aday = 1, ahour = 0, amin = 0, asec = 0, ausec = 0;
279 
280  mrb_get_args(mrb, "i|iiiiii",
281  &ayear, &amonth, &aday, &ahour, &amin, &asec, &ausec);
282  return mrb_time_wrap(mrb, mrb_class_ptr(self),
283  time_mktime(mrb, ayear, amonth, aday, ahour, amin, asec, ausec, MRB_TIMEZONE_LOCAL));
284 }
285 
286 
287 static mrb_value
288 mrb_time_eq(mrb_state *mrb, mrb_value self)
289 {
290  mrb_value other;
291  struct mrb_time *tm1, *tm2;
292  mrb_bool eq_p;
293 
294  mrb_get_args(mrb, "o", &other);
295  tm1 = DATA_CHECK_GET_PTR(mrb, self, &mrb_time_type, struct mrb_time);
296  tm2 = DATA_CHECK_GET_PTR(mrb, other, &mrb_time_type, struct mrb_time);
297  eq_p = tm1 && tm2 && tm1->sec == tm2->sec && tm1->usec == tm2->usec;
298 
299  return mrb_bool_value(eq_p);
300 }
301 
302 static mrb_value
303 mrb_time_cmp(mrb_state *mrb, mrb_value self)
304 {
305  mrb_value other;
306  struct mrb_time *tm1, *tm2;
307 
308  mrb_get_args(mrb, "o", &other);
309  tm1 = DATA_CHECK_GET_PTR(mrb, self, &mrb_time_type, struct mrb_time);
310  tm2 = DATA_CHECK_GET_PTR(mrb, other, &mrb_time_type, struct mrb_time);
311  if (!tm1 || !tm2) return mrb_nil_value();
312  if (tm1->sec > tm2->sec) {
313  return mrb_fixnum_value(1);
314  }
315  else if (tm1->sec < tm2->sec) {
316  return mrb_fixnum_value(-1);
317  }
318  /* tm1->sec == tm2->sec */
319  if (tm1->usec > tm2->usec) {
320  return mrb_fixnum_value(1);
321  }
322  else if (tm1->usec < tm2->usec) {
323  return mrb_fixnum_value(-1);
324  }
325  return mrb_fixnum_value(0);
326 }
327 
328 static mrb_value
329 mrb_time_plus(mrb_state *mrb, mrb_value self)
330 {
331  mrb_float f;
332  struct mrb_time *tm;
333 
334  mrb_get_args(mrb, "f", &f);
335  tm = DATA_GET_PTR(mrb, self, &mrb_time_type, struct mrb_time);
336  return mrb_time_make(mrb, mrb_obj_class(mrb, self), (double)tm->sec+f, tm->usec, tm->timezone);
337 }
338 
339 static mrb_value
340 mrb_time_minus(mrb_state *mrb, mrb_value self)
341 {
342  mrb_float f;
343  mrb_value other;
344  struct mrb_time *tm, *tm2;
345 
346  mrb_get_args(mrb, "o", &other);
347  tm = DATA_GET_PTR(mrb, self, &mrb_time_type, struct mrb_time);
348 
349  tm2 = DATA_CHECK_GET_PTR(mrb, other, &mrb_time_type, struct mrb_time);
350  if (tm2) {
351  f = (mrb_float)(tm->sec - tm2->sec)
352  + (mrb_float)(tm->usec - tm2->usec) / 1.0e6;
353  return mrb_float_value(mrb, f);
354  }
355  else {
356  mrb_get_args(mrb, "f", &f);
357  return mrb_time_make(mrb, mrb_obj_class(mrb, self), (double)tm->sec-f, tm->usec, tm->timezone);
358  }
359 }
360 
361 /* 15.2.19.7.30 */
362 /* Returns week day number of time. */
363 static mrb_value
364 mrb_time_wday(mrb_state *mrb, mrb_value self)
365 {
366  struct mrb_time *tm;
367 
368  tm = DATA_GET_PTR(mrb, self, &mrb_time_type, struct mrb_time);
369  return mrb_fixnum_value(tm->datetime.tm_wday);
370 }
371 
372 /* 15.2.19.7.31 */
373 /* Returns year day number of time. */
374 static mrb_value
375 mrb_time_yday(mrb_state *mrb, mrb_value self)
376 {
377  struct mrb_time *tm;
378 
379  tm = DATA_GET_PTR(mrb, self, &mrb_time_type, struct mrb_time);
380  return mrb_fixnum_value(tm->datetime.tm_yday + 1);
381 }
382 
383 /* 15.2.19.7.32 */
384 /* Returns year of time. */
385 static mrb_value
386 mrb_time_year(mrb_state *mrb, mrb_value self)
387 {
388  struct mrb_time *tm;
389 
390  tm = DATA_GET_PTR(mrb, self, &mrb_time_type, struct mrb_time);
391  return mrb_fixnum_value(tm->datetime.tm_year + 1900);
392 }
393 
394 /* 15.2.19.7.33 */
395 /* Returns name of time's timezone. */
396 static mrb_value
397 mrb_time_zone(mrb_state *mrb, mrb_value self)
398 {
399  struct mrb_time *tm;
400 
401  tm = DATA_GET_PTR(mrb, self, &mrb_time_type, struct mrb_time);
402  if (tm->timezone <= MRB_TIMEZONE_NONE) return mrb_nil_value();
403  if (tm->timezone >= MRB_TIMEZONE_LAST) return mrb_nil_value();
404  return mrb_str_new_cstr(mrb, timezone_names[tm->timezone]);
405 }
406 
407 /* 15.2.19.7.4 */
408 /* Returns a string that describes the time. */
409 static mrb_value
410 mrb_time_asctime(mrb_state *mrb, mrb_value self)
411 {
412  struct mrb_time *tm;
413  struct tm *d;
414  char buf[256];
415  int len;
416 
417  tm = DATA_GET_PTR(mrb, self, &mrb_time_type, struct mrb_time);
418  d = &tm->datetime;
419  len = snprintf(buf, sizeof(buf), "%s %s %02d %02d:%02d:%02d %s%d",
420  wday_names[d->tm_wday], mon_names[d->tm_mon], d->tm_mday,
421  d->tm_hour, d->tm_min, d->tm_sec,
422  tm->timezone == MRB_TIMEZONE_UTC ? "UTC " : "",
423  d->tm_year + 1900);
424  return mrb_str_new(mrb, buf, len);
425 }
426 
427 /* 15.2.19.7.6 */
428 /* Returns the day in the month of the time. */
429 static mrb_value
430 mrb_time_day(mrb_state *mrb, mrb_value self)
431 {
432  struct mrb_time *tm;
433 
434  tm = DATA_GET_PTR(mrb, self, &mrb_time_type, struct mrb_time);
435  if (!tm) return mrb_nil_value();
436  return mrb_fixnum_value(tm->datetime.tm_mday);
437 }
438 
439 
440 /* 15.2.19.7.7 */
441 /* Returns true if daylight saving was applied for this time. */
442 static mrb_value
443 mrb_time_dst_p(mrb_state *mrb, mrb_value self)
444 {
445  struct mrb_time *tm;
446 
447  tm = DATA_GET_PTR(mrb, self, &mrb_time_type, struct mrb_time);
448  return mrb_bool_value(tm->datetime.tm_isdst);
449 }
450 
451 /* 15.2.19.7.8 */
452 /* 15.2.19.7.10 */
453 /* Returns the Time object of the UTC(GMT) timezone. */
454 static mrb_value
455 mrb_time_getutc(mrb_state *mrb, mrb_value self)
456 {
457  struct mrb_time *tm, *tm2;
458 
459  tm = DATA_GET_PTR(mrb, self, &mrb_time_type, struct mrb_time);
460  tm2 = (struct mrb_time *)mrb_malloc(mrb, sizeof(*tm));
461  *tm2 = *tm;
462  tm2->timezone = MRB_TIMEZONE_UTC;
463  mrb_time_update_datetime(tm2);
464  return mrb_time_wrap(mrb, mrb_obj_class(mrb, self), tm2);
465 }
466 
467 /* 15.2.19.7.9 */
468 /* Returns the Time object of the LOCAL timezone. */
469 static mrb_value
470 mrb_time_getlocal(mrb_state *mrb, mrb_value self)
471 {
472  struct mrb_time *tm, *tm2;
473 
474  tm = DATA_GET_PTR(mrb, self, &mrb_time_type, struct mrb_time);
475  tm2 = (struct mrb_time *)mrb_malloc(mrb, sizeof(*tm));
476  *tm2 = *tm;
478  mrb_time_update_datetime(tm2);
479  return mrb_time_wrap(mrb, mrb_obj_class(mrb, self), tm2);
480 }
481 
482 /* 15.2.19.7.15 */
483 /* Returns hour of time. */
484 static mrb_value
485 mrb_time_hour(mrb_state *mrb, mrb_value self)
486 {
487  struct mrb_time *tm;
488 
489  tm = DATA_GET_PTR(mrb, self, &mrb_time_type, struct mrb_time);
490  return mrb_fixnum_value(tm->datetime.tm_hour);
491 }
492 
493 /* 15.2.19.7.16 */
494 /* Initializes a time by setting the amount of milliseconds since the epoch.*/
495 static mrb_value
496 mrb_time_initialize(mrb_state *mrb, mrb_value self)
497 {
498  mrb_int ayear = 0, amonth = 1, aday = 1, ahour = 0,
499  amin = 0, asec = 0, ausec = 0;
500  int n;
501  struct mrb_time *tm;
502 
503  tm = (struct mrb_time*)DATA_PTR(self);
504  if (tm) {
505  mrb_free(mrb, tm);
506  }
507  DATA_TYPE(self) = &mrb_time_type;
508  DATA_PTR(self) = NULL;
509 
510  n = mrb_get_args(mrb, "|iiiiiii",
511  &ayear, &amonth, &aday, &ahour, &amin, &asec, &ausec);
512  if (n == 0) {
513  tm = current_mrb_time(mrb);
514  }
515  else {
516  tm = time_mktime(mrb, ayear, amonth, aday, ahour, amin, asec, ausec, MRB_TIMEZONE_LOCAL);
517  }
518  DATA_PTR(self) = tm;
519  return self;
520 }
521 
522 /* 15.2.19.7.17(x) */
523 /* Initializes a copy of this time object. */
524 static mrb_value
525 mrb_time_initialize_copy(mrb_state *mrb, mrb_value copy)
526 {
527  mrb_value src;
528 
529  mrb_get_args(mrb, "o", &src);
530  if (mrb_obj_equal(mrb, copy, src)) return copy;
531  if (!mrb_obj_is_instance_of(mrb, src, mrb_obj_class(mrb, copy))) {
532  mrb_raise(mrb, E_TYPE_ERROR, "wrong argument class");
533  }
534  if (!DATA_PTR(copy)) {
535  DATA_PTR(copy) = mrb_malloc(mrb, sizeof(struct mrb_time));
536  DATA_TYPE(copy) = &mrb_time_type;
537  }
538  *(struct mrb_time *)DATA_PTR(copy) = *(struct mrb_time *)DATA_PTR(src);
539  return copy;
540 }
541 
542 /* 15.2.19.7.18 */
543 /* Sets the timezone attribute of the Time object to LOCAL. */
544 static mrb_value
545 mrb_time_localtime(mrb_state *mrb, mrb_value self)
546 {
547  struct mrb_time *tm;
548 
549  tm = DATA_GET_PTR(mrb, self, &mrb_time_type, struct mrb_time);
551  mrb_time_update_datetime(tm);
552  return self;
553 }
554 
555 /* 15.2.19.7.19 */
556 /* Returns day of month of time. */
557 static mrb_value
558 mrb_time_mday(mrb_state *mrb, mrb_value self)
559 {
560  struct mrb_time *tm;
561 
562  tm = DATA_GET_PTR(mrb, self, &mrb_time_type, struct mrb_time);
563  return mrb_fixnum_value(tm->datetime.tm_mday);
564 }
565 
566 /* 15.2.19.7.20 */
567 /* Returns minutes of time. */
568 static mrb_value
569 mrb_time_min(mrb_state *mrb, mrb_value self)
570 {
571  struct mrb_time *tm;
572 
573  tm = DATA_GET_PTR(mrb, self, &mrb_time_type, struct mrb_time);
574  return mrb_fixnum_value(tm->datetime.tm_min);
575 }
576 
577 /* 15.2.19.7.21 and 15.2.19.7.22 */
578 /* Returns month of time. */
579 static mrb_value
580 mrb_time_mon(mrb_state *mrb, mrb_value self)
581 {
582  struct mrb_time *tm;
583 
584  tm = DATA_GET_PTR(mrb, self, &mrb_time_type, struct mrb_time);
585  return mrb_fixnum_value(tm->datetime.tm_mon + 1);
586 }
587 
588 /* 15.2.19.7.23 */
589 /* Returns seconds in minute of time. */
590 static mrb_value
591 mrb_time_sec(mrb_state *mrb, mrb_value self)
592 {
593  struct mrb_time *tm;
594 
595  tm = DATA_GET_PTR(mrb, self, &mrb_time_type, struct mrb_time);
596  return mrb_fixnum_value(tm->datetime.tm_sec);
597 }
598 
599 
600 /* 15.2.19.7.24 */
601 /* Returns a Float with the time since the epoch in seconds. */
602 static mrb_value
603 mrb_time_to_f(mrb_state *mrb, mrb_value self)
604 {
605  struct mrb_time *tm;
606 
607  tm = DATA_GET_PTR(mrb, self, &mrb_time_type, struct mrb_time);
608  return mrb_float_value(mrb, (mrb_float)tm->sec + (mrb_float)tm->usec/1.0e6);
609 }
610 
611 /* 15.2.19.7.25 */
612 /* Returns a Fixnum with the time since the epoch in seconds. */
613 static mrb_value
614 mrb_time_to_i(mrb_state *mrb, mrb_value self)
615 {
616  struct mrb_time *tm;
617 
618  tm = DATA_GET_PTR(mrb, self, &mrb_time_type, struct mrb_time);
619  return mrb_fixnum_value(tm->sec);
620 }
621 
622 /* 15.2.19.7.26 */
623 /* Returns a Float with the time since the epoch in microseconds. */
624 static mrb_value
625 mrb_time_usec(mrb_state *mrb, mrb_value self)
626 {
627  struct mrb_time *tm;
628 
629  tm = DATA_GET_PTR(mrb, self, &mrb_time_type, struct mrb_time);
630  return mrb_fixnum_value(tm->usec);
631 }
632 
633 /* 15.2.19.7.27 */
634 /* Sets the timezone attribute of the Time object to UTC. */
635 static mrb_value
636 mrb_time_utc(mrb_state *mrb, mrb_value self)
637 {
638  struct mrb_time *tm;
639 
640  tm = DATA_GET_PTR(mrb, self, &mrb_time_type, struct mrb_time);
642  mrb_time_update_datetime(tm);
643  return self;
644 }
645 
646 /* 15.2.19.7.28 */
647 /* Returns true if this time is in the UTC timezone false if not. */
648 static mrb_value
649 mrb_time_utc_p(mrb_state *mrb, mrb_value self)
650 {
651  struct mrb_time *tm;
652 
653  tm = DATA_GET_PTR(mrb, self, &mrb_time_type, struct mrb_time);
654  return mrb_bool_value(tm->timezone == MRB_TIMEZONE_UTC);
655 }
656 
657 
658 void
660 {
661  struct RClass *tc;
662  /* ISO 15.2.19.2 */
663  tc = mrb_define_class(mrb, "Time", mrb->object_class);
665  mrb_include_module(mrb, tc, mrb_class_get(mrb, "Comparable"));
666  mrb_define_class_method(mrb, tc, "at", mrb_time_at, MRB_ARGS_ANY()); /* 15.2.19.6.1 */
667  mrb_define_class_method(mrb, tc, "gm", mrb_time_gm, MRB_ARGS_ARG(1,6)); /* 15.2.19.6.2 */
668  mrb_define_class_method(mrb, tc, "local", mrb_time_local, MRB_ARGS_ARG(1,6)); /* 15.2.19.6.3 */
669  mrb_define_class_method(mrb, tc, "mktime", mrb_time_local, MRB_ARGS_ARG(1,6));/* 15.2.19.6.4 */
670  mrb_define_class_method(mrb, tc, "now", mrb_time_now, MRB_ARGS_NONE()); /* 15.2.19.6.5 */
671  mrb_define_class_method(mrb, tc, "utc", mrb_time_gm, MRB_ARGS_ARG(1,6)); /* 15.2.19.6.6 */
672 
673  mrb_define_method(mrb, tc, "==" , mrb_time_eq , MRB_ARGS_REQ(1));
674  mrb_define_method(mrb, tc, "<=>" , mrb_time_cmp , MRB_ARGS_REQ(1)); /* 15.2.19.7.1 */
675  mrb_define_method(mrb, tc, "+" , mrb_time_plus , MRB_ARGS_REQ(1)); /* 15.2.19.7.2 */
676  mrb_define_method(mrb, tc, "-" , mrb_time_minus , MRB_ARGS_REQ(1)); /* 15.2.19.7.3 */
677  mrb_define_method(mrb, tc, "to_s" , mrb_time_asctime, MRB_ARGS_NONE());
678  mrb_define_method(mrb, tc, "inspect", mrb_time_asctime, MRB_ARGS_NONE());
679  mrb_define_method(mrb, tc, "asctime", mrb_time_asctime, MRB_ARGS_NONE()); /* 15.2.19.7.4 */
680  mrb_define_method(mrb, tc, "ctime" , mrb_time_asctime, MRB_ARGS_NONE()); /* 15.2.19.7.5 */
681  mrb_define_method(mrb, tc, "day" , mrb_time_day , MRB_ARGS_NONE()); /* 15.2.19.7.6 */
682  mrb_define_method(mrb, tc, "dst?" , mrb_time_dst_p , MRB_ARGS_NONE()); /* 15.2.19.7.7 */
683  mrb_define_method(mrb, tc, "getgm" , mrb_time_getutc , MRB_ARGS_NONE()); /* 15.2.19.7.8 */
684  mrb_define_method(mrb, tc, "getlocal",mrb_time_getlocal,MRB_ARGS_NONE()); /* 15.2.19.7.9 */
685  mrb_define_method(mrb, tc, "getutc" , mrb_time_getutc , MRB_ARGS_NONE()); /* 15.2.19.7.10 */
686  mrb_define_method(mrb, tc, "gmt?" , mrb_time_utc_p , MRB_ARGS_NONE()); /* 15.2.19.7.11 */
687  mrb_define_method(mrb, tc, "gmtime" , mrb_time_utc , MRB_ARGS_NONE()); /* 15.2.19.7.13 */
688  mrb_define_method(mrb, tc, "hour" , mrb_time_hour, MRB_ARGS_NONE()); /* 15.2.19.7.15 */
689  mrb_define_method(mrb, tc, "localtime", mrb_time_localtime, MRB_ARGS_NONE()); /* 15.2.19.7.18 */
690  mrb_define_method(mrb, tc, "mday" , mrb_time_mday, MRB_ARGS_NONE()); /* 15.2.19.7.19 */
691  mrb_define_method(mrb, tc, "min" , mrb_time_min, MRB_ARGS_NONE()); /* 15.2.19.7.20 */
692 
693  mrb_define_method(mrb, tc, "mon" , mrb_time_mon, MRB_ARGS_NONE()); /* 15.2.19.7.21 */
694  mrb_define_method(mrb, tc, "month", mrb_time_mon, MRB_ARGS_NONE()); /* 15.2.19.7.22 */
695 
696  mrb_define_method(mrb, tc, "sec" , mrb_time_sec, MRB_ARGS_NONE()); /* 15.2.19.7.23 */
697  mrb_define_method(mrb, tc, "to_i", mrb_time_to_i, MRB_ARGS_NONE()); /* 15.2.19.7.25 */
698  mrb_define_method(mrb, tc, "to_f", mrb_time_to_f, MRB_ARGS_NONE()); /* 15.2.19.7.24 */
699  mrb_define_method(mrb, tc, "usec", mrb_time_usec, MRB_ARGS_NONE()); /* 15.2.19.7.26 */
700  mrb_define_method(mrb, tc, "utc" , mrb_time_utc, MRB_ARGS_NONE()); /* 15.2.19.7.27 */
701  mrb_define_method(mrb, tc, "utc?", mrb_time_utc_p,MRB_ARGS_NONE()); /* 15.2.19.7.28 */
702  mrb_define_method(mrb, tc, "wday", mrb_time_wday, MRB_ARGS_NONE()); /* 15.2.19.7.30 */
703  mrb_define_method(mrb, tc, "yday", mrb_time_yday, MRB_ARGS_NONE()); /* 15.2.19.7.31 */
704  mrb_define_method(mrb, tc, "year", mrb_time_year, MRB_ARGS_NONE()); /* 15.2.19.7.32 */
705  mrb_define_method(mrb, tc, "zone", mrb_time_zone, MRB_ARGS_NONE()); /* 15.2.19.7.33 */
706 
707  mrb_define_method(mrb, tc, "initialize", mrb_time_initialize, MRB_ARGS_REQ(1)); /* 15.2.19.7.16 */
708  mrb_define_method(mrb, tc, "initialize_copy", mrb_time_initialize_copy, MRB_ARGS_REQ(1)); /* 15.2.19.7.17 */
709 
710  /*
711  methods not available:
712  gmt_offset(15.2.19.7.12)
713  gmtoff(15.2.19.7.14)
714  utc_offset(15.2.19.7.29)
715  */
716 }
717 
718 void
720 {
721 }