Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

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