Beispiel #1
0
/* Returns the Time object of the LOCAL timezone. */
static mrb_value
mrb_time_getlocal(mrb_state *mrb, mrb_value self)
{
  struct mrb_time *tm, *tm2;

  tm = mrb_get_datatype(mrb, self, &mrb_time_type);
  if (!tm) return self;
  tm2 = mrb_malloc(mrb, sizeof(*tm));
  *tm2 = *tm;
  tm2->timezone = MRB_TIMEZONE_LOCAL;
  mrb_time_update_datetime(tm2);
  return mrb_time_wrap(mrb, mrb_obj_class(mrb, self), tm2);
}
Beispiel #2
0
/* Allocates a mrb_time object and initializes it. */
static struct mrb_time*
mrb_time_alloc(mrb_state *mrb, mrb_float sec, mrb_float usec, enum mrb_timezone timezone)
{
  struct mrb_time *tm;

  tm = mrb_malloc(mrb, sizeof(struct mrb_time));
  tm->sec  = (time_t)sec;
  tm->usec = (sec - tm->sec) * 1.0e6 + usec;
  if (tm->usec < 0) {
    tm->sec--;
    tm->usec += 1.0e6;
  }
  tm->timezone = timezone;
  mrb_time_update_datetime(tm);

  return tm;
}
Beispiel #3
0
static struct mrb_time*
current_mrb_time(mrb_state *mrb)
{
  struct mrb_time *tm;

  tm = (struct mrb_time *)mrb_malloc(mrb, sizeof(*tm));
#if defined(TIME_UTC)
  {
    struct timespec ts;
    if (timespec_get(&ts, TIME_UTC) == 0) {
      mrb_free(mrb, tm);
      mrb_raise(mrb, E_RUNTIME_ERROR, "timespec_get() failed for unknown reasons");
    }
    tm->sec = ts.tv_sec;
    tm->usec = ts.tv_nsec / 1000;
  }
#elif defined(NO_GETTIMEOFDAY)
  {
    static time_t last_sec = 0, last_usec = 0;

    tm->sec  = time(NULL);
    if (tm->sec != last_sec) {
      last_sec = tm->sec;
      last_usec = 0;
    }
    else {
      /* add 1 usec to differentiate two times */
      last_usec += 1;
    }
    tm->usec = last_usec;
  }
#else
  {
    struct timeval tv;

    gettimeofday(&tv, NULL);
    tm->sec = tv.tv_sec;
    tm->usec = tv.tv_usec;
  }
#endif
  tm->timezone = MRB_TIMEZONE_LOCAL;
  mrb_time_update_datetime(tm);

  return tm;
}
Beispiel #4
0
/* Allocates a mrb_time object and initializes it. */
static struct mrb_time*
mrb_time_alloc(mrb_state *mrb, double sec, double usec, enum mrb_timezone timezone)
{
  struct mrb_time *tm;

  tm = (struct mrb_time *)mrb_malloc(mrb, sizeof(struct mrb_time));
  tm->sec  = (time_t)sec;
  tm->usec = (sec - tm->sec) * 1.0e6 + usec;
  while (tm->usec < 0) {
    tm->sec--;
    tm->usec += 1.0e6;
  }
  while (tm->usec > 1.0e6) {
    tm->sec++;
    tm->usec -= 1.0e6;
  }
  tm->timezone = timezone;
  mrb_time_update_datetime(tm);

  return tm;
}