Class: DateTime

Inherits:
Date
  • Object
show all
Defined in:
lib/date.rb,
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. The unit is in days. I do NOT recommend you to use this method.

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::ITALY, Date::JULIAN, Date::MONTHNAMES, Date::UNIXEPOCH

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Date

#+, #-, #<<, #<=>, #===, #>>, #_dump, _load, _parse, _parse_beat, _parse_day, _parse_ddd, _parse_eu, _parse_iso, _parse_iso2, _parse_jis, _parse_mday, _parse_mon, _parse_sla_eu, _parse_sla_ja, _parse_sla_us, _parse_time, _parse_us, _parse_vms, _parse_year, _strptime_i, #ajd, ajd_to_amjd, ajd_to_jd, #amjd, amjd_to_ajd, #asctime, civil_to_jd, commercial_to_jd, complete_frags, #cwday, #cweek, #cwyear, #day_fraction, day_fraction_to_time, #downto, #england, #eql?, fix_style, #gregorian, #gregorian?, gregorian?, gregorian_leap?, #hash, #initialize, #inspect, #italy, #jd, jd_to_ajd, jd_to_civil, jd_to_commercial, jd_to_ld, jd_to_mjd, jd_to_ordinal, jd_to_wday, jd_to_weeknum, #julian, julian?, #julian?, julian_leap?, #ld, ld_to_jd, #leap?, #mday, #mjd, mjd_to_jd, #mon, #new_start, #next, now, num_pattern?, ordinal_to_jd, rewrite_frags, s3e, #start, #step, time_to_day_fraction, #to_s, today, #upto, valid_civil?, valid_commercial?, valid_date_frags?, valid_jd?, valid_ordinal?, valid_time?, valid_time_frags?, valid_weeknum?, #wday, weeknum_to_jd, #yday, #year, zone_to_diff

Constructor Details

This class inherits a constructor from Date

Class Method Details

._strptime(str, fmt = '%FT%T%z')) ⇒ Object



1060
1061
1062
# File 'lib/date/format.rb', line 1060

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.



1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
# File 'lib/date.rb', line 1483

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 = (zone_to_diff(of) || 0).to_r/86400
  end
  new!(jd_to_ajd(jd, fr, of), of, sg)
end

.commercial(y = 1582, w = 41, d = 5, 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 1582, w to 41, and d to 5; this is the Day of Calendar Reform for Italy and the Catholic countries. The time values default to 0.



1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
# File 'lib/date.rb', line 1511

def self.commercial(y=1582, w=41, d=5, 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 = (zone_to_diff(of) || 0).to_r/86400
  end
  new!(jd_to_ajd(jd, fr, of), of, 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.



1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
# File 'lib/date.rb', line 1433

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 = (zone_to_diff(of) || 0).to_r/86400
  end
  new!(jd_to_ajd(jd, fr, of), of, sg)
end

.new_by_frags(elem, sg) ⇒ Object

:nodoc:



1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
# File 'lib/date.rb', line 1535

def self.new_by_frags(elem, sg) # :nodoc:
  elem = rewrite_frags(elem)
  elem = complete_frags(elem)
  unless (jd = valid_date_frags?(elem, sg)) &&
  (fr = valid_time_frags?(elem))
    raise ArgumentError, 'invalid date'
  end
  sf = (elem[:sec_fraction] || 0)
  fr += sf/86400
  of = (elem[:offset] || 0)
  of = of.to_r/86400
  new!(jd_to_ajd(jd, fr, of), of, 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.



1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
# File 'lib/date.rb', line 1458

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 = (zone_to_diff(of) || 0).to_r/86400
  end
  new!(jd_to_ajd(jd, fr, of), of, sg)
end

.parse(str = '-4712-01-01T00:00:00+00:00',, comp = false, 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.



1585
1586
1587
1588
# File 'lib/date.rb', line 1585

def self.parse(str='-4712-01-01T00:00:00+00:00', comp=false, sg=ITALY)
  elem = _parse(str, comp)
  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.



1565
1566
1567
1568
# File 'lib/date.rb', line 1565

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

.weeknum(y = 1582, w = 41, d = 5, f = 0, h = 0, min = 0, s = 0, of = 0, sg = ITALY) ⇒ Object

:nodoc:



1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
# File 'lib/date.rb', line 1522

def self.weeknum(y=1582, w=41, d=5, f=0, h=0, min=0, s=0, of=0, sg=ITALY) # :nodoc:
  unless (jd = valid_weeknum?(y, w, d, f, sg)) &&
  (fr = valid_time?(h, min, s))
    raise ArgumentError, 'invalid date'
  end
  if String === of
    of = (zone_to_diff(of) || 0).to_r/86400
  end
  new!(jd_to_ajd(jd, fr, of), of, sg)
end

Instance Method Details

#hourObject

#minObject

#new_offsetObject

#newofObject

#ofObject

#offsetObject

#secObject

#sec_fractionObject

#strftime(fmt = '%FT%T%:z')) ⇒ Object



1056
1057
1058
# File 'lib/date/format.rb', line 1056

def strftime(fmt='%FT%T%:z')
  super(fmt)
end

#zoneObject