Class: DateTime

Inherits:
Date
  • Object
show all
Defined in:
ext/date_ext/datetime.c

Constant Summary

Constants inherited from Date

Date::ABBR_DAYNAMES, Date::ABBR_MONTHNAMES, Date::DAYNAMES, Date::ENGLAND, Date::GREGORIAN, Date::ITALY, Date::JULIAN, Date::MONTHNAMES

Instance Method Summary collapse

Methods inherited from Date

_httpdate, _iso8601, _jisx0301, _parse, _rfc2822, _rfc3339, _xmlschema, #gregorian, #gregorian?, #julian?, #new_start, #start

Instance Method Details

#+(n) ⇒ DateTime

Returns a DateTime that is n days after the receiver. n can be negative, in which case it returns a DateTime before the receiver. n can be a Float including a fractional part, in which case it is added as a partial day.

DateTime.civil(2009, 1, 2, 6, 0, 0) + 2
# => #<DateTime 2009-01-04T06:00:00+00:00>
DateTime.civil(2009, 1, 2, 6, 0, 0) + -2
# => #<DateTime 2008-12-31T06:00:00+00:00>
DateTime.civil(2009, 1, 2, 6, 0, 0) + 0.5
# => #<DateTime 2009-01-02T18:00:00+00:00>

Returns:



1740
1741
1742
# File 'ext/date_ext/datetime.c', line 1740

static VALUE rhrdt_op_plus(VALUE self, VALUE other) {
   return rhrdt__add_days(self, NUM2DBL(other));
}

#-(n) ⇒ DateTime <br /> #-(date) ⇒ Float <br /> #-(datetime) ⇒ Float

If a Numeric argument is given, it is treated as an Float, and the number of days it represents is substracted from the receiver to return a new DateTime object. n can be negative, in which case the DateTime returned will be after the receiver.

If a Date argument is given, returns the number of days between the current date and the argument as an Float. If the receiver has no fractional component, will return a Float with no fractional component. The Date argument is assumed to have the same time zone offset as the receiver.

If a DateTime argument is given, returns the number of days between the receiver and the argument as a Float. This handles differences in the time zone offsets between the receiver and the argument.

Other types of arguments raise a TypeError.

DateTime.civil(2009, 1, 2) - 2
# => #<DateTime 2008-12-31T00:00:00+00:00>
DateTime.civil(2009, 1, 2) - 2.5
# => #<DateTime 2008-12-30T12:00:00+00:00>
DateTime.civil(2009, 1, 2) - Date.civil(2009, 1, 1)
# => 1.0
DateTime.civil(2009, 1, 2, 12, 0, 0) - Date.civil(2009, 1, 1)
# => 1.5
DateTime.civil(2009, 1, 2, 12, 0, 0, 0.5) - Date.civil(2009, 1, 1)
# => 1.5
DateTime.civil(2009, 1, 2) - DateTime.civil(2009, 1, 3, 12)
# => -1.5
DateTime.civil(2009, 1, 2, 0, 0, 0, 0.5) - DateTime.civil(2009, 1, 3, 12, 0, 0, -0.5)
# => -2.5

Overloads:

  • #-(n) ⇒ DateTime <br />

    Returns:

  • #-(date) ⇒ Float <br />

    Returns:

    • (Float <br />)
  • #-(datetime) ⇒ Float

    Returns:

    • (Float)


1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
# File 'ext/date_ext/datetime.c', line 1782

static VALUE rhrdt_op_minus(VALUE self, VALUE other) {
  rhrdt_t *dt;
  rhrdt_t *newdt;
  rhrd_t *newd;

  if (RTEST(rb_obj_is_kind_of(other, rb_cNumeric))) {
    Data_Get_Struct(self, rhrdt_t, dt);
    return rhrdt__add_days(self, -NUM2DBL(other));
  }
  if (RTEST((rb_obj_is_kind_of(other, rhrdt_class)))) {
    self = rhrdt__new_offset(self, 0.0);
    other = rhrdt__new_offset(other, 0.0);
    Data_Get_Struct(self, rhrdt_t, dt);
    Data_Get_Struct(other, rhrdt_t, newdt);
    RHRDT_FILL_JD(dt)
    RHRDT_FILL_NANOS(dt)
    RHRDT_FILL_JD(newdt)
    RHRDT_FILL_NANOS(newdt)
    if (dt->nanos == newdt->nanos) {
      return rb_float_new((double)(dt->jd - newdt->jd));
    } else if (dt->jd == newdt->jd) 
      return rb_float_new((double)(dt->nanos - newdt->nanos)/RHR_NANOS_PER_DAYD);
    else {
      return rb_float_new((dt->jd - newdt->jd) + (double)(dt->nanos - newdt->nanos)/RHR_NANOS_PER_DAYD);
    }
  }
  if (RTEST((rb_obj_is_kind_of(other, rhrd_class)))) {
    Data_Get_Struct(self, rhrdt_t, dt);
    Data_Get_Struct(other, rhrd_t, newd);
    RHRDT_FILL_JD(dt)
    RHRDT_FILL_NANOS(dt)
    RHR_FILL_JD(newd)
    return rb_float_new((dt->jd - newd->jd) + (double)dt->nanos/RHR_NANOS_PER_DAYD); 
  }
  rb_raise(rb_eTypeError, "expected numeric or date");
}

#<<(n) ⇒ DateTime

Returns a DateTime that is n months before the receiver. n can be negative, in which case it returns a DateTime after the receiver.

DateTime.civil(2009, 1, 2) << 2
# => #<DateTime 2008-11-02T00:00:00+00:00>
DateTime.civil(2009, 1, 2) << -2
# => #<DateTime 2009-03-02T00:00:00+00:00>

Returns:



1721
1722
1723
# File 'ext/date_ext/datetime.c', line 1721

static VALUE rhrdt_op_left_shift(VALUE self, VALUE other) {
  return rhrdt__add_months(self, -NUM2LONG(other));
}

#<=>(other) ⇒ -1, ...

If other is a DateTime, returns -1 if the absolute date and time of other is before the absolute time of the receiver chronologically, 0 if other is the same absolute date and time as the receiver, or 1 if the absolute date and time of other is before the receiver chronologically. Absolute date and time in this case means after taking account the time zone offset.

If other is a Date, return 0 if other has the same julian date as the receiver and the receiver has no fractional part, 1 if other has a julian date greater than the receiver’s, or -1 if other has a julian date less than the receiver’s or a julian date the same as the receiver’s and the receiver has a fractional part.

If other is a Numeric, convert it to an Float and compare it to the receiver’s julian date plus the fractional part.

For an unrecognized type, return nil.

Returns:

  • (-1, 0, 1, nil)


1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
# File 'ext/date_ext/datetime.c', line 1878

static VALUE rhrdt_op_spaceship(VALUE self, VALUE other) {
  rhrdt_t *dt, *odt;
  rhrd_t *od;
  double diff;
  int res;

  if (RTEST(rb_obj_is_kind_of(other, rhrdt_class))) {
    self = rhrdt__new_offset(self, 0.0);
    other = rhrdt__new_offset(other, 0.0);
    Data_Get_Struct(self, rhrdt_t, dt);
    Data_Get_Struct(other, rhrdt_t, odt);
    return LONG2NUM(rhrdt__spaceship(dt, odt));
  }
  if (RTEST(rb_obj_is_kind_of(other, rhrd_class))) {
    Data_Get_Struct(self, rhrdt_t, dt);
    Data_Get_Struct(other, rhrd_t, od);
    RHRDT_FILL_JD(dt)
    RHR_FILL_JD(od)
    RHR_SPACE_SHIP(res, dt->jd, od->jd)
    if (res == 0) {
      RHRDT_FILL_NANOS(dt)
      RHR_SPACE_SHIP(res, dt->nanos, 0)
    }
    return LONG2NUM(res);
  }
  if (RTEST((rb_obj_is_kind_of(other, rb_cNumeric)))) {
    Data_Get_Struct(self, rhrdt_t, dt);
    diff = NUM2DBL(other);
    RHRDT_FILL_JD(dt)
    RHR_SPACE_SHIP(res, dt->jd, (long)diff)
    if (res == 0) {
      RHRDT_FILL_NANOS(dt)
      RHR_SPACE_SHIP(res, dt->nanos, llround((diff - floor(diff)) * RHR_NANOS_PER_DAY))
    }
    return LONG2NUM(res);
  }
  return Qnil;
}

#===(other) ⇒ Boolean

If other is a Date, returns true if other is the same date as the receiver, or false otherwise.

If other is a DateTime, return true if +other has the same julian date as the receiver, or false otherwise.

If other is a Numeric, convert it to an Integer and return true if it is equal to the receiver’s julian date, or false otherwise.

Returns:

  • (Boolean)


1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
# File 'ext/date_ext/datetime.c', line 1832

static VALUE rhrdt_op_relationship(VALUE self, VALUE other) {
  rhrdt_t *dt, *odt;
  rhrd_t *o;
  long jd;

  if (RTEST(rb_obj_is_kind_of(other, rhrdt_class))) {
    Data_Get_Struct(other, rhrdt_t, odt);
    RHRDT_FILL_JD(odt)
    jd = odt->jd;
  } else if (RTEST(rb_obj_is_kind_of(other, rhrd_class))) {
    Data_Get_Struct(other, rhrd_t, o);
    RHR_FILL_JD(o)
    jd = o->jd;
  } else if (RTEST((rb_obj_is_kind_of(other, rb_cNumeric)))) {
    jd = NUM2LONG(other);
  } else {
    return Qfalse;
  }

  Data_Get_Struct(self, rhrdt_t, dt);
  RHRDT_FILL_JD(dt)
  return dt->jd == jd ? Qtrue : Qfalse;
}

#>>(n) ⇒ DateTime

Returns a DateTime that is n months after the receiver. n can be negative, in which case it returns a DateTime before the receiver.

DateTime.civil(2009, 1, 2) >> 2
# => #<DateTime 2009-03-02T00:00:00+00:00>
DateTime.civil(2009, 1, 2) >> -2
# => #<DateTime 2008-11-02T00:00:00+00:00>

Returns:



1705
1706
1707
# File 'ext/date_ext/datetime.c', line 1705

static VALUE rhrdt_op_right_shift(VALUE self, VALUE other) {
  return rhrdt__add_months(self, NUM2LONG(other));
}

#_dump(limit) ⇒ String

Returns a marshalled representation of the receiver as a String. Generally not called directly, usually called by Marshal.dump.

Returns:

  • (String)


937
938
939
940
941
942
943
# File 'ext/date_ext/datetime.c', line 937

static VALUE rhrdt__dump(VALUE self, VALUE limit) {
  rhrdt_t *d;
  Data_Get_Struct(self, rhrdt_t, d);
  RHRDT_FILL_JD(d)
  RHRDT_FILL_NANOS(d)
  return rb_marshal_dump(rb_ary_new3(3, LONG2NUM(d->jd), LL2NUM(d->nanos), LONG2NUM(d->offset)), LONG2NUM(NUM2LONG(limit) - 1));
}

#ajdFloat

Returns the date and time represented by the receiver as a astronomical julian day Float.

Returns:

  • (Float)


951
952
953
954
955
956
957
# File 'ext/date_ext/datetime.c', line 951

static VALUE rhrdt_ajd(VALUE self) {
  rhrdt_t *d;
  Data_Get_Struct(self, rhrdt_t, d);
  RHRDT_FILL_JD(d)
  RHRDT_FILL_NANOS(d)
  return rb_float_new(d->jd + (double)d->nanos/RHR_NANOS_PER_DAYD - d->offset/1440.0 - 0.5);
}

#amjdFloat

Returns the date and time represented by the receiver as a astronomical modified julian day Float.

Returns:

  • (Float)


965
966
967
968
969
970
971
# File 'ext/date_ext/datetime.c', line 965

static VALUE rhrdt_amjd(VALUE self) {
  rhrdt_t *d;
  Data_Get_Struct(self, rhrdt_t, d);
  RHRDT_FILL_JD(d)
  RHRDT_FILL_NANOS(d)
  return rb_float_new(d->jd + (double)d->nanos/RHR_NANOS_PER_DAYD - d->offset/1440.0 - RHR_JD_MJD);
}

#asctimeString Also known as: ctime

Returns a string representation of the receiver. Example:

DateTime.civil(2009, 1, 2, 3, 4, 5).asctime
# => "Fri Jan  2 03:04:05 2009"

Returns:

  • (String)


981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
# File 'ext/date_ext/datetime.c', line 981

static VALUE rhrdt_asctime(VALUE self) {
  VALUE s;
  rhrdt_t *d;
  int len;
  Data_Get_Struct(self, rhrdt_t, d);
  RHRDT_FILL_CIVIL(d)
  RHRDT_FILL_JD(d)
  RHRDT_FILL_HMS(d)

  s = rb_str_buf_new(128);
  len = snprintf(RSTRING_PTR(s), 128, "%s %s %2i %02i:%02i:%02i %04li", 
        rhrd__abbr_day_names[rhrd__jd_to_wday(d->jd)],
        rhrd__abbr_month_names[d->month],
        (int)d->day, (int)d->hour, (int)d->minute, (int)d->second,
        d->year);
  if (len == -1 || len > 127) {
    rb_raise(rb_eNoMemError, "in DateTime#asctime (in snprintf)");
  }

  RHR_RETURN_RESIZED_STR(s, len)
}

#cwdayInteger

Returns the commercial week day as an Integer. Example:

DateTime.civil(2009, 1, 2).cwday
# => 5
DateTime.civil(2010, 1, 2).cwday
# => 6

Returns:

  • (Integer)


1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
# File 'ext/date_ext/datetime.c', line 1013

static VALUE rhrdt_cwday(VALUE self) {
  rhrdt_t *d;
  rhrd_t n;
  RHR_CACHED_IV(self, rhrd_id_cwday)
  memset(&n, 0, sizeof(rhrd_t));
  Data_Get_Struct(self, rhrdt_t, d);
  RHRDT_FILL_JD(d)
  n.jd = d->jd;
  rhrd__fill_commercial(&n);
  rhrd__set_cw_ivs(self, &n);
  return LONG2NUM(n.day);
}

#cweekInteger

Returns the commercial week as an Integer. Example:

DateTime.civil(2009, 1, 2).cweek
# => 1
DateTime.civil(2010, 1, 2).cweek
# => 53

Returns:

  • (Integer)


1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
# File 'ext/date_ext/datetime.c', line 1036

static VALUE rhrdt_cweek(VALUE self) {
  rhrdt_t *d;
  rhrd_t n;
  RHR_CACHED_IV(self, rhrd_id_cweek)
  memset(&n, 0, sizeof(rhrd_t));
  Data_Get_Struct(self, rhrdt_t, d);
  RHRDT_FILL_JD(d)
  n.jd = d->jd;
  rhrd__fill_commercial(&n);
  rhrd__set_cw_ivs(self, &n);
  return LONG2NUM(n.month);
}

#cwyearInteger

Returns the commercial week year as an Integer. Example:

DateTime.civil(2009, 1, 2).cwyear
# => 2009
DateTime.civil(2010, 1, 2).cwyear
# => 2009

Returns:

  • (Integer)


1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
# File 'ext/date_ext/datetime.c', line 1059

static VALUE rhrdt_cwyear(VALUE self) {
  rhrdt_t *d;
  rhrd_t n;
  RHR_CACHED_IV(self, rhrd_id_cwyear)
  memset(&n, 0, sizeof(rhrd_t));
  Data_Get_Struct(self, rhrdt_t, d);
  RHRDT_FILL_JD(d)
  n.jd = d->jd;
  rhrd__fill_commercial(&n);
  rhrd__set_cw_ivs(self, &n);
  return LONG2NUM(n.year);
}

#dayInteger Also known as: mday

Returns the day of the month as an Integer. Example:

DateTime.civil(2009, 1, 2).day
# => 2

Returns:

  • (Integer)


1080
1081
1082
1083
1084
1085
# File 'ext/date_ext/datetime.c', line 1080

static VALUE rhrdt_day(VALUE self) {
  rhrdt_t *dt;
  Data_Get_Struct(self, rhrdt_t, dt);
  RHRDT_FILL_CIVIL(dt)
  return LONG2NUM(dt->day);
}

#day_fractionFloat

Returns the fraction of the day as a Float. Example:

DateTime.civil(2009, 1, 2).day_fraction
# => 0.0
DateTime.civil(2009, 1, 2, 12).day_fraction
# => 0.5
DateTime.civil(2009, 1, 2, 6).day_fraction
# => 0.25

Returns:

  • (Float)


1099
1100
1101
1102
1103
1104
# File 'ext/date_ext/datetime.c', line 1099

static VALUE rhrdt_day_fraction(VALUE self) {
  rhrdt_t *dt;
  Data_Get_Struct(self, rhrdt_t, dt);
  RHRDT_FILL_NANOS(dt)
  return rb_float_new((double)dt->nanos/RHR_NANOS_PER_DAYD);
}

#downto(target) {|datetime| ... } ⇒ DateTime

Equivalent to calling step with the target as the first argument and -1 as the second argument. Returns self.

DateTime.civil(2009, 1, 2).downto(DateTime.civil(2009, 1, 1)) do |datetime|
  puts datetime
end
# Output:
# 2009-01-02T00:00:00+00:00
# 2009-01-01T00:00:00+00:00

Yields:

  • (datetime)

Returns:



1119
1120
1121
1122
1123
1124
# File 'ext/date_ext/datetime.c', line 1119

static VALUE rhrdt_downto(VALUE self, VALUE other) {
  VALUE argv[2];
  argv[0] = other;
  argv[1] = INT2FIX(-1);
  return rhrdt_step(2, argv, self);
}

#eql?(datetime) ⇒ Boolean

Returns true only if the datetime given is the same date and time as the receiver. If date is an instance of Date, returns true only if date is for the same date as the receiver and the receiver has no fractional component. Otherwise, returns false. Example:

DateTime.civil(2009, 1, 2, 12).eql?(DateTime.civil(2009, 1, 2, 12))
# => true
DateTime.civil(2009, 1, 2, 12).eql?(DateTime.civil(2009, 1, 2, 11))
# => false
DateTime.civil(2009, 1, 2).eql?(Date.civil(2009, 1, 2))
# => true
DateTime.civil(2009, 1, 2, 1).eql?(Date.civil(2009, 1, 2))
# => false

Returns:

  • (Boolean)


1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
# File 'ext/date_ext/datetime.c', line 1143

static VALUE rhrdt_eql_q(VALUE self, VALUE other) {
  rhrdt_t *dt, *odt;
  rhrd_t *o;
  long diff;

  if (RTEST(rb_obj_is_kind_of(other, rhrdt_class))) {
    self = rhrdt__new_offset(self, 0.0);
    other = rhrdt__new_offset(other, 0.0);
    Data_Get_Struct(self, rhrdt_t, dt);
    Data_Get_Struct(other, rhrdt_t, odt);
    return rhrdt__spaceship(dt, odt) == 0 ? Qtrue : Qfalse;
  } else if (RTEST(rb_obj_is_kind_of(other, rhrd_class))) {
    Data_Get_Struct(self, rhrdt_t, dt);
    Data_Get_Struct(other, rhrd_t, o);
    RHRDT_FILL_JD(dt)
    RHR_FILL_JD(o)
    RHR_SPACE_SHIP(diff, dt->jd, o->jd)
    if (diff == 0) {
      RHRDT_FILL_NANOS(dt)
      RHR_SPACE_SHIP(diff, dt->nanos, 0)
    }
    return diff == 0 ? Qtrue : Qfalse;
  }
  return Qfalse;
}

#friday?Boolean

friday?() -> true or false

Returns true if the receiver is a Friday, false otherwise.

Returns:

  • (Boolean)


2708
2709
2710
# File 'ext/date_ext/datetime.c', line 2708

static VALUE rhrdt_friday_q(VALUE self) {
  return rhrdt__day_q(self, 5);
}

#hashInteger

Return an Integer hash value for the receiver, such that an equal date and time will have the same hash value.

Returns:

  • (Integer)


1175
1176
1177
1178
1179
1180
1181
# File 'ext/date_ext/datetime.c', line 1175

static VALUE rhrdt_hash(VALUE self) {
  rhrdt_t *d;
  VALUE new = rhrdt__new_offset(self, 0.0); 
  RHR_CACHED_IV(self, rhrd_id_hash)
  Data_Get_Struct(new, rhrdt_t, d);
  return rb_ivar_set(self, rhrd_id_hash, rb_funcall(rb_ary_new3(2, LONG2NUM(d->jd), LL2NUM(d->nanos)), rhrd_id_hash, 0));
}

#hourInteger

Returns the hour of the day as an Integer. Example:

DateTime.civil(2009, 1, 2, 12, 13, 14).hour
# => 12

Returns:

  • (Integer)


1191
1192
1193
1194
1195
1196
# File 'ext/date_ext/datetime.c', line 1191

static VALUE rhrdt_hour(VALUE self) {
  rhrdt_t *dt;
  Data_Get_Struct(self, rhrdt_t, dt);
  RHRDT_FILL_HMS(dt)
  return LONG2NUM(dt->hour);
}

#httpdateObject

httpdate() -> String

Returns the receiver as a String in HTTP format. Example:

DateTime.civil(2009, 1, 2, 3, 4, 5).httpdate
# => "Fri, 02 Jan 2009 03:04:05 GMT"


2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
# File 'ext/date_ext/datetime.c', line 2209

static VALUE rhrdt_httpdate(VALUE self) {
  VALUE s;
  rhrdt_t *d;
  int len;
  s = rhrdt__new_offset(self, 0.0);
  Data_Get_Struct(s, rhrdt_t, d);
  RHRDT_FILL_JD(d)
  RHRDT_FILL_CIVIL(d)
  RHRDT_FILL_HMS(d)

  s = rb_str_buf_new(128);
  len = snprintf(RSTRING_PTR(s), 128, "%s, %02i %s %04li %02i:%02i:%02i GMT", 
        rhrd__abbr_day_names[rhrd__jd_to_wday(d->jd)],
        (int)d->day,
        rhrd__abbr_month_names[d->month],
        d->year, (int)d->hour, (int)d->minute, (int)d->second);
  if (len == -1 || len > 127) {
    rb_raise(rb_eNoMemError, "in DateTime#httpdate (in snprintf)");
  }

  RHR_RETURN_RESIZED_STR(s, len)
}

#inspectString

Return a developer-friendly string containing the civil date and time for the receiver. Example:

DateTime.civil(2009, 1, 2, 3, 4, 5, 0.5).inspect
# => "#<DateTime 2009-01-02T03:04:05+12:00>"

Returns:

  • (String)


1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
# File 'ext/date_ext/datetime.c', line 1207

static VALUE rhrdt_inspect(VALUE self) {
  VALUE s;
  rhrdt_t *dt;
  int len;
  Data_Get_Struct(self, rhrdt_t, dt);
  RHRDT_FILL_CIVIL(dt)
  RHRDT_FILL_HMS(dt)

  s = rb_str_buf_new(128);
  len = snprintf(RSTRING_PTR(s), 128, "#<DateTime %04li-%02i-%02iT%02i:%02i:%02i%+03i:%02i>",
        dt->year, (int)dt->month, (int)dt->day, (int)dt->hour, (int)dt->minute, (int)dt->second, dt->offset/60, abs(dt->offset % 60));
  if (len == -1 || len > 127) {
    rb_raise(rb_eNoMemError, "in DateTime#inspect (in snprintf)");
  }

  RHR_RETURN_RESIZED_STR(s, len)
}

#iso8601(*args) ⇒ Object Also known as: rfc3339, xmlschema

iso8601(n=0) -> String

Returns the receiver as a String in ISO8601 format. If an argument is given, it should be an Integer representing the number of decimal places to use for the fractional seconds. Example:

DateTime.civil(2009, 1, 2, 3, 4, 5, 0.5).iso8601
# => "2009-01-02T03:04:05+12:00"
DateTime.civil(2009, 1, 2, 3, 4, 5, 0.5).iso8601(4)
# => "2009-01-02T03:04:05.0000+12:00"


2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
# File 'ext/date_ext/datetime.c', line 2246

static VALUE rhrdt_iso8601(int argc, VALUE *argv, VALUE self) {
  long i;
  VALUE s;
  rhrdt_t *dt;
  char * str;
  int len;
  Data_Get_Struct(self, rhrdt_t, dt);
  RHRDT_FILL_CIVIL(dt)

  switch(argc) {
    case 1:
      i = NUM2LONG(argv[0]);
      break;
    case 0:
      i = 0;
      break;
    default:
      rb_raise(rb_eArgError, "wrong number of arguments: %i for 1", argc);
      break;
  }

  s = rb_str_buf_new(128);
  str = RSTRING_PTR(s);

  len = snprintf(str, 128, "%04li-%02i-%02i", dt->year, (int)dt->month, (int)dt->day);
  if (len == -1 || len > 127) {
    rb_raise(rb_eNoMemError, "in DateTime#to_s (in snprintf)");
  }

  len = rhrdt__add_iso_time_format(dt, str, len, i);
  RHR_RETURN_RESIZED_STR(s, len)
}

#jdInteger

Return the julian day number for the receiver as an Integer.

DateTime.civil(2009, 1, 2).jd
# => 2454834

Returns:

  • (Integer)


1233
1234
1235
1236
1237
1238
# File 'ext/date_ext/datetime.c', line 1233

static VALUE rhrdt_jd(VALUE self) {
  rhrdt_t *d;
  Data_Get_Struct(self, rhrdt_t, d);
  RHRDT_FILL_JD(d)
  return LONG2NUM(d->jd);
}

#jisx0301(*args) ⇒ Object

jisx0301(n=0) -> String

Returns the receiver as a String in JIS X 0301 format. If an argument is given, it should be an Integer representing the number of decimal places to use for the fractional seconds. Example:

Date.civil(2009, 1, 2, 3, 4, 5, 0.5).jisx0301
# => "H21.01.02T03:04:05+12:00"
Date.civil(2009, 1, 2, 3, 4, 5, 0.5).jisx0301(4)
# => "H21.01.02T03:04:05.0000+12:00"


2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
# File 'ext/date_ext/datetime.c', line 2293

static VALUE rhrdt_jisx0301(int argc, VALUE *argv, VALUE self) {
  VALUE s;
  rhrdt_t *d;
  int len;
  int i;
  char c;
  char * str;
  long year;
  Data_Get_Struct(self, rhrdt_t, d);
  RHRDT_FILL_CIVIL(d)
  RHRDT_FILL_JD(d)

  switch(argc) {
    case 1:
      i = NUM2LONG(argv[0]);
      break;
    case 0:
      i = 0;
      break;
    default:
      rb_raise(rb_eArgError, "wrong number of arguments: %i for 1", argc);
      break;
  }

  s = rb_str_buf_new(128);
  str = RSTRING_PTR(s); 

  if (d->jd < 2405160) {
    len = snprintf(str, 128, "%04li-%02i-%02i", d->year, (int)d->month, (int)d->day);
  } else {
    if (d->jd >= 2447535) {
      c = 'H';
      year = d->year - 1988;
    } else if (d->jd >= 2424875) {
      c = 'S';
      year = d->year - 1925;
    } else if (d->jd >= 2419614) {
      c = 'T';
      year = d->year - 1911;
    } else {
      c = 'M';
      year = d->year - 1867;
    }
    len = snprintf(RSTRING_PTR(s), 128, "%c%02li.%02i.%02i", c, year, (int)d->month, (int)d->day);
  }
  if (len == -1 || len > 127) {
    rb_raise(rb_eNoMemError, "in DateTime#jisx0301 (in snprintf)");
  }

  len = rhrdt__add_iso_time_format(d, str, len, i);
  RHR_RETURN_RESIZED_STR(s, len)
}

#ldInteger

Return the number of days since the Lilian Date (the day of calendar reform in Italy).

DateTime.civil(2009, 1, 2).ld
# => 155674

Returns:

  • (Integer)


1249
1250
1251
1252
1253
1254
# File 'ext/date_ext/datetime.c', line 1249

static VALUE rhrdt_ld(VALUE self) {
  rhrdt_t *d;
  Data_Get_Struct(self, rhrdt_t, d);
  RHRDT_FILL_JD(d)
  return LONG2NUM(d->jd - RHR_JD_LD);
}

#leap?Boolean

Return true if the current year for this date is a leap year in the Gregorian calendar, false otherwise.

DateTime.civil(2009, 1, 2).leap?
# => false
DateTime.civil(2008, 1, 2).leap?
# => true

Returns:

  • (Boolean)


1267
1268
1269
1270
1271
1272
# File 'ext/date_ext/datetime.c', line 1267

static VALUE rhrdt_leap_q(VALUE self) {
  rhrdt_t *d;
  Data_Get_Struct(self, rhrdt_t, d);
  RHRDT_FILL_CIVIL(d)
  return rhrd__leap_year(d->year) ? Qtrue : Qfalse;
}

#minInteger Also known as: minute

Returns the minute of the hour as an Integer. Example:

DateTime.civil(2009, 1, 2, 12, 13, 14).min
# => 13

Returns:

  • (Integer)


1282
1283
1284
1285
1286
1287
# File 'ext/date_ext/datetime.c', line 1282

static VALUE rhrdt_min(VALUE self) {
  rhrdt_t *dt;
  Data_Get_Struct(self, rhrdt_t, dt);
  RHRDT_FILL_HMS(dt)
  return LONG2NUM(dt->minute);
}

#mjdInteger

Return the number of days since 1858-11-17.

DateTime.civil(2009, 1, 2).mjd
# => 54833

Returns:

  • (Integer)


1297
1298
1299
1300
1301
1302
# File 'ext/date_ext/datetime.c', line 1297

static VALUE rhrdt_mjd(VALUE self) {
  rhrdt_t *d;
  Data_Get_Struct(self, rhrdt_t, d);
  RHRDT_FILL_JD(d)
  return LONG2NUM(d->jd - RHR_JD_MJD);
}

#monday?Boolean

monday?() -> true or false

Returns true if the receiver is a Monday, false otherwise.

Returns:

  • (Boolean)


2668
2669
2670
# File 'ext/date_ext/datetime.c', line 2668

static VALUE rhrdt_monday_q(VALUE self) {
  return rhrdt__day_q(self, 1);
}

#monthInteger Also known as: mon

Returns the number of the month as an Integer. Example:

DateTime.civil(2009, 1, 2).month
# => 1

Returns:

  • (Integer)


1312
1313
1314
1315
1316
1317
# File 'ext/date_ext/datetime.c', line 1312

static VALUE rhrdt_month(VALUE self) {
  rhrdt_t *dt;
  Data_Get_Struct(self, rhrdt_t, dt);
  RHRDT_FILL_CIVIL(dt)
  return LONG2NUM(dt->month);
}

#new_offsetDateTime Also known as: newof

Returns a DateTime with the same absolute time as the current time, but a potentially different local time. The returned value will be equal to the receiver. Raises ArgumentError if an invalid offset is specified. Example:

DateTime.civil(2009, 1, 2).new_offset(0.5)
# => #<DateTime 2009-01-02T12:00:00+12:00>
DateTime.civil(2009, 1, 2).new_offset(0.5)
# => #<DateTime 2009-01-01T12:00:00-12:00>

Returns:



1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
# File 'ext/date_ext/datetime.c', line 1333

static VALUE rhrdt_new_offset(int argc, VALUE *argv, VALUE self) {
  double offset;

  switch(argc) {
    case 0:
      offset = 0;
      break;
    case 1:
      if (RTEST(rb_obj_is_kind_of(argv[0], rb_cString))) {
        offset = NUM2LONG(rhrd_s_zone_to_diff(self, argv[0]))/RHR_SECONDS_PER_DAYD;
      } else {
        offset = NUM2DBL(argv[0]);
      }
      break;
    default:
      rb_raise(rb_eArgError, "wrong number of arguments: %i for 1", argc);
      break;
  }
  return rhrdt__new_offset(self, offset);
}

#nextDateTime Also known as: succ

Returns the DateTime after the receiver’s date. If the receiver has a fractional day component, the result will have the same fractional day component.

DateTime.civil(2009, 1, 2, 12).next
# => #<DateTime 2009-01-03T12:00:00+00:00>

Returns:



1364
1365
1366
# File 'ext/date_ext/datetime.c', line 1364

static VALUE rhrdt_next(VALUE self) {
   return rhrdt__add_days(self, 1.0);
}

#next_day(*args) ⇒ Object

next_day(n=1) -> DateTime

Returns a DateTime n days after the receiver. If n is negative, returns a DateTime before the receiver. The new DateTime is returned with the same fractional part and offset as the receiver.

DateTime.civil(2009, 1, 2, 12).next_day
# => #<DateTime 2009-01-03T12:00:00+00:00>
DateTime.civil(2009, 1, 2, 12).next_day(2)
# => #<DateTime 2009-01-04T12:00:00+00:00>


2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
# File 'ext/date_ext/datetime.c', line 2359

static VALUE rhrdt_next_day(int argc, VALUE *argv, VALUE self) {
  long i;

  switch(argc) {
    case 0:
      i = 1;
      break;
    case 1:
      i = NUM2LONG(argv[0]);
      break;
    default:
      rb_raise(rb_eArgError, "wrong number of arguments: %i for 1", argc);
      break;
  }

   return rhrdt__add_days(self, (double)i);
}

#next_month(*args) ⇒ Object

next_month(n=1) -> DateTime

Returns a DateTime n months after the receiver. If n is negative, returns a DateTime before the receiver. The new DateTime is returned with the same fractional part and offset as the receiver.

DateTime.civil(2009, 1, 2, 12).next_month
# => #<DateTime 2009-02-02T12:00:00+00:00>
DateTime.civil(2009, 1, 2, 12).next_month(2)
# => #<DateTime 2009-03-02T12:00:00+00:00>


2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
# File 'ext/date_ext/datetime.c', line 2390

static VALUE rhrdt_next_month(int argc, VALUE *argv, VALUE self) {
  long i;

  switch(argc) {
    case 0:
      i = 1;
      break;
    case 1:
      i = NUM2LONG(argv[0]);
      break;
    default:
      rb_raise(rb_eArgError, "wrong number of arguments: %i for 1", argc);
      break;
  }

  return rhrdt__add_months(self, i);
}

#next_year(*args) ⇒ Object

next_year(n=1) -> DateTime

Returns a DateTime n years after the receiver. If n is negative, returns a DateTime before the receiver. The new DateTime is returned with the same fractional part and offset as the receiver.

DateTime.civil(2009, 1, 2, 12).next_year
# => #<DateTime 2010-01-02T12:00:00+00:00>
DateTime.civil(2009, 1, 2, 12).next_year(2)
# => #<DateTime 2011-01-02T12:00:00+00:00>


2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
# File 'ext/date_ext/datetime.c', line 2421

static VALUE rhrdt_next_year(int argc, VALUE *argv, VALUE self) {
  long i;

  switch(argc) {
    case 0:
      i = 1;
      break;
    case 1:
      i = NUM2LONG(argv[0]);
      break;
    default:
      rb_raise(rb_eArgError, "wrong number of arguments: %i for 1", argc);
      break;
  }

  return rhrdt__add_years(self, i);
}

#offsetFloat Also known as: of

Returns a Float representing the offset from UTC as a fraction of the day, where 0.5 would be 12 hours ahead of UTC (“+12:00”), and -0.5 would be 12 hours behind UTC (“-12:00”).

DateTime.civil(2009, 1, 2, 12, 13, 14, -0.5).offset
# => -0.5

Returns:

  • (Float)


1378
1379
1380
1381
1382
1383
# File 'ext/date_ext/datetime.c', line 1378

static VALUE rhrdt_offset(VALUE self) {
  rhrdt_t *dt;
  RHR_CACHED_IV(self, rhrd_id_offset)
  Data_Get_Struct(self, rhrdt_t, dt);
  return rb_ivar_set(self, rhrd_id_offset, rb_float_new(dt->offset/1440.0));
}

#prev_day(*args) ⇒ Object

prev_day(n=1) -> DateTime

Returns a DateTime n days before the receiver. If n is negative, returns a DateTime after the receiver. The new DateTime is returned with the same fractional part and offset as the receiver.

DateTime.civil(2009, 1, 2, 12).prev_day
# => #<DateTime 2009-01-01T12:00:00+00:00>
DateTime.civil(2009, 1, 2, 12).prev_day(2)
# => #<DateTime 2008-12-31T12:00:00+00:00>


2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
# File 'ext/date_ext/datetime.c', line 2452

static VALUE rhrdt_prev_day(int argc, VALUE *argv, VALUE self) {
  long i;

  switch(argc) {
    case 0:
      i = -1;
      break;
    case 1:
      i = -NUM2LONG(argv[0]);
      break;
    default:
      rb_raise(rb_eArgError, "wrong number of arguments: %i for 1", argc);
      break;
  }

   return rhrdt__add_days(self, (double)i);
}

#prev_month(*args) ⇒ Object

prev_month(n=1) -> DateTime

Returns a DateTime n months before the receiver. If n is negative, returns a DateTime after the receiver. The new DateTime is returned with the same fractional part and offset as the receiver.

DateTime.civil(2009, 1, 2, 12).prev_month
# => #<DateTime 2008-12-02T12:00:00+00:00>
DateTime.civil(2009, 1, 2, 12).prev_month(2)
# => #<DateTime 2008-11-02T12:00:00+00:00>


2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
# File 'ext/date_ext/datetime.c', line 2483

static VALUE rhrdt_prev_month(int argc, VALUE *argv, VALUE self) {
  long i;

  switch(argc) {
    case 0:
      i = -1;
      break;
    case 1:
      i = -NUM2LONG(argv[0]);
      break;
    default:
      rb_raise(rb_eArgError, "wrong number of arguments: %i for 1", argc);
      break;
  }

  return rhrdt__add_months(self, i);
}

#prev_year(*args) ⇒ Object

prev_year(n=1) -> DateTime

Returns a DateTime n years before the receiver. If n is negative, returns a DateTime after the receiver. The new DateTime is returned with the same fractional part and offset as the receiver.

DateTime.civil(2009, 1, 2, 12).prev_year
# => #<DateTime 2008-01-02T12:00:00+00:00>
DateTime.civil(2009, 1, 2, 12).prev_year(2)
# => #<DateTime 2007-01-02T12:00:00+00:00>


2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
# File 'ext/date_ext/datetime.c', line 2514

static VALUE rhrdt_prev_year(int argc, VALUE *argv, VALUE self) {
  long i;

  switch(argc) {
    case 0:
      i = -1;
      break;
    case 1:
      i = -NUM2LONG(argv[0]);
      break;
    default:
      rb_raise(rb_eArgError, "wrong number of arguments: %i for 1", argc);
      break;
  }

  return rhrdt__add_years(self, i);
}

#rfc2822Object Also known as: rfc822

rfc2822() -> String

Returns the receiver as a String in RFC2822 format. Example:

DateTime.civil(2009, 1, 2, 3, 4, 5, 0.5).rfc2822
# => "Fri, 2 Jan 2009 03:04:05 +1200"


2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
# File 'ext/date_ext/datetime.c', line 2541

static VALUE rhrdt_rfc2822(VALUE self) {
  VALUE s;
  rhrdt_t *d;
  int len;
  Data_Get_Struct(self, rhrdt_t, d);
  RHRDT_FILL_CIVIL(d)
  RHRDT_FILL_JD(d)
  RHRDT_FILL_HMS(d)

  s = rb_str_buf_new(128);
  len = snprintf(RSTRING_PTR(s), 128, "%s, %i %s %04li %02i:%02i:%02i %+03i%02i", 
        rhrd__abbr_day_names[rhrd__jd_to_wday(d->jd)],
        (int)d->day,
        rhrd__abbr_month_names[d->month],
        d->year, (int)d->hour, (int)d->minute, (int)d->second, d->offset/60, abs(d->offset % 60));
  if (len == -1 || len > 127) {
    rb_raise(rb_eNoMemError, "in DateTime#rfc2822 (in snprintf)");
  }

  RHR_RETURN_RESIZED_STR(s, len)
}

#saturday?Boolean

saturday?() -> true or false

Returns true if the receiver is a Saturday, false otherwise.

Returns:

  • (Boolean)


2718
2719
2720
# File 'ext/date_ext/datetime.c', line 2718

static VALUE rhrdt_saturday_q(VALUE self) {
  return rhrdt__day_q(self, 6);
}

#secInteger Also known as: second

Returns the second of the minute as an Integer. Example:

DateTime.civil(2009, 1, 2, 12, 13, 14).sec
# => 14

Returns:

  • (Integer)


1393
1394
1395
1396
1397
1398
# File 'ext/date_ext/datetime.c', line 1393

static VALUE rhrdt_sec(VALUE self) {
  rhrdt_t *dt;
  Data_Get_Struct(self, rhrdt_t, dt);
  RHRDT_FILL_HMS(dt)
  return LONG2NUM(dt->second);
}

#sec_fractionFloat Also known as: second_fraction

On ruby 1.8, returns a Float representing the fraction of the second as a fraction of the day, which will always be in the range [0.0, 1/86400.0).

(DateTime.civil(2009, 1, 2, 12, 13, 14) + (1.5/86400)).sec_fraction
# => 0.000005787037

On ruby 1.9, returns a Float representing the fraction of the second, which will always be in the range [0,1).

(DateTime.civil(2009, 1, 2, 12, 13, 14) + (1.5/86400)).sec_fraction
# => 0.5

Returns:

  • (Float)


1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
# File 'ext/date_ext/datetime.c', line 1415

static VALUE rhrdt_sec_fraction(VALUE self) {
  rhrdt_t *dt;
  Data_Get_Struct(self, rhrdt_t, dt);
  RHRDT_FILL_NANOS(dt)
#ifdef RUBY19
  return rb_float_new((double)(dt->nanos % RHR_NANOS_PER_SECOND)/RHR_NANOS_PER_SECONDD);
#else
  return rb_float_new((double)(dt->nanos % RHR_NANOS_PER_SECOND)/RHR_NANOS_PER_DAYD);
#endif
}

#step(target, step = 1) {|datetime| ... } ⇒ DateTime

Yields DateTime objects between the receiver and the target date (inclusive), with step days between each yielded date. step may be a an Integer, in which case whole days are added, or it can be a Float, in which case fractional days are added. step can be negative, in which case the dates are yielded in reverse chronological order. Returns self in all cases.

If target is equal to the receiver, yields self once regardless of step. It target is less than receiver and step is nonnegative, or target is greater than receiver and step is nonpositive, does not yield.

DateTime.civil(2009, 1, 2).step(DateTime.civil(2009, 1, 6), 2) do |datetime|
  puts datetime
end
# Output:
# 2009-01-02T00:00:00+00:00
# 2009-01-04T00:00:00+00:00
# 2009-01-06T00:00:00+00:00
#

Yields:

  • (datetime)

Returns:



1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
# File 'ext/date_ext/datetime.c', line 1450

static VALUE rhrdt_step(int argc, VALUE *argv, VALUE self) {
  rhrdt_t *d, *ndt, *d0;
  rhrd_t *nd;
  double step, limit;
  long long step_nanos, limit_nanos, current_nanos;
  long step_jd, limit_jd, current_jd;
  VALUE rlimit, new, rstep, new_off, klass;
  new_off = rhrdt__new_offset(self, 0.0);
  Data_Get_Struct(self, rhrdt_t, d);
  Data_Get_Struct(new_off, rhrdt_t, d0);

  switch(argc) {
    case 1:
      step_nanos = 0;
      step_jd = 1;
      rstep = LONG2NUM(step_jd);
      break;
    case 2:
      rstep = argv[1];
      step = NUM2DBL(rstep);
      step_jd = (long)floor(step);
      step_nanos = llround((step - step_jd)*RHR_NANOS_PER_DAY);
      break;
    default:
      rb_raise(rb_eArgError, "wrong number of arguments: %i for 2", argc);
      break;
  }
  rlimit = argv[0];
  klass = rb_obj_class(self);

#ifdef RUBY19
  if (!rb_block_given_p()) {
    return rb_funcall(self, rhrd_id_to_enum, 3, rhrd_sym_step, rlimit, rstep);
  }
#else
  rb_need_block();
#endif

  if (RTEST(rb_obj_is_kind_of(rlimit, rb_cNumeric))) {
    limit = NUM2DBL(rlimit);
    limit_jd = (long)floor(limit);
    limit_nanos = llround((limit - limit_jd)*RHR_NANOS_PER_DAY);
  } else if (RTEST((rb_obj_is_kind_of(rlimit, rhrdt_class)))) {
    rlimit = rhrdt__new_offset(rlimit, 0.0);
    Data_Get_Struct(rlimit, rhrdt_t, ndt);
    RHRDT_FILL_JD(ndt)
    RHRDT_FILL_NANOS(ndt)
    limit_jd = ndt->jd; 
    limit_nanos = ndt->nanos;
  } else if (RTEST((rb_obj_is_kind_of(rlimit, rhrd_class)))) {
    Data_Get_Struct(rlimit, rhrd_t, nd);
    RHR_FILL_JD(nd)
    limit_jd = nd->jd; 
    limit_nanos = d->offset*RHR_NANOS_PER_MINUTE;
    if (limit_nanos < 0) {
      limit_jd--;
      limit_nanos += RHR_NANOS_PER_DAY;
    }
  } else {
    rb_raise(rb_eTypeError, "expected numeric or date");
  }

  current_jd = d0->jd;
  current_nanos = d0->nanos;
  new = rhrdt__from_jd_nanos(klass, current_jd, current_nanos, d->offset);
  if (limit_jd > current_jd || (limit_jd == current_jd && limit_nanos > current_nanos)) {
    if (step_jd > 0 || (step_jd == 0 && step_nanos > 0)) {
      while (limit_jd > current_jd || (limit_jd == current_jd && limit_nanos >= current_nanos)) {
        rb_yield(new);
        new = rhrdt__from_jd_nanos(klass, current_jd + step_jd, current_nanos + step_nanos, d->offset);
        Data_Get_Struct(new, rhrdt_t, ndt);
        current_jd = ndt->jd;
        current_nanos = ndt->nanos;
      }
    }
  } else if (limit_jd < current_jd || (limit_jd == current_jd && limit_nanos < current_nanos)) {
    if (step_jd < 0 || (step_jd == 0 && step_nanos < 0)) {
      while (limit_jd < current_jd || (limit_jd == current_jd && limit_nanos <= current_nanos)) {
        rb_yield(new);
        new = rhrdt__from_jd_nanos(klass, current_jd + step_jd, current_nanos + step_nanos, d->offset);
        Data_Get_Struct(new, rhrdt_t, ndt);
        current_jd = ndt->jd;
        current_nanos = ndt->nanos;
      }
    }
  } else {
    rb_yield(self);
  }

  return self;
}

#strftimeString <br /> #strftime(format) ⇒ String

If no argument is provided, returns a string in ISO8601 format, just like to_s. If an argument is provided, uses it as a format string and returns a String based on the format. See Date#strftime for the supported formats.

Overloads:

  • #strftimeString <br />

    Returns:

    • (String <br />)
  • #strftime(format) ⇒ String

    Returns:

    • (String)


1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
# File 'ext/date_ext/datetime.c', line 1551

static VALUE rhrdt_strftime(int argc, VALUE *argv, VALUE self) {
  rhrdt_t* dt;
  VALUE r;

  switch(argc) {
    case 0:
      return rhrdt_to_s(self);
    case 1:
      r = rb_str_to_str(argv[0]);
      break;
    default:
      rb_raise(rb_eArgError, "wrong number of arguments: %i for 1", argc);
      break;
  }

  Data_Get_Struct(self, rhrdt_t, dt);
  RHRDT_FILL_CIVIL(dt)
  RHRDT_FILL_JD(dt)
  RHRDT_FILL_HMS(dt)
  RHRDT_FILL_NANOS(dt)
  return rhrd__strftime(dt, RSTRING_PTR(r), RSTRING_LEN(r));
}

#sunday?Boolean

sunday?() -> true or false

Returns true if the receiver is a Sunday, false otherwise.

Returns:

  • (Boolean)


2658
2659
2660
# File 'ext/date_ext/datetime.c', line 2658

static VALUE rhrdt_sunday_q(VALUE self) {
  return rhrdt__day_q(self, 0);
}

#thursday?Boolean

thursday?() -> true or false

Returns true if the receiver is a Thursday, false otherwise.

Returns:

  • (Boolean)


2698
2699
2700
# File 'ext/date_ext/datetime.c', line 2698

static VALUE rhrdt_thursday_q(VALUE self) {
  return rhrdt__day_q(self, 4);
}

#to_dateObject

to_date() -> Date

Returns a Date with the same date as the receiver, ignoring any fractional parts or offsets.

DateTime.civil(2009, 1, 2, 12).to_date
# => #<Date 2009-01-02>


2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
# File 'ext/date_ext/datetime.c', line 2573

static VALUE rhrdt_to_date(VALUE self) {
  rhrd_t *d;
  rhrdt_t *dt;
  VALUE rd = Data_Make_Struct(rhrd_class, rhrd_t, NULL, -1, d);
  Data_Get_Struct(self, rhrdt_t, dt);

  if (RHR_HAS_CIVIL(dt)) {
    d->year = dt->year;
    d->month = dt->month;
    d->day = dt->day;
    d->flags |= RHR_HAVE_CIVIL;
  }
  if (RHR_HAS_JD(dt)) {
    d->jd = dt->jd;
    d->flags |= RHR_HAVE_JD;
  }

  return rd;
}

#to_sString

Returns the receiver as an ISO8601 formatted string.

DateTime.civil(2009, 1, 2, 12, 13, 14, 0.5).to_s
# => "2009-01-02T12:13:14+12:00"

Returns:

  • (String)


1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
# File 'ext/date_ext/datetime.c', line 1582

static VALUE rhrdt_to_s(VALUE self) {
  VALUE s;
  rhrdt_t *dt;
  int len;
  Data_Get_Struct(self, rhrdt_t, dt);
  RHRDT_FILL_CIVIL(dt)
  RHRDT_FILL_HMS(dt)

  s = rb_str_buf_new(128);
  len = snprintf(RSTRING_PTR(s), 128, "%04li-%02i-%02iT%02i:%02i:%02i%+03i:%02i",
        dt->year, (int)dt->month, (int)dt->day, (int)dt->hour, (int)dt->minute, (int)dt->second, dt->offset/60, abs(dt->offset % 60));
  if (len == -1 || len > 127) {
    rb_raise(rb_eNoMemError, "in DateTime#to_s (in snprintf)");
  }

  RHR_RETURN_RESIZED_STR(s, len)
}

#to_timeObject

to_time() -> Time

Returns a Time in local time with the same year, month, day, hour, minute, and second as the receiver (in absoute time).

DateTime.civil(2009, 1, 2, 5).to_time
# => 2009-01-01 21:00:00 -0800


2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
# File 'ext/date_ext/datetime.c', line 2603

static VALUE rhrdt_to_time(VALUE self) {
  long h, m, s;
  rhrdt_t *dt;
  Data_Get_Struct(self, rhrdt_t, dt);
  RHRDT_FILL_JD(dt)
  RHRDT_FILL_NANOS(dt)
  self = rhrdt__from_jd_nanos(rb_obj_class(self), dt->jd, dt->nanos - dt->offset * RHR_NANOS_PER_MINUTE, 0);
  Data_Get_Struct(self, rhrdt_t, dt);
  RHRDT_FILL_CIVIL(dt)
  RHRDT_FILL_HMS(dt)

  s = (long)(dt->nanos/RHR_NANOS_PER_SECOND);
  h = s/RHR_SECONDS_PER_HOUR;
  m = (s % RHR_SECONDS_PER_HOUR) / 60;
  return rb_funcall(rb_funcall(rb_cTime, rhrd_id_utc, 6, LONG2NUM(dt->year), LONG2NUM(dt->month), LONG2NUM(dt->day), LONG2NUM(h), LONG2NUM(m), rb_float_new(s % 60 + (double)(dt->nanos % RHR_NANOS_PER_SECOND)/RHR_NANOS_PER_SECONDD)), rhrd_id_localtime, 0);
}

#tuesday?Boolean

tuesday?() -> true or false

Returns true if the receiver is a Tuesday, false otherwise.

Returns:

  • (Boolean)


2678
2679
2680
# File 'ext/date_ext/datetime.c', line 2678

static VALUE rhrdt_tuesday_q(VALUE self) {
  return rhrdt__day_q(self, 2);
}

#upto(target) {|datetime| ... } ⇒ DateTime

Equivalent to calling step with the target as the first argument. Returns self.

DateTime.civil(2009, 1, 1).upto(DateTime.civil(2009, 1, 2)) do |datetime|
  puts datetime
end
# Output:
# 2009-01-01T00:00:00+00:00
# 2009-01-02T00:00:00+00:00

Yields:

  • (datetime)

Returns:



1612
1613
1614
1615
1616
# File 'ext/date_ext/datetime.c', line 1612

static VALUE rhrdt_upto(VALUE self, VALUE other) {
  VALUE argv[1];
  argv[0] = other;
  return rhrdt_step(1, argv, self);
}

#wdayInteger

Returns the day of the week as an Integer, where Sunday is 0 and Saturday is 6. Example:

DateTime.civil(2009, 1, 2).wday
# => 5

Returns:

  • (Integer)


1627
1628
1629
1630
1631
1632
# File 'ext/date_ext/datetime.c', line 1627

static VALUE rhrdt_wday(VALUE self) {
  rhrdt_t *d;
  Data_Get_Struct(self, rhrdt_t, d);
  RHRDT_FILL_JD(d)
  return LONG2NUM(rhrd__jd_to_wday(d->jd));
}

#wednesday?Boolean

wednesday?() -> true or false

Returns true if the receiver is a Wednesday, false otherwise.

Returns:

  • (Boolean)


2688
2689
2690
# File 'ext/date_ext/datetime.c', line 2688

static VALUE rhrdt_wednesday_q(VALUE self) {
  return rhrdt__day_q(self, 3);
}

#ydayInteger

Returns the day of the year as an Integer, where January 1st is 1 and December 31 is 365 (or 366 if the year is a leap year). Example:

DateTime.civil(2009, 2, 2).yday
# => 33

Returns:

  • (Integer)


1644
1645
1646
1647
1648
1649
# File 'ext/date_ext/datetime.c', line 1644

static VALUE rhrdt_yday(VALUE self) {
  rhrdt_t *d;
  Data_Get_Struct(self, rhrdt_t, d);
  RHRDT_FILL_CIVIL(d)
  return LONG2NUM(rhrd__ordinal_day(d->year, d->month, d->day));
}

#yearInteger

Returns the year as an Integer. Example:

DateTime.civil(2009, 1, 2).year
# => 2009

Returns:

  • (Integer)


1659
1660
1661
1662
1663
1664
# File 'ext/date_ext/datetime.c', line 1659

static VALUE rhrdt_year(VALUE self) {
  rhrdt_t *dt;
  Data_Get_Struct(self, rhrdt_t, dt);
  RHRDT_FILL_CIVIL(dt)
  return LONG2NUM(dt->year);
}

#zoneString

Returns the time zone as a formatted string. This always uses a numeric representation based on the offset, as DateTime instances do not keep information about named timezones.

DateTime.civil(2009, 1, 2, 12, 13, 14, 0.5).zone
# => "+12:00"

Returns:

  • (String)


1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
# File 'ext/date_ext/datetime.c', line 1676

static VALUE rhrdt_zone(VALUE self) {
  VALUE s;
  rhrdt_t *dt;
  int len;
  Data_Get_Struct(self, rhrdt_t, dt);

  s = rb_str_buf_new(128);
  len = snprintf(RSTRING_PTR(s), 128, "%+03i:%02i", dt->offset/60, abs(dt->offset % 60));
  if (len == -1 || len > 127) {
    rb_raise(rb_eNoMemError, "in DateTime#zone (in snprintf)");
  }

  RHR_RETURN_RESIZED_STR(s, len)
}