Class: DateTime
- Defined in:
- lib/date.rb,
lib/date.rb,
lib/date/format.rb
Overview
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.
Constant Summary
Constants inherited from Date
Date::ABBR_DAYNAMES, Date::ABBR_MONTHNAMES, Date::DAYNAMES, Date::ENGLAND, Date::GREGORIAN, Date::HALF_DAYS_IN_DAY, Date::HOURS_IN_DAY, Date::ITALY, Date::JULIAN, Date::LD_EPOCH_IN_CJD, Date::MILLISECONDS_IN_DAY, Date::MILLISECONDS_IN_SECOND, Date::MINUTES_IN_DAY, Date::MJD_EPOCH_IN_AJD, Date::MJD_EPOCH_IN_CJD, Date::MONTHNAMES, Date::NANOSECONDS_IN_DAY, Date::NANOSECONDS_IN_SECOND, Date::SECONDS_IN_DAY, Date::UNIX_EPOCH_IN_AJD, Date::UNIX_EPOCH_IN_CJD
Class Method Summary collapse
- ._strptime(str, fmt = '%FT%T%z') ⇒ Object
-
.civil(y = -4712,, m = 1, d = 1, h = 0, min = 0, s = 0, of = 0, sg = ITALY) ⇒ Object
(also: new)
Create a new DateTime object corresponding to the specified Civil Date and hour
h, minutemin, seconds. -
.commercial(y = -4712,, w = 1, d = 1, h = 0, min = 0, s = 0, of = 0, sg = ITALY) ⇒ Object
Create a new DateTime object corresponding to the specified Commercial Date and hour
h, minutemin, seconds. -
.httpdate(str = 'Mon, 01 Jan -4712 00:00:00 GMT', sg = ITALY) ⇒ Object
:nodoc:.
-
.iso8601(str = '-4712-01-01T00:00:00+00:00', sg = ITALY) ⇒ Object
:nodoc:.
-
.jd(jd = 0, h = 0, min = 0, s = 0, of = 0, sg = ITALY) ⇒ Object
Create a new DateTime object corresponding to the specified Julian Day Number
jdand hourh, minutemin, seconds. -
.jisx0301(str = '-4712-01-01T00:00:00+00:00', sg = ITALY) ⇒ Object
:nodoc:.
-
.ordinal(y = -4712,, d = 1, h = 0, min = 0, s = 0, of = 0, sg = ITALY) ⇒ Object
Create a new DateTime object corresponding to the specified Ordinal Date and hour
h, minutemin, seconds. -
.parse(str = '-4712-01-01T00:00:00+00:00', comp = true, sg = ITALY) ⇒ Object
Create a new DateTime object by parsing from a String, without specifying the format.
-
.rfc2822(str = 'Mon, 1 Jan -4712 00:00:00 +0000', sg = ITALY) ⇒ Object
(also: rfc822)
:nodoc:.
-
.rfc3339(str = '-4712-01-01T00:00:00+00:00', sg = ITALY) ⇒ Object
:nodoc:.
-
.strptime(str = '-4712-01-01T00:00:00+00:00', fmt = '%FT%T%z', sg = ITALY) ⇒ Object
Create a new DateTime object by parsing from a String according to a specified format.
-
.xmlschema(str = '-4712-01-01T00:00:00+00:00', sg = ITALY) ⇒ Object
:nodoc:.
Instance Method Summary collapse
- #iso8601(n = 0) ⇒ Object
- #jisx0301(n = 0) ⇒ Object
- #rfc3339(n = 0) ⇒ Object
- #strftime(fmt = '%FT%T%:z') ⇒ Object
- #to_date ⇒ Object
- #to_datetime ⇒ Object
-
#to_s ⇒ Object
4p.
- #to_time ⇒ Object
-
#xmlschema(n = 0) ⇒ Object
:nodoc:.
Methods inherited from Date
#+, #-, #<<, #<=>, #===, #>>, _httpdate, _iso8601, _jisx0301, _parse, _rfc2822, _rfc3339, _xmlschema, #ajd, #amjd, #asctime, #cwday, #cweek, #cwyear, #day_fraction, #downto, #england, #eql?, #gregorian, #gregorian?, gregorian_leap?, #hash, #httpdate, #initialize, #inspect, #italy, #jd, #julian, #julian?, julian_leap?, #ld, #leap?, #marshal_dump, #marshal_load, #mday, #mjd, #mon, #new_start, #next, #next_day, #next_month, #next_year, once, #prev_day, #prev_month, #prev_year, #rfc2822, #start, #step, today, #upto, valid_civil?, valid_commercial?, valid_jd?, valid_ordinal?, #wday, #yday, #year
Constructor Details
This class inherits a constructor from Date
Class Method Details
._strptime(str, fmt = '%FT%T%z') ⇒ Object
1313 1314 1315 |
# File 'lib/date/format.rb', line 1313 def self._strptime(str, fmt='%FT%T%z') super(str, fmt) end |
.civil(y = -4712,, m = 1, d = 1, h = 0, min = 0, s = 0, of = 0, sg = ITALY) ⇒ Object Also known as: new
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.
1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 |
# File 'lib/date.rb', line 1606 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 |
.commercial(y = -4712,, w = 1, d = 1, h = 0, min = 0, s = 0, of = 0, sg = ITALY) ⇒ Object
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.
1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 |
# File 'lib/date.rb', line 1634 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 |
.httpdate(str = 'Mon, 01 Jan -4712 00:00:00 GMT', sg = ITALY) ⇒ Object
:nodoc:
1746 1747 1748 1749 |
# File 'lib/date.rb', line 1746 def self.httpdate(str='Mon, 01 Jan -4712 00:00:00 GMT', sg=ITALY) # :nodoc: elem = _httpdate(str) new_by_frags(elem, sg) end |
.iso8601(str = '-4712-01-01T00:00:00+00:00', sg = ITALY) ⇒ Object
:nodoc:
1724 1725 1726 1727 |
# File 'lib/date.rb', line 1724 def self.iso8601(str='-4712-01-01T00:00:00+00:00', sg=ITALY) # :nodoc: elem = _iso8601(str) new_by_frags(elem, sg) end |
.jd(jd = 0, h = 0, min = 0, s = 0, of = 0, sg = ITALY) ⇒ Object
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.
1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 |
# File 'lib/date.rb', line 1556 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 |
.jisx0301(str = '-4712-01-01T00:00:00+00:00', sg = ITALY) ⇒ Object
:nodoc:
1751 1752 1753 1754 |
# File 'lib/date.rb', line 1751 def self.jisx0301(str='-4712-01-01T00:00:00+00:00', sg=ITALY) # :nodoc: elem = _jisx0301(str) new_by_frags(elem, sg) end |
.ordinal(y = -4712,, d = 1, h = 0, min = 0, s = 0, of = 0, sg = ITALY) ⇒ Object
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.
1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 |
# File 'lib/date.rb', line 1581 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 |
.parse(str = '-4712-01-01T00:00:00+00:00', comp = true, sg = ITALY) ⇒ Object
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.
1719 1720 1721 1722 |
# File 'lib/date.rb', line 1719 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 |
.rfc2822(str = 'Mon, 1 Jan -4712 00:00:00 +0000', sg = ITALY) ⇒ Object Also known as: rfc822
:nodoc:
1739 1740 1741 1742 |
# File 'lib/date.rb', line 1739 def self.rfc2822(str='Mon, 1 Jan -4712 00:00:00 +0000', sg=ITALY) # :nodoc: elem = _rfc2822(str) new_by_frags(elem, sg) end |
.rfc3339(str = '-4712-01-01T00:00:00+00:00', sg = ITALY) ⇒ Object
:nodoc:
1729 1730 1731 1732 |
# File 'lib/date.rb', line 1729 def self.rfc3339(str='-4712-01-01T00:00:00+00:00', sg=ITALY) # :nodoc: elem = _rfc3339(str) new_by_frags(elem, sg) end |
.strptime(str = '-4712-01-01T00:00:00+00:00', fmt = '%FT%T%z', sg = ITALY) ⇒ Object
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.
1699 1700 1701 1702 |
# File 'lib/date.rb', line 1699 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 |
.xmlschema(str = '-4712-01-01T00:00:00+00:00', sg = ITALY) ⇒ Object
:nodoc:
1734 1735 1736 1737 |
# File 'lib/date.rb', line 1734 def self.xmlschema(str='-4712-01-01T00:00:00+00:00', sg=ITALY) # :nodoc: elem = _xmlschema(str) new_by_frags(elem, sg) end |
Instance Method Details
#iso8601(n = 0) ⇒ Object
1329 1330 1331 |
# File 'lib/date/format.rb', line 1329 def iso8601(n=0) super() + iso8601_timediv(n) end |
#jisx0301(n = 0) ⇒ Object
1337 1338 1339 |
# File 'lib/date/format.rb', line 1337 def jisx0301(n=0) super() + iso8601_timediv(n) end |
#rfc3339(n = 0) ⇒ Object
1333 |
# File 'lib/date/format.rb', line 1333 def rfc3339(n=0) iso8601(n) end |
#strftime(fmt = '%FT%T%:z') ⇒ Object
1309 1310 1311 |
# File 'lib/date/format.rb', line 1309 def strftime(fmt='%FT%T%:z') super(fmt) end |
#to_date ⇒ Object
1827 |
# File 'lib/date.rb', line 1827 def to_date() Date.new!(jd_to_ajd(jd, 0, 0), 0, @sg) end |
#to_datetime ⇒ Object
1828 |
# File 'lib/date.rb', line 1828 def to_datetime() self end |
#to_s ⇒ Object
4p
1759 1760 1761 1762 |
# File 'lib/date.rb', line 1759 def to_s # 4p format('%.4d-%02d-%02dT%02d:%02d:%02d%s', year, mon, mday, hour, min, sec, zone) end |
#to_time ⇒ Object
1819 1820 1821 1822 1823 1824 1825 |
# File 'lib/date.rb', line 1819 def to_time d = new_offset(0) d.instance_eval do Time.utc(year(), mon(), mday(), hour(), min(), sec + sec_fraction) end. getlocal end |
#xmlschema(n = 0) ⇒ Object
:nodoc:
1335 |
# File 'lib/date/format.rb', line 1335 def xmlschema(n=0) iso8601(n) end |