source: EcnlProtoTool/trunk/mruby-1.3.0/mrbgems/mruby-time/src/time.c@ 331

Last change on this file since 331 was 331, checked in by coas-nagasima, 6 years ago

prototoolに関連するプロジェクトをnewlibからmuslを使うよう変更・更新
ntshellをnewlibの下位の実装から、muslのsyscallの実装に変更・更新
以下のOSSをアップデート
・mruby-1.3.0
・musl-1.1.18
・onigmo-6.1.3
・tcc-0.9.27
以下のOSSを追加
・openssl-1.1.0e
・curl-7.57.0
・zlib-1.2.11
以下のmrbgemsを追加
・iij/mruby-digest
・iij/mruby-env
・iij/mruby-errno
・iij/mruby-iijson
・iij/mruby-ipaddr
・iij/mruby-mock
・iij/mruby-require
・iij/mruby-tls-openssl

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-csrc;charset=UTF-8
File size: 24.2 KB
Line 
1/*
2** time.c - Time class
3**
4** See Copyright Notice in mruby.h
5*/
6
7#include <math.h>
8#include <time.h>
9#include <mruby.h>
10#include <mruby/class.h>
11#include <mruby/data.h>
12
13#ifndef DISABLE_STDIO
14#include <stdio.h>
15#else
16#include <string.h>
17#endif
18
19#define NDIV(x,y) (-(-((x)+1)/(y))-1)
20
21#if _MSC_VER < 1800
22double round(double x) {
23 if (x >= 0.0) {
24 return (double)((int)(x + 0.5));
25 }
26 else {
27 return (double)((int)(x - 0.5));
28 }
29}
30#endif
31
32#if !defined(__MINGW64__) && defined(_WIN32)
33# define llround(x) round(x)
34#endif
35
36#if defined(__MINGW64__) || defined(__MINGW32__)
37# include <sys/time.h>
38#endif
39
40/** Time class configuration */
41
42/* gettimeofday(2) */
43/* C99 does not have gettimeofday that is required to retrieve microseconds */
44/* uncomment following macro on platforms without gettimeofday(2) */
45/* #define NO_GETTIMEOFDAY */
46
47/* gmtime(3) */
48/* C99 does not have reentrant gmtime_r() so it might cause troubles under */
49/* multi-threading environment. undef following macro on platforms that */
50/* does not have gmtime_r() and localtime_r(). */
51/* #define NO_GMTIME_R */
52
53#ifdef _WIN32
54#if _MSC_VER
55/* Win32 platform do not provide gmtime_r/localtime_r; emulate them using gmtime_s/localtime_s */
56#define gmtime_r(tp, tm) ((gmtime_s((tm), (tp)) == 0) ? (tm) : NULL)
57#define localtime_r(tp, tm) ((localtime_s((tm), (tp)) == 0) ? (tm) : NULL)
58#else
59#define NO_GMTIME_R
60#endif
61#endif
62
63/* asctime(3) */
64/* mruby usually use its own implementation of struct tm to string conversion */
65/* except when DISABLE_STDIO is set. In that case, it uses asctime() or asctime_r(). */
66/* By default mruby tries to use asctime_r() which is reentrant. */
67/* Undef following macro on platforms that does not have asctime_r(). */
68/* #define NO_ASCTIME_R */
69
70/* timegm(3) */
71/* mktime() creates tm structure for localtime; timegm() is for UTC time */
72/* define following macro to use probably faster timegm() on the platform */
73/* #define USE_SYSTEM_TIMEGM */
74
75/** end of Time class configuration */
76
77#ifndef NO_GETTIMEOFDAY
78# ifdef _WIN32
79# define WIN32_LEAN_AND_MEAN /* don't include winsock.h */
80# include <windows.h>
81# define gettimeofday my_gettimeofday
82
83# ifdef _MSC_VER
84# define UI64(x) x##ui64
85# else
86# define UI64(x) x##ull
87# endif
88
89typedef long suseconds_t;
90
91# if (!defined __MINGW64__) && (!defined __MINGW32__)
92struct timeval {
93 time_t tv_sec;
94 suseconds_t tv_usec;
95};
96# endif
97
98static int
99gettimeofday(struct timeval *tv, void *tz)
100{
101 if (tz) {
102 mrb_assert(0); /* timezone is not supported */
103 }
104 if (tv) {
105 union {
106 FILETIME ft;
107 unsigned __int64 u64;
108 } t;
109 GetSystemTimeAsFileTime(&t.ft); /* 100 ns intervals since Windows epoch */
110 t.u64 -= UI64(116444736000000000); /* Unix epoch bias */
111 t.u64 /= 10; /* to microseconds */
112 tv->tv_sec = (time_t)(t.u64 / (1000 * 1000));
113 tv->tv_usec = t.u64 % (1000 * 1000);
114 }
115 return 0;
116}
117# else
118# include <sys/time.h>
119# endif
120#endif
121#ifdef NO_GMTIME_R
122#define gmtime_r(t,r) gmtime(t)
123#define localtime_r(t,r) localtime(t)
124#endif
125
126#ifndef USE_SYSTEM_TIMEGM
127#define timegm my_timgm
128
129static unsigned int
130is_leapyear(unsigned int y)
131{
132 return (y % 4) == 0 && ((y % 100) != 0 || (y % 400) == 0);
133}
134
135static time_t
136timegm(struct tm *tm)
137{
138 static const unsigned int ndays[2][12] = {
139 {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
140 {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
141 };
142 time_t r = 0;
143 int i;
144 unsigned int *nday = (unsigned int*) ndays[is_leapyear(tm->tm_year+1900)];
145
146 for (i = 70; i < tm->tm_year; ++i)
147 r += is_leapyear(i+1900) ? 366*24*60*60 : 365*24*60*60;
148 for (i = 0; i < tm->tm_mon; ++i)
149 r += nday[i] * 24 * 60 * 60;
150 r += (tm->tm_mday - 1) * 24 * 60 * 60;
151 r += tm->tm_hour * 60 * 60;
152 r += tm->tm_min * 60;
153 r += tm->tm_sec;
154 return r;
155}
156#endif
157
158/* Since we are limited to using ISO C99, this implementation is based
159* on time_t. That means the resolution of time is only precise to the
160* second level. Also, there are only 2 timezones, namely UTC and LOCAL.
161*/
162
163enum mrb_timezone {
164 MRB_TIMEZONE_NONE = 0,
165 MRB_TIMEZONE_UTC = 1,
166 MRB_TIMEZONE_LOCAL = 2,
167 MRB_TIMEZONE_LAST = 3
168};
169
170typedef struct mrb_timezone_name {
171 const char name[8];
172 size_t len;
173} mrb_timezone_name;
174
175static const mrb_timezone_name timezone_names[] = {
176 { "none", sizeof("none") - 1 },
177 { "UTC", sizeof("UTC") - 1 },
178 { "LOCAL", sizeof("LOCAL") - 1 },
179};
180
181#ifndef DISABLE_STDIO
182static const char mon_names[12][4] = {
183 "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
184};
185
186static const char wday_names[7][4] = {
187 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat",
188};
189#endif
190
191struct mrb_time {
192 time_t sec;
193 time_t usec;
194 enum mrb_timezone timezone;
195 struct tm datetime;
196};
197
198static const struct mrb_data_type mrb_time_type = { "Time", mrb_free };
199
200/** Updates the datetime of a mrb_time based on it's timezone and
201seconds setting. Returns self on success, NULL of failure. */
202static struct mrb_time*
203time_update_datetime(mrb_state *mrb, struct mrb_time *self)
204{
205 struct tm *aid;
206
207 if (self->timezone == MRB_TIMEZONE_UTC) {
208 aid = gmtime_r(&self->sec, &self->datetime);
209 }
210 else {
211 aid = localtime_r(&self->sec, &self->datetime);
212 }
213 if (!aid) {
214 mrb_raisef(mrb, E_ARGUMENT_ERROR, "%S out of Time range", mrb_float_value(mrb, (mrb_float)self->sec));
215 /* not reached */
216 return NULL;
217 }
218#ifdef NO_GMTIME_R
219 self->datetime = *aid; /* copy data */
220#endif
221
222 return self;
223}
224
225static mrb_value
226mrb_time_wrap(mrb_state *mrb, struct RClass *tc, struct mrb_time *tm)
227{
228 return mrb_obj_value(Data_Wrap_Struct(mrb, tc, &mrb_time_type, tm));
229}
230
231void mrb_check_num_exact(mrb_state *mrb, mrb_float num);
232
233/* Allocates a mrb_time object and initializes it. */
234static struct mrb_time*
235time_alloc(mrb_state *mrb, double sec, double usec, enum mrb_timezone timezone)
236{
237 struct mrb_time *tm;
238 time_t tsec = 0;
239
240 mrb_check_num_exact(mrb, (mrb_float)sec);
241 mrb_check_num_exact(mrb, (mrb_float)usec);
242
243 if (sizeof(time_t) == 4 && (sec > (double)INT32_MAX || (double)INT32_MIN > sec)) {
244 goto out_of_range;
245 }
246 if (sizeof(time_t) == 8 && (sec > (double)INT64_MAX || (double)INT64_MIN > sec)) {
247 goto out_of_range;
248 }
249 tsec = (time_t)sec;
250 if ((sec > 0 && tsec < 0) || (sec < 0 && (double)tsec > sec)) {
251 out_of_range:
252 mrb_raisef(mrb, E_ARGUMENT_ERROR, "%S out of Time range", mrb_float_value(mrb, sec));
253 }
254 tm = (struct mrb_time *)mrb_malloc(mrb, sizeof(struct mrb_time));
255 tm->sec = tsec;
256 tm->usec = (time_t)llround((sec - tm->sec) * 1.0e6 + usec);
257 if (tm->usec < 0) {
258 long sec2 = (long)NDIV(usec,1000000); /* negative div */
259 tm->usec -= sec2 * 1000000;
260 tm->sec += sec2;
261 }
262 else if (tm->usec >= 1000000) {
263 long sec2 = (long)(usec / 1000000);
264 tm->usec -= sec2 * 1000000;
265 tm->sec += sec2;
266 }
267 tm->timezone = timezone;
268 time_update_datetime(mrb, tm);
269
270 return tm;
271}
272
273static mrb_value
274mrb_time_make(mrb_state *mrb, struct RClass *c, double sec, double usec, enum mrb_timezone timezone)
275{
276 return mrb_time_wrap(mrb, c, time_alloc(mrb, sec, usec, timezone));
277}
278
279static struct mrb_time*
280current_mrb_time(mrb_state *mrb)
281{
282 struct mrb_time *tm;
283
284 tm = (struct mrb_time *)mrb_malloc(mrb, sizeof(*tm));
285#if defined(TIME_UTC)
286 {
287 struct timespec ts;
288 if (timespec_get(&ts, TIME_UTC) == 0) {
289 mrb_free(mrb, tm);
290 mrb_raise(mrb, E_RUNTIME_ERROR, "timespec_get() failed for unknown reasons");
291 }
292 tm->sec = ts.tv_sec;
293 tm->usec = ts.tv_nsec / 1000;
294 }
295#elif defined(NO_GETTIMEOFDAY)
296 {
297 static time_t last_sec = 0, last_usec = 0;
298
299 tm->sec = time(NULL);
300 if (tm->sec != last_sec) {
301 last_sec = tm->sec;
302 last_usec = 0;
303 }
304 else {
305 /* add 1 usec to differentiate two times */
306 last_usec += 1;
307 }
308 tm->usec = last_usec;
309 }
310#else
311 {
312 struct timeval tv;
313
314 gettimeofday(&tv, NULL);
315 tm->sec = tv.tv_sec;
316 tm->usec = tv.tv_usec;
317 }
318#endif
319 tm->timezone = MRB_TIMEZONE_LOCAL;
320 time_update_datetime(mrb, tm);
321
322 return tm;
323}
324
325/* Allocates a new Time object with given millis value. */
326static mrb_value
327mrb_time_now(mrb_state *mrb, mrb_value self)
328{
329 return mrb_time_wrap(mrb, mrb_class_ptr(self), current_mrb_time(mrb));
330}
331
332/* 15.2.19.6.1 */
333/* Creates an instance of time at the given time in seconds, etc. */
334static mrb_value
335mrb_time_at(mrb_state *mrb, mrb_value self)
336{
337 mrb_float f, f2 = 0;
338
339 mrb_get_args(mrb, "f|f", &f, &f2);
340 return mrb_time_make(mrb, mrb_class_ptr(self), f, f2, MRB_TIMEZONE_LOCAL);
341}
342
343static struct mrb_time*
344time_mktime(mrb_state *mrb, mrb_int ayear, mrb_int amonth, mrb_int aday,
345 mrb_int ahour, mrb_int amin, mrb_int asec, mrb_int ausec,
346 enum mrb_timezone timezone)
347{
348 time_t nowsecs;
349 struct tm nowtime = { 0 };
350
351 nowtime.tm_year = (int)ayear - 1900;
352 nowtime.tm_mon = (int)amonth - 1;
353 nowtime.tm_mday = (int)aday;
354 nowtime.tm_hour = (int)ahour;
355 nowtime.tm_min = (int)amin;
356 nowtime.tm_sec = (int)asec;
357 nowtime.tm_isdst = -1;
358
359 if (nowtime.tm_mon < 0 || nowtime.tm_mon > 11
360 || nowtime.tm_mday < 1 || nowtime.tm_mday > 31
361 || nowtime.tm_hour < 0 || nowtime.tm_hour > 24
362 || (nowtime.tm_hour == 24 && (nowtime.tm_min > 0 || nowtime.tm_sec > 0))
363 || nowtime.tm_min < 0 || nowtime.tm_min > 59
364 || nowtime.tm_sec < 0 || nowtime.tm_sec > 60)
365 mrb_raise(mrb, E_RUNTIME_ERROR, "argument out of range");
366
367 if (timezone == MRB_TIMEZONE_UTC) {
368 nowsecs = timegm(&nowtime);
369 }
370 else {
371 nowsecs = mktime(&nowtime);
372 }
373 if (nowsecs == (time_t)-1) {
374 mrb_raise(mrb, E_ARGUMENT_ERROR, "Not a valid time.");
375 }
376
377 return time_alloc(mrb, (double)nowsecs, ausec, timezone);
378}
379
380/* 15.2.19.6.2 */
381/* Creates an instance of time at the given time in UTC. */
382static mrb_value
383mrb_time_gm(mrb_state *mrb, mrb_value self)
384{
385 mrb_int ayear = 0, amonth = 1, aday = 1, ahour = 0, amin = 0, asec = 0, ausec = 0;
386
387 mrb_get_args(mrb, "i|iiiiii",
388 &ayear, &amonth, &aday, &ahour, &amin, &asec, &ausec);
389 return mrb_time_wrap(mrb, mrb_class_ptr(self),
390 time_mktime(mrb, ayear, amonth, aday, ahour, amin, asec, ausec, MRB_TIMEZONE_UTC));
391}
392
393
394/* 15.2.19.6.3 */
395/* Creates an instance of time at the given time in local time zone. */
396static mrb_value
397mrb_time_local(mrb_state *mrb, mrb_value self)
398{
399 mrb_int ayear = 0, amonth = 1, aday = 1, ahour = 0, amin = 0, asec = 0, ausec = 0;
400
401 mrb_get_args(mrb, "i|iiiiii",
402 &ayear, &amonth, &aday, &ahour, &amin, &asec, &ausec);
403 return mrb_time_wrap(mrb, mrb_class_ptr(self),
404 time_mktime(mrb, ayear, amonth, aday, ahour, amin, asec, ausec, MRB_TIMEZONE_LOCAL));
405}
406
407static struct mrb_time*
408time_get_ptr(mrb_state *mrb, mrb_value time)
409{
410 struct mrb_time *tm;
411
412 tm = DATA_GET_PTR(mrb, time, &mrb_time_type, struct mrb_time);
413 if (!tm) {
414 mrb_raise(mrb, E_ARGUMENT_ERROR, "uninitialized time");
415 }
416 return tm;
417}
418
419static mrb_value
420mrb_time_eq(mrb_state *mrb, mrb_value self)
421{
422 mrb_value other;
423 struct mrb_time *tm1, *tm2;
424 mrb_bool eq_p;
425
426 mrb_get_args(mrb, "o", &other);
427 tm1 = DATA_GET_PTR(mrb, self, &mrb_time_type, struct mrb_time);
428 tm2 = DATA_CHECK_GET_PTR(mrb, other, &mrb_time_type, struct mrb_time);
429 eq_p = tm1 && tm2 && tm1->sec == tm2->sec && tm1->usec == tm2->usec;
430
431 return mrb_bool_value(eq_p);
432}
433
434static mrb_value
435mrb_time_cmp(mrb_state *mrb, mrb_value self)
436{
437 mrb_value other;
438 struct mrb_time *tm1, *tm2;
439
440 mrb_get_args(mrb, "o", &other);
441 tm1 = DATA_GET_PTR(mrb, self, &mrb_time_type, struct mrb_time);
442 tm2 = DATA_CHECK_GET_PTR(mrb, other, &mrb_time_type, struct mrb_time);
443 if (!tm1 || !tm2) return mrb_nil_value();
444 if (tm1->sec > tm2->sec) {
445 return mrb_fixnum_value(1);
446 }
447 else if (tm1->sec < tm2->sec) {
448 return mrb_fixnum_value(-1);
449 }
450 /* tm1->sec == tm2->sec */
451 if (tm1->usec > tm2->usec) {
452 return mrb_fixnum_value(1);
453 }
454 else if (tm1->usec < tm2->usec) {
455 return mrb_fixnum_value(-1);
456 }
457 return mrb_fixnum_value(0);
458}
459
460static mrb_value
461mrb_time_plus(mrb_state *mrb, mrb_value self)
462{
463 mrb_float f;
464 struct mrb_time *tm;
465
466 mrb_get_args(mrb, "f", &f);
467 tm = time_get_ptr(mrb, self);
468 return mrb_time_make(mrb, mrb_obj_class(mrb, self), (double)tm->sec+f, (double)tm->usec, tm->timezone);
469}
470
471static mrb_value
472mrb_time_minus(mrb_state *mrb, mrb_value self)
473{
474 mrb_float f;
475 mrb_value other;
476 struct mrb_time *tm, *tm2;
477
478 mrb_get_args(mrb, "o", &other);
479 tm = time_get_ptr(mrb, self);
480 tm2 = DATA_CHECK_GET_PTR(mrb, other, &mrb_time_type, struct mrb_time);
481 if (tm2) {
482 f = (mrb_float)(tm->sec - tm2->sec)
483 + (mrb_float)(tm->usec - tm2->usec) / 1.0e6;
484 return mrb_float_value(mrb, f);
485 }
486 else {
487 mrb_get_args(mrb, "f", &f);
488 return mrb_time_make(mrb, mrb_obj_class(mrb, self), (double)tm->sec-f, (double)tm->usec, tm->timezone);
489 }
490}
491
492/* 15.2.19.7.30 */
493/* Returns week day number of time. */
494static mrb_value
495mrb_time_wday(mrb_state *mrb, mrb_value self)
496{
497 struct mrb_time *tm;
498
499 tm = time_get_ptr(mrb, self);
500 return mrb_fixnum_value(tm->datetime.tm_wday);
501}
502
503/* 15.2.19.7.31 */
504/* Returns year day number of time. */
505static mrb_value
506mrb_time_yday(mrb_state *mrb, mrb_value self)
507{
508 struct mrb_time *tm;
509
510 tm = time_get_ptr(mrb, self);
511 return mrb_fixnum_value(tm->datetime.tm_yday + 1);
512}
513
514/* 15.2.19.7.32 */
515/* Returns year of time. */
516static mrb_value
517mrb_time_year(mrb_state *mrb, mrb_value self)
518{
519 struct mrb_time *tm;
520
521 tm = time_get_ptr(mrb, self);
522 return mrb_fixnum_value(tm->datetime.tm_year + 1900);
523}
524
525/* 15.2.19.7.33 */
526/* Returns name of time's timezone. */
527static mrb_value
528mrb_time_zone(mrb_state *mrb, mrb_value self)
529{
530 struct mrb_time *tm;
531
532 tm = time_get_ptr(mrb, self);
533 if (tm->timezone <= MRB_TIMEZONE_NONE) return mrb_nil_value();
534 if (tm->timezone >= MRB_TIMEZONE_LAST) return mrb_nil_value();
535 return mrb_str_new_static(mrb,
536 timezone_names[tm->timezone].name,
537 timezone_names[tm->timezone].len);
538}
539
540/* 15.2.19.7.4 */
541/* Returns a string that describes the time. */
542static mrb_value
543mrb_time_asctime(mrb_state *mrb, mrb_value self)
544{
545 struct mrb_time *tm = time_get_ptr(mrb, self);
546 struct tm *d = &tm->datetime;
547 int len;
548
549#if defined(DISABLE_STDIO)
550 char *s;
551# ifdef NO_ASCTIME_R
552 s = asctime(d);
553# else
554 char buf[32];
555 s = asctime_r(d, buf);
556# endif
557 len = strlen(s)-1; /* truncate the last newline */
558#else
559 char buf[256];
560
561 len = snprintf(buf, sizeof(buf), "%s %s %02d %02d:%02d:%02d %s%d",
562 wday_names[d->tm_wday], mon_names[d->tm_mon], d->tm_mday,
563 d->tm_hour, d->tm_min, d->tm_sec,
564 tm->timezone == MRB_TIMEZONE_UTC ? "UTC " : "",
565 d->tm_year + 1900);
566#endif
567 return mrb_str_new(mrb, buf, len);
568}
569
570/* 15.2.19.7.6 */
571/* Returns the day in the month of the time. */
572static mrb_value
573mrb_time_day(mrb_state *mrb, mrb_value self)
574{
575 struct mrb_time *tm;
576
577 tm = time_get_ptr(mrb, self);
578 return mrb_fixnum_value(tm->datetime.tm_mday);
579}
580
581
582/* 15.2.19.7.7 */
583/* Returns true if daylight saving was applied for this time. */
584static mrb_value
585mrb_time_dst_p(mrb_state *mrb, mrb_value self)
586{
587 struct mrb_time *tm;
588
589 tm = time_get_ptr(mrb, self);
590 return mrb_bool_value(tm->datetime.tm_isdst);
591}
592
593/* 15.2.19.7.8 */
594/* 15.2.19.7.10 */
595/* Returns the Time object of the UTC(GMT) timezone. */
596static mrb_value
597mrb_time_getutc(mrb_state *mrb, mrb_value self)
598{
599 struct mrb_time *tm, *tm2;
600
601 tm = time_get_ptr(mrb, self);
602 tm2 = (struct mrb_time *)mrb_malloc(mrb, sizeof(*tm));
603 *tm2 = *tm;
604 tm2->timezone = MRB_TIMEZONE_UTC;
605 time_update_datetime(mrb, tm2);
606 return mrb_time_wrap(mrb, mrb_obj_class(mrb, self), tm2);
607}
608
609/* 15.2.19.7.9 */
610/* Returns the Time object of the LOCAL timezone. */
611static mrb_value
612mrb_time_getlocal(mrb_state *mrb, mrb_value self)
613{
614 struct mrb_time *tm, *tm2;
615
616 tm = time_get_ptr(mrb, self);
617 tm2 = (struct mrb_time *)mrb_malloc(mrb, sizeof(*tm));
618 *tm2 = *tm;
619 tm2->timezone = MRB_TIMEZONE_LOCAL;
620 time_update_datetime(mrb, tm2);
621 return mrb_time_wrap(mrb, mrb_obj_class(mrb, self), tm2);
622}
623
624/* 15.2.19.7.15 */
625/* Returns hour of time. */
626static mrb_value
627mrb_time_hour(mrb_state *mrb, mrb_value self)
628{
629 struct mrb_time *tm;
630
631 tm = time_get_ptr(mrb, self);
632 return mrb_fixnum_value(tm->datetime.tm_hour);
633}
634
635/* 15.2.19.7.16 */
636/* Initializes a time by setting the amount of milliseconds since the epoch.*/
637static mrb_value
638mrb_time_initialize(mrb_state *mrb, mrb_value self)
639{
640 mrb_int ayear = 0, amonth = 1, aday = 1, ahour = 0,
641 amin = 0, asec = 0, ausec = 0;
642 int n;
643 struct mrb_time *tm;
644
645 n = mrb_get_args(mrb, "|iiiiiii",
646 &ayear, &amonth, &aday, &ahour, &amin, &asec, &ausec);
647 tm = (struct mrb_time*)DATA_PTR(self);
648 if (tm) {
649 mrb_free(mrb, tm);
650 }
651 mrb_data_init(self, NULL, &mrb_time_type);
652
653 if (n == 0) {
654 tm = current_mrb_time(mrb);
655 }
656 else {
657 tm = time_mktime(mrb, ayear, amonth, aday, ahour, amin, asec, ausec, MRB_TIMEZONE_LOCAL);
658 }
659 mrb_data_init(self, tm, &mrb_time_type);
660 return self;
661}
662
663/* 15.2.19.7.17(x) */
664/* Initializes a copy of this time object. */
665static mrb_value
666mrb_time_initialize_copy(mrb_state *mrb, mrb_value copy)
667{
668 mrb_value src;
669 struct mrb_time *t1, *t2;
670
671 mrb_get_args(mrb, "o", &src);
672 if (mrb_obj_equal(mrb, copy, src)) return copy;
673 if (!mrb_obj_is_instance_of(mrb, src, mrb_obj_class(mrb, copy))) {
674 mrb_raise(mrb, E_TYPE_ERROR, "wrong argument class");
675 }
676 t1 = (struct mrb_time *)DATA_PTR(copy);
677 t2 = (struct mrb_time *)DATA_PTR(src);
678 if (!t2) {
679 mrb_raise(mrb, E_ARGUMENT_ERROR, "uninitialized time");
680 }
681 if (!t1) {
682 t1 = (struct mrb_time *)mrb_malloc(mrb, sizeof(struct mrb_time));
683 mrb_data_init(copy, t1, &mrb_time_type);
684 }
685 *t1 = *t2;
686 return copy;
687}
688
689/* 15.2.19.7.18 */
690/* Sets the timezone attribute of the Time object to LOCAL. */
691static mrb_value
692mrb_time_localtime(mrb_state *mrb, mrb_value self)
693{
694 struct mrb_time *tm;
695
696 tm = time_get_ptr(mrb, self);
697 tm->timezone = MRB_TIMEZONE_LOCAL;
698 time_update_datetime(mrb, tm);
699 return self;
700}
701
702/* 15.2.19.7.19 */
703/* Returns day of month of time. */
704static mrb_value
705mrb_time_mday(mrb_state *mrb, mrb_value self)
706{
707 struct mrb_time *tm;
708
709 tm = time_get_ptr(mrb, self);
710 return mrb_fixnum_value(tm->datetime.tm_mday);
711}
712
713/* 15.2.19.7.20 */
714/* Returns minutes of time. */
715static mrb_value
716mrb_time_min(mrb_state *mrb, mrb_value self)
717{
718 struct mrb_time *tm;
719
720 tm = time_get_ptr(mrb, self);
721 return mrb_fixnum_value(tm->datetime.tm_min);
722}
723
724/* 15.2.19.7.21 and 15.2.19.7.22 */
725/* Returns month of time. */
726static mrb_value
727mrb_time_mon(mrb_state *mrb, mrb_value self)
728{
729 struct mrb_time *tm;
730
731 tm = time_get_ptr(mrb, self);
732 return mrb_fixnum_value(tm->datetime.tm_mon + 1);
733}
734
735/* 15.2.19.7.23 */
736/* Returns seconds in minute of time. */
737static mrb_value
738mrb_time_sec(mrb_state *mrb, mrb_value self)
739{
740 struct mrb_time *tm;
741
742 tm = time_get_ptr(mrb, self);
743 return mrb_fixnum_value(tm->datetime.tm_sec);
744}
745
746
747/* 15.2.19.7.24 */
748/* Returns a Float with the time since the epoch in seconds. */
749static mrb_value
750mrb_time_to_f(mrb_state *mrb, mrb_value self)
751{
752 struct mrb_time *tm;
753
754 tm = time_get_ptr(mrb, self);
755 return mrb_float_value(mrb, (mrb_float)tm->sec + (mrb_float)tm->usec/1.0e6);
756}
757
758/* 15.2.19.7.25 */
759/* Returns a Fixnum with the time since the epoch in seconds. */
760static mrb_value
761mrb_time_to_i(mrb_state *mrb, mrb_value self)
762{
763 struct mrb_time *tm;
764
765 tm = time_get_ptr(mrb, self);
766 if (tm->sec > MRB_INT_MAX || tm->sec < MRB_INT_MIN) {
767 return mrb_float_value(mrb, (mrb_float)tm->sec);
768 }
769 return mrb_fixnum_value((mrb_int)tm->sec);
770}
771
772/* 15.2.19.7.26 */
773/* Returns a Float with the time since the epoch in microseconds. */
774static mrb_value
775mrb_time_usec(mrb_state *mrb, mrb_value self)
776{
777 struct mrb_time *tm;
778
779 tm = time_get_ptr(mrb, self);
780 if (tm->usec > MRB_INT_MAX || tm->usec < MRB_INT_MIN) {
781 return mrb_float_value(mrb, (mrb_float)tm->usec);
782 }
783 return mrb_fixnum_value((mrb_int)tm->usec);
784}
785
786/* 15.2.19.7.27 */
787/* Sets the timezone attribute of the Time object to UTC. */
788static mrb_value
789mrb_time_utc(mrb_state *mrb, mrb_value self)
790{
791 struct mrb_time *tm;
792
793 tm = time_get_ptr(mrb, self);
794 tm->timezone = MRB_TIMEZONE_UTC;
795 time_update_datetime(mrb, tm);
796 return self;
797}
798
799/* 15.2.19.7.28 */
800/* Returns true if this time is in the UTC timezone false if not. */
801static mrb_value
802mrb_time_utc_p(mrb_state *mrb, mrb_value self)
803{
804 struct mrb_time *tm;
805
806 tm = time_get_ptr(mrb, self);
807 return mrb_bool_value(tm->timezone == MRB_TIMEZONE_UTC);
808}
809
810
811void
812mrb_mruby_time_gem_init(mrb_state* mrb)
813{
814 struct RClass *tc;
815 /* ISO 15.2.19.2 */
816 tc = mrb_define_class(mrb, "Time", mrb->object_class);
817 MRB_SET_INSTANCE_TT(tc, MRB_TT_DATA);
818 mrb_include_module(mrb, tc, mrb_module_get(mrb, "Comparable"));
819 mrb_define_class_method(mrb, tc, "at", mrb_time_at, MRB_ARGS_ARG(1, 1)); /* 15.2.19.6.1 */
820 mrb_define_class_method(mrb, tc, "gm", mrb_time_gm, MRB_ARGS_ARG(1,6)); /* 15.2.19.6.2 */
821 mrb_define_class_method(mrb, tc, "local", mrb_time_local, MRB_ARGS_ARG(1,6)); /* 15.2.19.6.3 */
822 mrb_define_class_method(mrb, tc, "mktime", mrb_time_local, MRB_ARGS_ARG(1,6));/* 15.2.19.6.4 */
823 mrb_define_class_method(mrb, tc, "now", mrb_time_now, MRB_ARGS_NONE()); /* 15.2.19.6.5 */
824 mrb_define_class_method(mrb, tc, "utc", mrb_time_gm, MRB_ARGS_ARG(1,6)); /* 15.2.19.6.6 */
825
826 mrb_define_method(mrb, tc, "==" , mrb_time_eq , MRB_ARGS_REQ(1));
827 mrb_define_method(mrb, tc, "<=>" , mrb_time_cmp , MRB_ARGS_REQ(1)); /* 15.2.19.7.1 */
828 mrb_define_method(mrb, tc, "+" , mrb_time_plus , MRB_ARGS_REQ(1)); /* 15.2.19.7.2 */
829 mrb_define_method(mrb, tc, "-" , mrb_time_minus , MRB_ARGS_REQ(1)); /* 15.2.19.7.3 */
830 mrb_define_method(mrb, tc, "to_s" , mrb_time_asctime, MRB_ARGS_NONE());
831 mrb_define_method(mrb, tc, "inspect", mrb_time_asctime, MRB_ARGS_NONE());
832 mrb_define_method(mrb, tc, "asctime", mrb_time_asctime, MRB_ARGS_NONE()); /* 15.2.19.7.4 */
833 mrb_define_method(mrb, tc, "ctime" , mrb_time_asctime, MRB_ARGS_NONE()); /* 15.2.19.7.5 */
834 mrb_define_method(mrb, tc, "day" , mrb_time_day , MRB_ARGS_NONE()); /* 15.2.19.7.6 */
835 mrb_define_method(mrb, tc, "dst?" , mrb_time_dst_p , MRB_ARGS_NONE()); /* 15.2.19.7.7 */
836 mrb_define_method(mrb, tc, "getgm" , mrb_time_getutc , MRB_ARGS_NONE()); /* 15.2.19.7.8 */
837 mrb_define_method(mrb, tc, "getlocal",mrb_time_getlocal,MRB_ARGS_NONE()); /* 15.2.19.7.9 */
838 mrb_define_method(mrb, tc, "getutc" , mrb_time_getutc , MRB_ARGS_NONE()); /* 15.2.19.7.10 */
839 mrb_define_method(mrb, tc, "gmt?" , mrb_time_utc_p , MRB_ARGS_NONE()); /* 15.2.19.7.11 */
840 mrb_define_method(mrb, tc, "gmtime" , mrb_time_utc , MRB_ARGS_NONE()); /* 15.2.19.7.13 */
841 mrb_define_method(mrb, tc, "hour" , mrb_time_hour, MRB_ARGS_NONE()); /* 15.2.19.7.15 */
842 mrb_define_method(mrb, tc, "localtime", mrb_time_localtime, MRB_ARGS_NONE()); /* 15.2.19.7.18 */
843 mrb_define_method(mrb, tc, "mday" , mrb_time_mday, MRB_ARGS_NONE()); /* 15.2.19.7.19 */
844 mrb_define_method(mrb, tc, "min" , mrb_time_min, MRB_ARGS_NONE()); /* 15.2.19.7.20 */
845
846 mrb_define_method(mrb, tc, "mon" , mrb_time_mon, MRB_ARGS_NONE()); /* 15.2.19.7.21 */
847 mrb_define_method(mrb, tc, "month", mrb_time_mon, MRB_ARGS_NONE()); /* 15.2.19.7.22 */
848
849 mrb_define_method(mrb, tc, "sec" , mrb_time_sec, MRB_ARGS_NONE()); /* 15.2.19.7.23 */
850 mrb_define_method(mrb, tc, "to_i", mrb_time_to_i, MRB_ARGS_NONE()); /* 15.2.19.7.25 */
851 mrb_define_method(mrb, tc, "to_f", mrb_time_to_f, MRB_ARGS_NONE()); /* 15.2.19.7.24 */
852 mrb_define_method(mrb, tc, "usec", mrb_time_usec, MRB_ARGS_NONE()); /* 15.2.19.7.26 */
853 mrb_define_method(mrb, tc, "utc" , mrb_time_utc, MRB_ARGS_NONE()); /* 15.2.19.7.27 */
854 mrb_define_method(mrb, tc, "utc?", mrb_time_utc_p,MRB_ARGS_NONE()); /* 15.2.19.7.28 */
855 mrb_define_method(mrb, tc, "wday", mrb_time_wday, MRB_ARGS_NONE()); /* 15.2.19.7.30 */
856 mrb_define_method(mrb, tc, "yday", mrb_time_yday, MRB_ARGS_NONE()); /* 15.2.19.7.31 */
857 mrb_define_method(mrb, tc, "year", mrb_time_year, MRB_ARGS_NONE()); /* 15.2.19.7.32 */
858 mrb_define_method(mrb, tc, "zone", mrb_time_zone, MRB_ARGS_NONE()); /* 15.2.19.7.33 */
859
860 mrb_define_method(mrb, tc, "initialize", mrb_time_initialize, MRB_ARGS_REQ(1)); /* 15.2.19.7.16 */
861 mrb_define_method(mrb, tc, "initialize_copy", mrb_time_initialize_copy, MRB_ARGS_REQ(1)); /* 15.2.19.7.17 */
862
863 /*
864 methods not available:
865 gmt_offset(15.2.19.7.12)
866 gmtoff(15.2.19.7.14)
867 utc_offset(15.2.19.7.29)
868 */
869}
870
871void
872mrb_mruby_time_gem_final(mrb_state* mrb)
873{
874}
Note: See TracBrowser for help on using the repository browser.