class DateTime
Class representing a date and time.
See the documentation to the file date.rb for an overview.
DateTime
objects are immutable once created.
Other methods.¶ ↑
The following methods are defined in Date
, but declared private there. They are made public in DateTime
. They are documented here.
hour()¶ ↑
Get the hour-of-the-day of the time. This is given using the 24-hour clock, counting from midnight. The first hour after midnight is hour 0; the last hour of the day is hour 23.
min()¶ ↑
Get the minute-of-the-hour of the time.
sec()¶ ↑
Get the second-of-the-minute of the time.
sec_fraction()¶ ↑
Get the fraction of a second of the time. This is returned as a Rational
.
zone()¶ ↑
Get the time zone as a String. This is representation of the time offset such as “+1000”, not the true time-zone name.
offset()¶ ↑
Get the time zone offset as a fraction of a day. This is returned as a Rational
.
new_offset(of=0)¶ ↑
Create a new DateTime
object, identical to the current one, except with a new time zone offset of of
. of
is the new offset from UTC as a fraction of a day.
Public Class Methods
# File lib/project/format.rb, line 1288 def self._strptime(str, fmt='%FT%T%z') super(str, fmt) end
Create a new DateTime
object corresponding to the specified Civil Date
and hour h
, minute min
, second s
.
The 24-hour clock is used. Negative values of h
, min
, and sec
are treating as counting backwards from the end of the next larger unit (e.g. a min
of -2 is treated as 58). No wraparound is performed. If an invalid time portion is specified, an ArgumentError is raised.
of
is the offset from UTC as a fraction of a day (defaults to 0). sg
specifies the Day of Calendar Reform.
y
defaults to -4712, m
to 1, and d
to 1; this is Julian Day Number day 0. The time values default to 0.
# File lib/project/date.rb, line 1617 def self.civil(y=-4712, m=1, d=1, h=0, min=0, s=0, of=0, sg=ITALY) unless (jd = _valid_civil?(y, m, d, sg)) && (fr = _valid_time?(h, min, s)) raise ArgumentError, 'invalid date' end if String === of of = Rational(zone_to_diff(of) || 0, 86400) end new!(jd_to_ajd(jd, fr, of), of, sg) end
Create a new DateTime
object corresponding to the specified Commercial Date
and hour h
, minute min
, second s
.
The 24-hour clock is used. Negative values of h
, min
, and sec
are treating as counting backwards from the end of the next larger unit (e.g. a min
of -2 is treated as 58). No wraparound is performed. If an invalid time portion is specified, an ArgumentError is raised.
of
is the offset from UTC as a fraction of a day (defaults to 0). sg
specifies the Day of Calendar Reform.
y
defaults to -4712, w
to 1, and d
to 1; this is Julian Day Number day 0. The time values default to 0.
# File lib/project/date.rb, line 1645 def self.commercial(y=-4712, w=1, d=1, h=0, min=0, s=0, of=0, sg=ITALY) unless (jd = _valid_commercial?(y, w, d, sg)) && (fr = _valid_time?(h, min, s)) raise ArgumentError, 'invalid date' end if String === of of = Rational(zone_to_diff(of) || 0, 86400) end new!(jd_to_ajd(jd, fr, of), of, sg) end
Create a new DateTime
object corresponding to the specified Julian Day Number jd
and hour h
, minute min
, second s
.
The 24-hour clock is used. Negative values of h
, min
, and sec
are treating as counting backwards from the end of the next larger unit (e.g. a min
of -2 is treated as 58). No wraparound is performed. If an invalid time portion is specified, an ArgumentError is raised.
of
is the offset from UTC as a fraction of a day (defaults to 0). sg
specifies the Day of Calendar Reform.
All day/time values default to 0.
# File lib/project/date.rb, line 1567 def self.jd(jd=0, h=0, min=0, s=0, of=0, sg=ITALY) unless (jd = _valid_jd?(jd, sg)) && (fr = _valid_time?(h, min, s)) raise ArgumentError, 'invalid date' end if String === of of = Rational(zone_to_diff(of) || 0, 86400) end new!(jd_to_ajd(jd, fr, of), of, sg) end
Create a new DateTime
object representing the current time.
sg
specifies the Day of Calendar Reform.
# File lib/project/date.rb, line 1815 def self.now(sg=ITALY) t = Time.now jd = civil_to_jd(t.year, t.mon, t.mday, sg) fr = time_to_day_fraction(t.hour, t.min, [t.sec, 59].min) + Rational(t.nsec, 86400_000_000_000) of = Rational(t.utc_offset, 86400) new!(jd_to_ajd(jd, fr, of), of, sg) end
Create a new DateTime
object corresponding to the specified Ordinal Date
and hour h
, minute min
, second s
.
The 24-hour clock is used. Negative values of h
, min
, and sec
are treating as counting backwards from the end of the next larger unit (e.g. a min
of -2 is treated as 58). No wraparound is performed. If an invalid time portion is specified, an ArgumentError is raised.
of
is the offset from UTC as a fraction of a day (defaults to 0). sg
specifies the Day of Calendar Reform.
y
defaults to -4712, and d
to 1; this is Julian Day Number day 0. The time values default to 0.
# File lib/project/date.rb, line 1592 def self.ordinal(y=-4712, d=1, h=0, min=0, s=0, of=0, sg=ITALY) unless (jd = _valid_ordinal?(y, d, sg)) && (fr = _valid_time?(h, min, s)) raise ArgumentError, 'invalid date' end if String === of of = Rational(zone_to_diff(of) || 0, 86400) end new!(jd_to_ajd(jd, fr, of), of, sg) end
Create a new DateTime
object by parsing from a String, without specifying the format.
str
is a String holding a date-time representation. comp
specifies whether to interpret 2-digit years as 19XX (>= 69) or 20XX (< 69); the default is not to. The method will attempt to parse a date-time from the String using various heuristics; see #_parse in date/format.rb for more details. If parsing fails, an ArgumentError will be raised.
The default str
is ‘-4712-01-01T00:00:00+00:00’; this is Julian Day Number day 0.
sg
specifies the Day of Calendar Reform.
# File lib/project/date.rb, line 1730 def self.parse(str='-4712-01-01T00:00:00+00:00', comp=true, sg=ITALY) elem = _parse(str, comp) new_by_frags(elem, sg) end
Create a new DateTime
object by parsing from a String according to a specified format.
str
is a String holding a date-time representation. fmt
is the format that the date-time is in. See date/format.rb for details on supported formats.
The default str
is ‘-4712-01-01T00:00:00+00:00’, and the default fmt
is ‘%FT%T%z’. This gives midnight on Julian Day Number day 0.
sg
specifies the Day of Calendar Reform.
An ArgumentError will be raised if str
cannot be parsed.
# File lib/project/date.rb, line 1710 def self.strptime(str='-4712-01-01T00:00:00+00:00', fmt='%FT%T%z', sg=ITALY) elem = _strptime(str, fmt) new_by_frags(elem, sg) end
Private Class Methods
# File lib/project/date.rb, line 844 def self.nth_kday(y=-4712, m=1, n=1, k=1, sg=ITALY) unless jd = _valid_nth_kday?(y, m, n, k, sg) raise ArgumentError, 'invalid date' end new!(jd_to_ajd(jd, 0, 0), 0, sg) end
Create a new Date
object representing today.
sg
specifies the Day of Calendar Reform.
# File lib/project/date.rb, line 1806 def self.today(sg=ITALY) t = Time.now jd = civil_to_jd(t.year, t.mon, t.mday, sg) new!(jd_to_ajd(jd, 0, 0), 0, sg) end
# File lib/project/date.rb, line 835 def self.weeknum(y=-4712, w=0, d=1, f=0, sg=ITALY) unless jd = _valid_weeknum?(y, w, d, f, sg) raise ArgumentError, 'invalid date' end new!(jd_to_ajd(jd, 0, 0), 0, sg) end
Public Instance Methods
Date#iso8601
# File lib/project/format.rb, line 1304 def iso8601(n=0) super() + iso8601_timediv(n) end
Date#jisx0301
# File lib/project/format.rb, line 1312 def jisx0301(n=0) super() + iso8601_timediv(n) end
# File lib/project/format.rb, line 1308 def rfc3339(n=0) iso8601(n) end
Date#strftime
# File lib/project/format.rb, line 1284 def strftime(fmt='%FT%T%:z') super(fmt) end
# File lib/project/date.rb, line 1839 def to_date() Date.new!(jd_to_ajd(jd, 0, 0), 0, @sg) end
# File lib/project/date.rb, line 1840 def to_datetime() self end
# File lib/project/date.rb, line 1770 def to_s # 4p format('%.4d-%02d-%02dT%02d:%02d:%02d%s', year, mon, mday, hour, min, sec, zone) end
# File lib/project/date.rb, line 1830 def to_time d = new_offset(0) d.instance_eval do Time.utc(year, mon, mday, hour, min, sec + sec_fraction) end. getlocal end