summaryrefslogtreecommitdiffhomepage
path: root/src/time.c
diff options
context:
space:
mode:
authorYukihiro "Matz" Matsumoto <[email protected]>2012-05-07 01:40:16 -0700
committerYukihiro "Matz" Matsumoto <[email protected]>2012-05-07 01:40:16 -0700
commit32faeedb1eb19e0d4ab86a2106987f7a485e376e (patch)
tree2f130fefd212914ed7361b1aec811ab4f1bd4622 /src/time.c
parent9182ab8764b669b18a949343839481711ce5a4a9 (diff)
parent5482c2eeb368d7715519f9c54facb66687a9e4cb (diff)
downloadmruby-32faeedb1eb19e0d4ab86a2106987f7a485e376e.tar.gz
mruby-32faeedb1eb19e0d4ab86a2106987f7a485e376e.zip
Merge pull request #104 from mattn/gmtime_r
time functions on windows.
Diffstat (limited to 'src/time.c')
-rw-r--r--src/time.c32
1 files changed, 32 insertions, 0 deletions
diff --git a/src/time.c b/src/time.c
index a9ac3eb86..16884d9d8 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 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,