diff options
| author | mattn <[email protected]> | 2012-05-07 14:24:23 +0900 |
|---|---|---|
| committer | mattn <[email protected]> | 2012-05-07 14:24:23 +0900 |
| commit | 5143314297ee631860fa22e8f8f2db027784e687 (patch) | |
| tree | e0899cfc24cd48a1e09bd20473928fb40a90e4be /src/time.c | |
| parent | b666b7d112b0c558fcec14ec1d36ceae2efc0a9b (diff) | |
| download | mruby-5143314297ee631860fa22e8f8f2db027784e687.tar.gz mruby-5143314297ee631860fa22e8f8f2db027784e687.zip | |
time functions on windows.
Diffstat (limited to 'src/time.c')
| -rw-r--r-- | src/time.c | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/src/time.c b/src/time.c index a9ac3eb86..b312c5aec 100644 --- a/src/time.c +++ b/src/time.c @@ -16,7 +16,9 @@ #undef USE_GETTIMEOFDAY /* C99 does not have gettimeofday */ #define USE_GETTIMEOFDAY /* need gettimeofday to retrieve microseconds */ #undef USE_GMTIME_R /* C99 does not have reentrant gmtime_r */ +#ifndef _WIN32 #define USE_GMTIME_R /* use reentrant gmtime_r */ +#endif #undef USE_TIMEGM /* C99 does not have timegm */ #define USE_TIMEGM /* use faster gmtime */ @@ -189,6 +191,36 @@ mrb_time_at(mrb_state *mrb, mrb_value self) return mrb_time_make(mrb, mrb_class_ptr(self), f, MRB_TIMEZONE_LOCAL); } +#ifdef _WIN32 +static int +is_leapyear(unsigned int y) +{ + return (y % 4) == 0 && ((y % 100) != 0 || (y % 400) == 0); +} + +static time_t +timegm(struct tm *tm) +{ + static const unsigned int ndays[2][12] = { + {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}, + {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31} + }; + time_t r = 0; + int i; + unsigned int *nday = ndays[is_leapyear(tm->tm_year+1900)]; + + for (i = 70; i < tm->tm_year; ++i) + r += is_leapyear(i+1900) ? 366*24*60*60 : 365*24*60*60; + for (i = 0; i < tm->tm_mon; ++i) + r += nday[i] * 24 * 60 * 60; + r += (tm->tm_mday - 1) * 24 * 60 * 60; + r += tm->tm_hour * 60 * 60; + r += tm->tm_min * 60; + r += tm->tm_sec; + return r; +} +#endif + static struct mrb_time* time_mktime(mrb_state *mrb, mrb_int ayear, mrb_int amonth, mrb_int aday, mrb_int ahour, mrb_int amin, mrb_int asec, mrb_int ausec, |
