[270] | 1 | /*
|
---|
| 2 | ** time.c - Time class
|
---|
| 3 | **
|
---|
| 4 | ** See Copyright Notice in mruby.h
|
---|
| 5 | */
|
---|
| 6 |
|
---|
| 7 | #include <math.h>
|
---|
| 8 | #include <time.h>
|
---|
[331] | 9 | #include <mruby.h>
|
---|
| 10 | #include <mruby/class.h>
|
---|
| 11 | #include <mruby/data.h>
|
---|
[270] | 12 |
|
---|
[331] | 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
|
---|
| 22 | double 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 |
|
---|
[270] | 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 |
|
---|
[331] | 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 |
|
---|
[270] | 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 |
|
---|
| 89 | typedef long suseconds_t;
|
---|
| 90 |
|
---|
| 91 | # if (!defined __MINGW64__) && (!defined __MINGW32__)
|
---|
| 92 | struct timeval {
|
---|
| 93 | time_t tv_sec;
|
---|
| 94 | suseconds_t tv_usec;
|
---|
| 95 | };
|
---|
| 96 | # endif
|
---|
| 97 |
|
---|
| 98 | static int
|
---|
| 99 | gettimeofday(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 |
|
---|
| 129 | static unsigned int
|
---|
| 130 | is_leapyear(unsigned int y)
|
---|
| 131 | {
|
---|
| 132 | return (y % 4) == 0 && ((y % 100) != 0 || (y % 400) == 0);
|
---|
| 133 | }
|
---|
| 134 |
|
---|
| 135 | static time_t
|
---|
| 136 | timegm(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 |
|
---|
| 163 | enum mrb_timezone {
|
---|
| 164 | MRB_TIMEZONE_NONE = 0,
|
---|
| 165 | MRB_TIMEZONE_UTC = 1,
|
---|
| 166 | MRB_TIMEZONE_LOCAL = 2,
|
---|
| 167 | MRB_TIMEZONE_LAST = 3
|
---|
| 168 | };
|
---|
| 169 |
|
---|
| 170 | typedef struct mrb_timezone_name {
|
---|
| 171 | const char name[8];
|
---|
| 172 | size_t len;
|
---|
| 173 | } mrb_timezone_name;
|
---|
| 174 |
|
---|
| 175 | static const mrb_timezone_name timezone_names[] = {
|
---|
| 176 | { "none", sizeof("none") - 1 },
|
---|
| 177 | { "UTC", sizeof("UTC") - 1 },
|
---|
| 178 | { "LOCAL", sizeof("LOCAL") - 1 },
|
---|
| 179 | };
|
---|
| 180 |
|
---|
[331] | 181 | #ifndef DISABLE_STDIO
|
---|
[270] | 182 | static const char mon_names[12][4] = {
|
---|
| 183 | "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
|
---|
| 184 | };
|
---|
| 185 |
|
---|
| 186 | static const char wday_names[7][4] = {
|
---|
| 187 | "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat",
|
---|
| 188 | };
|
---|
[331] | 189 | #endif
|
---|
[270] | 190 |
|
---|
| 191 | struct mrb_time {
|
---|
| 192 | time_t sec;
|
---|
| 193 | time_t usec;
|
---|
| 194 | enum mrb_timezone timezone;
|
---|
| 195 | struct tm datetime;
|
---|
| 196 | };
|
---|
| 197 |
|
---|
| 198 | static 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
|
---|
| 201 | seconds setting. Returns self on success, NULL of failure. */
|
---|
| 202 | static struct mrb_time*
|
---|
[331] | 203 | time_update_datetime(mrb_state *mrb, struct mrb_time *self)
|
---|
[270] | 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 | }
|
---|
[331] | 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 | }
|
---|
[270] | 218 | #ifdef NO_GMTIME_R
|
---|
| 219 | self->datetime = *aid; /* copy data */
|
---|
| 220 | #endif
|
---|
| 221 |
|
---|
| 222 | return self;
|
---|
| 223 | }
|
---|
| 224 |
|
---|
| 225 | static mrb_value
|
---|
| 226 | mrb_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 |
|
---|
[331] | 231 | void mrb_check_num_exact(mrb_state *mrb, mrb_float num);
|
---|
[270] | 232 |
|
---|
| 233 | /* Allocates a mrb_time object and initializes it. */
|
---|
| 234 | static struct mrb_time*
|
---|
| 235 | time_alloc(mrb_state *mrb, double sec, double usec, enum mrb_timezone timezone)
|
---|
| 236 | {
|
---|
| 237 | struct mrb_time *tm;
|
---|
[331] | 238 | time_t tsec = 0;
|
---|
[270] | 239 |
|
---|
[331] | 240 | mrb_check_num_exact(mrb, (mrb_float)sec);
|
---|
| 241 | mrb_check_num_exact(mrb, (mrb_float)usec);
|
---|
| 242 |
|
---|
[270] | 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 | }
|
---|
[331] | 249 | tsec = (time_t)sec;
|
---|
| 250 | if ((sec > 0 && tsec < 0) || (sec < 0 && (double)tsec > sec)) {
|
---|
[270] | 251 | out_of_range:
|
---|
| 252 | mrb_raisef(mrb, E_ARGUMENT_ERROR, "%S out of Time range", mrb_float_value(mrb, sec));
|
---|
| 253 | }
|
---|
[331] | 254 | tm = (struct mrb_time *)mrb_malloc(mrb, sizeof(struct mrb_time));
|
---|
| 255 | tm->sec = tsec;
|
---|
[270] | 256 | tm->usec = (time_t)llround((sec - tm->sec) * 1.0e6 + usec);
|
---|
[331] | 257 | if (tm->usec < 0) {
|
---|
| 258 | long sec2 = (long)NDIV(usec,1000000); /* negative div */
|
---|
| 259 | tm->usec -= sec2 * 1000000;
|
---|
| 260 | tm->sec += sec2;
|
---|
[270] | 261 | }
|
---|
[331] | 262 | else if (tm->usec >= 1000000) {
|
---|
| 263 | long sec2 = (long)(usec / 1000000);
|
---|
| 264 | tm->usec -= sec2 * 1000000;
|
---|
| 265 | tm->sec += sec2;
|
---|
[270] | 266 | }
|
---|
| 267 | tm->timezone = timezone;
|
---|
[331] | 268 | time_update_datetime(mrb, tm);
|
---|
[270] | 269 |
|
---|
| 270 | return tm;
|
---|
| 271 | }
|
---|
| 272 |
|
---|
| 273 | static mrb_value
|
---|
| 274 | mrb_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 |
|
---|
| 279 | static struct mrb_time*
|
---|
| 280 | current_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;
|
---|
[331] | 320 | time_update_datetime(mrb, tm);
|
---|
[270] | 321 |
|
---|
| 322 | return tm;
|
---|
| 323 | }
|
---|
| 324 |
|
---|
| 325 | /* Allocates a new Time object with given millis value. */
|
---|
| 326 | static mrb_value
|
---|
| 327 | mrb_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. */
|
---|
| 334 | static mrb_value
|
---|
| 335 | mrb_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 |
|
---|
| 343 | static struct mrb_time*
|
---|
| 344 | time_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;
|
---|
[331] | 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 |
|
---|
[270] | 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. */
|
---|
| 382 | static mrb_value
|
---|
| 383 | mrb_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. */
|
---|
| 396 | static mrb_value
|
---|
| 397 | mrb_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 |
|
---|
[331] | 407 | static struct mrb_time*
|
---|
| 408 | time_get_ptr(mrb_state *mrb, mrb_value time)
|
---|
| 409 | {
|
---|
| 410 | struct mrb_time *tm;
|
---|
[270] | 411 |
|
---|
[331] | 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 |
|
---|
[270] | 419 | static mrb_value
|
---|
| 420 | mrb_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);
|
---|
[331] | 427 | tm1 = DATA_GET_PTR(mrb, self, &mrb_time_type, struct mrb_time);
|
---|
[270] | 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 |
|
---|
| 434 | static mrb_value
|
---|
| 435 | mrb_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);
|
---|
[331] | 441 | tm1 = DATA_GET_PTR(mrb, self, &mrb_time_type, struct mrb_time);
|
---|
[270] | 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 |
|
---|
| 460 | static mrb_value
|
---|
| 461 | mrb_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);
|
---|
[331] | 467 | tm = time_get_ptr(mrb, self);
|
---|
[270] | 468 | return mrb_time_make(mrb, mrb_obj_class(mrb, self), (double)tm->sec+f, (double)tm->usec, tm->timezone);
|
---|
| 469 | }
|
---|
| 470 |
|
---|
| 471 | static mrb_value
|
---|
| 472 | mrb_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);
|
---|
[331] | 479 | tm = time_get_ptr(mrb, self);
|
---|
[270] | 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. */
|
---|
| 494 | static mrb_value
|
---|
| 495 | mrb_time_wday(mrb_state *mrb, mrb_value self)
|
---|
| 496 | {
|
---|
| 497 | struct mrb_time *tm;
|
---|
| 498 |
|
---|
[331] | 499 | tm = time_get_ptr(mrb, self);
|
---|
[270] | 500 | return mrb_fixnum_value(tm->datetime.tm_wday);
|
---|
| 501 | }
|
---|
| 502 |
|
---|
| 503 | /* 15.2.19.7.31 */
|
---|
| 504 | /* Returns year day number of time. */
|
---|
| 505 | static mrb_value
|
---|
| 506 | mrb_time_yday(mrb_state *mrb, mrb_value self)
|
---|
| 507 | {
|
---|
| 508 | struct mrb_time *tm;
|
---|
| 509 |
|
---|
[331] | 510 | tm = time_get_ptr(mrb, self);
|
---|
[270] | 511 | return mrb_fixnum_value(tm->datetime.tm_yday + 1);
|
---|
| 512 | }
|
---|
| 513 |
|
---|
| 514 | /* 15.2.19.7.32 */
|
---|
| 515 | /* Returns year of time. */
|
---|
| 516 | static mrb_value
|
---|
| 517 | mrb_time_year(mrb_state *mrb, mrb_value self)
|
---|
| 518 | {
|
---|
| 519 | struct mrb_time *tm;
|
---|
| 520 |
|
---|
[331] | 521 | tm = time_get_ptr(mrb, self);
|
---|
[270] | 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. */
|
---|
| 527 | static mrb_value
|
---|
| 528 | mrb_time_zone(mrb_state *mrb, mrb_value self)
|
---|
| 529 | {
|
---|
| 530 | struct mrb_time *tm;
|
---|
| 531 |
|
---|
[331] | 532 | tm = time_get_ptr(mrb, self);
|
---|
[270] | 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. */
|
---|
| 542 | static mrb_value
|
---|
| 543 | mrb_time_asctime(mrb_state *mrb, mrb_value self)
|
---|
| 544 | {
|
---|
[331] | 545 | struct mrb_time *tm = time_get_ptr(mrb, self);
|
---|
| 546 | struct tm *d = &tm->datetime;
|
---|
[270] | 547 | int len;
|
---|
| 548 |
|
---|
[331] | 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 |
|
---|
[270] | 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);
|
---|
[331] | 566 | #endif
|
---|
[270] | 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. */
|
---|
| 572 | static mrb_value
|
---|
| 573 | mrb_time_day(mrb_state *mrb, mrb_value self)
|
---|
| 574 | {
|
---|
| 575 | struct mrb_time *tm;
|
---|
| 576 |
|
---|
[331] | 577 | tm = time_get_ptr(mrb, self);
|
---|
[270] | 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. */
|
---|
| 584 | static mrb_value
|
---|
| 585 | mrb_time_dst_p(mrb_state *mrb, mrb_value self)
|
---|
| 586 | {
|
---|
| 587 | struct mrb_time *tm;
|
---|
| 588 |
|
---|
[331] | 589 | tm = time_get_ptr(mrb, self);
|
---|
[270] | 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. */
|
---|
| 596 | static mrb_value
|
---|
| 597 | mrb_time_getutc(mrb_state *mrb, mrb_value self)
|
---|
| 598 | {
|
---|
| 599 | struct mrb_time *tm, *tm2;
|
---|
| 600 |
|
---|
[331] | 601 | tm = time_get_ptr(mrb, self);
|
---|
[270] | 602 | tm2 = (struct mrb_time *)mrb_malloc(mrb, sizeof(*tm));
|
---|
| 603 | *tm2 = *tm;
|
---|
| 604 | tm2->timezone = MRB_TIMEZONE_UTC;
|
---|
[331] | 605 | time_update_datetime(mrb, tm2);
|
---|
[270] | 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. */
|
---|
| 611 | static mrb_value
|
---|
| 612 | mrb_time_getlocal(mrb_state *mrb, mrb_value self)
|
---|
| 613 | {
|
---|
| 614 | struct mrb_time *tm, *tm2;
|
---|
| 615 |
|
---|
[331] | 616 | tm = time_get_ptr(mrb, self);
|
---|
[270] | 617 | tm2 = (struct mrb_time *)mrb_malloc(mrb, sizeof(*tm));
|
---|
| 618 | *tm2 = *tm;
|
---|
| 619 | tm2->timezone = MRB_TIMEZONE_LOCAL;
|
---|
[331] | 620 | time_update_datetime(mrb, tm2);
|
---|
[270] | 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. */
|
---|
| 626 | static mrb_value
|
---|
| 627 | mrb_time_hour(mrb_state *mrb, mrb_value self)
|
---|
| 628 | {
|
---|
| 629 | struct mrb_time *tm;
|
---|
| 630 |
|
---|
[331] | 631 | tm = time_get_ptr(mrb, self);
|
---|
[270] | 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.*/
|
---|
| 637 | static mrb_value
|
---|
| 638 | mrb_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 |
|
---|
[331] | 645 | n = mrb_get_args(mrb, "|iiiiiii",
|
---|
| 646 | &ayear, &amonth, &aday, &ahour, &amin, &asec, &ausec);
|
---|
[270] | 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. */
|
---|
| 665 | static mrb_value
|
---|
| 666 | mrb_time_initialize_copy(mrb_state *mrb, mrb_value copy)
|
---|
| 667 | {
|
---|
| 668 | mrb_value src;
|
---|
[331] | 669 | struct mrb_time *t1, *t2;
|
---|
[270] | 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 | }
|
---|
[331] | 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");
|
---|
[270] | 680 | }
|
---|
[331] | 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;
|
---|
[270] | 686 | return copy;
|
---|
| 687 | }
|
---|
| 688 |
|
---|
| 689 | /* 15.2.19.7.18 */
|
---|
| 690 | /* Sets the timezone attribute of the Time object to LOCAL. */
|
---|
| 691 | static mrb_value
|
---|
| 692 | mrb_time_localtime(mrb_state *mrb, mrb_value self)
|
---|
| 693 | {
|
---|
| 694 | struct mrb_time *tm;
|
---|
| 695 |
|
---|
[331] | 696 | tm = time_get_ptr(mrb, self);
|
---|
[270] | 697 | tm->timezone = MRB_TIMEZONE_LOCAL;
|
---|
[331] | 698 | time_update_datetime(mrb, tm);
|
---|
[270] | 699 | return self;
|
---|
| 700 | }
|
---|
| 701 |
|
---|
| 702 | /* 15.2.19.7.19 */
|
---|
| 703 | /* Returns day of month of time. */
|
---|
| 704 | static mrb_value
|
---|
| 705 | mrb_time_mday(mrb_state *mrb, mrb_value self)
|
---|
| 706 | {
|
---|
| 707 | struct mrb_time *tm;
|
---|
| 708 |
|
---|
[331] | 709 | tm = time_get_ptr(mrb, self);
|
---|
[270] | 710 | return mrb_fixnum_value(tm->datetime.tm_mday);
|
---|
| 711 | }
|
---|
| 712 |
|
---|
| 713 | /* 15.2.19.7.20 */
|
---|
| 714 | /* Returns minutes of time. */
|
---|
| 715 | static mrb_value
|
---|
| 716 | mrb_time_min(mrb_state *mrb, mrb_value self)
|
---|
| 717 | {
|
---|
| 718 | struct mrb_time *tm;
|
---|
| 719 |
|
---|
[331] | 720 | tm = time_get_ptr(mrb, self);
|
---|
[270] | 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. */
|
---|
| 726 | static mrb_value
|
---|
| 727 | mrb_time_mon(mrb_state *mrb, mrb_value self)
|
---|
| 728 | {
|
---|
| 729 | struct mrb_time *tm;
|
---|
| 730 |
|
---|
[331] | 731 | tm = time_get_ptr(mrb, self);
|
---|
[270] | 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. */
|
---|
| 737 | static mrb_value
|
---|
| 738 | mrb_time_sec(mrb_state *mrb, mrb_value self)
|
---|
| 739 | {
|
---|
| 740 | struct mrb_time *tm;
|
---|
| 741 |
|
---|
[331] | 742 | tm = time_get_ptr(mrb, self);
|
---|
[270] | 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. */
|
---|
| 749 | static mrb_value
|
---|
| 750 | mrb_time_to_f(mrb_state *mrb, mrb_value self)
|
---|
| 751 | {
|
---|
| 752 | struct mrb_time *tm;
|
---|
| 753 |
|
---|
[331] | 754 | tm = time_get_ptr(mrb, self);
|
---|
[270] | 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. */
|
---|
| 760 | static mrb_value
|
---|
| 761 | mrb_time_to_i(mrb_state *mrb, mrb_value self)
|
---|
| 762 | {
|
---|
| 763 | struct mrb_time *tm;
|
---|
| 764 |
|
---|
[331] | 765 | tm = time_get_ptr(mrb, self);
|
---|
[270] | 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. */
|
---|
| 774 | static mrb_value
|
---|
| 775 | mrb_time_usec(mrb_state *mrb, mrb_value self)
|
---|
| 776 | {
|
---|
| 777 | struct mrb_time *tm;
|
---|
| 778 |
|
---|
[331] | 779 | tm = time_get_ptr(mrb, self);
|
---|
[270] | 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. */
|
---|
| 788 | static mrb_value
|
---|
| 789 | mrb_time_utc(mrb_state *mrb, mrb_value self)
|
---|
| 790 | {
|
---|
| 791 | struct mrb_time *tm;
|
---|
| 792 |
|
---|
[331] | 793 | tm = time_get_ptr(mrb, self);
|
---|
[270] | 794 | tm->timezone = MRB_TIMEZONE_UTC;
|
---|
[331] | 795 | time_update_datetime(mrb, tm);
|
---|
[270] | 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. */
|
---|
| 801 | static mrb_value
|
---|
| 802 | mrb_time_utc_p(mrb_state *mrb, mrb_value self)
|
---|
| 803 | {
|
---|
| 804 | struct mrb_time *tm;
|
---|
| 805 |
|
---|
[331] | 806 | tm = time_get_ptr(mrb, self);
|
---|
[270] | 807 | return mrb_bool_value(tm->timezone == MRB_TIMEZONE_UTC);
|
---|
| 808 | }
|
---|
| 809 |
|
---|
| 810 |
|
---|
| 811 | void
|
---|
| 812 | mrb_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 |
|
---|
| 871 | void
|
---|
| 872 | mrb_mruby_time_gem_final(mrb_state* mrb)
|
---|
| 873 | {
|
---|
| 874 | }
|
---|