Class: DateTime
- Defined in:
- ext/date/date_core.c,
ext/json/lib/json/add/date_time.rb
Constant Summary
Constants inherited from Date
Date::ABBR_DAYNAMES, Date::ABBR_MONTHNAMES, Date::DAYNAMES, Date::ENGLAND, Date::GREGORIAN, Date::ITALY, Date::JULIAN, Date::MONTHNAMES, Date::VERSION
Class Method Summary collapse
-
._strptime(string[, format = '%FT%T%z']) ⇒ Hash
Parses the given representation of date and time with the given template, and returns a hash of parsed elements.
-
.civil(*args) ⇒ Object
Same as DateTime.new.
-
.commercial([cwyear = -4712[, cweek=1[, cwday=1[, hour=0[, minute=0[, second=0[, offset=0[, start=Date::ITALY]]]]]]]]) ⇒ Object
Creates a DateTime object denoting the given week date.
-
.httpdate(string = 'Mon, 01 Jan -4712 00:00:00 GMT'[, start=Date::ITALY]) ⇒ Object
Creates a new DateTime object by parsing from a string according to some RFC 2616 format.
-
.iso8601(string = '-4712-01-01T00:00:00+00:00'[, start=Date::ITALY], limit: 128) ⇒ Object
Creates a new DateTime object by parsing from a string according to some typical ISO 8601 formats.
-
.jd([jd = 0[, hour=0[, minute=0[, second=0[, offset=0[, start=Date::ITALY]]]]]]) ⇒ Object
Creates a DateTime object denoting the given chronological Julian day number.
-
.jisx0301(string = '-4712-01-01T00:00:00+00:00'[, start=Date::ITALY], limit: 128) ⇒ Object
Creates a new DateTime object by parsing from a string according to some typical JIS X 0301 formats.
-
.json_create(object) ⇒ Object
See #as_json.
-
.new(*args) ⇒ Object
Same as DateTime.new.
-
.now([start = Date::ITALY]) ⇒ Object
Creates a DateTime object denoting the present time.
-
.nth_kday(*args) ⇒ Object
:nodoc:.
-
.ordinal([year = -4712[, yday=1[, hour=0[, minute=0[, second=0[, offset=0[, start=Date::ITALY]]]]]]]) ⇒ Object
Creates a DateTime object denoting the given ordinal date.
-
.parse(string = '-4712-01-01T00:00:00+00:00'[, comp=true[, start=Date::ITALY]], limit: 128) ⇒ Object
Parses the given representation of date and time, and creates a DateTime object.
-
.rfc2822(*args) ⇒ Object
Creates a new DateTime object by parsing from a string according to some typical RFC 2822 formats.
-
.rfc3339(string = '-4712-01-01T00:00:00+00:00'[, start=Date::ITALY], limit: 128) ⇒ Object
Creates a new DateTime object by parsing from a string according to some typical RFC 3339 formats.
-
.rfc822(*args) ⇒ Object
Creates a new DateTime object by parsing from a string according to some typical RFC 2822 formats.
-
.strptime([string = '-4712-01-01T00:00:00+00:00'[, format='%FT%T%z'[ ,start=Date::ITALY]]]) ⇒ Object
Parses the given representation of date and time with the given template, and creates a DateTime object.
-
.weeknum(*args) ⇒ Object
:nodoc:.
-
.xmlschema(string = '-4712-01-01T00:00:00+00:00'[, start=Date::ITALY], limit: 128) ⇒ Object
Creates a new DateTime object by parsing from a string according to some typical XML Schema formats.
Instance Method Summary collapse
-
#as_json ⇒ Object
Methods
DateTime#as_json
andDateTime.json_create
may be used to serialize and deserialize a DateTime object; see Marshal. -
#deconstruct_keys(array_of_names_or_nil) ⇒ Hash
Returns a hash of the name/value pairs, to use in pattern matching.
-
#hour ⇒ Integer
Returns the hour in range (0..23):.
-
#iso8601(*args) ⇒ Object
This method is equivalent to strftime(‘%FT%T%:z’).
-
#jisx0301([n = 0]) ⇒ String
Returns a string in a JIS X 0301 format.
-
#min ⇒ Integer
Returns the minute in range (0..59):.
-
#min ⇒ Integer
Returns the minute in range (0..59):.
-
#new_offset([offset = 0]) ⇒ Object
Duplicates self and resets its offset.
-
#offset ⇒ Object
Returns the offset.
-
#rfc3339([n = 0]) ⇒ String
This method is equivalent to strftime(‘%FT%T%:z’).
-
#sec ⇒ Integer
Returns the second in range (0..59):.
-
#sec_fraction ⇒ Object
Returns the fractional part of the second in range (Rational(0, 1)…Rational(1, 1)):.
-
#sec ⇒ Integer
Returns the second in range (0..59):.
-
#sec_fraction ⇒ Object
Returns the fractional part of the second in range (Rational(0, 1)…Rational(1, 1)):.
-
#strftime(format = '%FT%T%:z') ⇒ String
Returns a string representation of
self
, formatted according the given +format:. -
#to_date ⇒ Object
Returns a Date object which denotes self.
-
#to_datetime ⇒ self
Returns self.
-
#to_json(*args) ⇒ Object
Returns a JSON string representing
self
:. -
#to_s ⇒ String
Returns a string in an ISO 8601 format.
-
#to_time ⇒ Time
Returns a Time object which denotes self.
-
#xmlschema(*args) ⇒ Object
This method is equivalent to strftime(‘%FT%T%:z’).
-
#zone ⇒ String
Returns the timezone.
Methods inherited from Date
#+, #-, #<<, #<=>, #===, #>>, _httpdate, _iso8601, _jisx0301, _load, _parse, _rfc2822, _rfc3339, _rfc822, _xmlschema, #ajd, #amjd, #asctime, #ctime, #cwday, #cweek, #cwyear, #day, #day_fraction, #downto, #england, #eql?, #fill, #friday?, #gregorian, #gregorian?, gregorian_leap?, #hash, #httpdate, #infinite?, #initialize, #initialize_copy, #inspect, #inspect_raw, #italy, #jd, #julian, #julian?, julian_leap?, #ld, #leap?, leap?, #marshal_dump, #marshal_dump_old, #marshal_load, #mday, #mjd, #mon, #monday?, #month, new!, #new_start, #next, #next_day, #next_month, #next_year, #nth_kday?, #prev_day, #prev_month, #prev_year, #rfc2822, #rfc822, #saturday?, #step, #succ, #sunday?, test_all, test_civil, test_commercial, test_nth_kday, test_ordinal, test_unit_conv, test_weeknum, #thursday?, today, #tuesday?, #upto, valid_civil?, valid_commercial?, valid_date?, valid_jd?, valid_ordinal?, #wday, #wednesday?, #yday, #year
Constructor Details
This class inherits a constructor from Date
Class Method Details
._strptime(string[, format = '%FT%T%z']) ⇒ Hash
Parses the given representation of date and time with the given template, and returns a hash of parsed elements. _strptime does not support specification of flags and width unlike strftime.
See also strptime(3) and #strftime.
8340 8341 8342 8343 8344 |
# File 'ext/date/date_core.c', line 8340
static VALUE
datetime_s__strptime(int argc, VALUE *argv, VALUE klass)
{
return date_s__strptime_internal(argc, argv, klass, "%FT%T%z");
}
|
.civil(*args) ⇒ Object
Same as DateTime.new.
7802 7803 7804 7805 7806 |
# File 'ext/date/date_core.c', line 7802
static VALUE
datetime_s_civil(int argc, VALUE *argv, VALUE klass)
{
return datetime_initialize(argc, argv, d_lite_s_alloc_complex(klass));
}
|
.commercial([cwyear = -4712[, cweek=1[, cwday=1[, hour=0[, minute=0[, second=0[, offset=0[, start=Date::ITALY]]]]]]]]) ⇒ Object
7917 7918 7919 7920 7921 7922 7923 7924 7925 7926 7927 7928 7929 7930 7931 7932 7933 7934 7935 7936 7937 7938 7939 7940 7941 7942 7943 7944 7945 7946 7947 7948 7949 7950 7951 7952 7953 7954 7955 7956 7957 7958 7959 7960 7961 7962 7963 7964 7965 7966 7967 7968 7969 7970 7971 7972 7973 7974 7975 7976 7977 7978 7979 7980 7981 7982 7983 7984 7985 7986 7987 |
# File 'ext/date/date_core.c', line 7917
static VALUE
datetime_s_commercial(int argc, VALUE *argv, VALUE klass)
{
VALUE vy, vw, vd, vh, vmin, vs, vof, vsg, y, fr, fr2, ret;
int w, d, h, min, s, rof;
double sg;
rb_scan_args(argc, argv, "08", &vy, &vw, &vd, &vh, &vmin, &vs, &vof, &vsg);
y = INT2FIX(-4712);
w = 1;
d = 1;
h = min = s = 0;
fr2 = INT2FIX(0);
rof = 0;
sg = DEFAULT_SG;
switch (argc) {
case 8:
val2sg(vsg, sg);
case 7:
val2off(vof, rof);
case 6:
check_numeric(vs, "second");
num2int_with_frac(s, positive_inf);
case 5:
check_numeric(vmin, "minute");
num2int_with_frac(min, 5);
case 4:
check_numeric(vh, "hour");
num2int_with_frac(h, 4);
case 3:
check_numeric(vd, "cwday");
num2int_with_frac(d, 3);
case 2:
check_numeric(vw, "cweek");
w = NUM2INT(vw);
case 1:
check_numeric(vy, "year");
y = vy;
}
{
VALUE nth;
int ry, rw, rd, rh, rmin, rs, rjd, rjd2, ns;
if (!valid_commercial_p(y, w, d, sg,
&nth, &ry,
&rw, &rd, &rjd,
&ns))
rb_raise(eDateError, "invalid date");
if (!c_valid_time_p(h, min, s, &rh, &rmin, &rs))
rb_raise(eDateError, "invalid date");
canon24oc();
rjd2 = jd_local_to_utc(rjd,
time_to_df(rh, rmin, rs),
rof);
ret = d_complex_new_internal(klass,
nth, rjd2,
0, INT2FIX(0),
rof, sg,
0, 0, 0,
rh, rmin, rs,
HAVE_JD | HAVE_TIME);
}
add_frac();
return ret;
}
|
.httpdate(string = 'Mon, 01 Jan -4712 00:00:00 GMT'[, start=Date::ITALY]) ⇒ Object
Creates a new DateTime object by parsing from a string according to some RFC 2616 format.
DateTime.httpdate('Sat, 03 Feb 2001 04:05:06 GMT')
#=> #<DateTime: 2001-02-03T04:05:06+00:00 …>
Raise an ArgumentError when the string length is longer than limit. You can stop this check by passing limit: nil
, but note that it may take a long time to parse.
8633 8634 8635 8636 8637 8638 8639 8640 8641 8642 8643 8644 8645 8646 8647 8648 8649 8650 8651 8652 8653 8654 8655 8656 8657 |
# File 'ext/date/date_core.c', line 8633
static VALUE
datetime_s_httpdate(int argc, VALUE *argv, VALUE klass)
{
VALUE str, sg, opt;
rb_scan_args(argc, argv, "02:", &str, &sg, &opt);
if (!NIL_P(opt)) argc--;
switch (argc) {
case 0:
str = rb_str_new2("Mon, 01 Jan -4712 00:00:00 GMT");
case 1:
sg = INT2FIX(DEFAULT_SG);
}
{
int argc2 = 1;
VALUE argv2[2], hash;
argv2[0] = str;
argv2[1] = opt;
if (!NIL_P(opt)) argc2++;
hash = date_s__httpdate(argc2, argv2, klass);
return dt_new_by_frags(klass, hash, sg);
}
}
|
.iso8601(string = '-4712-01-01T00:00:00+00:00'[, start=Date::ITALY], limit: 128) ⇒ Object
Creates a new DateTime object by parsing from a string according to some typical ISO 8601 formats.
DateTime.iso8601('2001-02-03T04:05:06+07:00')
#=> #<DateTime: 2001-02-03T04:05:06+07:00 …>
DateTime.iso8601('20010203T040506+0700')
#=> #<DateTime: 2001-02-03T04:05:06+07:00 …>
DateTime.iso8601('2001-W05-6T04:05:06+07:00')
#=> #<DateTime: 2001-02-03T04:05:06+07:00 …>
Raise an ArgumentError when the string length is longer than limit. You can stop this check by passing limit: nil
, but note that it may take a long time to parse.
8472 8473 8474 8475 8476 8477 8478 8479 8480 8481 8482 8483 8484 8485 8486 8487 8488 8489 8490 8491 8492 8493 8494 8495 8496 |
# File 'ext/date/date_core.c', line 8472
static VALUE
datetime_s_iso8601(int argc, VALUE *argv, VALUE klass)
{
VALUE str, sg, opt;
rb_scan_args(argc, argv, "02:", &str, &sg, &opt);
if (!NIL_P(opt)) argc--;
switch (argc) {
case 0:
str = rb_str_new2("-4712-01-01T00:00:00+00:00");
case 1:
sg = INT2FIX(DEFAULT_SG);
}
{
int argc2 = 1;
VALUE argv2[2], hash;
argv2[0] = str;
argv2[1] = opt;
if (!NIL_P(opt)) argc2--;
hash = date_s__iso8601(argc2, argv2, klass);
return dt_new_by_frags(klass, hash, sg);
}
}
|
.jd([jd = 0[, hour=0[, minute=0[, second=0[, offset=0[, start=Date::ITALY]]]]]]) ⇒ Object
7659 7660 7661 7662 7663 7664 7665 7666 7667 7668 7669 7670 7671 7672 7673 7674 7675 7676 7677 7678 7679 7680 7681 7682 7683 7684 7685 7686 7687 7688 7689 7690 7691 7692 7693 7694 7695 7696 7697 7698 7699 7700 7701 7702 7703 7704 7705 7706 7707 7708 7709 7710 7711 7712 7713 7714 7715 7716 7717 |
# File 'ext/date/date_core.c', line 7659
static VALUE
datetime_s_jd(int argc, VALUE *argv, VALUE klass)
{
VALUE vjd, vh, vmin, vs, vof, vsg, jd, fr, fr2, ret;
int h, min, s, rof;
double sg;
rb_scan_args(argc, argv, "06", &vjd, &vh, &vmin, &vs, &vof, &vsg);
jd = INT2FIX(0);
h = min = s = 0;
fr2 = INT2FIX(0);
rof = 0;
sg = DEFAULT_SG;
switch (argc) {
case 6:
val2sg(vsg, sg);
case 5:
val2off(vof, rof);
case 4:
check_numeric(vs, "second");
num2int_with_frac(s, positive_inf);
case 3:
check_numeric(vmin, "minute");
num2int_with_frac(min, 3);
case 2:
check_numeric(vh, "hour");
num2int_with_frac(h, 2);
case 1:
check_numeric(vjd, "jd");
num2num_with_frac(jd, 1);
}
{
VALUE nth;
int rh, rmin, rs, rjd, rjd2;
if (!c_valid_time_p(h, min, s, &rh, &rmin, &rs))
rb_raise(eDateError, "invalid date");
canon24oc();
decode_jd(jd, &nth, &rjd);
rjd2 = jd_local_to_utc(rjd,
time_to_df(rh, rmin, rs),
rof);
ret = d_complex_new_internal(klass,
nth, rjd2,
0, INT2FIX(0),
rof, sg,
0, 0, 0,
rh, rmin, rs,
HAVE_JD | HAVE_TIME);
}
add_frac();
return ret;
}
|
.jisx0301(string = '-4712-01-01T00:00:00+00:00'[, start=Date::ITALY], limit: 128) ⇒ Object
Creates a new DateTime object by parsing from a string according to some typical JIS X 0301 formats.
DateTime.jisx0301('H13.02.03T04:05:06+07:00')
#=> #<DateTime: 2001-02-03T04:05:06+07:00 …>
For no-era year, legacy format, Heisei is assumed.
DateTime.jisx0301('13.02.03T04:05:06+07:00')
#=> #<DateTime: 2001-02-03T04:05:06+07:00 …>
Raise an ArgumentError when the string length is longer than limit. You can stop this check by passing limit: nil
, but note that it may take a long time to parse.
8678 8679 8680 8681 8682 8683 8684 8685 8686 8687 8688 8689 8690 8691 8692 8693 8694 8695 8696 8697 8698 8699 8700 8701 8702 |
# File 'ext/date/date_core.c', line 8678
static VALUE
datetime_s_jisx0301(int argc, VALUE *argv, VALUE klass)
{
VALUE str, sg, opt;
rb_scan_args(argc, argv, "02:", &str, &sg, &opt);
if (!NIL_P(opt)) argc--;
switch (argc) {
case 0:
str = rb_str_new2("-4712-01-01T00:00:00+00:00");
case 1:
sg = INT2FIX(DEFAULT_SG);
}
{
int argc2 = 1;
VALUE argv2[2], hash;
argv2[0] = str;
argv2[1] = opt;
if (!NIL_P(opt)) argc2++;
hash = date_s__jisx0301(argc2, argv2, klass);
return dt_new_by_frags(klass, hash, sg);
}
}
|
.json_create(object) ⇒ Object
See #as_json.
10 11 12 13 14 15 16 17 18 19 20 |
# File 'ext/json/lib/json/add/date_time.rb', line 10 def self.json_create(object) args = object.values_at('y', 'm', 'd', 'H', 'M', 'S') of_a, of_b = object['of'].split('/') if of_b and of_b != '0' args << Rational(of_a.to_i, of_b.to_i) else args << of_a end args << object['sg'] civil(*args) end |
.new(*args) ⇒ Object
Same as DateTime.new.
7802 7803 7804 7805 7806 |
# File 'ext/date/date_core.c', line 7802
static VALUE
datetime_s_civil(int argc, VALUE *argv, VALUE klass)
{
return datetime_initialize(argc, argv, d_lite_s_alloc_complex(klass));
}
|
.now([start = Date::ITALY]) ⇒ Object
Creates a DateTime object denoting the present time.
DateTime.now #=> #<DateTime: 2011-06-11T21:20:44+09:00 ...>
8139 8140 8141 8142 8143 8144 8145 8146 8147 8148 8149 8150 8151 8152 8153 8154 8155 8156 8157 8158 8159 8160 8161 8162 8163 8164 8165 8166 8167 8168 8169 8170 8171 8172 8173 8174 8175 8176 8177 8178 8179 8180 8181 8182 8183 8184 8185 8186 8187 8188 8189 8190 8191 8192 8193 8194 8195 8196 8197 8198 8199 8200 8201 8202 8203 8204 8205 8206 8207 8208 8209 8210 8211 8212 8213 8214 8215 8216 8217 8218 8219 8220 8221 8222 8223 8224 8225 8226 8227 8228 8229 8230 8231 8232 8233 8234 8235 8236 8237 8238 8239 8240 8241 |
# File 'ext/date/date_core.c', line 8139
static VALUE
datetime_s_now(int argc, VALUE *argv, VALUE klass)
{
VALUE vsg, nth, ret;
double sg;
#ifdef HAVE_CLOCK_GETTIME
struct timespec ts;
#else
struct timeval tv;
#endif
time_t sec;
struct tm tm;
long sf, of;
int y, ry, m, d, h, min, s;
rb_scan_args(argc, argv, "01", &vsg);
if (argc < 1)
sg = DEFAULT_SG;
else
sg = NUM2DBL(vsg);
#ifdef HAVE_CLOCK_GETTIME
if (clock_gettime(CLOCK_REALTIME, &ts) == -1)
rb_sys_fail("clock_gettime");
sec = ts.tv_sec;
#else
if (gettimeofday(&tv, NULL) == -1)
rb_sys_fail("gettimeofday");
sec = tv.tv_sec;
#endif
tzset();
if (!localtime_r(&sec, &tm))
rb_sys_fail("localtime");
y = tm.tm_year + 1900;
m = tm.tm_mon + 1;
d = tm.tm_mday;
h = tm.tm_hour;
min = tm.tm_min;
s = tm.tm_sec;
if (s == 60)
s = 59;
#ifdef HAVE_STRUCT_TM_TM_GMTOFF
of = tm.tm_gmtoff;
#elif defined(HAVE_TIMEZONE)
#if defined(HAVE_ALTZONE) && !defined(_AIX)
of = (long)-((tm.tm_isdst > 0) ? altzone : timezone);
#else
of = (long)-timezone;
if (tm.tm_isdst) {
time_t sec2;
tm.tm_isdst = 0;
sec2 = mktime(&tm);
of += (long)difftime(sec2, sec);
}
#endif
#elif defined(HAVE_TIMEGM)
{
time_t sec2;
sec2 = timegm(&tm);
of = (long)difftime(sec2, sec);
}
#else
{
struct tm tm2;
time_t sec2;
if (!gmtime_r(&sec, &tm2))
rb_sys_fail("gmtime");
tm2.tm_isdst = tm.tm_isdst;
sec2 = mktime(&tm2);
of = (long)difftime(sec, sec2);
}
#endif
#ifdef HAVE_CLOCK_GETTIME
sf = ts.tv_nsec;
#else
sf = tv.tv_usec * 1000;
#endif
if (of < -DAY_IN_SECONDS || of > DAY_IN_SECONDS) {
of = 0;
rb_warning("invalid offset is ignored");
}
decode_year(INT2FIX(y), -1, &nth, &ry);
ret = d_complex_new_internal(klass,
nth, 0,
0, LONG2NUM(sf),
(int)of, GREGORIAN,
ry, m, d,
h, min, s,
HAVE_CIVIL | HAVE_TIME);
{
get_d1(ret);
set_sg(dat, sg);
}
return ret;
}
|
.nth_kday(*args) ⇒ Object
:nodoc:
8061 8062 8063 8064 8065 8066 8067 8068 8069 8070 8071 8072 8073 8074 8075 8076 8077 8078 8079 8080 8081 8082 8083 8084 8085 8086 8087 8088 8089 8090 8091 8092 8093 8094 8095 8096 8097 8098 8099 8100 8101 8102 8103 8104 8105 8106 8107 8108 8109 8110 8111 8112 8113 8114 8115 8116 8117 8118 8119 8120 8121 8122 8123 8124 8125 8126 8127 8128 |
# File 'ext/date/date_core.c', line 8061
static VALUE
datetime_s_nth_kday(int argc, VALUE *argv, VALUE klass)
{
VALUE vy, vm, vn, vk, vh, vmin, vs, vof, vsg, y, fr, fr2, ret;
int m, n, k, h, min, s, rof;
double sg;
rb_scan_args(argc, argv, "09", &vy, &vm, &vn, &vk,
&vh, &vmin, &vs, &vof, &vsg);
y = INT2FIX(-4712);
m = 1;
n = 1;
k = 1;
h = min = s = 0;
fr2 = INT2FIX(0);
rof = 0;
sg = DEFAULT_SG;
switch (argc) {
case 9:
val2sg(vsg, sg);
case 8:
val2off(vof, rof);
case 7:
num2int_with_frac(s, positive_inf);
case 6:
num2int_with_frac(min, 6);
case 5:
num2int_with_frac(h, 5);
case 4:
num2int_with_frac(k, 4);
case 3:
n = NUM2INT(vn);
case 2:
m = NUM2INT(vm);
case 1:
y = vy;
}
{
VALUE nth;
int ry, rm, rn, rk, rh, rmin, rs, rjd, rjd2, ns;
if (!valid_nth_kday_p(y, m, n, k, sg,
&nth, &ry,
&rm, &rn, &rk, &rjd,
&ns))
rb_raise(eDateError, "invalid date");
if (!c_valid_time_p(h, min, s, &rh, &rmin, &rs))
rb_raise(eDateError, "invalid date");
canon24oc();
rjd2 = jd_local_to_utc(rjd,
time_to_df(rh, rmin, rs),
rof);
ret = d_complex_new_internal(klass,
nth, rjd2,
0, INT2FIX(0),
rof, sg,
0, 0, 0,
rh, rmin, rs,
HAVE_JD | HAVE_TIME);
}
add_frac();
return ret;
}
|
.ordinal([year = -4712[, yday=1[, hour=0[, minute=0[, second=0[, offset=0[, start=Date::ITALY]]]]]]]) ⇒ Object
Creates a DateTime object denoting the given ordinal date.
DateTime.ordinal(2001,34) #=> #<DateTime: 2001-02-03T00:00:00+00:00 ...>
DateTime.ordinal(2001,34,4,5,6,'+7')
#=> #<DateTime: 2001-02-03T04:05:06+07:00 …>
DateTime.ordinal(2001,-332,-20,-55,-54,'+7')
#=> #<DateTime: 2001-02-03T04:05:06+07:00 …>
7731 7732 7733 7734 7735 7736 7737 7738 7739 7740 7741 7742 7743 7744 7745 7746 7747 7748 7749 7750 7751 7752 7753 7754 7755 7756 7757 7758 7759 7760 7761 7762 7763 7764 7765 7766 7767 7768 7769 7770 7771 7772 7773 7774 7775 7776 7777 7778 7779 7780 7781 7782 7783 7784 7785 7786 7787 7788 7789 7790 7791 7792 7793 7794 7795 7796 7797 |
# File 'ext/date/date_core.c', line 7731
static VALUE
datetime_s_ordinal(int argc, VALUE *argv, VALUE klass)
{
VALUE vy, vd, vh, vmin, vs, vof, vsg, y, fr, fr2, ret;
int d, h, min, s, rof;
double sg;
rb_scan_args(argc, argv, "07", &vy, &vd, &vh, &vmin, &vs, &vof, &vsg);
y = INT2FIX(-4712);
d = 1;
h = min = s = 0;
fr2 = INT2FIX(0);
rof = 0;
sg = DEFAULT_SG;
switch (argc) {
case 7:
val2sg(vsg, sg);
case 6:
val2off(vof, rof);
case 5:
check_numeric(vs, "second");
num2int_with_frac(s, positive_inf);
case 4:
check_numeric(vmin, "minute");
num2int_with_frac(min, 4);
case 3:
check_numeric(vh, "hour");
num2int_with_frac(h, 3);
case 2:
check_numeric(vd, "yday");
num2int_with_frac(d, 2);
case 1:
check_numeric(vy, "year");
y = vy;
}
{
VALUE nth;
int ry, rd, rh, rmin, rs, rjd, rjd2, ns;
if (!valid_ordinal_p(y, d, sg,
&nth, &ry,
&rd, &rjd,
&ns))
rb_raise(eDateError, "invalid date");
if (!c_valid_time_p(h, min, s, &rh, &rmin, &rs))
rb_raise(eDateError, "invalid date");
canon24oc();
rjd2 = jd_local_to_utc(rjd,
time_to_df(rh, rmin, rs),
rof);
ret = d_complex_new_internal(klass,
nth, rjd2,
0, INT2FIX(0),
rof, sg,
0, 0, 0,
rh, rmin, rs,
HAVE_JD | HAVE_TIME);
}
add_frac();
return ret;
}
|
.parse(string = '-4712-01-01T00:00:00+00:00'[, comp=true[, start=Date::ITALY]], limit: 128) ⇒ Object
Parses the given representation of date and time, and creates a DateTime object.
This method does not function as a validator. If the input string does not match valid formats strictly, you may get a cryptic result. Should consider to use DateTime.strptime instead of this method as possible.
If the optional second argument is true and the detected year is in the range “00” to “99”, makes it full.
DateTime.parse('2001-02-03T04:05:06+07:00')
#=> #<DateTime: 2001-02-03T04:05:06+07:00 …>
DateTime.parse('20010203T040506+0700')
#=> #<DateTime: 2001-02-03T04:05:06+07:00 …>
DateTime.parse('3rd Feb 2001 04:05:06 PM')
#=> #<DateTime: 2001-02-03T16:05:06+00:00 …>
Raise an ArgumentError when the string length is longer than limit. You can stop this check by passing limit: nil
, but note that it may take a long time to parse.
8425 8426 8427 8428 8429 8430 8431 8432 8433 8434 8435 8436 8437 8438 8439 8440 8441 8442 8443 8444 8445 8446 8447 8448 8449 8450 8451 8452 |
# File 'ext/date/date_core.c', line 8425
static VALUE
datetime_s_parse(int argc, VALUE *argv, VALUE klass)
{
VALUE str, comp, sg, opt;
rb_scan_args(argc, argv, "03:", &str, &comp, &sg, &opt);
if (!NIL_P(opt)) argc--;
switch (argc) {
case 0:
str = rb_str_new2("-4712-01-01T00:00:00+00:00");
case 1:
comp = Qtrue;
case 2:
sg = INT2FIX(DEFAULT_SG);
}
{
int argc2 = 2;
VALUE argv2[3], hash;
argv2[0] = str;
argv2[1] = comp;
argv2[2] = opt;
if (!NIL_P(opt)) argc2++;
hash = date_s__parse(argc2, argv2, klass);
return dt_new_by_frags(klass, hash, sg);
}
}
|
.rfc2822(string = 'Mon, 1 Jan -4712 00:00:00 +0000'[, start=Date::ITALY], limit: 128) ⇒ Object .rfc822(string = 'Mon, 1 Jan -4712 00:00:00 +0000'[, start=Date::ITALY], limit: 128) ⇒ Object
Creates a new DateTime object by parsing from a string according to some typical RFC 2822 formats.
DateTime.rfc2822('Sat, 3 Feb 2001 04:05:06 +0700')
#=> #<DateTime: 2001-02-03T04:05:06+07:00 …>
Raise an ArgumentError when the string length is longer than limit. You can stop this check by passing limit: nil
, but note that it may take a long time to parse.
8593 8594 8595 8596 8597 8598 8599 8600 8601 8602 8603 8604 8605 8606 8607 8608 8609 8610 8611 8612 8613 8614 8615 8616 8617 |
# File 'ext/date/date_core.c', line 8593
static VALUE
datetime_s_rfc2822(int argc, VALUE *argv, VALUE klass)
{
VALUE str, sg, opt;
rb_scan_args(argc, argv, "02:", &str, &sg, &opt);
if (!NIL_P(opt)) argc--;
switch (argc) {
case 0:
str = rb_str_new2("Mon, 1 Jan -4712 00:00:00 +0000");
case 1:
sg = INT2FIX(DEFAULT_SG);
}
{
int argc2 = 1;
VALUE argv2[2], hash;
argv2[0] = str;
argv2[1] = opt;
if (!NIL_P(opt)) argc2++;
hash = date_s__rfc2822(argc2, argv2, klass);
return dt_new_by_frags(klass, hash, sg);
}
}
|
.rfc3339(string = '-4712-01-01T00:00:00+00:00'[, start=Date::ITALY], limit: 128) ⇒ Object
Creates a new DateTime object by parsing from a string according to some typical RFC 3339 formats.
DateTime.rfc3339('2001-02-03T04:05:06+07:00')
#=> #<DateTime: 2001-02-03T04:05:06+07:00 …>
Raise an ArgumentError when the string length is longer than limit. You can stop this check by passing limit: nil
, but note that it may take a long time to parse.
8512 8513 8514 8515 8516 8517 8518 8519 8520 8521 8522 8523 8524 8525 8526 8527 8528 8529 8530 8531 8532 8533 8534 8535 8536 |
# File 'ext/date/date_core.c', line 8512
static VALUE
datetime_s_rfc3339(int argc, VALUE *argv, VALUE klass)
{
VALUE str, sg, opt;
rb_scan_args(argc, argv, "02:", &str, &sg, &opt);
if (!NIL_P(opt)) argc--;
switch (argc) {
case 0:
str = rb_str_new2("-4712-01-01T00:00:00+00:00");
case 1:
sg = INT2FIX(DEFAULT_SG);
}
{
int argc2 = 1;
VALUE argv2[2], hash;
argv2[0] = str;
argv2[1] = opt;
if (!NIL_P(opt)) argc2++;
hash = date_s__rfc3339(argc2, argv2, klass);
return dt_new_by_frags(klass, hash, sg);
}
}
|
.rfc2822(string = 'Mon, 1 Jan -4712 00:00:00 +0000'[, start=Date::ITALY], limit: 128) ⇒ Object .rfc822(string = 'Mon, 1 Jan -4712 00:00:00 +0000'[, start=Date::ITALY], limit: 128) ⇒ Object
Creates a new DateTime object by parsing from a string according to some typical RFC 2822 formats.
DateTime.rfc2822('Sat, 3 Feb 2001 04:05:06 +0700')
#=> #<DateTime: 2001-02-03T04:05:06+07:00 …>
Raise an ArgumentError when the string length is longer than limit. You can stop this check by passing limit: nil
, but note that it may take a long time to parse.
8593 8594 8595 8596 8597 8598 8599 8600 8601 8602 8603 8604 8605 8606 8607 8608 8609 8610 8611 8612 8613 8614 8615 8616 8617 |
# File 'ext/date/date_core.c', line 8593
static VALUE
datetime_s_rfc2822(int argc, VALUE *argv, VALUE klass)
{
VALUE str, sg, opt;
rb_scan_args(argc, argv, "02:", &str, &sg, &opt);
if (!NIL_P(opt)) argc--;
switch (argc) {
case 0:
str = rb_str_new2("Mon, 1 Jan -4712 00:00:00 +0000");
case 1:
sg = INT2FIX(DEFAULT_SG);
}
{
int argc2 = 1;
VALUE argv2[2], hash;
argv2[0] = str;
argv2[1] = opt;
if (!NIL_P(opt)) argc2++;
hash = date_s__rfc2822(argc2, argv2, klass);
return dt_new_by_frags(klass, hash, sg);
}
}
|
.strptime([string = '-4712-01-01T00:00:00+00:00'[, format='%FT%T%z'[ ,start=Date::ITALY]]]) ⇒ Object
Parses the given representation of date and time with the given template, and creates a DateTime object. strptime does not support specification of flags and width unlike strftime.
DateTime.strptime('2001-02-03T04:05:06+07:00', '%Y-%m-%dT%H:%M:%S%z')
#=> #<DateTime: 2001-02-03T04:05:06+07:00 …>
DateTime.strptime('03-02-2001 04:05:06 PM', '%d-%m-%Y %I:%M:%S %p')
#=> #<DateTime: 2001-02-03T16:05:06+00:00 …>
DateTime.strptime('2001-W05-6T04:05:06+07:00', '%G-W%V-%uT%H:%M:%S%z')
#=> #<DateTime: 2001-02-03T04:05:06+07:00 …>
DateTime.strptime('2001 04 6 04 05 06 +7', '%Y %U %w %H %M %S %z')
#=> #<DateTime: 2001-02-03T04:05:06+07:00 …>
DateTime.strptime('2001 05 6 04 05 06 +7', '%Y %W %u %H %M %S %z')
#=> #<DateTime: 2001-02-03T04:05:06+07:00 …>
DateTime.strptime('-1', '%s')
#=> #<DateTime: 1969-12-31T23:59:59+00:00 …>
DateTime.strptime('-1000', '%Q')
#=> #<DateTime: 1969-12-31T23:59:59+00:00 …>
DateTime.strptime('sat3feb014pm+7', '%a%d%b%y%H%p%z')
#=> #<DateTime: 2001-02-03T16:00:00+07:00 …>
See also strptime(3) and #strftime.
8373 8374 8375 8376 8377 8378 8379 8380 8381 8382 8383 8384 8385 8386 8387 8388 8389 8390 8391 8392 8393 8394 8395 8396 8397 |
# File 'ext/date/date_core.c', line 8373
static VALUE
datetime_s_strptime(int argc, VALUE *argv, VALUE klass)
{
VALUE str, fmt, sg;
rb_scan_args(argc, argv, "03", &str, &fmt, &sg);
switch (argc) {
case 0:
str = rb_str_new2("-4712-01-01T00:00:00+00:00");
case 1:
fmt = rb_str_new2("%FT%T%z");
case 2:
sg = INT2FIX(DEFAULT_SG);
}
{
VALUE argv2[2], hash;
argv2[0] = str;
argv2[1] = fmt;
hash = date_s__strptime(2, argv2, klass);
return dt_new_by_frags(klass, hash, sg);
}
}
|
.weeknum(*args) ⇒ Object
:nodoc:
7991 7992 7993 7994 7995 7996 7997 7998 7999 8000 8001 8002 8003 8004 8005 8006 8007 8008 8009 8010 8011 8012 8013 8014 8015 8016 8017 8018 8019 8020 8021 8022 8023 8024 8025 8026 8027 8028 8029 8030 8031 8032 8033 8034 8035 8036 8037 8038 8039 8040 8041 8042 8043 8044 8045 8046 8047 8048 8049 8050 8051 8052 8053 8054 8055 8056 8057 8058 |
# File 'ext/date/date_core.c', line 7991
static VALUE
datetime_s_weeknum(int argc, VALUE *argv, VALUE klass)
{
VALUE vy, vw, vd, vf, vh, vmin, vs, vof, vsg, y, fr, fr2, ret;
int w, d, f, h, min, s, rof;
double sg;
rb_scan_args(argc, argv, "09", &vy, &vw, &vd, &vf,
&vh, &vmin, &vs, &vof, &vsg);
y = INT2FIX(-4712);
w = 0;
d = 1;
f = 0;
h = min = s = 0;
fr2 = INT2FIX(0);
rof = 0;
sg = DEFAULT_SG;
switch (argc) {
case 9:
val2sg(vsg, sg);
case 8:
val2off(vof, rof);
case 7:
num2int_with_frac(s, positive_inf);
case 6:
num2int_with_frac(min, 6);
case 5:
num2int_with_frac(h, 5);
case 4:
f = NUM2INT(vf);
case 3:
num2int_with_frac(d, 4);
case 2:
w = NUM2INT(vw);
case 1:
y = vy;
}
{
VALUE nth;
int ry, rw, rd, rh, rmin, rs, rjd, rjd2, ns;
if (!valid_weeknum_p(y, w, d, f, sg,
&nth, &ry,
&rw, &rd, &rjd,
&ns))
rb_raise(eDateError, "invalid date");
if (!c_valid_time_p(h, min, s, &rh, &rmin, &rs))
rb_raise(eDateError, "invalid date");
canon24oc();
rjd2 = jd_local_to_utc(rjd,
time_to_df(rh, rmin, rs),
rof);
ret = d_complex_new_internal(klass,
nth, rjd2,
0, INT2FIX(0),
rof, sg,
0, 0, 0,
rh, rmin, rs,
HAVE_JD | HAVE_TIME);
}
add_frac();
return ret;
}
|
.xmlschema(string = '-4712-01-01T00:00:00+00:00'[, start=Date::ITALY], limit: 128) ⇒ Object
Creates a new DateTime object by parsing from a string according to some typical XML Schema formats.
DateTime.xmlschema('2001-02-03T04:05:06+07:00')
#=> #<DateTime: 2001-02-03T04:05:06+07:00 …>
Raise an ArgumentError when the string length is longer than limit. You can stop this check by passing limit: nil
, but note that it may take a long time to parse.
8552 8553 8554 8555 8556 8557 8558 8559 8560 8561 8562 8563 8564 8565 8566 8567 8568 8569 8570 8571 8572 8573 8574 8575 8576 |
# File 'ext/date/date_core.c', line 8552
static VALUE
datetime_s_xmlschema(int argc, VALUE *argv, VALUE klass)
{
VALUE str, sg, opt;
rb_scan_args(argc, argv, "02:", &str, &sg, &opt);
if (!NIL_P(opt)) argc--;
switch (argc) {
case 0:
str = rb_str_new2("-4712-01-01T00:00:00+00:00");
case 1:
sg = INT2FIX(DEFAULT_SG);
}
{
int argc2 = 1;
VALUE argv2[2], hash;
argv2[0] = str;
argv2[1] = opt;
if (!NIL_P(opt)) argc2++;
hash = date_s__xmlschema(argc2, argv2, klass);
return dt_new_by_frags(klass, hash, sg);
}
}
|
Instance Method Details
#as_json ⇒ Object
Methods DateTime#as_json
and DateTime.json_create
may be used to serialize and deserialize a DateTime object; see Marshal.
Method DateTime#as_json
serializes self
, returning a 2-element hash representing self
:
require 'json/add/datetime'
x = DateTime.now.as_json
# => {"json_class"=>"DateTime", "y"=>2023, "m"=>11, "d"=>21, "sg"=>2299161.0}
Method JSON.create
deserializes such a hash, returning a DateTime object:
DateTime.json_create(x) # BUG? Raises Date::Error "invalid date"
39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'ext/json/lib/json/add/date_time.rb', line 39 def as_json(*) { JSON.create_id => self.class.name, 'y' => year, 'm' => month, 'd' => day, 'H' => hour, 'M' => min, 'S' => sec, 'of' => offset.to_s, 'sg' => start, } end |
#deconstruct_keys(array_of_names_or_nil) ⇒ Hash
Returns a hash of the name/value pairs, to use in pattern matching. Possible keys are: :year
, :month
, :day
, :wday
, :yday
, :hour
, :min
, :sec
, :sec_fraction
, :zone
.
Possible usages:
dt = DateTime.new(2022, 10, 5, 13, 30)
if d in wday: 1..5, hour: 10..18 # uses deconstruct_keys underneath
puts "Working time"
end
#=> prints "Working time"
case dt
in year: ...2022
puts "too old"
in month: ..9
puts "quarter 1-3"
in wday: 1..5, month:
puts "working day in month #{month}"
end
#=> prints "working day in month 10"
Note that deconstruction by pattern can also be combined with class check:
if d in DateTime(wday: 1..5, hour: 10..18, day: ..7)
puts "Working time, first week of the month"
end
8854 8855 8856 8857 8858 |
# File 'ext/date/date_core.c', line 8854
static VALUE
dt_lite_deconstruct_keys(VALUE self, VALUE keys)
{
return deconstruct_keys(self, keys, /* is_datetime=true */ 1);
}
|
#hour ⇒ Integer
5589 5590 5591 5592 5593 5594 |
# File 'ext/date/date_core.c', line 5589
static VALUE
d_lite_hour(VALUE self)
{
get_d1(self);
return INT2FIX(m_hour(dat));
}
|
#iso8601([n = 0]) ⇒ String #xmlschema([n = 0]) ⇒ String
8767 8768 8769 8770 8771 8772 8773 8774 8775 8776 8777 8778 |
# File 'ext/date/date_core.c', line 8767
static VALUE
dt_lite_iso8601(int argc, VALUE *argv, VALUE self)
{
long n = 0;
rb_check_arity(argc, 0, 1);
if (argc >= 1)
n = NUM2LONG(argv[0]);
return rb_str_append(strftimev("%Y-%m-%d", self, set_tmx),
iso8601_timediv(self, n));
}
|
#jisx0301([n = 0]) ⇒ String
8806 8807 8808 8809 8810 8811 8812 8813 8814 8815 8816 8817 |
# File 'ext/date/date_core.c', line 8806
static VALUE
dt_lite_jisx0301(int argc, VALUE *argv, VALUE self)
{
long n = 0;
rb_check_arity(argc, 0, 1);
if (argc >= 1)
n = NUM2LONG(argv[0]);
return rb_str_append(d_lite_jisx0301(self),
iso8601_timediv(self, n));
}
|
#min ⇒ Integer
5605 5606 5607 5608 5609 5610 |
# File 'ext/date/date_core.c', line 5605
static VALUE
d_lite_min(VALUE self)
{
get_d1(self);
return INT2FIX(m_min(dat));
}
|
#min ⇒ Integer
5605 5606 5607 5608 5609 5610 |
# File 'ext/date/date_core.c', line 5605
static VALUE
d_lite_min(VALUE self)
{
get_d1(self);
return INT2FIX(m_min(dat));
}
|
#new_offset([offset = 0]) ⇒ Object
5928 5929 5930 5931 5932 5933 5934 5935 5936 5937 5938 5939 5940 5941 |
# File 'ext/date/date_core.c', line 5928
static VALUE
d_lite_new_offset(int argc, VALUE *argv, VALUE self)
{
VALUE vof;
int rof;
rb_scan_args(argc, argv, "01", &vof);
rof = 0;
if (argc >= 1)
val2off(vof, rof);
return dup_obj_with_new_offset(self, rof);
}
|
#offset ⇒ Object
5653 5654 5655 5656 5657 5658 |
# File 'ext/date/date_core.c', line 5653
static VALUE
d_lite_offset(VALUE self)
{
get_d1(self);
return m_of_in_day(dat);
}
|
#rfc3339([n = 0]) ⇒ String
8790 8791 8792 8793 8794 |
# File 'ext/date/date_core.c', line 8790
static VALUE
dt_lite_rfc3339(int argc, VALUE *argv, VALUE self)
{
return dt_lite_iso8601(argc, argv, self);
}
|
#sec ⇒ Integer
5621 5622 5623 5624 5625 5626 |
# File 'ext/date/date_core.c', line 5621
static VALUE
d_lite_sec(VALUE self)
{
get_d1(self);
return INT2FIX(m_sec(dat));
}
|
#sec_fraction ⇒ Object
5638 5639 5640 5641 5642 5643 |
# File 'ext/date/date_core.c', line 5638
static VALUE
d_lite_sec_fraction(VALUE self)
{
get_d1(self);
return m_sf_in_sec(dat);
}
|
#sec ⇒ Integer
5621 5622 5623 5624 5625 5626 |
# File 'ext/date/date_core.c', line 5621
static VALUE
d_lite_sec(VALUE self)
{
get_d1(self);
return INT2FIX(m_sec(dat));
}
|
#sec_fraction ⇒ Object
5638 5639 5640 5641 5642 5643 |
# File 'ext/date/date_core.c', line 5638
static VALUE
d_lite_sec_fraction(VALUE self)
{
get_d1(self);
return m_sf_in_sec(dat);
}
|
#strftime(format = '%FT%T%:z') ⇒ String
Returns a string representation of self
, formatted according the given +format:
DateTime.now.strftime # => "2022-07-01T11:03:19-05:00"
For other formats, see Formats for Dates and Times:
8733 8734 8735 8736 8737 8738 |
# File 'ext/date/date_core.c', line 8733
static VALUE
dt_lite_strftime(int argc, VALUE *argv, VALUE self)
{
return date_strftime_internal(argc, argv, self,
"%Y-%m-%dT%H:%M:%S%:z", set_tmx);
}
|
#to_date ⇒ Object
Returns a Date object which denotes self.
9074 9075 9076 9077 9078 9079 9080 9081 9082 9083 9084 9085 9086 9087 9088 9089 9090 9091 9092 9093 9094 9095 9096 9097 9098 |
# File 'ext/date/date_core.c', line 9074
static VALUE
datetime_to_date(VALUE self)
{
get_d1a(self);
if (simple_dat_p(adat)) {
VALUE new = d_lite_s_alloc_simple(cDate);
{
get_d1b(new);
bdat->s = adat->s;
bdat->s.jd = m_local_jd(adat);
return new;
}
}
else {
VALUE new = d_lite_s_alloc_simple(cDate);
{
get_d1b(new);
copy_complex_to_simple(new, &bdat->s, &adat->c);
bdat->s.jd = m_local_jd(adat);
bdat->s.flags &= ~(HAVE_DF | HAVE_TIME | COMPLEX_DAT);
return new;
}
}
}
|
#to_datetime ⇒ self
Returns self.
9106 9107 9108 9109 9110 |
# File 'ext/date/date_core.c', line 9106
static VALUE
datetime_to_datetime(VALUE self)
{
return self;
}
|
#to_json(*args) ⇒ Object
62 63 64 |
# File 'ext/json/lib/json/add/date_time.rb', line 62 def to_json(*args) as_json.to_json(*args) end |
#to_s ⇒ String
8714 8715 8716 8717 8718 |
# File 'ext/date/date_core.c', line 8714
static VALUE
dt_lite_to_s(VALUE self)
{
return strftimev("%Y-%m-%dT%H:%M:%S%:z", self, set_tmx);
}
|
#to_time ⇒ Time
Returns a Time object which denotes self.
9039 9040 9041 9042 9043 9044 9045 9046 9047 9048 9049 9050 9051 9052 9053 9054 9055 9056 9057 9058 9059 9060 9061 9062 9063 9064 9065 9066 |
# File 'ext/date/date_core.c', line 9039
static VALUE
datetime_to_time(VALUE self)
{
get_d1(self);
if (m_julian_p(dat)) {
self = d_lite_gregorian(self);
get_d1a(self);
dat = adat;
}
{
VALUE t;
t = rb_funcall(rb_cTime,
rb_intern("new"),
7,
m_real_year(dat),
INT2FIX(m_mon(dat)),
INT2FIX(m_mday(dat)),
INT2FIX(m_hour(dat)),
INT2FIX(m_min(dat)),
f_add(INT2FIX(m_sec(dat)),
m_sf_in_sec(dat)),
INT2FIX(m_of(dat)));
return t;
}
}
|
#iso8601([n = 0]) ⇒ String #xmlschema([n = 0]) ⇒ String
8767 8768 8769 8770 8771 8772 8773 8774 8775 8776 8777 8778 |
# File 'ext/date/date_core.c', line 8767
static VALUE
dt_lite_iso8601(int argc, VALUE *argv, VALUE self)
{
long n = 0;
rb_check_arity(argc, 0, 1);
if (argc >= 1)
n = NUM2LONG(argv[0]);
return rb_str_append(strftimev("%Y-%m-%d", self, set_tmx),
iso8601_timediv(self, n));
}
|