From 5143314297ee631860fa22e8f8f2db027784e687 Mon Sep 17 00:00:00 2001 From: mattn Date: Mon, 7 May 2012 14:24:23 +0900 Subject: time functions on windows. --- src/time.c | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) 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, -- cgit v1.2.3 From 5482c2eeb368d7715519f9c54facb66687a9e4cb Mon Sep 17 00:00:00 2001 From: mattn Date: Mon, 7 May 2012 15:44:10 +0900 Subject: remove warning. --- src/time.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/time.c b/src/time.c index b312c5aec..16884d9d8 100644 --- a/src/time.c +++ b/src/time.c @@ -192,7 +192,7 @@ mrb_time_at(mrb_state *mrb, mrb_value self) } #ifdef _WIN32 -static int +static unsigned int is_leapyear(unsigned int y) { return (y % 4) == 0 && ((y % 100) != 0 || (y % 400) == 0); @@ -207,7 +207,7 @@ timegm(struct tm *tm) }; time_t r = 0; int i; - unsigned int *nday = ndays[is_leapyear(tm->tm_year+1900)]; + unsigned int *nday = (unsigned int*) 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; -- cgit v1.2.3