Class: DateTime
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
-
#+(n) ⇒ DateTime
Returns a
DateTime
that isn
days after the receiver. -
#-(other) ⇒ Object
If a
Numeric
argument is given, it is treated as anFloat
, and the number of days it represents is substracted from the receiver to return a newDateTime
object. -
#<<(n) ⇒ DateTime
Returns a
DateTime
that isn
months before the receiver. -
#===(other) ⇒ Boolean
If
other
is aDate
, returnstrue
ifother
is the same date as the receiver, orfalse
otherwise. -
#>>(n) ⇒ DateTime
Returns a
DateTime
that isn
months after the receiver. -
#_dump(limit) ⇒ String
Returns a marshalled representation of the receiver as a
String
. -
#ajd ⇒ Float
Returns the date and time represented by the receiver as a astronomical julian day
Float
. -
#amjd ⇒ Float
Returns the date and time represented by the receiver as a astronomical modified julian day
Float
. -
#asctime ⇒ String
(also: #ctime)
Returns a string representation of the receiver.
-
#clone ⇒ DateTime
Returns a clone of the receiver.
-
#cwday ⇒ Integer
Returns the commercial week day as an
Integer
. -
#cweek ⇒ Integer
Returns the commercial week as an
Integer
. -
#cwyear ⇒ Integer
Returns the commercial week year as an
Integer
. -
#day ⇒ Integer
(also: #mday)
Returns the day of the month as an
Integer
. -
#day_fraction ⇒ Float
Returns the fraction of the day as a
Float
. -
#downto(target) {|datetime| ... } ⇒ DateTime
Equivalent to calling
step
with thetarget
as the first argument and-1
as the second argument. -
#dup ⇒ DateTime
Returns a dup of the receiver.
-
#eql?(datetime) ⇒ Boolean
Returns true only if the
datetime
given is the same date and time as the receiver. -
#friday? ⇒ Boolean
friday?() -> true or false.
-
#hash ⇒ Integer
Return an
Integer
hash value for the receiver, such that an equal date and time will have the same hash value. -
#hour ⇒ Integer
Returns the hour of the day as an
Integer
. -
#httpdate ⇒ Object
httpdate() -> String.
-
#inspect ⇒ String
Return a developer-friendly string containing the civil date and time for the receiver.
-
#iso8601(*args) ⇒ Object
(also: #rfc3339, #xmlschema)
iso8601(n=0) -> String.
-
#jd ⇒ Integer
Return the julian day number for the receiver as an
Integer
. -
#jisx0301(*args) ⇒ Object
jisx0301(n=0) -> String.
-
#ld ⇒ Integer
Return the number of days since the Lilian Date (the day of calendar reform in Italy).
-
#leap? ⇒ Boolean
Return
true
if the current year for this date is a leap year in the Gregorian calendar,false
otherwise. -
#min ⇒ Integer
(also: #minute)
Returns the minute of the hour as an
Integer
. -
#mjd ⇒ Integer
Return the number of days since 1858-11-17.
-
#monday? ⇒ Boolean
monday?() -> true or false.
-
#month ⇒ Integer
(also: #mon)
Returns the number of the month as an
Integer
. -
#new_offset ⇒ DateTime
(also: #newof)
Returns a
DateTime
with the same absolute time as the current time, but a potentially different local time. -
#next ⇒ DateTime
(also: #succ)
Returns the
DateTime
after the receiver’s date. -
#next_day(*args) ⇒ Object
next_day(n=1) -> DateTime.
-
#next_month(*args) ⇒ Object
next_month(n=1) -> DateTime.
-
#next_year(*args) ⇒ Object
next_year(n=1) -> DateTime.
-
#offset ⇒ Float
(also: #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”). -
#prev_day(*args) ⇒ Object
prev_day(n=1) -> DateTime.
-
#prev_month(*args) ⇒ Object
prev_month(n=1) -> DateTime.
-
#prev_year(*args) ⇒ Object
prev_year(n=1) -> DateTime.
-
#rfc2822 ⇒ Object
(also: #rfc822)
rfc2822() -> String.
-
#saturday? ⇒ Boolean
saturday?() -> true or false.
-
#sec ⇒ Integer
(also: #second)
Returns the second of the minute as an
Integer
. -
#sec_fraction ⇒ Float
(also: #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). -
#step(target, step = 1) {|datetime| ... } ⇒ DateTime
Yields
DateTime
objects between the receiver and thetarget
date (inclusive), withstep
days between each yielded date. -
#strftime(*args) ⇒ Object
If no argument is provided, returns a string in ISO8601 format, just like
to_s
. -
#sunday? ⇒ Boolean
sunday?() -> true or false.
-
#thursday? ⇒ Boolean
thursday?() -> true or false.
-
#to_date ⇒ Object
to_date() -> Date.
-
#to_s ⇒ String
Returns the receiver as an ISO8601 formatted string.
-
#to_time ⇒ Object
to_time() -> Time.
-
#tuesday? ⇒ Boolean
tuesday?() -> true or false.
-
#upto(target) {|datetime| ... } ⇒ DateTime
Equivalent to calling
step
with thetarget
as the first argument. -
#wday ⇒ Integer
Returns the day of the week as an
Integer
, where Sunday is 0 and Saturday is 6. -
#wednesday? ⇒ Boolean
wednesday?() -> true or false.
-
#yday ⇒ Integer
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). -
#year ⇒ Integer
Returns the year as an
Integer
. -
#zone ⇒ String
Returns the time zone as a formatted string.
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>
1829 1830 1831 |
# File 'ext/date_ext/datetime.c', line 1829
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
1871 1872 1873 1874 1875 1876 1877 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 |
# File 'ext/date_ext/datetime.c', line 1871
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
1810 1811 1812 |
# File 'ext/date_ext/datetime.c', line 1810
static VALUE rhrdt_op_left_shift(VALUE self, VALUE other) {
return rhrdt__add_months(self, -NUM2LONG(other));
}
|
#===(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.
1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 |
# File 'ext/date_ext/datetime.c', line 1921
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
1794 1795 1796 |
# File 'ext/date_ext/datetime.c', line 1794
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
.
999 1000 1001 1002 1003 1004 1005 |
# File 'ext/date_ext/datetime.c', line 999
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));
}
|
#ajd ⇒ Float
Returns the date and time represented by the receiver as a astronomical julian day Float
.
1013 1014 1015 1016 1017 1018 1019 |
# File 'ext/date_ext/datetime.c', line 1013
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);
}
|
#amjd ⇒ Float
Returns the date and time represented by the receiver as a astronomical modified julian day Float
.
1027 1028 1029 1030 1031 1032 1033 |
# File 'ext/date_ext/datetime.c', line 1027
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);
}
|
#asctime ⇒ String 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"
1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 |
# File 'ext/date_ext/datetime.c', line 1043
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 %2d %02d:%02d:%02d %04ld",
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)
}
|
#clone ⇒ DateTime
Returns a clone of the receiver.
1070 1071 1072 1073 1074 1075 1076 1077 |
# File 'ext/date_ext/datetime.c', line 1070 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; } |
#cwday ⇒ Integer
1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 |
# File 'ext/date_ext/datetime.c', line 1089
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);
}
|
#cweek ⇒ Integer
1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 |
# File 'ext/date_ext/datetime.c', line 1112
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);
}
|
#cwyear ⇒ Integer
1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 |
# File 'ext/date_ext/datetime.c', line 1135
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);
}
|
#day ⇒ Integer Also known as: mday
Returns the day of the month as an Integer
. Example:
DateTime.civil(2009, 1, 2).day
# => 2
1156 1157 1158 1159 1160 1161 |
# File 'ext/date_ext/datetime.c', line 1156
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_fraction ⇒ Float
1175 1176 1177 1178 1179 1180 |
# File 'ext/date_ext/datetime.c', line 1175
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
1209 1210 1211 1212 1213 1214 |
# File 'ext/date_ext/datetime.c', line 1209
static VALUE rhrdt_downto(VALUE self, VALUE other) {
VALUE argv[2];
argv[0] = other;
argv[1] = INT2FIX(-1);
return rhrdt_step(2, argv, self);
}
|
#dup ⇒ DateTime
Returns a dup of the receiver.
1187 1188 1189 1190 1191 1192 1193 1194 |
# File 'ext/date_ext/datetime.c', line 1187 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
1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 |
# File 'ext/date_ext/datetime.c', line 1233
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.
2736 2737 2738 |
# File 'ext/date_ext/datetime.c', line 2736 static VALUE rhrdt_friday_q(VALUE self) { return rhrdt__day_q(self, 5); } |
#hash ⇒ Integer
Return an Integer
hash value for the receiver, such that an equal date and time will have the same hash value.
1265 1266 1267 1268 1269 1270 1271 |
# File 'ext/date_ext/datetime.c', line 1265
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));
}
|
#hour ⇒ Integer
Returns the hour of the day as an Integer
. Example:
DateTime.civil(2009, 1, 2, 12, 13, 14).hour
# => 12
1281 1282 1283 1284 1285 1286 |
# File 'ext/date_ext/datetime.c', line 1281
static VALUE rhrdt_hour(VALUE self) {
rhrdt_t *dt;
Data_Get_Struct(self, rhrdt_t, dt);
RHRDT_FILL_HMS(dt)
return LONG2NUM(dt->hour);
}
|
#httpdate ⇒ Object
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"
2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 |
# File 'ext/date_ext/datetime.c', line 2237
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, %02d %s %04ld %02d:%02d:%02d 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)
}
|
#inspect ⇒ String
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>"
1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 |
# File 'ext/date_ext/datetime.c', line 1297
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 %04ld-%02d-%02dT%02d:%02d:%02d%+03d:%02d>",
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"
2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 |
# File 'ext/date_ext/datetime.c', line 2274
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: %d for 1", argc);
break;
}
s = rb_str_buf_new(128);
str = RSTRING_PTR(s);
len = snprintf(str, 128, "%04ld-%02d-%02d", 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)
}
|
#jd ⇒ Integer
Return the julian day number for the receiver as an Integer
.
DateTime.civil(2009, 1, 2).jd
# => 2454834
1323 1324 1325 1326 1327 1328 |
# File 'ext/date_ext/datetime.c', line 1323
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"
2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 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 |
# File 'ext/date_ext/datetime.c', line 2321
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: %d for 1", argc);
break;
}
s = rb_str_buf_new(128);
str = RSTRING_PTR(s);
if (d->jd < 2405160) {
len = snprintf(str, 128, "%04ld-%02d-%02d", 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%02ld.%02d.%02d", 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)
}
|
#ld ⇒ Integer
Return the number of days since the Lilian Date (the day of calendar reform in Italy).
DateTime.civil(2009, 1, 2).ld
# => 155674
1339 1340 1341 1342 1343 1344 |
# File 'ext/date_ext/datetime.c', line 1339
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
1357 1358 1359 1360 1361 1362 |
# File 'ext/date_ext/datetime.c', line 1357
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;
}
|
#min ⇒ Integer Also known as: minute
Returns the minute of the hour as an Integer
. Example:
DateTime.civil(2009, 1, 2, 12, 13, 14).min
# => 13
1372 1373 1374 1375 1376 1377 |
# File 'ext/date_ext/datetime.c', line 1372
static VALUE rhrdt_min(VALUE self) {
rhrdt_t *dt;
Data_Get_Struct(self, rhrdt_t, dt);
RHRDT_FILL_HMS(dt)
return LONG2NUM(dt->minute);
}
|
#mjd ⇒ Integer
Return the number of days since 1858-11-17.
DateTime.civil(2009, 1, 2).mjd
# => 54833
1387 1388 1389 1390 1391 1392 |
# File 'ext/date_ext/datetime.c', line 1387
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.
2696 2697 2698 |
# File 'ext/date_ext/datetime.c', line 2696 static VALUE rhrdt_monday_q(VALUE self) { return rhrdt__day_q(self, 1); } |
#month ⇒ Integer Also known as: mon
Returns the number of the month as an Integer
. Example:
DateTime.civil(2009, 1, 2).month
# => 1
1402 1403 1404 1405 1406 1407 |
# File 'ext/date_ext/datetime.c', line 1402
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_offset ⇒ DateTime 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>
1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 |
# File 'ext/date_ext/datetime.c', line 1423
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: %d for 1", argc);
break;
}
return rhrdt__new_offset(self, offset);
}
|
#next ⇒ DateTime 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>
1450 1451 1452 |
# File 'ext/date_ext/datetime.c', line 1450 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>
2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 |
# File 'ext/date_ext/datetime.c', line 2387
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: %d 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>
2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 |
# File 'ext/date_ext/datetime.c', line 2418
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: %d 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>
2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 |
# File 'ext/date_ext/datetime.c', line 2449
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: %d for 1", argc);
break;
}
return rhrdt__add_years(self, i);
}
|
#offset ⇒ Float 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
1464 1465 1466 1467 1468 1469 |
# File 'ext/date_ext/datetime.c', line 1464
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>
2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 |
# File 'ext/date_ext/datetime.c', line 2480
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: %d 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>
2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 |
# File 'ext/date_ext/datetime.c', line 2511
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: %d 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>
2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 |
# File 'ext/date_ext/datetime.c', line 2542
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: %d for 1", argc);
break;
}
return rhrdt__add_years(self, i);
}
|
#rfc2822 ⇒ Object 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"
2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 |
# File 'ext/date_ext/datetime.c', line 2569
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, %d %s %04ld %02d:%02d:%02d %+03d%02d",
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.
2746 2747 2748 |
# File 'ext/date_ext/datetime.c', line 2746 static VALUE rhrdt_saturday_q(VALUE self) { return rhrdt__day_q(self, 6); } |
#sec ⇒ Integer Also known as: second
Returns the second of the minute as an Integer
. Example:
DateTime.civil(2009, 1, 2, 12, 13, 14).sec
# => 14
1479 1480 1481 1482 1483 1484 |
# File 'ext/date_ext/datetime.c', line 1479
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_fraction ⇒ Float 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
1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 |
# File 'ext/date_ext/datetime.c', line 1501
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
#
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 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 |
# File 'ext/date_ext/datetime.c', line 1536
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: %d 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;
}
|
#strftime ⇒ String <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.
1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 |
# File 'ext/date_ext/datetime.c', line 1640
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: %d 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.
2686 2687 2688 |
# File 'ext/date_ext/datetime.c', line 2686 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.
2726 2727 2728 |
# File 'ext/date_ext/datetime.c', line 2726 static VALUE rhrdt_thursday_q(VALUE self) { return rhrdt__day_q(self, 4); } |
#to_date ⇒ Object
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>
2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 |
# File 'ext/date_ext/datetime.c', line 2601
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_s ⇒ String
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"
1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 |
# File 'ext/date_ext/datetime.c', line 1671
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, "%04ld-%02d-%02dT%02d:%02d:%02d%+03d:%02d",
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_time ⇒ Object
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
2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 |
# File 'ext/date_ext/datetime.c', line 2631
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.
2706 2707 2708 |
# File 'ext/date_ext/datetime.c', line 2706 static VALUE rhrdt_tuesday_q(VALUE self) { return rhrdt__day_q(self, 2); } |
#upto(target) {|datetime| ... } ⇒ DateTime
1701 1702 1703 1704 1705 |
# File 'ext/date_ext/datetime.c', line 1701
static VALUE rhrdt_upto(VALUE self, VALUE other) {
VALUE argv[1];
argv[0] = other;
return rhrdt_step(1, argv, self);
}
|
#wday ⇒ Integer
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
1716 1717 1718 1719 1720 1721 |
# File 'ext/date_ext/datetime.c', line 1716
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.
2716 2717 2718 |
# File 'ext/date_ext/datetime.c', line 2716 static VALUE rhrdt_wednesday_q(VALUE self) { return rhrdt__day_q(self, 3); } |
#yday ⇒ Integer
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
1733 1734 1735 1736 1737 1738 |
# File 'ext/date_ext/datetime.c', line 1733
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));
}
|
#year ⇒ Integer
Returns the year as an Integer
. Example:
DateTime.civil(2009, 1, 2).year
# => 2009
1748 1749 1750 1751 1752 1753 |
# File 'ext/date_ext/datetime.c', line 1748
static VALUE rhrdt_year(VALUE self) {
rhrdt_t *dt;
Data_Get_Struct(self, rhrdt_t, dt);
RHRDT_FILL_CIVIL(dt)
return LONG2NUM(dt->year);
}
|
#zone ⇒ String
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"
1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 |
# File 'ext/date_ext/datetime.c', line 1765
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, "%+03d:%02d", 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)
}
|