Project

General

Profile

Bug #4692 ยป 0001-Fix-indentation.patch

rspeicher (Robert Speicher), 05/14/2011 05:40 AM

View differences:

ext/date/lib/date.rb
# month's numerical representation indexed into this array
# gives the name of that month (hence the first element is nil).
MONTHNAMES = [nil] + %w(January February March April May June July
August September October November December)
August September October November December)
# 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
......
# Abbreviated month names, in English.
ABBR_MONTHNAMES = [nil] + %w(Jan Feb Mar Apr May Jun
Jul Aug Sep Oct Nov Dec)
Jul Aug Sep Oct Nov Dec)
# Abbreviated day names, in English.
ABBR_DAYNAMES = %w(Sun Mon Tue Wed Thu Fri Sat)
......
when Infinity; return d <=> other.d
when Numeric; return d
else
begin
l, r = other.coerce(self)
return l <=> r
rescue NoMethodError
end
begin
l, r = other.coerce(self)
return l <=> r
rescue NoMethodError
end
end
nil
end
......
case other
when Numeric; return -d, d
else
super
super
end
end
def to_f
return 0 if @d == 0
if @d > 0
Float::INFINITY
Float::INFINITY
else
-Float::INFINITY
-Float::INFINITY
end
end
......
def find_fdoy(y, sg) # :nodoc:
j = nil
1.upto(31) do |d|
break if j = _valid_civil_r?(y, 1, d, sg)
break if j = _valid_civil_r?(y, 1, d, sg)
end
j
end
......
def find_ldoy(y, sg) # :nodoc:
j = nil
31.downto(1) do |d|
break if j = _valid_civil_r?(y, 12, d, sg)
break if j = _valid_civil_r?(y, 12, d, sg)
end
j
end
......
def find_fdom(y, m, sg) # :nodoc:
j = nil
1.upto(31) do |d|
break if j = _valid_civil_r?(y, m, d, sg)
break if j = _valid_civil_r?(y, m, d, sg)
end
j
end
......
def find_ldom(y, m, sg) # :nodoc:
j = nil
31.downto(1) do |d|
break if j = _valid_civil_r?(y, m, d, sg)
break if j = _valid_civil_r?(y, m, d, sg)
end
j
end
......
# Returns the corresponding Julian Day Number.
def civil_to_jd(y, m, d, sg=GREGORIAN) # :nodoc:
if m <= 2
y -= 1
m += 12
y -= 1
m += 12
end
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
(30.6001 * (m + 1)).floor +
d + b - 1524
if jd < sg
jd -= b
jd -= b
end
jd
end
......
# as a three-element array.
def jd_to_civil(jd, sg=GREGORIAN) # :nodoc:
if jd < sg
a = jd
a = jd
else
x = ((jd - 1867216.25) / 36524.25).floor
a = jd + 1 + x - (x / 4.0).floor
x = ((jd - 1867216.25) / 36524.25).floor
a = jd + 1 + x - (x / 4.0).floor
end
b = a + 1524
c = ((b - 122.1) / 365.25).floor
......
e = ((b - d) / 30.6001).floor
dom = b - d - (30.6001 * e).floor
if e <= 13
m = e - 1
y = c - 4716
m = e - 1
y = c - 4716
else
m = e - 13
y = c - 4715
m = e - 13
y = c - 4715
end
return y, m, dom
end
......
def commercial_to_jd(y, w, d, sg=GREGORIAN) # :nodoc:
j = find_fdoy(y, sg) + 3
(j - (((j - 1) + 1) % 7)) +
7 * (w - 1) +
(d - 1)
7 * (w - 1) +
(d - 1)
end
# Convert a Julian Day Number to a Commercial Date
......
a = jd_to_civil(jd - 3, sg)[0]
j = commercial_to_jd(a + 1, 1, 1, sg)
if jd >= j
y = a + 1
y = a + 1
else
j = commercial_to_jd(a, 1, 1, sg)
y = a
j = commercial_to_jd(a, 1, 1, sg)
y = a
end
w = 1 + ((jd - j) / 7).floor
d = (jd + 1) % 7
......
def nth_kday_to_jd(y, m, n, k, sg=GREGORIAN) # :nodoc:
j = if n > 0
find_fdom(y, m, sg) - 1
else
find_ldom(y, m, sg) + 7
end
find_fdom(y, m, sg) - 1
else
find_ldom(y, m, sg) + 7
end
(j - (((j - k) + 1) % 7)) + 7 * n
end
......
Rational(Rational(1, 2), 2) # a challenge
def time_to_day_fraction(h, min, s)
Rational(h * 3600 + min * 60 + s, 86400) # 4p
Rational(h * 3600 + min * 60 + s, 86400) # 4p
end
rescue
def time_to_day_fraction(h, min, s)
if Integer === h && Integer === min && Integer === s
Rational(h * 3600 + min * 60 + s, 86400) # 4p
else
(h * 3600 + min * 60 + s).to_r/86400 # 4p
end
if Integer === h && Integer === min && Integer === s
Rational(h * 3600 + min * 60 + s, 86400) # 4p
else
(h * 3600 + min * 60 + s).to_r/86400 # 4p
end
end
end
......
# +sg+ specifies the Day of Calendar Reform.
def _valid_ordinal_r? (y, d, sg=GREGORIAN) # :nodoc:
if d < 0
return unless j = find_ldoy(y, sg)
ny, nd = jd_to_ordinal(j + d + 1, sg)
return unless ny == y
d = nd
return unless j = find_ldoy(y, sg)
ny, nd = jd_to_ordinal(j + d + 1, sg)
return unless ny == y
d = nd
end
jd = ordinal_to_jd(y, d, sg)
return unless [y, d] == jd_to_ordinal(jd, sg)
......
# +sg+ specifies the Day of Calendar Reform.
def _valid_civil_r? (y, m, d, sg=GREGORIAN) # :nodoc:
if m < 0
m += 13
m += 13
end
if d < 0
return unless j = find_ldom(y, m, sg)
ny, nm, nd = jd_to_civil(j + d + 1, sg)
return unless [ny, nm] == [y, m]
d = nd
return unless j = find_ldom(y, m, sg)
ny, nm, nd = jd_to_civil(j + d + 1, sg)
return unless [ny, nm] == [y, m]
d = nd
end
jd = civil_to_jd(y, m, d, sg)
return unless [y, m, d] == jd_to_civil(jd, sg)
......
# +sg+ specifies the Day of Calendar Reform.
def _valid_commercial_r? (y, w, d, sg=GREGORIAN) # :nodoc:
if d < 0
d += 8
d += 8
end
if w < 0
ny, nw, =
jd_to_commercial(commercial_to_jd(y + 1, 1, 1, sg) + w * 7, sg)
return unless ny == y
w = nw
ny, nw, =
jd_to_commercial(commercial_to_jd(y + 1, 1, 1, sg) + w * 7, sg)
return unless ny == y
w = nw
end
jd = commercial_to_jd(y, w, d, sg)
return unless [y, w, d] == jd_to_commercial(jd, sg)
......
def _valid_weeknum_r? (y, w, d, f, sg=GREGORIAN) # :nodoc:
if d < 0
d += 7
d += 7
end
if w < 0
ny, nw, =
jd_to_weeknum(weeknum_to_jd(y + 1, 1, f, f, sg) + w * 7, f, sg)
return unless ny == y
w = nw
ny, nw, =
jd_to_weeknum(weeknum_to_jd(y + 1, 1, f, f, sg) + w * 7, f, sg)
return unless ny == y
w = nw
end
jd = weeknum_to_jd(y, w, d, f, sg)
return unless [y, w, d] == jd_to_weeknum(jd, f, sg)
......
def _valid_nth_kday_r? (y, m, n, k, sg=GREGORIAN) # :nodoc:
if k < 0
k += 7
k += 7
end
if n < 0
ny, nm = (y * 12 + m).divmod(12)
nm, = (nm + 1) .divmod(1)
ny, nm, nn, =
jd_to_nth_kday(nth_kday_to_jd(ny, nm, 1, k, sg) + n * 7, sg)
return unless [ny, nm] == [y, m]
n = nn
ny, nm = (y * 12 + m).divmod(12)
nm, = (nm + 1) .divmod(1)
ny, nm, nn, =
jd_to_nth_kday(nth_kday_to_jd(ny, nm, 1, k, sg) + n * 7, sg)
return unless [ny, nm] == [y, m]
n = nn
end
jd = nth_kday_to_jd(y, m, n, k, sg)
return unless [y, m, n, k] == jd_to_nth_kday(jd, sg)
......
min += 60 if min < 0
s += 60 if s < 0
return unless ((0...24) === h &&
(0...60) === min &&
(0...60) === s) ||
(24 == h &&
0 == min &&
0 == s)
(0...60) === min &&
(0...60) === s) ||
(24 == h &&
0 == min &&
0 == s)
time_to_day_fraction(h, min, s)
end
......
def self.new!(ajd=0, of=0, sg=ITALY)
jd, df = ajd_to_jd(ajd, 0)
if !(Fixnum === jd) ||
jd < sg || df !=0 || of != 0 ||
jd < sg || df !=0 || of != 0 ||
jd < -327 || jd > 366963925
return new_r!(ajd, of, sg)
end
......
def self.complete_frags(elem) # :nodoc:
i = 0
g = [[:time, [:hour, :min, :sec]],
[nil, [:jd]],
[:ordinal, [:year, :yday, :hour, :min, :sec]],
[:civil, [:year, :mon, :mday, :hour, :min, :sec]],
[:commercial, [:cwyear, :cweek, :cwday, :hour, :min, :sec]],
[:wday, [:wday, :hour, :min, :sec]],
[:wnum0, [:year, :wnum0, :wday, :hour, :min, :sec]],
[:wnum1, [:year, :wnum1, :wday, :hour, :min, :sec]],
[nil, [:cwyear, :cweek, :wday, :hour, :min, :sec]],
[nil, [:year, :wnum0, :cwday, :hour, :min, :sec]],
[nil, [:year, :wnum1, :cwday, :hour, :min, :sec]]].
[nil, [:jd]],
[:ordinal, [:year, :yday, :hour, :min, :sec]],
[:civil, [:year, :mon, :mday, :hour, :min, :sec]],
[:commercial, [:cwyear, :cweek, :cwday, :hour, :min, :sec]],
[:wday, [:wday, :hour, :min, :sec]],
[:wnum0, [:year, :wnum0, :wday, :hour, :min, :sec]],
[:wnum1, [:year, :wnum1, :wday, :hour, :min, :sec]],
[nil, [:cwyear, :cweek, :wday, :hour, :min, :sec]],
[nil, [:year, :wnum0, :cwday, :hour, :min, :sec]],
[nil, [:year, :wnum1, :cwday, :hour, :min, :sec]]].
collect{|k, a| e = elem.values_at(*a).compact; [k, a, e]}.
select{|k, a, e| e.size > 0}.
sort_by{|k, a, e| [e.size, i -= 1]}.last
......
case g[0]
when :ordinal
elem[:year] ||= d.year
elem[:yday] ||= 1
elem[:year] ||= d.year
elem[:yday] ||= 1
when :civil
g[1].each do |e|
break if elem[e]
elem[e] = d.__send__(e)
end
elem[:mon] ||= 1
elem[:mday] ||= 1
g[1].each do |e|
break if elem[e]
elem[e] = d.__send__(e)
end
elem[:mon] ||= 1
elem[:mday] ||= 1
when :commercial
g[1].each do |e|
break if elem[e]
elem[e] = d.__send__(e)
end
elem[:cweek] ||= 1
elem[:cwday] ||= 1
g[1].each do |e|
break if elem[e]
elem[e] = d.__send__(e)
end
elem[:cweek] ||= 1
elem[:cwday] ||= 1
when :wday
elem[:jd] ||= (d - d.wday + elem[:wday]).jd
elem[:jd] ||= (d - d.wday + elem[:wday]).jd
when :wnum0
g[1].each do |e|
break if elem[e]
elem[e] = d.__send__(e)
end
elem[:wnum0] ||= 0
elem[:wday] ||= 0
g[1].each do |e|
break if elem[e]
elem[e] = d.__send__(e)
end
elem[:wnum0] ||= 0
elem[:wday] ||= 0
when :wnum1
g[1].each do |e|
break if elem[e]
elem[e] = d.__send__(e)
end
elem[:wnum1] ||= 0
elem[:wday] ||= 1
g[1].each do |e|
break if elem[e]
elem[e] = d.__send__(e)
end
elem[:wnum1] ||= 0
elem[:wday] ||= 1
end
end
if g && g[0] == :time
if self <= DateTime
d ||= Date.today
elem[:jd] ||= d.jd
d ||= Date.today
elem[:jd] ||= d.jd
end
end
......
catch :jd do
a = elem.values_at(:jd)
if a.all?
if jd = _valid_jd_r?(*(a << sg))
throw :jd, jd
end
if jd = _valid_jd_r?(*(a << sg))
throw :jd, jd
end
end
a = elem.values_at(:year, :yday)
if a.all?
if jd = _valid_ordinal_r?(*(a << sg))
throw :jd, jd
end
if jd = _valid_ordinal_r?(*(a << sg))
throw :jd, jd
end
end
a = elem.values_at(:year, :mon, :mday)
if a.all?
if jd = _valid_civil_r?(*(a << sg))
throw :jd, jd
end
if jd = _valid_civil_r?(*(a << sg))
throw :jd, jd
end
end
a = elem.values_at(:cwyear, :cweek, :cwday)
if a[2].nil? && elem[:wday]
a[2] = elem[:wday].nonzero? || 7
a[2] = elem[:wday].nonzero? || 7
end
if a.all?
if jd = _valid_commercial_r?(*(a << sg))
throw :jd, jd
end
if jd = _valid_commercial_r?(*(a << sg))
throw :jd, jd
end
end
a = elem.values_at(:year, :wnum0, :wday)
if a[2].nil? && elem[:cwday]
a[2] = elem[:cwday] % 7
a[2] = elem[:cwday] % 7
end
if a.all?
if jd = _valid_weeknum_r?(*(a << 0 << sg))
throw :jd, jd
end
if jd = _valid_weeknum_r?(*(a << 0 << sg))
throw :jd, jd
end
end
a = elem.values_at(:year, :wnum1, :wday)
if a[2]
a[2] = (a[2] - 1) % 7
a[2] = (a[2] - 1) % 7
end
if a[2].nil? && elem[:cwday]
a[2] = (elem[:cwday] - 1) % 7
a[2] = (elem[:cwday] - 1) % 7
end
if a.all?
if jd = _valid_weeknum_r?(*(a << 1 << sg))
throw :jd, jd
end
if jd = _valid_weeknum_r?(*(a << 1 << sg))
throw :jd, jd
end
end
end
end
......
def once(*ids) # :nodoc: -- restricted
for id in ids
module_eval <<-"end;"
alias_method :__#{id.object_id}__, :#{id.to_s}
private :__#{id.object_id}__
def #{id.to_s}(*args)
__ca__[#{id.object_id}] ||= __#{id.object_id}__(*args)
end
end;
module_eval <<-"end;"
alias_method :__#{id.object_id}__, :#{id.to_s}
private :__#{id.object_id}__
def #{id.to_s}(*args)
__ca__[#{id.object_id}] ||= __#{id.object_id}__(*args)
end
end;
end
end # <<dummy
......
def leap_r? # :nodoc:
jd_to_civil(civil_to_jd(year, 3, 1, fix_style) - 1,
fix_style)[-1] == 29
fix_style)[-1] == 29
end
once :leap_r?
......
case n
when Numeric
if Float === n
n = Rational((n * 86400000000000).round, 86400000000000)
n = Rational((n * 86400000000000).round, 86400000000000)
end
return self.class.new_r!(ajd + n, offset, start)
end
......
case x
when Numeric
if Float === x
x = Rational((x * 86400000000000).round, 86400000000000)
x = Rational((x * 86400000000000).round, 86400000000000)
end
return self.class.new_r!(ajd - x, offset, start)
when Date
......
when Date; return jd == other.jd
else
begin
l, r = other.coerce(self)
return l === r
l, r = other.coerce(self)
return l === r
rescue NoMethodError
end
end
......
sf, ssf = (sf * 1000000000).divmod(1)
odf, osf = (of * 86400).divmod(1)
if !(Fixnum === jd) ||
jd < sg || ssf != 0 || osf != 0 ||
jd < sg || ssf != 0 || osf != 0 ||
jd < -327 || jd > 366963925
return new_r!(ajd, of, sg)
end
......
def self.jd_r(jd=0, h=0, min=0, s=0, of=0, sg=ITALY) # :nodoc:
unless (jd = _valid_jd_r?(jd, sg)) &&
(fr = _valid_time_r?(h, min, s))
(fr = _valid_time_r?(h, min, s))
raise ArgumentError, 'invalid date'
end
if String === of
......
def self.ordinal_r(y=-4712, d=1, h=0, min=0, s=0, of=0, sg=ITALY) # :nodoc:
unless (jd = _valid_ordinal_r?(y, d, sg)) &&
(fr = _valid_time_r?(h, min, s))
(fr = _valid_time_r?(h, min, s))
raise ArgumentError, 'invalid date'
end
if String === of
......
def self.civil_r(y=-4712, m=1, d=1, h=0, min=0, s=0, of=0, sg=ITALY) # :nodoc:
unless (jd = _valid_civil_r?(y, m, d, sg)) &&
(fr = _valid_time_r?(h, min, s))
(fr = _valid_time_r?(h, min, s))
raise ArgumentError, 'invalid date'
end
if String === of
......
def self.commercial_r(y=-4712, w=1, d=1, h=0, min=0, s=0, of=0, sg=ITALY) # :nodoc:
unless (jd = _valid_commercial_r?(y, w, d, sg)) &&
(fr = _valid_time_r?(h, min, s))
(fr = _valid_time_r?(h, min, s))
raise ArgumentError, 'invalid date'
end
if String === of
......
def self.weeknum(y=-4712, w=0, d=1, f=0, h=0, min=0, s=0, of=0, sg=ITALY) # :nodoc:
unless (jd = _valid_weeknum_r?(y, w, d, f, sg)) &&
(fr = _valid_time_r?(h, min, s))
(fr = _valid_time_r?(h, min, s))
raise ArgumentError, 'invalid date'
end
if String === of
......
def self.nth_kday(y=-4712, m=1, n=1, k=1, h=0, min=0, s=0, of=0, sg=ITALY) # :nodoc:
unless (jd = _valid_nth_kday_r?(y, m, n, k, sg)) &&
(fr = _valid_time_r?(h, min, s))
(fr = _valid_time_r?(h, min, s))
raise ArgumentError, 'invalid date'
end
if String === of
......
elem = rewrite_frags(elem)
elem = complete_frags(elem)
unless (jd = valid_date_frags?(elem, sg)) &&
(fr = valid_time_frags?(elem))
(fr = valid_time_frags?(elem))
raise ArgumentError, 'invalid date'
end
fr += (elem[:sec_fraction] || 0) / 86400
......
def to_s_r # :nodoc: # 4p
format('%.4d-%02d-%02dT%02d:%02d:%02d%s',
year, mon, mday, hour, min, sec, zone)
year, mon, mday, hour, min, sec, zone)
end
private :to_s_r
......
def to_time
d = new_offset(0)
d.instance_eval do
Time.utc(year, mon, mday, hour, min, sec +
sec_fraction)
Time.utc(year, mon, mday, hour, min, sec + sec_fraction)
end.
getlocal
end
    (1-1/1)