Class: Date

Inherits:
Object show all
Includes:
Comparable
Defined in:
lib/date.rb,
lib/date.rb,
lib/date/format.rb

Overview

Class representing a date.

See the documentation to the file date.rb for an overview.

Internally, the date is represented as an Astronomical Julian Day Number, ajd. The Day of Calendar Reform, sg, is also stored, for conversions to other date formats. (There is also an of field for a time zone offset, but this is only for the use of the DateTime subclass.)

A new Date object is created using one of the object creation class methods named after the corresponding date format, and the arguments appropriate to that date format; for instance, Date::civil() (aliased to Date::new()) with year, month, and day-of-month, or Date::ordinal() with year and day-of-year. All of these object creation class methods also take the Day of Calendar Reform as an optional argument.

Date objects are immutable once created.

Once a Date has been created, date values can be retrieved for the different date formats supported using instance methods. For instance, #mon() gives the Civil month, #cwday() gives the Commercial day of the week, and #yday() gives the Ordinal day of the year. Date values can be retrieved in any format, regardless of what format was used to create the Date instance.

The Date class includes the Comparable module, allowing date objects to be compared and sorted, ranges of dates to be created, and so forth.

Direct Known Subclasses

DateTime, DateTimeME

Defined Under Namespace

Modules: Format Classes: Infinity

Constant Summary collapse

ITALY =

The Julian Day Number of the Day of Calendar Reform for Italy and the Catholic countries.

2299161
ENGLAND =

The Julian Day Number of the Day of Calendar Reform for England and her Colonies.

2361222
JULIAN =

A constant used to indicate that a Date should always use the Julian calendar.

Infinity.new
GREGORIAN =

A constant used to indicate that a Date should always use the Gregorian calendar.

Infinity.new(-1)
UNIXEPOCH =

1970-01-01 :nodoc:

2440588
MONTHNAMES =

Full month names, in English. Months count from 1 to 12; a month’s numerical representation indexed into this array gives the name of that month (hence the first element is nil).

[nil] + %w(January February March April May June July
August September October November December)
DAYNAMES =

Full names of days of the week, in English. Days of the week count from 0 to 6 (except in the commercial week); a day’s numerical representation indexed into this array gives the name of that day.

%w(Sunday Monday Tuesday Wednesday Thursday Friday Saturday)
ABBR_MONTHNAMES =

Abbreviated month names, in English.

[nil] + %w(Jan Feb Mar Apr May Jun
Jul Aug Sep Oct Nov Dec)
ABBR_DAYNAMES =

Abbreviated day names, in English.

%w(Sun Mon Tue Wed Thu Fri Sat)

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Comparable

#<, #<=, #==, #>, #>=, #between?

Constructor Details

#initialize(ajd = 0, of = 0, sg = ITALY) ⇒ Date

NOTE this is the documentation for the method new!(). If you are reading this as the documentation for new(), that is because rdoc doesn’t fully support the aliasing of the initialize() method. new() is in fact an alias for #civil(): read the documentation for that method instead.

Create a new Date object.

ajd is the Astronomical Julian Day Number. of is the offset from UTC as a fraction of a day. Both default to 0.

sg specifies the Day of Calendar Reform to use for this Date object.

Using one of the factory methods such as Date::civil is generally easier and safer.



1118
# File 'lib/date.rb', line 1118

def initialize(ajd=0, of=0, sg=ITALY) @ajd, @of, @sg = ajd, of, sg end

Class Method Details

._httpdate(str) ⇒ Object

:nodoc:



1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
# File 'lib/date/format.rb', line 1221

def self._httpdate(str) # :nodoc:
  if /\A\s*(#{Format::ABBR_DAYS.keys.join('|')})\s*,\s+
	\d{2}\s+
	(#{Format::ABBR_MONTHS.keys.join('|')})\s+
	-?\d{4}\s+ # allow minus, anyway
	\d{2}:\d{2}:\d{2}\s+
	gmt\s*\z/inox =~ str
    _rfc2822(str)
  elsif /\A\s*(#{Format::DAYS.keys.join('|')})\s*,\s+
	\d{2}\s*-\s*
	(#{Format::ABBR_MONTHS.keys.join('|')})\s*-\s*
	\d{2}\s+
	\d{2}:\d{2}:\d{2}\s+
	gmt\s*\z/inox =~ str
    _parse(str)
  elsif /\A\s*(#{Format::ABBR_DAYS.keys.join('|')})\s+
	(#{Format::ABBR_MONTHS.keys.join('|')})\s+
	\d{1,2}\s+
	\d{2}:\d{2}:\d{2}\s+
	\d{4}\s*\z/inox =~ str
    _parse(str)
  end
end

._iso8601(str) ⇒ Object

:nodoc:



1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
# File 'lib/date/format.rb', line 1122

def self._iso8601(str) # :nodoc:
  if /\A\s*(([-+]?\d{2,}|-)-\d{2}-\d{2}|
     ([-+]?\d{2,})?-\d{3}|
     (\d{2}|\d{4})?-w\d{2}-\d|
     -w-\d)
	(t
	\d{2}:\d{2}(:\d{2}([,.]\d+)?)?
	(z|[-+]\d{2}(:?\d{2})?)?)?\s*\z/inx =~ str
    _parse(str)
  elsif /\A\s*(([-+]?(\d{2}|\d{4})|--)\d{2}\d{2}|
     ([-+]?(\d{2}|\d{4}))?\d{3}|-\d{3}|
     (\d{2}|\d{4})?w\d{2}\d)
	(t?
	\d{2}\d{2}(\d{2}([,.]\d+)?)?
	(z|[-+]\d{2}(\d{2})?)?)?\s*\z/inx =~ str
    _parse(str)
  elsif /\A\s*(\d{2}:\d{2}(:\d{2}([,.]\d+)?)?
	(z|[-+]\d{2}(:?\d{2})?)?)?\s*\z/inx =~ str
    _parse(str)
  elsif /\A\s*(\d{2}\d{2}(\d{2}([,.]\d+)?)?
	(z|[-+]\d{2}(\d{2})?)?)?\s*\z/inx =~ str
    _parse(str)
  end
end

._jisx0301(str) ⇒ Object

:nodoc:



1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
# File 'lib/date/format.rb', line 1245

def self._jisx0301(str) # :nodoc:
  if /\A\s*[mtsh]?\d{2}\.\d{2}\.\d{2}
	(t
	(\d{2}:\d{2}(:\d{2}([,.]\d*)?)?
	(z|[-+]\d{2}(:?\d{2})?)?)?)?\s*\z/inx =~ str
    if /\A\s*\d/ =~ str
	_parse(str.sub(/\A\s*(\d)/, 'h\1'))
    else
	_parse(str)
    end
  else
    _iso8601(str)
  end
end

._parse(str, comp = true) ⇒ Object



1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
# File 'lib/date/format.rb', line 1056

def self._parse(str, comp=true)
  str = str.dup

  e = Date::Format::Bag.new

  e._comp = comp

  str.gsub!(/[^-+',.\/:@[:alnum:]\[\]\x80-\xff]+/n, ' ')

  _parse_time(str, e) # || _parse_beat(str, e)
  _parse_day(str, e)

  _parse_eu(str, e)     ||
  _parse_us(str, e)     ||
  _parse_iso(str, e)    ||
  _parse_jis(str, e)    ||
  _parse_vms(str, e)    ||
  _parse_sla(str, e)    ||
  _parse_dot(str, e)    ||
  _parse_iso2(str, e)   ||
  _parse_year(str, e)   ||
  _parse_mon(str, e)    ||
  _parse_mday(str, e)   ||
  _parse_ddd(str, e)

  if str.sub!(/\b(bc\b|bce\b|b\.c\.|b\.c\.e\.)/in, ' ')
    if e.year
	e.year = -e.year + 1
    end
  end

  if str.sub!(/\A\s*(\d{1,2})\s*\z/n, ' ')
    if e.hour && !e.mday
	v = $1.to_i
	if (1..31) === v
 e.mday = v
	end
    end
    if e.mday && !e.hour
	v = $1.to_i
	if (0..24) === v
 e.hour = v
	end
    end
  end

  if e._comp
    if e.cwyear
	if e.cwyear >= 0 && e.cwyear <= 99
 e.cwyear += if e.cwyear >= 69
      then 1900 else 2000 end
	end
    end
    if e.year
	if e.year >= 0 && e.year <= 99
 e.year += if e.year >= 69
    then 1900 else 2000 end
	end
    end
  end

  e.offset ||= zone_to_diff(e.zone) if e.zone

  e.to_hash
end

._rfc2822(str) ⇒ Object Also known as: _rfc822

:nodoc:



1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
# File 'lib/date/format.rb', line 1200

def self._rfc2822(str) # :nodoc:
  if /\A\s*(?:(?:#{Format::ABBR_DAYS.keys.join('|')})\s*,\s+)?
	\d{1,2}\s+
	(?:#{Format::ABBR_MONTHS.keys.join('|')})\s+
	-?(\d{2,})\s+ # allow minus, anyway
	\d{2}:\d{2}(:\d{2})?\s*
	(?:[-+]\d{4}|ut|gmt|e[sd]t|c[sd]t|m[sd]t|p[sd]t|[a-ik-z])\s*\z/inox =~ str
    e = _parse(str, false)
    if $1.size < 4
	if e[:year] < 50
 e[:year] += 2000
	elsif e[:year] < 1000
 e[:year] += 1900
	end
    end
    e
  end
end

._rfc3339(str) ⇒ Object

:nodoc:



1147
1148
1149
1150
1151
1152
1153
1154
# File 'lib/date/format.rb', line 1147

def self._rfc3339(str) # :nodoc:
  if /\A\s*-?\d{4}-\d{2}-\d{2} # allow minus, anyway
	(t|\s)
	\d{2}:\d{2}:\d{2}(\.\d+)?
	(z|[-+]\d{2}:\d{2})\s*\z/inx =~ str
    _parse(str)
  end
end

._strptime(str, fmt = '%F') ⇒ Object



600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
# File 'lib/date/format.rb', line 600

def self._strptime(str, fmt='%F')
  str = str.dup
  e = Format::Bag.new
  return unless _strptime_i(str, fmt, e)

  if e._cent
    if e.cwyear
	e.cwyear += e._cent * 100
    end
    if e.year
	e.  year += e._cent * 100
    end
  end

  if e._merid
    if e.hour
	e.hour %= 12
	e.hour += e._merid
    end
  end

  unless str.empty?
    e.leftover = str
  end

  e.to_hash
end

._xmlschema(str) ⇒ Object

:nodoc:



1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
# File 'lib/date/format.rb', line 1156

def self._xmlschema(str) # :nodoc:
  if /\A\s*(-?\d{4,})(?:-(\d{2})(?:-(\d{2}))?)?
	(?:t
 (\d{2}):(\d{2}):(\d{2})(?:\.(\d+))?)?
	(z|[-+]\d{2}:\d{2})?\s*\z/inx =~ str
    e = Format::Bag.new
    e.year = $1.to_i
    e.mon = $2.to_i if $2
    e.mday = $3.to_i if $3
    e.hour = $4.to_i if $4
    e.min = $5.to_i if $5
    e.sec = $6.to_i if $6
    e.sec_fraction = Rational($7.to_i, 10**$7.size) if $7
    if $8
	e.zone = $8
	e.offset = zone_to_diff($8)
    end
    e.to_hash
  elsif /\A\s*(\d{2}):(\d{2}):(\d{2})(?:\.(\d+))?
	(z|[-+]\d{2}:\d{2})?\s*\z/inx =~ str
    e = Format::Bag.new
    e.hour = $1.to_i if $1
    e.min = $2.to_i if $2
    e.sec = $3.to_i if $3
    e.sec_fraction = Rational($4.to_i, 10**$4.size) if $4
    if $5
	e.zone = $5
	e.offset = zone_to_diff($5)
    end
    e.to_hash
  elsif /\A\s*(?:--(\d{2})(?:-(\d{2}))?|---(\d{2}))
	(z|[-+]\d{2}:\d{2})?\s*\z/inx =~ str
    e = Format::Bag.new
    e.mon = $1.to_i if $1
    e.mday = $2.to_i if $2
    e.mday = $3.to_i if $3
    if $4
	e.zone = $4
	e.offset = zone_to_diff($4)
    end
    e.to_hash
  end
end

.civil(y = -4712,, m = 1, d = 1, sg = ITALY) ⇒ Object

Create a new Date object for the Civil Date specified by year y, month m, and day-of-month d.

m and d can be negative, in which case they count backwards from the end of the year and the end of the month respectively. No wraparound is performed, however, and invalid values cause an ArgumentError to be raised. can be negative

y defaults to -4712, m to 1, and d to 1; this is Julian Day Number day 0.

sg specifies the Day of Calendar Reform.



787
788
789
790
791
792
# File 'lib/date.rb', line 787

def self.civil(y=-4712, m=1, d=1, sg=ITALY)
  unless jd = _valid_civil?(y, m, d, sg)
    raise ArgumentError, 'invalid date'
  end
  new!(jd_to_ajd(jd, 0, 0), 0, sg)
end

.commercial(y = -4712,, w = 1, d = 1, sg = ITALY) ⇒ Object

Create a new Date object for the Commercial Date specified by year y, week-of-year w, and day-of-week d.

Monday is day-of-week 1; Sunday is day-of-week 7.

w and d can be negative, in which case they count backwards from the end of the year and the end of the week respectively. No wraparound is performed, however, and invalid values cause an ArgumentError to be raised.

y defaults to -4712, w to 1, and d to 1; this is Julian Day Number day 0.

sg specifies the Day of Calendar Reform.



810
811
812
813
814
815
# File 'lib/date.rb', line 810

def self.commercial(y=-4712, w=1, d=1, sg=ITALY)
  unless jd = _valid_commercial?(y, w, d, sg)
    raise ArgumentError, 'invalid date'
  end
  new!(jd_to_ajd(jd, 0, 0), 0, sg)
end

.gregorian_leap?(y) ⇒ Boolean Also known as: leap?

Is a year a leap year in the Gregorian calendar?

All years divisible by 4 are leap years in the Gregorian calendar, except for years divisible by 100 and not by 400.

Returns:

  • (Boolean)


706
# File 'lib/date.rb', line 706

def self.gregorian_leap? (y) y % 4 == 0 && y % 100 != 0 || y % 400 == 0 end

.httpdate(str = 'Mon, 01 Jan -4712 00:00:00 GMT', sg = ITALY) ⇒ Object

:nodoc:



1071
1072
1073
1074
# File 'lib/date.rb', line 1071

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-01', sg = ITALY) ⇒ Object

:nodoc:



1049
1050
1051
1052
# File 'lib/date.rb', line 1049

def self.iso8601(str='-4712-01-01', sg=ITALY) # :nodoc:
  elem = _iso8601(str)
  new_by_frags(elem, sg)
end

.jd(jd = 0, sg = ITALY) ⇒ Object

Create a new Date object from a Julian Day Number.

jd is the Julian Day Number; if not specified, it defaults to 0. sg specifies the Day of Calendar Reform.



752
753
754
755
# File 'lib/date.rb', line 752

def self.jd(jd=0, sg=ITALY)
  jd = _valid_jd?(jd, sg)
  new!(jd_to_ajd(jd, 0, 0), 0, sg)
end

.jisx0301(str = '-4712-01-01', sg = ITALY) ⇒ Object

:nodoc:



1076
1077
1078
1079
# File 'lib/date.rb', line 1076

def self.jisx0301(str='-4712-01-01', sg=ITALY) # :nodoc:
  elem = _jisx0301(str)
  new_by_frags(elem, sg)
end

.julian_leap?(y) ⇒ Boolean

Is a year a leap year in the Julian calendar?

All years divisible by 4 are leap years in the Julian calendar.

Returns:

  • (Boolean)


700
# File 'lib/date.rb', line 700

def self.julian_leap? (y) y % 4 == 0 end

.newObject Also known as: new!

Create a new Date object for the Civil Date specified by year y, month m, and day-of-month d.

m and d can be negative, in which case they count backwards from the end of the year and the end of the month respectively. No wraparound is performed, however, and invalid values cause an ArgumentError to be raised. can be negative

y defaults to -4712, m to 1, and d to 1; this is Julian Day Number day 0.

sg specifies the Day of Calendar Reform.



794
795
796
797
798
799
# File 'lib/date.rb', line 794

def self.civil(y=-4712, m=1, d=1, sg=ITALY)
  unless jd = _valid_civil?(y, m, d, sg)
    raise ArgumentError, 'invalid date'
  end
  new!(jd_to_ajd(jd, 0, 0), 0, sg)
end

.once(*ids) ⇒ Object

:nodoc:



1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
# File 'lib/date.rb', line 1083

def once(*ids) # :nodoc:
  for id in ids
	module_eval <<-"end;"
	  alias_method :__#{id.to_i}__, :#{id.to_s}
	  private :__#{id.to_i}__
	  def #{id.to_s}(*args, &block)
 (@__#{id.to_i}__ ||= [__#{id.to_i}__(*args, &block)])[0]
	  end
	end;
  end
end

.ordinal(y = -4712,, d = 1, sg = ITALY) ⇒ Object

Create a new Date object from an Ordinal Date, specified by year y and day-of-year d. d can be negative, in which it counts backwards from the end of the year. No year wraparound is performed, however. An invalid value for d results in an ArgumentError being raised.

y defaults to -4712, and d to 1; this is Julian Day Number day 0.

sg specifies the Day of Calendar Reform.



767
768
769
770
771
772
# File 'lib/date.rb', line 767

def self.ordinal(y=-4712, d=1, sg=ITALY)
  unless jd = _valid_ordinal?(y, d, sg)
    raise ArgumentError, 'invalid date'
  end
  new!(jd_to_ajd(jd, 0, 0), 0, sg)
end

.parse(str = '-4712-01-01', hints = {}, sg = ITALY) ⇒ Object

Create a new Date object by parsing from a String, without specifying the format.

str is a String holding a date 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 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-01’; this is Julian Day Number day 0.

sg specifies the Day of Calendar Reform.



1044
1045
1046
1047
# File 'lib/date.rb', line 1044

def self.parse(str='-4712-01-01', hints={}, sg=ITALY)
  elem = _parse(str, hints)
  new_by_frags(elem, sg)
end

.rfc2822(str = 'Mon, 1 Jan -4712 00:00:00 +0000', sg = ITALY) ⇒ Object

:nodoc:



1064
1065
1066
1067
# File 'lib/date.rb', line 1064

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:



1054
1055
1056
1057
# File 'lib/date.rb', line 1054

def self.rfc3339(str='-4712-01-01T00:00:00+00:00', sg=ITALY) # :nodoc:
  elem = _rfc3339(str)
  new_by_frags(elem, sg)
end

.rfc822Object

:nodoc:



1069
1070
1071
1072
# File 'lib/date.rb', line 1069

def self.rfc2822(str='Mon, 1 Jan -4712 00:00:00 +0000', sg=ITALY) # :nodoc:
  elem = _rfc2822(str)
  new_by_frags(elem, sg)
end

.strptime(str = '-4712-01-01', fmt = '%F', sg = ITALY) ⇒ Object

Create a new Date object by parsing from a String according to a specified format.

str is a String holding a date representation. fmt is the format that the date is in. See date/format.rb for details on supported formats.

The default str is ‘-4712-01-01’, and the default fmt is ‘%F’, which means Year-Month-Day_of_Month. This gives Julian Day Number day 0.

sg specifies the Day of Calendar Reform.

An ArgumentError will be raised if str cannot be parsed.



1024
1025
1026
1027
# File 'lib/date.rb', line 1024

def self.strptime(str='-4712-01-01', fmt='%F', sg=ITALY)
  elem = _strptime(str, fmt)
  new_by_frags(elem, sg)
end

.today(sg = ITALY) ⇒ Object

Create a new Date object representing today.

sg specifies the Day of Calendar Reform.



1765
# File 'lib/date.rb', line 1765

def self.today(sg=ITALY) Time.now.to_date    .new_start(sg) end

.valid_civil?(y, m, d, sg = ITALY) ⇒ Boolean Also known as: valid_date?

Returns:

  • (Boolean)


719
720
721
# File 'lib/date.rb', line 719

def self.valid_civil? (y, m, d, sg=ITALY)
  !!_valid_civil?(y, m, d, sg)
end

.valid_commercial?(y, w, d, sg = ITALY) ⇒ Boolean

Returns:

  • (Boolean)


725
726
727
# File 'lib/date.rb', line 725

def self.valid_commercial? (y, w, d, sg=ITALY)
  !!_valid_commercial?(y, w, d, sg)
end

.valid_jd?(jd, sg = ITALY) ⇒ Boolean

Returns:

  • (Boolean)


711
712
713
# File 'lib/date.rb', line 711

def self.valid_jd? (jd, sg=ITALY)
  !!_valid_jd?(jd, sg)
end

.valid_ordinal?(y, d, sg = ITALY) ⇒ Boolean

Returns:

  • (Boolean)


715
716
717
# File 'lib/date.rb', line 715

def self.valid_ordinal? (y, d, sg=ITALY)
  !!_valid_ordinal?(y, d, sg)
end

.xmlschema(str = '-4712-01-01', sg = ITALY) ⇒ Object

:nodoc:



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

def self.xmlschema(str='-4712-01-01', sg=ITALY) # :nodoc:
  elem = _xmlschema(str)
  new_by_frags(elem, sg)
end

Instance Method Details

#+(n) ⇒ Object

Return a new Date object that is n days later than the current one.

n may be a negative value, in which case the new Date is earlier than the current one; however, #-() might be more intuitive.

If n is not a Numeric, a TypeError will be thrown. In particular, two Dates cannot be added to each other.

Raises:

  • (TypeError)


1308
1309
1310
1311
1312
1313
# File 'lib/date.rb', line 1308

def + (n)
  case n
  when Numeric; return self.class.new!(@ajd + n, @of, @sg)
  end
  raise TypeError, 'expected numeric'
end

#-(x) ⇒ Object

If x is a Numeric value, create a new Date object that is x days earlier than the current one.

If x is a Date, return the number of days between the two dates; or, more precisely, how many days later the current date is than x.

If x is neither Numeric nor a Date, a TypeError is raised.

Raises:

  • (TypeError)


1323
1324
1325
1326
1327
1328
1329
# File 'lib/date.rb', line 1323

def - (x)
  case x
  when Numeric; return self.class.new!(@ajd - x, @of, @sg)
  when Date;    return @ajd - x.ajd
  end
  raise TypeError, 'expected numeric or date'
end

#<<(n) ⇒ Object

Return a new Date object that is n months earlier than the current one.

If the day-of-the-month of the current Date is greater than the last day of the target month, the day-of-the-month of the returned Date will be the last day of the target month.



1392
# File 'lib/date.rb', line 1392

def << (n) self >> -n end

#<=>(other) ⇒ Object

Compare this date with another date.

other can also be a Numeric value, in which case it is interpreted as an Astronomical Julian Day Number.

Comparison is by Astronomical Julian Day Number, including fractional days. This means that both the time and the timezone offset are taken into account when comparing two DateTime instances. When comparing a DateTime instance with a Date instance, the time of the latter will be considered as falling on midnight UTC.



1342
1343
1344
1345
1346
1347
1348
# File 'lib/date.rb', line 1342

def <=> (other)
  case other
  when Numeric; return @ajd <=> other
  when Date;    return @ajd <=> other.ajd
  end
  nil
end

#===(other) ⇒ Object

The relationship operator for Date.

Compares dates by Julian Day Number. When comparing two DateTime instances, or a DateTime with a Date, the instances will be regarded as equivalent if they fall on the same date in local time.



1356
1357
1358
1359
1360
1361
1362
# File 'lib/date.rb', line 1356

def === (other)
  case other
  when Numeric; return jd == other
  when Date;    return jd == other.jd
  end
  false
end

#>>(n) ⇒ Object

Return a new Date object that is n months later than the current one.

If the day-of-the-month of the current Date is greater than the last day of the target month, the day-of-the-month of the returned Date will be the last day of the target month.



1378
1379
1380
1381
1382
1383
1384
# File 'lib/date.rb', line 1378

def >> (n)
  y, m = (year * 12 + (mon - 1) + n).divmod(12)
  m,   = (m + 1)                    .divmod(1)
  d = mday
  d -= 1 until jd2 = _valid_civil?(y, m, d, @sg)
  self + (jd2 - jd)
end

#ajdObject

Get the date as an Astronomical Julian Day Number.



1121
# File 'lib/date.rb', line 1121

def ajd() @ajd end

#amjdObject

Get the date as an Astronomical Modified Julian Day Number.



1124
# File 'lib/date.rb', line 1124

def amjd() ajd_to_amjd(@ajd) end

#asctimeObject Also known as: ctime

alias_method :format, :strftime



367
# File 'lib/date/format.rb', line 367

def asctime() strftime('%c') end

#cwdayObject

Get the commercial day of the week of this date. Monday is commercial day-of-week 1; Sunday is commercial day-of-week 7.



1216
# File 'lib/date.rb', line 1216

def cwday() commercial[2] end

#cweekObject

Get the commercial week of the year of this date.



1212
# File 'lib/date.rb', line 1212

def cweek() commercial[1] end

#cwyearObject

Get the commercial year of this date. See Commercial Date in the introduction for how this differs from the normal year.



1209
# File 'lib/date.rb', line 1209

def cwyear() commercial[0] end

#day_fractionObject

Get any fractional day part of the date.



1132
# File 'lib/date.rb', line 1132

def day_fraction() ajd_to_jd(@ajd, @of)[1] end

#downto(min, &block) ⇒ Object

Step backward one day at a time until we reach min (inclusive), yielding each date as we go.



1432
1433
1434
# File 'lib/date.rb', line 1432

def downto(min, &block) # :yield: date
  step(min, -1, &block)
end

#englandObject

Create a copy of this Date object that uses the English/Colonial Day of Calendar Reform.



1278
# File 'lib/date.rb', line 1278

def england() new_start(self.class::ENGLAND) end

#eql?(other) ⇒ Boolean

Is this Date equal to other?

other must both be a Date object, and represent the same date.

Returns:

  • (Boolean)


1439
# File 'lib/date.rb', line 1439

def eql? (other) Date === other && self == other end

#gregorianObject

Create a copy of this Date object that always uses the Gregorian Calendar.



1286
# File 'lib/date.rb', line 1286

def gregorian() new_start(self.class::GREGORIAN) end

#gregorian?Boolean

Is the current date new-style (Gregorian Calendar)?

Returns:

  • (Boolean)


1246
# File 'lib/date.rb', line 1246

def gregorian? () !julian? end

#hashObject

Calculate a hash value for this date.



1442
# File 'lib/date.rb', line 1442

def hash() @ajd.hash end

#httpdateObject

:nodoc:



381
# File 'lib/date/format.rb', line 381

def httpdate() new_offset(0).strftime('%a, %d %b %Y %T GMT') end

#inspectObject

Return internal object state as a programmer-readable string.



1445
# File 'lib/date.rb', line 1445

def inspect() format('#<%s: %s,%s,%s>', self.class, @ajd, @of, @sg) end

#iso8601Object



371
# File 'lib/date/format.rb', line 371

def iso8601() strftime('%F') end

#italyObject

Create a copy of this Date object that uses the Italian/Catholic Day of Calendar Reform.



1274
# File 'lib/date.rb', line 1274

def italy() new_start(self.class::ITALY) end

#jdObject

Get the date as a Julian Day Number.



1129
# File 'lib/date.rb', line 1129

def jd() ajd_to_jd(@ajd, @of)[0] end

#jisx0301Object



383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
# File 'lib/date/format.rb', line 383

def jisx0301
  if jd < 2405160
    iso8601
  else
    case jd
    when 2405160...2419614
	g = 'M%02d' % (year - 1867)
    when 2419614...2424875
	g = 'T%02d' % (year - 1911)
    when 2424875...2447535
	g = 'S%02d' % (year - 1925)
    else
	g = 'H%02d' % (year - 1988)
    end
    g + strftime('.%m.%d')
  end
end

#julianObject

Create a copy of this Date object that always uses the Julian Calendar.



1282
# File 'lib/date.rb', line 1282

def julian() new_start(self.class::JULIAN) end

#julian?Boolean

Is the current date old-style (Julian Calendar)?

Returns:

  • (Boolean)


1243
# File 'lib/date.rb', line 1243

def julian? () jd < @sg end

#ldObject

Get the date as the number of days since the Day of Calendar Reform (in Italy and the Catholic countries).



1139
# File 'lib/date.rb', line 1139

def ld() jd_to_ld(jd) end

#leap?Boolean

Is this a leap year?

Returns:

  • (Boolean)


1259
1260
1261
1262
# File 'lib/date.rb', line 1259

def leap?
  jd_to_civil(civil_to_jd(year, 3, 1, fix_style) - 1,
fix_style)[-1] == 29
end

#marshal_dumpObject

Dump to Marshal format.



1453
# File 'lib/date.rb', line 1453

def marshal_dump() [@ajd, @of, @sg] end

#marshal_load(a) ⇒ Object

Load from Marshall format.



1456
# File 'lib/date.rb', line 1456

def marshal_load(a) @ajd, @of, @sg, = a end

#mdayObject Also known as: day

Get the day-of-the-month of this date.



1172
# File 'lib/date.rb', line 1172

def mday() civil[2] end

#mjdObject

Get the date as a Modified Julian Day Number.



1135
# File 'lib/date.rb', line 1135

def mjd() jd_to_mjd(jd) end

#monObject Also known as: month

Get the month of this date.

January is month 1.



1169
# File 'lib/date.rb', line 1169

def mon() civil[1] end

#new_start(sg = self.class::ITALY) ⇒ Object

Create a copy of this Date object using a new Day of Calendar Reform.



1270
# File 'lib/date.rb', line 1270

def new_start(sg=self.class::ITALY) self.class.new!(@ajd, @of, sg) end

#nextObject Also known as: succ

Return a new Date one day after this one.



1368
# File 'lib/date.rb', line 1368

def next() next_day end

#next_day(n = 1) ⇒ Object



1364
# File 'lib/date.rb', line 1364

def next_day(n=1) self + n end

#next_month(n = 1) ⇒ Object



1394
# File 'lib/date.rb', line 1394

def next_month(n=1) self >> n end

#next_year(n = 1) ⇒ Object



1397
# File 'lib/date.rb', line 1397

def next_year(n=1) self >> n * 12 end

#prev_day(n = 1) ⇒ Object



1365
# File 'lib/date.rb', line 1365

def prev_day(n=1) self - n end

#prev_month(n = 1) ⇒ Object



1395
# File 'lib/date.rb', line 1395

def prev_month(n=1) self << n end

#prev_year(n = 1) ⇒ Object



1398
# File 'lib/date.rb', line 1398

def prev_year(n=1) self << n * 12 end

#rfc2822Object Also known as: rfc822



377
# File 'lib/date/format.rb', line 377

def rfc2822() strftime('%a, %-d %b %Y %T %z') end

#rfc3339Object



373
# File 'lib/date/format.rb', line 373

def rfc3339() iso8601 end

#startObject

When is the Day of Calendar Reform for this Date object?



1267
# File 'lib/date.rb', line 1267

def start() @sg end

#step(limit, step = 1) ⇒ Object

Step the current date forward step days at a time (or backward, if step is negative) until we reach limit (inclusive), yielding the resultant date at each step.



1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
# File 'lib/date.rb', line 1406

def step(limit, step=1) # :yield: date
=begin
  if step.zero?
    raise ArgumentError, "step can't be 0"
  end
=end
  unless block_given?
    return to_enum(:step, limit, step)
  end
  da = self
  op = %w(- <= >=)[step <=> 0]
  while da.__send__(op, limit)
    yield da
    da += step
  end
  self
end

#strftime(fmt = '%F') ⇒ Object



243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
# File 'lib/date/format.rb', line 243

def strftime(fmt='%F')
  fmt.gsub(/%([-_0^#]+)?(\d+)?([EO]?(?::{1,3}z|.))/m) do
    f = {}
    m = $&
    s, w, c = $1, $2, $3
    if s
	s.scan(/./) do |k|
 case k
 when '-'; f[:p] = '-'
 when '_'; f[:p] = "\s"
 when '0'; f[:p] = '0'
 when '^'; f[:u] = true
 when '#'; f[:x] = true
 end
	end
    end
    if w
	f[:w] = w.to_i
    end
    case c
    when 'A'; emit_ad(DAYNAMES[wday], 0, f)
    when 'a'; emit_ad(ABBR_DAYNAMES[wday], 0, f)
    when 'B'; emit_ad(MONTHNAMES[mon], 0, f)
    when 'b'; emit_ad(ABBR_MONTHNAMES[mon], 0, f)
    when 'C', 'EC'; emit_sn((year / 100).floor, 2, f)
    when 'c', 'Ec'; emit_a(strftime('%a %b %e %H:%M:%S %Y'), 0, f)
    when 'D'; emit_a(strftime('%m/%d/%y'), 0, f)
    when 'd', 'Od'; emit_n(mday(), 2, f)
    when 'e', 'Oe'; emit_a(mday(), 2, f)
    when 'F'
	if m == '%F'
 format('%.4d-%02d-%02d', year(), mon(), mday()) # 4p
	else
 emit_a(strftime('%Y-%m-%d'), 0, f)
	end
    when 'G'; emit_sn(cwyear(), 4, f)
    when 'g'; emit_n(cwyear % 100, 2, f)
    when 'H', 'OH'; emit_n(hour(), 2, f)
    when 'h'; emit_ad(strftime('%b'), 0, f)
    when 'I', 'OI'; emit_n((hour % 12).nonzero? || 12, 2, f)
    when 'j'; emit_n(yday(), 3, f)
    when 'k'; emit_a(hour(), 2, f)
    when 'L'
	w = f[:w] || 3
	u = 10**w
	emit_n((sec_fraction * u).floor, w, f)
    when 'l'; emit_a((hour % 12).nonzero? || 12, 2, f)
    when 'M', 'OM'; emit_n(min(), 2, f)
    when 'm', 'Om'; emit_n(mon(), 2, f)
    when 'N'
	w = f[:w] || 9
	u = 10**w
	emit_n((sec_fraction * u).floor, w, f)
    when 'n'; emit_a("\n", 0, f)
    when 'P'; emit_ad(strftime('%p').downcase, 0, f)
    when 'p'; emit_au(if hour < 12 then 'AM' else 'PM' end, 0, f)
    when 'Q'
	s = ((ajd - UNIX_EPOCH_IN_AJD) / MILLISECONDS_IN_DAY).round
	emit_sn(s, 1, f)
    when 'R'; emit_a(strftime('%H:%M'), 0, f)
    when 'r'; emit_a(strftime('%I:%M:%S %p'), 0, f)
    when 'S', 'OS'; emit_n(sec(), 2, f)
    when 's'
	s = ((ajd - UNIX_EPOCH_IN_AJD) / Rational(1, 86400)).round
	emit_sn(s, 1, f)
    when 'T'
	if m == '%T'
 format('%02d:%02d:%02d', hour(), min(), sec()) # 4p
	else
 emit_a(strftime('%H:%M:%S'), 0, f)
	end
    when 't'; emit_a("\t", 0, f)
    when 'U', 'W', 'OU', 'OW'
	emit_n(if c[-1,1] == 'U' then wnum0 else wnum1 end, 2, f)
    when 'u', 'Ou'; emit_n(cwday(), 1, f)
    when 'V', 'OV'; emit_n(cweek(), 2, f)
    when 'v'; emit_a(strftime('%e-%b-%Y'), 0, f)
    when 'w', 'Ow'; emit_n(wday(), 1, f)
    when 'X', 'EX'; emit_a(strftime('%H:%M:%S'), 0, f)
    when 'x', 'Ex'; emit_a(strftime('%m/%d/%y'), 0, f)
    when 'Y', 'EY'; emit_sn(year(), 4, f)
    when 'y', 'Ey', 'Oy'; emit_n(year % 100, 2, f)
    when 'Z'; emit_au(strftime('%:z'), 0, f)
    when /\A(:{0,3})z/
	t = $1.size
	sign = if offset < 0 then -1 else +1 end
	fr = offset.abs
	ss = fr.div(Rational(1, 86400)) # 4p
	hh, ss = ss.divmod(3600)
	mm, ss = ss.divmod(60)
	if t == 3
 if    ss.nonzero? then t =  2
 elsif mm.nonzero? then t =  1
 else                   t = -1
 end
	end
	case t
	when -1
 tail = []
 sep = ''
	when 0
 f[:w] -= 2 if f[:w]
 tail = ['%02d' % mm]
 sep = ''
	when 1
 f[:w] -= 3 if f[:w]
 tail = ['%02d' % mm]
 sep = ':'
	when 2
 f[:w] -= 6 if f[:w]
 tail = ['%02d' % mm, '%02d' % ss]
 sep = ':'
	end
	([emit_z(sign * hh, 2, f)] + tail).join(sep)
    when '%'; emit_a('%', 0, f)
    when '+'; emit_a(strftime('%a %b %e %H:%M:%S %Z %Y'), 0, f)
    else
	m
    end
  end
end

#to_dateObject



1759
# File 'lib/date.rb', line 1759

def to_date() self end

#to_datetimeObject



1760
# File 'lib/date.rb', line 1760

def to_datetime() DateTime.new!(jd_to_ajd(jd, 0, 0), @of, @sg) end

#to_sObject

Return the date as a human-readable string.

The format used is YYYY-MM-DD.



1450
# File 'lib/date.rb', line 1450

def to_s() strftime end

#to_timeObject



1758
# File 'lib/date.rb', line 1758

def to_time() Time.local(year(), mon(), mday()) end

#upto(max, &block) ⇒ Object

Step forward one day at a time until we reach max (inclusive), yielding each date as we go.



1426
1427
1428
# File 'lib/date.rb', line 1426

def upto(max, &block) # :yield: date
  step(max, +1, &block)
end

#wdayObject

Get the week day of this date. Sunday is day-of-week 0; Saturday is day-of-week 6.



1220
# File 'lib/date.rb', line 1220

def wday() jd_to_wday(jd) end

#xmlschemaObject

:nodoc:



375
# File 'lib/date/format.rb', line 375

def xmlschema() iso8601 end

#ydayObject

Get the day-of-the-year of this date.

January 1 is day-of-the-year 1



1164
# File 'lib/date.rb', line 1164

def yday() ordinal[1] end

#yearObject

Get the year of this date.



1159
# File 'lib/date.rb', line 1159

def year() civil[0] end