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>


3586
3587
3588
3589
3590
3591
3592
3593
3594
# File 'ext/date_ext/date_ext.c', line 3586

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>


2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
# File 'ext/date_ext/datetime.c', line 2658

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


3607
3608
3609
# File 'ext/date_ext/date_ext.c', line 3607

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