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:



1795
1796
1797
# File 'ext/date_ext/datetime.c', line 1795

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)


1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
# File 'ext/date_ext/datetime.c', line 1837

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:



1776
1777
1778
# File 'ext/date_ext/datetime.c', line 1776

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)


1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
# File 'ext/date_ext/datetime.c', line 1933

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)


1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
# File 'ext/date_ext/datetime.c', line 1887

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:



1760
1761
1762
# File 'ext/date_ext/datetime.c', line 1760

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)


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

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)


979
980
981
982
983
984
985
# File 'ext/date_ext/datetime.c', line 979

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)


993
994
995
996
997
998
999
# File 'ext/date_ext/datetime.c', line 993

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)


1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
# File 'ext/date_ext/datetime.c', line 1009

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)
}

#cloneDateTime

Returns a clone of the receiver.

Returns:



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

static VALUE rhrdt_clone(VALUE self) {
  rhrdt_t *d, *nd;
  VALUE rd = rb_call_super(0, NULL);
  Data_Get_Struct(self, rhrdt_t, d);
  Data_Get_Struct(rd, rhrdt_t, nd);
  memcpy(nd, d, sizeof(rhrdt_t));
  return rd;
}

#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)


1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
# File 'ext/date_ext/datetime.c', line 1055

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)


1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
# File 'ext/date_ext/datetime.c', line 1078

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)


1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
# File 'ext/date_ext/datetime.c', line 1101

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)


1122
1123
1124
1125
1126
1127
# File 'ext/date_ext/datetime.c', line 1122

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)


1141
1142
1143
1144
1145
1146
# File 'ext/date_ext/datetime.c', line 1141

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:



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

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

#dupDateTime

Returns a dup of the receiver.

Returns:



1153
1154
1155
1156
1157
1158
1159
1160
# File 'ext/date_ext/datetime.c', line 1153

static VALUE rhrdt_dup(VALUE self) {
  rhrdt_t *d, *nd;
  VALUE rd = rb_call_super(0, NULL);
  Data_Get_Struct(self, rhrdt_t, d);
  Data_Get_Struct(rd, rhrdt_t, nd);
  memcpy(nd, d, sizeof(rhrdt_t));
  return rd;
}

#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)


1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
# File 'ext/date_ext/datetime.c', line 1199

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)


2763
2764
2765
# File 'ext/date_ext/datetime.c', line 2763

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)


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

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)


1247
1248
1249
1250
1251
1252
# File 'ext/date_ext/datetime.c', line 1247

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"


2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
# File 'ext/date_ext/datetime.c', line 2264

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)


1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
# File 'ext/date_ext/datetime.c', line 1263

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"


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
# File 'ext/date_ext/datetime.c', line 2301

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)


1289
1290
1291
1292
1293
1294
# File 'ext/date_ext/datetime.c', line 1289

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"


2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
# File 'ext/date_ext/datetime.c', line 2348

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)


1305
1306
1307
1308
1309
1310
# File 'ext/date_ext/datetime.c', line 1305

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)


1323
1324
1325
1326
1327
1328
# File 'ext/date_ext/datetime.c', line 1323

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)


1338
1339
1340
1341
1342
1343
# File 'ext/date_ext/datetime.c', line 1338

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)


1353
1354
1355
1356
1357
1358
# File 'ext/date_ext/datetime.c', line 1353

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)


2723
2724
2725
# File 'ext/date_ext/datetime.c', line 2723

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)


1368
1369
1370
1371
1372
1373
# File 'ext/date_ext/datetime.c', line 1368

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:



1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
# File 'ext/date_ext/datetime.c', line 1389

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

  switch(argc) {
    case 0:
      offset = 0;
      break;
    case 1:
      offset= rhrdt__constructor_offset(rb_obj_class(self), 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:



1416
1417
1418
# File 'ext/date_ext/datetime.c', line 1416

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>


2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
# File 'ext/date_ext/datetime.c', line 2414

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>


2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
# File 'ext/date_ext/datetime.c', line 2445

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>


2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
# File 'ext/date_ext/datetime.c', line 2476

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)


1430
1431
1432
1433
1434
1435
# File 'ext/date_ext/datetime.c', line 1430

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>


2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
# File 'ext/date_ext/datetime.c', line 2507

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>


2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
# File 'ext/date_ext/datetime.c', line 2538

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>


2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
# File 'ext/date_ext/datetime.c', line 2569

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"


2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
# File 'ext/date_ext/datetime.c', line 2596

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)


2773
2774
2775
# File 'ext/date_ext/datetime.c', line 2773

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)


1445
1446
1447
1448
1449
1450
# File 'ext/date_ext/datetime.c', line 1445

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)


1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
# File 'ext/date_ext/datetime.c', line 1467

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:



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
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
# File 'ext/date_ext/datetime.c', line 1502

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);
      if (step_jd == 0 && step_nanos == 0) {
        rb_raise(rb_eArgError, "step can't be 0");
      }
      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)


1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
# File 'ext/date_ext/datetime.c', line 1606

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)


2713
2714
2715
# File 'ext/date_ext/datetime.c', line 2713

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)


2753
2754
2755
# File 'ext/date_ext/datetime.c', line 2753

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>


2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
# File 'ext/date_ext/datetime.c', line 2628

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)


1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
# File 'ext/date_ext/datetime.c', line 1637

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


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

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)


2733
2734
2735
# File 'ext/date_ext/datetime.c', line 2733

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:



1667
1668
1669
1670
1671
# File 'ext/date_ext/datetime.c', line 1667

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)


1682
1683
1684
1685
1686
1687
# File 'ext/date_ext/datetime.c', line 1682

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)


2743
2744
2745
# File 'ext/date_ext/datetime.c', line 2743

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)


1699
1700
1701
1702
1703
1704
# File 'ext/date_ext/datetime.c', line 1699

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)


1714
1715
1716
1717
1718
1719
# File 'ext/date_ext/datetime.c', line 1714

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)


1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
# File 'ext/date_ext/datetime.c', line 1731

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)
}