source: EcnlProtoTool/trunk/mruby-2.1.1/mrbgems/mruby-time/src/time.c@ 439

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

mrubyを2.1.1に更新

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