Skip to content

Commit

Permalink
update time configuration; now config macros can be supplied by -D co…
Browse files Browse the repository at this point in the history
…mpiler option
  • Loading branch information
matz committed May 7, 2012
1 parent 395cb9c commit de262d5
Showing 1 changed file with 32 additions and 19 deletions.
51 changes: 32 additions & 19 deletions src/time.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -166,17 +179,17 @@ 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;

gettimeofday(&tv, NULL);
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);
Expand Down

0 comments on commit de262d5

Please sign in to comment.