diff options
| author | Yukihiro Matsumoto <[email protected]> | 2012-05-07 23:37:12 +0900 |
|---|---|---|
| committer | Yukihiro Matsumoto <[email protected]> | 2012-05-07 23:37:12 +0900 |
| commit | 395cb9c1e591d07d90b32150bd46cf2c8ac936e9 (patch) | |
| tree | af087e6865584146666b32c332b2e2fb511f0fd7 /src/time.c | |
| parent | e12a38779b36ebb40aa42d8481a8167b4fe0df93 (diff) | |
| download | mruby-395cb9c1e591d07d90b32150bd46cf2c8ac936e9.tar.gz mruby-395cb9c1e591d07d90b32150bd46cf2c8ac936e9.zip | |
use portable gmtime
Diffstat (limited to 'src/time.c')
| -rw-r--r-- | src/time.c | 71 |
1 files changed, 26 insertions, 45 deletions
diff --git a/src/time.c b/src/time.c index ed42f1f76..b9edea147 100644 --- a/src/time.c +++ b/src/time.c @@ -19,8 +19,7 @@ #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 */ +#undef USE_TIMEGM /* define to use systems timegm(3) */ #ifdef USE_GETTIMEOFDAY #include <sys/time.h> @@ -31,22 +30,34 @@ #endif #ifndef USE_TIMEGM -time_t +#define timegm my_timgm + +static unsigned int +is_leapyear(unsigned int y) +{ + return (y % 4) == 0 && ((y % 100) != 0 || (y % 400) == 0); +} + +static time_t timegm(struct tm *tm) { - time_t ret; - char *tz; + 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 = (unsigned int*) ndays[is_leapyear(tm->tm_year+1900)]; - tz = getenv("TZ"); - setenv("TZ", "", 1); - tzset(); - ret = mktime(tm); - if (tz) - setenv("TZ", tz, 1); - else - unsetenv("TZ"); - tzset(); - return ret; + 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 @@ -191,36 +202,6 @@ 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 unsigned 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 = (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; - 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, |
