summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorYukihiro Matsumoto <[email protected]>2012-05-07 23:54:45 +0900
committerYukihiro Matsumoto <[email protected]>2012-05-07 23:54:45 +0900
commitde262d5ba863ffd9bc9e2dfdfc59739b1654ab02 (patch)
tree9de864f8cfa285426a72d3cfb8e62e1f78005174
parent395cb9c1e591d07d90b32150bd46cf2c8ac936e9 (diff)
downloadmruby-de262d5ba863ffd9bc9e2dfdfc59739b1654ab02.tar.gz
mruby-de262d5ba863ffd9bc9e2dfdfc59739b1654ab02.zip
update time configuration; now config macros can be supplied by -D compiler option
-rw-r--r--src/time.c51
1 files changed, 32 insertions, 19 deletions
diff --git a/src/time.c b/src/time.c
index b9edea147..bd6b87424 100644
--- a/src/time.c
+++ b/src/time.c
@@ -12,24 +12,40 @@
#include "mruby/class.h"
#include "mruby/data.h"
-/* Time class configuration */
-#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 */
+/** Time class configuration */
+
+/* gettimeofday(2) */
+/* C99 does not have gettimeofday that is required to retrieve microseconds */
+/* uncomment following macro on platforms without gettimeofday(2) */
+/* #define NO_USE_GETTIMEOFDAY */
+
+/* gmtime(3) */
+/* C99 does not have reentrant gmtime_r() so it might cause troubles under */
+/* multi-threading environment. undef following macro on platforms that */
+/* does not have gmtime_r() and localtime_r(). */
+/* #define NO_USE_GMTIME_R */
+
+#ifdef _WIN32
+/* unfortunately Win32 platform do not provide gmtime_r/localtime_r */
+#define NO_USE_GMTIME_R
#endif
-#undef USE_TIMEGM /* define to use systems timegm(3) */
-#ifdef USE_GETTIMEOFDAY
+/* timegm(3) */
+/* mktime() creates tm structure for localtime; timegm() is for UTF time */
+/* define following macro to use probably faster timegm() on the platform */
+/* #define USE_SYSTEM_TIMEGM */
+
+/** end of Time class configuration */
+
+#ifndef NO_USE_GETTIMEOFDAY
#include <sys/time.h>
#endif
-#ifndef USE_GMTIME_R
+#ifndef NO_USE_GMTIME_R
#define gmtime_r(t,r) gmtime(t)
-#define localtime_r(t,r) localtime(t)
+#define localtime_r(t,r) (tzset(),localtime(t))
#endif
-#ifndef USE_TIMEGM
+#ifndef USE_SYSTEM_TIMEGM
#define timegm my_timgm
static unsigned int
@@ -119,13 +135,10 @@ mrb_time_update_datetime(struct mrb_time *self)
aid = gmtime_r(&self->sec, &self->datetime);
}
else {
-#ifdef USE_GMTIME_R
- tzset();
-#endif
aid = localtime_r(&self->sec, &self->datetime);
}
if(!aid) return NULL;
-#ifndef USE_GMTIME_R
+#ifndef NO_USE_GMTIME_R
self->datetime = *aid; // copy data
#endif
@@ -166,7 +179,10 @@ current_time(mrb_state *mrb)
struct mrb_time *tm;
tm = mrb_malloc(mrb, sizeof(*tm));
-#ifdef USE_GETTIMEOFDAY
+#ifdef NO_USE_GETTIMEOFDAY
+ tm->sec = time(NULL);
+ tm->usec = 0;
+#else
{
struct timeval tv;
@@ -174,9 +190,6 @@ current_time(mrb_state *mrb)
tm->sec = tv.tv_sec;
tm->usec = tv.tv_usec;
}
-#else
- tm->sec = time(NULL);
- tm->usec = 0;
#endif
tm->timezone = MRB_TIMEZONE_LOCAL;
mrb_time_update_datetime(tm);