2002-05-18 08:41:51 -04:00
|
|
|
# date.rb: Written by Tadayoshi Funaba 1998-2002
|
2002-06-22 11:26:58 -04:00
|
|
|
# $Id: date.rb,v 2.8 2002-06-08 00:39:51+09 tadf Exp $
|
2002-05-18 08:41:51 -04:00
|
|
|
|
|
|
|
require 'rational'
|
|
|
|
require 'date/format'
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
class Date
|
2000-07-18 03:37:02 -04:00
|
|
|
|
1998-01-16 07:13:05 -05:00
|
|
|
include Comparable
|
2000-07-18 03:37:02 -04:00
|
|
|
|
|
|
|
MONTHNAMES = [nil] + %w(January February March April May June July
|
|
|
|
August September October November December)
|
|
|
|
|
|
|
|
DAYNAMES = %w(Sunday Monday Tuesday Wednesday Thursday Friday Saturday)
|
|
|
|
|
2002-05-18 08:41:51 -04:00
|
|
|
ABBR_MONTHNAMES = [nil] + %w(Jan Feb Mar Apr May Jun
|
|
|
|
Jul Aug Sep Oct Nov Dec)
|
|
|
|
|
|
|
|
ABBR_DAYNAMES = %w(Sun Mon Tue Wed Thu Fri Sat)
|
|
|
|
|
2000-07-18 03:37:02 -04:00
|
|
|
ITALY = 2299161 # 1582-10-15
|
|
|
|
ENGLAND = 2361222 # 1752-09-14
|
|
|
|
JULIAN = false
|
|
|
|
GREGORIAN = true
|
|
|
|
|
2002-05-18 08:41:51 -04:00
|
|
|
def self.os? (jd, sg)
|
|
|
|
case sg
|
|
|
|
when Numeric; jd < sg
|
|
|
|
else; not sg
|
1998-01-16 07:13:05 -05:00
|
|
|
end
|
2002-05-18 08:41:51 -04:00
|
|
|
end
|
1999-01-19 23:59:39 -05:00
|
|
|
|
2002-05-18 08:41:51 -04:00
|
|
|
def self.ns? (jd, sg) not os?(jd, sg) end
|
1999-01-19 23:59:39 -05:00
|
|
|
|
2002-05-18 08:41:51 -04:00
|
|
|
def self.civil_to_jd(y, m, d, sg=GREGORIAN)
|
|
|
|
if m <= 2
|
|
|
|
y -= 1
|
|
|
|
m += 12
|
2000-07-18 03:37:02 -04:00
|
|
|
end
|
2002-05-18 08:41:51 -04:00
|
|
|
a = (y / 100.0).floor
|
|
|
|
b = 2 - a + (a / 4.0).floor
|
|
|
|
jd = (365.25 * (y + 4716)).floor +
|
|
|
|
(30.6001 * (m + 1)).floor +
|
|
|
|
d + b - 1524
|
|
|
|
if os?(jd, sg)
|
|
|
|
jd -= b
|
2000-07-18 03:37:02 -04:00
|
|
|
end
|
2002-05-18 08:41:51 -04:00
|
|
|
jd
|
|
|
|
end
|
1999-01-19 23:59:39 -05:00
|
|
|
|
2002-05-18 08:41:51 -04:00
|
|
|
def self.jd_to_civil(jd, sg=GREGORIAN)
|
|
|
|
if os?(jd, sg)
|
|
|
|
a = jd
|
|
|
|
else
|
|
|
|
x = ((jd - 1867216.25) / 36524.25).floor
|
|
|
|
a = jd + 1 + x - (x / 4.0).floor
|
2000-07-18 03:37:02 -04:00
|
|
|
end
|
2002-05-18 08:41:51 -04:00
|
|
|
b = a + 1524
|
|
|
|
c = ((b - 122.1) / 365.25).floor
|
|
|
|
d = (365.25 * c).floor
|
|
|
|
e = ((b - d) / 30.6001).floor
|
|
|
|
dom = b - d - (30.6001 * e).floor
|
|
|
|
if e <= 13
|
|
|
|
m = e - 1
|
|
|
|
y = c - 4716
|
|
|
|
else
|
|
|
|
m = e - 13
|
|
|
|
y = c - 4715
|
2000-07-18 03:37:02 -04:00
|
|
|
end
|
2002-05-18 08:41:51 -04:00
|
|
|
return y, m, dom
|
|
|
|
end
|
2000-07-06 03:21:26 -04:00
|
|
|
|
2002-05-18 08:41:51 -04:00
|
|
|
def self.ordinal_to_jd(y, d, sg=GREGORIAN)
|
|
|
|
civil_to_jd(y, 1, d, sg)
|
|
|
|
end
|
2000-07-18 03:37:02 -04:00
|
|
|
|
2002-05-18 08:41:51 -04:00
|
|
|
def self.jd_to_ordinal(jd, sg=GREGORIAN)
|
|
|
|
y = jd_to_civil(jd, sg)[0]
|
|
|
|
doy = jd - civil_to_jd(y - 1, 12, 31, ns?(jd, sg))
|
|
|
|
return y, doy
|
|
|
|
end
|
2000-07-18 03:37:02 -04:00
|
|
|
|
2002-05-18 08:41:51 -04:00
|
|
|
def self.jd_to_commercial(jd, sg=GREGORIAN)
|
|
|
|
ns = ns?(jd, sg)
|
|
|
|
a = jd_to_civil(jd - 3, ns)[0]
|
|
|
|
y = if jd >= commercial_to_jd(a + 1, 1, 1, ns) then a + 1 else a end
|
|
|
|
w = 1 + (jd - commercial_to_jd(y, 1, 1, ns)) / 7
|
|
|
|
d = (jd + 1) % 7
|
|
|
|
if d.zero? then d = 7 end
|
|
|
|
return y, w, d
|
|
|
|
end
|
2000-07-18 03:37:02 -04:00
|
|
|
|
2002-05-18 08:41:51 -04:00
|
|
|
def self.commercial_to_jd(y, w, d, ns=GREGORIAN)
|
|
|
|
jd = civil_to_jd(y, 1, 4, ns)
|
|
|
|
(jd - (((jd - 1) + 1) % 7)) +
|
|
|
|
7 * (w - 1) +
|
|
|
|
(d - 1)
|
|
|
|
end
|
2000-07-18 03:37:02 -04:00
|
|
|
|
2002-06-22 11:26:58 -04:00
|
|
|
%w(self.clfloor clfloor).each do |name|
|
|
|
|
module_eval <<-"end;"
|
|
|
|
def #{name}(x, y=1)
|
|
|
|
q, r = x.divmod(y)
|
|
|
|
q = q.to_i
|
|
|
|
return q, r
|
|
|
|
end
|
|
|
|
end;
|
2002-05-18 08:41:51 -04:00
|
|
|
end
|
2000-07-18 03:37:02 -04:00
|
|
|
|
2002-06-22 11:26:58 -04:00
|
|
|
private_class_method :clfloor
|
|
|
|
private :clfloor
|
|
|
|
|
|
|
|
def self.ajd_to_jd(ajd, of=0) clfloor(ajd + of + 1.to_r/2) end
|
|
|
|
def self.jd_to_ajd(jd, fr, of=0) jd + fr - of - 1.to_r/2 end
|
2000-07-18 03:37:02 -04:00
|
|
|
|
2002-05-18 08:41:51 -04:00
|
|
|
def self.day_fraction_to_time(fr)
|
|
|
|
h, fr = clfloor(fr, 1.to_r/24)
|
|
|
|
min, fr = clfloor(fr, 1.to_r/1440)
|
|
|
|
s, fr = clfloor(fr, 1.to_r/86400)
|
|
|
|
return h, min, s, fr
|
|
|
|
end
|
2000-07-18 03:37:02 -04:00
|
|
|
|
2002-05-18 08:41:51 -04:00
|
|
|
def self.time_to_day_fraction(h, min, s)
|
|
|
|
h.to_r/24 + min.to_r/1440 + s.to_r/86400
|
|
|
|
end
|
2000-07-18 03:37:02 -04:00
|
|
|
|
2002-06-22 11:26:58 -04:00
|
|
|
def self.amjd_to_ajd(amjd) amjd + 4800001.to_r/2 end
|
|
|
|
def self.ajd_to_amjd(ajd) ajd - 4800001.to_r/2 end
|
|
|
|
def self.mjd_to_jd(mjd) mjd + 2400001 end
|
|
|
|
def self.jd_to_mjd(jd) jd - 2400001 end
|
2002-05-18 08:41:51 -04:00
|
|
|
def self.ld_to_jd(ld) ld + 2299160 end
|
|
|
|
def self.jd_to_ld(jd) jd - 2299160 end
|
2000-07-18 03:37:02 -04:00
|
|
|
|
2002-05-18 08:41:51 -04:00
|
|
|
def self.jd_to_wday(jd) (jd + 1) % 7 end
|
2000-07-18 03:37:02 -04:00
|
|
|
|
2002-05-18 08:41:51 -04:00
|
|
|
def self.julian_leap? (y) y % 4 == 0 end
|
|
|
|
def self.gregorian_leap? (y) y % 4 == 0 and y % 100 != 0 or y % 400 == 0 end
|
|
|
|
|
|
|
|
class << self; alias_method :leap?, :gregorian_leap? end
|
|
|
|
class << self; alias_method :new0, :new end
|
|
|
|
|
2002-06-22 11:26:58 -04:00
|
|
|
def self.valid_jd? (jd, sg=ITALY) jd end
|
2002-05-18 08:41:51 -04:00
|
|
|
|
2002-06-22 11:26:58 -04:00
|
|
|
def self.jd(jd=0, sg=ITALY)
|
|
|
|
jd = valid_jd?(jd, sg)
|
|
|
|
new0(jd_to_ajd(jd, 0, 0), 0, sg)
|
2002-05-18 08:41:51 -04:00
|
|
|
end
|
|
|
|
|
2002-06-22 11:26:58 -04:00
|
|
|
def self.valid_ordinal? (y, d, sg=ITALY)
|
2002-05-18 08:41:51 -04:00
|
|
|
if d < 0
|
|
|
|
ny, = clfloor(y + 1, 1)
|
|
|
|
jd = ordinal_to_jd(ny, d + 1, sg)
|
|
|
|
ns = ns?(jd, sg)
|
|
|
|
return unless [y] == jd_to_ordinal(jd, sg)[0..0]
|
|
|
|
return unless [ny, 1] == jd_to_ordinal(jd - d, ns)
|
|
|
|
else
|
|
|
|
jd = ordinal_to_jd(y, d, sg)
|
|
|
|
return unless [y, d] == jd_to_ordinal(jd, sg)
|
1998-01-16 07:13:05 -05:00
|
|
|
end
|
2002-05-18 08:41:51 -04:00
|
|
|
jd
|
|
|
|
end
|
2000-07-18 03:37:02 -04:00
|
|
|
|
2002-06-22 11:26:58 -04:00
|
|
|
def self.ordinal(y=-4712, d=1, sg=ITALY)
|
|
|
|
unless jd = valid_ordinal?(y, d, sg)
|
2002-05-18 08:41:51 -04:00
|
|
|
raise ArgumentError, 'invalid date'
|
1998-01-16 07:13:05 -05:00
|
|
|
end
|
2002-06-22 11:26:58 -04:00
|
|
|
new0(jd_to_ajd(jd, 0, 0), 0, sg)
|
2002-05-18 08:41:51 -04:00
|
|
|
end
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2002-06-22 11:26:58 -04:00
|
|
|
def self.valid_civil? (y, m, d, sg=ITALY)
|
2002-05-18 08:41:51 -04:00
|
|
|
if m < 0
|
|
|
|
m += 13
|
2000-07-18 03:37:02 -04:00
|
|
|
end
|
2002-05-18 08:41:51 -04:00
|
|
|
if d < 0
|
|
|
|
ny, nm = clfloor(y * 12 + m, 12)
|
|
|
|
nm, = clfloor(nm + 1, 1)
|
|
|
|
jd = civil_to_jd(ny, nm, d + 1, sg)
|
|
|
|
ns = ns?(jd, sg)
|
|
|
|
return unless [y, m] == jd_to_civil(jd, sg)[0..1]
|
|
|
|
return unless [ny, nm, 1] == jd_to_civil(jd - d, ns)
|
|
|
|
else
|
|
|
|
jd = civil_to_jd(y, m, d, sg)
|
|
|
|
return unless [y, m, d] == jd_to_civil(jd, sg)
|
|
|
|
end
|
|
|
|
jd
|
|
|
|
end
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2002-06-22 11:26:58 -04:00
|
|
|
class << self; alias_method :valid_date?, :valid_civil? end
|
2002-05-18 08:41:51 -04:00
|
|
|
|
2002-06-22 11:26:58 -04:00
|
|
|
def self.civil(y=-4712, m=1, d=1, sg=ITALY)
|
|
|
|
unless jd = valid_civil?(y, m, d, sg)
|
2002-05-18 08:41:51 -04:00
|
|
|
raise ArgumentError, 'invalid date'
|
1999-01-19 23:59:39 -05:00
|
|
|
end
|
2002-06-22 11:26:58 -04:00
|
|
|
new0(jd_to_ajd(jd, 0, 0), 0, sg)
|
2002-05-18 08:41:51 -04:00
|
|
|
end
|
2000-07-18 03:37:02 -04:00
|
|
|
|
2002-06-22 11:26:58 -04:00
|
|
|
class << self; alias_method :new, :civil end
|
2002-05-18 08:41:51 -04:00
|
|
|
|
2002-06-22 11:26:58 -04:00
|
|
|
def self.valid_commercial? (y, w, d, sg=ITALY)
|
2002-05-18 08:41:51 -04:00
|
|
|
if d < 0
|
|
|
|
d += 8
|
1998-01-16 07:13:05 -05:00
|
|
|
end
|
2002-05-18 08:41:51 -04:00
|
|
|
if w < 0
|
|
|
|
w = jd_to_commercial(commercial_to_jd(y + 1, 1, 1) + w * 7)[1]
|
|
|
|
end
|
|
|
|
jd = commercial_to_jd(y, w, d)
|
|
|
|
return unless ns?(jd, sg)
|
|
|
|
return unless [y, w, d] == jd_to_commercial(jd)
|
|
|
|
jd
|
|
|
|
end
|
2000-07-18 03:37:02 -04:00
|
|
|
|
2002-06-22 11:26:58 -04:00
|
|
|
def self.commercial(y=1582, w=41, d=5, sg=ITALY)
|
|
|
|
unless jd = valid_commercial?(y, w, d, sg)
|
2002-05-18 08:41:51 -04:00
|
|
|
raise ArgumentError, 'invalid date'
|
1998-01-16 07:13:05 -05:00
|
|
|
end
|
2002-06-22 11:26:58 -04:00
|
|
|
new0(jd_to_ajd(jd, 0, 0), 0, sg)
|
2002-05-18 08:41:51 -04:00
|
|
|
end
|
2000-07-18 03:37:02 -04:00
|
|
|
|
2002-05-18 08:41:51 -04:00
|
|
|
def self.new_with_hash(elem, sg)
|
|
|
|
elem ||= {}
|
2003-05-05 06:05:11 -04:00
|
|
|
y, m, d = elem.values_at(:year, :mon, :mday)
|
2002-05-18 08:41:51 -04:00
|
|
|
if [y, m, d].include? nil
|
|
|
|
raise ArgumentError, 'invalid date'
|
|
|
|
else
|
2002-06-22 11:26:58 -04:00
|
|
|
civil(y, m, d, sg)
|
1998-01-16 07:13:05 -05:00
|
|
|
end
|
2002-05-18 08:41:51 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
private_class_method :new_with_hash
|
|
|
|
|
|
|
|
def self.strptime(str='-4712-01-01', fmt='%F', sg=ITALY)
|
|
|
|
elem = _strptime(str, fmt)
|
|
|
|
new_with_hash(elem, sg)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.parse(str='-4712-01-01', comp=false, sg=ITALY)
|
|
|
|
elem = _parse(str, comp)
|
|
|
|
new_with_hash(elem, sg)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.today(sg=ITALY)
|
|
|
|
jd = civil_to_jd(*(Time.now.to_a[3..5].reverse << sg))
|
2002-06-22 11:26:58 -04:00
|
|
|
new0(jd_to_ajd(jd, 0, 0), 0, sg)
|
2002-05-18 08:41:51 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
class << self
|
2000-07-06 03:21:26 -04:00
|
|
|
|
2000-07-18 03:37:02 -04:00
|
|
|
def once(*ids)
|
|
|
|
for id in ids
|
|
|
|
module_eval <<-"end;"
|
2002-06-22 11:26:58 -04:00
|
|
|
alias_method :__#{id.to_i}__, :#{id.to_s}
|
2000-12-25 01:29:27 -05:00
|
|
|
private :__#{id.to_i}__
|
2002-06-22 11:26:58 -04:00
|
|
|
def #{id.to_s}(*args, &block)
|
2001-05-07 05:26:29 -04:00
|
|
|
(@__#{id.to_i}__ ||= [__#{id.to_i}__(*args, &block)])[0]
|
2000-07-18 03:37:02 -04:00
|
|
|
end
|
|
|
|
end;
|
2000-07-06 03:21:26 -04:00
|
|
|
end
|
|
|
|
end
|
2000-07-18 03:37:02 -04:00
|
|
|
|
|
|
|
private :once
|
|
|
|
|
2000-07-06 03:21:26 -04:00
|
|
|
end
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2002-06-22 11:26:58 -04:00
|
|
|
def initialize(ajd=0, of=0, sg=ITALY) @ajd, @of, @sg = ajd, of, sg end
|
2000-07-18 03:37:02 -04:00
|
|
|
|
2002-06-22 11:26:58 -04:00
|
|
|
def ajd() @ajd end
|
2002-10-02 12:45:35 -04:00
|
|
|
def amjd() self.class.ajd_to_amjd(@ajd) end
|
2000-07-18 03:37:02 -04:00
|
|
|
|
2002-06-22 11:26:58 -04:00
|
|
|
once :amjd
|
2000-07-18 03:37:02 -04:00
|
|
|
|
2002-10-02 12:45:35 -04:00
|
|
|
def jd() self.class.ajd_to_jd(@ajd, @of)[0] end
|
|
|
|
def day_fraction() self.class.ajd_to_jd(@ajd, @of)[1] end
|
|
|
|
def mjd() self.class.jd_to_mjd(jd) end
|
|
|
|
def ld() self.class.jd_to_ld(jd) end
|
2000-07-18 03:37:02 -04:00
|
|
|
|
2002-06-22 11:26:58 -04:00
|
|
|
once :jd, :day_fraction, :mjd, :ld
|
2000-07-18 03:37:02 -04:00
|
|
|
|
2002-10-02 12:45:35 -04:00
|
|
|
def civil() self.class.jd_to_civil(jd, @sg) end
|
|
|
|
def ordinal() self.class.jd_to_ordinal(jd, @sg) end
|
|
|
|
def commercial() self.class.jd_to_commercial(jd, @sg) end
|
2000-07-18 03:37:02 -04:00
|
|
|
|
|
|
|
once :civil, :ordinal, :commercial
|
|
|
|
private :civil, :ordinal, :commercial
|
|
|
|
|
|
|
|
def year() civil[0] end
|
|
|
|
def yday() ordinal[1] end
|
|
|
|
def mon() civil[1] end
|
2002-05-18 08:41:51 -04:00
|
|
|
def mday() civil[2] end
|
2000-07-18 03:37:02 -04:00
|
|
|
|
|
|
|
alias_method :month, :mon
|
2002-05-18 08:41:51 -04:00
|
|
|
alias_method :day, :mday
|
2000-07-18 03:37:02 -04:00
|
|
|
|
2002-10-02 12:45:35 -04:00
|
|
|
def time() self.class.day_fraction_to_time(day_fraction) end
|
2000-07-18 03:37:02 -04:00
|
|
|
|
2002-05-18 08:41:51 -04:00
|
|
|
once :time
|
|
|
|
private :time
|
|
|
|
|
|
|
|
def hour() time[0] end
|
|
|
|
def min() time[1] end
|
|
|
|
def sec() time[2] end
|
|
|
|
def sec_fraction() time[3] end
|
|
|
|
|
|
|
|
private :hour, :min, :sec, :sec_fraction
|
|
|
|
|
|
|
|
def zone
|
|
|
|
['Z',
|
|
|
|
format('%+.2d%02d',
|
2002-06-22 11:26:58 -04:00
|
|
|
(@of / (1.to_r/24)).to_i,
|
|
|
|
(@of.abs % (1.to_r/24) / (1.to_r/1440)).to_i)
|
|
|
|
][@of<=>0]
|
2002-05-18 08:41:51 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
private :zone
|
2000-07-18 03:37:02 -04:00
|
|
|
|
|
|
|
def cwyear() commercial[0] end
|
|
|
|
def cweek() commercial[1] end
|
|
|
|
def cwday() commercial[2] end
|
|
|
|
|
2002-10-02 12:45:35 -04:00
|
|
|
def wday() self.class.jd_to_wday(jd) end
|
2000-07-18 03:37:02 -04:00
|
|
|
|
|
|
|
once :wday
|
|
|
|
|
2002-10-02 12:45:35 -04:00
|
|
|
def os? () self.class.os?(jd, @sg) end
|
|
|
|
def ns? () self.class.ns?(jd, @sg) end
|
2000-07-18 03:37:02 -04:00
|
|
|
|
|
|
|
once :os?, :ns?
|
|
|
|
|
|
|
|
def leap?
|
2002-10-02 12:45:35 -04:00
|
|
|
self.class.jd_to_civil(self.class.civil_to_jd(year, 3, 1, ns?) - 1,
|
2000-07-18 03:37:02 -04:00
|
|
|
ns?)[-1] == 29
|
1998-01-16 07:13:05 -05:00
|
|
|
end
|
2000-07-18 03:37:02 -04:00
|
|
|
|
|
|
|
once :leap?
|
|
|
|
|
2002-06-22 11:26:58 -04:00
|
|
|
def start() @sg end
|
2002-10-02 12:45:35 -04:00
|
|
|
def new_start(sg=self.class::ITALY) self.class.new0(@ajd, @of, sg) end
|
2000-07-18 03:37:02 -04:00
|
|
|
|
2002-10-02 12:45:35 -04:00
|
|
|
def italy() new_start(self.class::ITALY) end
|
|
|
|
def england() new_start(self.class::ENGLAND) end
|
|
|
|
def julian() new_start(self.class::JULIAN) end
|
|
|
|
def gregorian() new_start(self.class::GREGORIAN) end
|
2000-07-18 03:37:02 -04:00
|
|
|
|
2002-06-22 11:26:58 -04:00
|
|
|
def offset() @of end
|
2002-10-02 12:45:35 -04:00
|
|
|
def new_offset(of=0) self.class.new0(@ajd, of, @sg) end
|
2002-05-18 08:41:51 -04:00
|
|
|
|
2002-06-22 11:26:58 -04:00
|
|
|
private :offset, :new_offset
|
2002-05-18 08:41:51 -04:00
|
|
|
|
2000-07-18 03:37:02 -04:00
|
|
|
def + (n)
|
|
|
|
case n
|
2002-10-02 12:45:35 -04:00
|
|
|
when Numeric; return self.class.new0(@ajd + n, @of, @sg)
|
2000-07-18 03:37:02 -04:00
|
|
|
end
|
|
|
|
raise TypeError, 'expected numeric'
|
1998-01-16 07:13:05 -05:00
|
|
|
end
|
2000-07-18 03:37:02 -04:00
|
|
|
|
|
|
|
def - (x)
|
|
|
|
case x
|
2002-10-02 12:45:35 -04:00
|
|
|
when Numeric; return self.class.new0(@ajd - x, @of, @sg)
|
2002-06-22 11:26:58 -04:00
|
|
|
when Date; return @ajd - x.ajd
|
2000-07-18 03:37:02 -04:00
|
|
|
end
|
|
|
|
raise TypeError, 'expected numeric or date'
|
1998-01-16 07:13:05 -05:00
|
|
|
end
|
2000-07-18 03:37:02 -04:00
|
|
|
|
|
|
|
def <=> (other)
|
|
|
|
case other
|
2002-06-22 11:26:58 -04:00
|
|
|
when Numeric; return @ajd <=> other
|
|
|
|
when Date; return @ajd <=> other.ajd
|
1998-01-16 07:13:05 -05:00
|
|
|
end
|
2003-03-03 00:17:39 -05:00
|
|
|
nil
|
1998-01-16 07:13:05 -05:00
|
|
|
end
|
2000-07-18 03:37:02 -04:00
|
|
|
|
|
|
|
def === (other)
|
|
|
|
case other
|
|
|
|
when Numeric; return jd == other
|
|
|
|
when Date; return jd == other.jd
|
|
|
|
end
|
2003-03-03 00:17:39 -05:00
|
|
|
false
|
1998-01-16 07:13:05 -05:00
|
|
|
end
|
|
|
|
|
2000-07-18 03:37:02 -04:00
|
|
|
def >> (n)
|
2002-06-22 11:26:58 -04:00
|
|
|
y, m = clfloor(year * 12 + (mon - 1) + n, 12)
|
|
|
|
m, = clfloor(m + 1, 1)
|
2000-07-18 03:37:02 -04:00
|
|
|
d = mday
|
2002-10-02 12:45:35 -04:00
|
|
|
d -= 1 until jd2 = self.class.valid_civil?(y, m, d, ns?)
|
2000-07-18 03:37:02 -04:00
|
|
|
self + (jd2 - jd)
|
|
|
|
end
|
2000-07-06 03:21:26 -04:00
|
|
|
|
2000-07-18 03:37:02 -04:00
|
|
|
def << (n) self >> -n end
|
2000-07-06 03:21:26 -04:00
|
|
|
|
2000-07-18 03:37:02 -04:00
|
|
|
def step(limit, step)
|
|
|
|
da = self
|
|
|
|
op = [:-,:<=,:>=][step<=>0]
|
|
|
|
while da.__send__(op, limit)
|
|
|
|
yield da
|
|
|
|
da += step
|
|
|
|
end
|
|
|
|
self
|
1998-01-16 07:13:05 -05:00
|
|
|
end
|
|
|
|
|
2000-07-18 03:37:02 -04:00
|
|
|
def upto(max, &block) step(max, +1, &block) end
|
|
|
|
def downto(min, &block) step(min, -1, &block) end
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2000-07-18 03:37:02 -04:00
|
|
|
def succ() self + 1 end
|
|
|
|
|
|
|
|
alias_method :next, :succ
|
|
|
|
|
|
|
|
def eql? (other) Date === other and self == other end
|
2002-06-22 11:26:58 -04:00
|
|
|
def hash() @ajd.hash end
|
2000-07-18 03:37:02 -04:00
|
|
|
|
2002-10-02 12:45:35 -04:00
|
|
|
def inspect() format('#<%s: %s,%s,%s>', self.class, @ajd, @of, @sg) end
|
2002-05-18 08:41:51 -04:00
|
|
|
def to_s() strftime end
|
2000-07-18 03:37:02 -04:00
|
|
|
|
2002-06-22 11:26:58 -04:00
|
|
|
def _dump(limit) Marshal.dump([@ajd, @of, @sg], -1) end
|
|
|
|
|
|
|
|
# def self._load(str) new0(*Marshal.load(str)) end
|
|
|
|
|
|
|
|
def self._load(str)
|
|
|
|
a = Marshal.load(str)
|
|
|
|
if a.size == 2
|
|
|
|
ajd, sg = a
|
|
|
|
of = 0
|
|
|
|
ajd -= 1.to_r/2
|
|
|
|
else
|
|
|
|
ajd, of, sg = a
|
|
|
|
end
|
|
|
|
new0(ajd, of, sg)
|
|
|
|
end
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
end
|
2002-05-18 08:41:51 -04:00
|
|
|
|
|
|
|
class DateTime < Date
|
|
|
|
|
2002-06-22 11:26:58 -04:00
|
|
|
def self.valid_time? (h, min, s)
|
2002-05-18 08:41:51 -04:00
|
|
|
h += 24 if h < 0
|
|
|
|
min += 60 if min < 0
|
|
|
|
s += 60 if s < 0
|
|
|
|
return unless (0..24) === h and
|
|
|
|
(0..59) === min and
|
|
|
|
(0..59) === s
|
|
|
|
time_to_day_fraction(h, min, s)
|
|
|
|
end
|
|
|
|
|
2002-06-22 11:26:58 -04:00
|
|
|
def self.jd(jd=0, h=0, min=0, s=0, of=0, sg=ITALY)
|
|
|
|
unless (jd = valid_jd?(jd, sg)) and
|
|
|
|
(fr = valid_time?(h, min, s))
|
2002-05-18 08:41:51 -04:00
|
|
|
raise ArgumentError, 'invalid date'
|
|
|
|
end
|
2002-06-22 11:26:58 -04:00
|
|
|
new0(jd_to_ajd(jd, fr, of), of, sg)
|
2002-05-18 08:41:51 -04:00
|
|
|
end
|
|
|
|
|
2002-06-22 11:26:58 -04:00
|
|
|
def self.ordinal(y=-4712, d=1, h=0, min=0, s=0, of=0, sg=ITALY)
|
|
|
|
unless (jd = valid_ordinal?(y, d, sg)) and
|
|
|
|
(fr = valid_time?(h, min, s))
|
2002-05-18 08:41:51 -04:00
|
|
|
raise ArgumentError, 'invalid date'
|
|
|
|
end
|
2002-06-22 11:26:58 -04:00
|
|
|
new0(jd_to_ajd(jd, fr, of), of, sg)
|
2002-05-18 08:41:51 -04:00
|
|
|
end
|
|
|
|
|
2002-06-22 11:26:58 -04:00
|
|
|
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)) and
|
|
|
|
(fr = valid_time?(h, min, s))
|
2002-05-18 08:41:51 -04:00
|
|
|
raise ArgumentError, 'invalid date'
|
|
|
|
end
|
2002-06-22 11:26:58 -04:00
|
|
|
new0(jd_to_ajd(jd, fr, of), of, sg)
|
2002-05-18 08:41:51 -04:00
|
|
|
end
|
|
|
|
|
2002-06-22 11:26:58 -04:00
|
|
|
class << self; alias_method :new, :civil end
|
2002-05-18 08:41:51 -04:00
|
|
|
|
2002-06-22 11:26:58 -04:00
|
|
|
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)) and
|
|
|
|
(fr = valid_time?(h, min, s))
|
2002-05-18 08:41:51 -04:00
|
|
|
raise ArgumentError, 'invalid date'
|
|
|
|
end
|
2002-06-22 11:26:58 -04:00
|
|
|
new0(jd_to_ajd(jd, fr, of), of, sg)
|
2002-05-18 08:41:51 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.new_with_hash(elem, sg)
|
|
|
|
elem ||= {}
|
|
|
|
y, m, d, h, min, s, of =
|
2003-05-05 06:05:11 -04:00
|
|
|
elem.values_at(:year, :mon, :mday, :hour, :min, :sec, :offset)
|
2002-05-18 08:41:51 -04:00
|
|
|
h ||= 0
|
|
|
|
min ||= 0
|
|
|
|
s ||= 0
|
|
|
|
of ||= 0
|
|
|
|
if [y, m, d].include? nil
|
|
|
|
raise ArgumentError, 'invalid date'
|
|
|
|
else
|
2002-06-22 11:26:58 -04:00
|
|
|
civil(y, m, d, h, min, s, of.to_r/86400, sg)
|
2002-05-18 08:41:51 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private_class_method :new_with_hash
|
|
|
|
|
|
|
|
def self.strptime(str='-4712-01-01T00:00:00Z', fmt='%FT%T%Z', sg=ITALY)
|
|
|
|
elem = _strptime(str, fmt)
|
|
|
|
new_with_hash(elem, sg)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.parse(str='-4712-01-01T00:00:00Z', comp=false, sg=ITALY)
|
|
|
|
elem = _parse(str, comp)
|
|
|
|
new_with_hash(elem, sg)
|
|
|
|
end
|
|
|
|
|
|
|
|
class << self; undef_method :today end
|
|
|
|
|
|
|
|
def self.now(sg=ITALY)
|
|
|
|
i = Time.now
|
|
|
|
a = i.to_a[0..5].reverse
|
|
|
|
jd = civil_to_jd(*(a[0,3] << sg))
|
|
|
|
fr = time_to_day_fraction(*(a[3,3])) + i.usec.to_r/86400000000
|
|
|
|
d = Time.gm(*i.to_a).to_i - i.to_i
|
|
|
|
d += d / d.abs if d.nonzero?
|
|
|
|
of = (d / 60).to_r/1440
|
2002-06-22 11:26:58 -04:00
|
|
|
new0(jd_to_ajd(jd, fr, of), of, sg)
|
|
|
|
end
|
|
|
|
|
|
|
|
public :hour, :min, :sec, :sec_fraction, :zone, :offset, :new_offset
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
class Date
|
|
|
|
|
|
|
|
[ %w(exist1? valid_jd?),
|
|
|
|
%w(exist2? valid_ordinal?),
|
|
|
|
%w(exist3? valid_date?),
|
|
|
|
%w(exist? valid_date?),
|
|
|
|
%w(existw? valid_commercial?),
|
|
|
|
%w(new1 jd),
|
|
|
|
%w(new2 ordinal),
|
|
|
|
%w(new3 new),
|
|
|
|
%w(neww commercial)
|
|
|
|
].each do |old, new|
|
|
|
|
module_eval <<-"end;"
|
|
|
|
def self.#{old}(*args, &block)
|
|
|
|
if $VERBOSE
|
|
|
|
$stderr.puts("\#{caller.shift.sub(/:in .*/, '')}: " \
|
|
|
|
"warning: \#{self}::#{old} is deprecated; " \
|
|
|
|
"use \#{self}::#{new}")
|
|
|
|
end
|
|
|
|
#{new}(*args, &block)
|
|
|
|
end
|
|
|
|
end;
|
|
|
|
end
|
|
|
|
|
|
|
|
[ %w(sg start),
|
|
|
|
%w(newsg new_start),
|
|
|
|
%w(of offset),
|
|
|
|
%w(newof new_offset)
|
|
|
|
].each do |old, new|
|
|
|
|
module_eval <<-"end;"
|
|
|
|
def #{old}(*args, &block)
|
|
|
|
if $VERBOSE
|
|
|
|
$stderr.puts("\#{caller.shift.sub(/:in .*/, '')}: " \
|
2002-10-02 12:45:35 -04:00
|
|
|
"warning: \#{self.class}\##{old} is deprecated; " \
|
|
|
|
"use \#{self.class}\##{new}")
|
2002-06-22 11:26:58 -04:00
|
|
|
end
|
|
|
|
#{new}(*args, &block)
|
|
|
|
end
|
|
|
|
end;
|
2002-05-18 08:41:51 -04:00
|
|
|
end
|
|
|
|
|
2002-06-22 11:26:58 -04:00
|
|
|
private :of, :newof
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
class DateTime < Date
|
|
|
|
|
|
|
|
public :of, :newof
|
2002-05-18 08:41:51 -04:00
|
|
|
|
|
|
|
end
|