Class: Time

Inherits:
Object
  • Object
show all
Defined in:
(unknown)

Instance Method Summary collapse

Instance Method Details

#to_dateObject

to_date() -> Date

Returns a Date with the same year, month, and day as the receiver in local time.

Time.local(2009, 1, 2).to_date
# => #<Date 2009-01-02>


3547
3548
3549
3550
3551
3552
3553
3554
3555
# File 'ext/date_ext/date_ext.c', line 3547

static VALUE rhrd_time_to_date(VALUE self) {
  rhrd_t *d;
  VALUE rd;
  rd = Data_Make_Struct(rhrd_class, rhrd_t, NULL, -1, d);
  d->jd = rhrd__unix_to_jd(NUM2LONG(rb_funcall(self, rhrd_id_to_i, 0)) + NUM2LONG(rb_funcall(self, rhrd_id_utc_offset, 0)));
  d->flags = RHR_HAVE_JD;
  RHR_CHECK_JD(d)
  return rd;
}

#to_datetimeObject

to_datetime() -> DateTime

Returns a DateTime with the same year, month, day, hour, minute, and second as the receiver in local time.

Time.local(2009, 1, 2, 12).to_datetime
# => #<DateTime 2009-01-02T12:00:00-08:00>


2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
# File 'ext/date_ext/datetime.c', line 2685

static VALUE rhrdt_time_to_datetime(VALUE self) {
  rhrdt_t *dt;
  VALUE rd;
  long t, offset;
  rd = Data_Make_Struct(rhrdt_class, rhrdt_t, NULL, -1, dt);

  offset = NUM2LONG(rb_funcall(self, rhrd_id_utc_offset, 0));
  t = NUM2LONG(rb_funcall(self, rhrd_id_to_i, 0)) + offset;
  dt->jd = rhrd__unix_to_jd(t);
#ifdef RUBY19
  dt->nanos = rhrd__mod(t, RHR_SECONDS_PER_DAY) * RHR_NANOS_PER_SECOND + NUM2LONG(rb_funcall(self, rhrd_id_nsec, 0));
#else
  dt->nanos = rhrd__mod(t, RHR_SECONDS_PER_DAY) * RHR_NANOS_PER_SECOND + NUM2LONG(rb_funcall(self, rhrd_id_usec, 0)) * 1000;
#endif
  dt->offset = (short)(offset/60);
  dt->flags |= RHR_HAVE_JD | RHR_HAVE_NANOS;
  RHR_CHECK_JD(dt);
  return rd;
}

#to_timeObject

to_time() -> Time

Returns a copy of the receiver in local time.

Time.local(2009, 1, 2).to_time
# => 2009-01-02 00:00:00 -0800
Time.local(2009, 1, 2).getutc.to_time
# => 2009-01-02 00:00:00 -0800


3568
3569
3570
# File 'ext/date_ext/date_ext.c', line 3568

static VALUE rhrd_time_to_time(VALUE self) {
  return rb_funcall(self, rhrd_id_getlocal, 0);
}