mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
updated based on date2 3.9.3.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@11005 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
3537afda47
commit
ffc6df2b67
3 changed files with 101 additions and 24 deletions
|
@ -1,3 +1,7 @@
|
||||||
|
Sun Sep 24 12:10:04 2006 Tadayoshi Funaba <tadf@dotrb.org>
|
||||||
|
|
||||||
|
* lib/date.rb, lib/date/format.rb: updated based on date2 3.9.3.
|
||||||
|
|
||||||
Sun Sep 24 06:55:36 2006 Nobuyoshi Nakada <nobu@ruby-lang.org>
|
Sun Sep 24 06:55:36 2006 Nobuyoshi Nakada <nobu@ruby-lang.org>
|
||||||
|
|
||||||
* io.c (rb_io_print): no special handling for nil as well as puts.
|
* io.c (rb_io_print): no special handling for nil as well as puts.
|
||||||
|
|
102
lib/date.rb
102
lib/date.rb
|
@ -6,7 +6,7 @@
|
||||||
# Documentation: William Webber <william@williamwebber.com>
|
# Documentation: William Webber <william@williamwebber.com>
|
||||||
#
|
#
|
||||||
#--
|
#--
|
||||||
# $Id: date.rb,v 2.24 2006-09-22 23:26:52+09 tadf Exp $
|
# $Id: date.rb,v 2.25 2006-09-24 10:43:22+09 tadf Exp $
|
||||||
#++
|
#++
|
||||||
#
|
#
|
||||||
# == Overview
|
# == Overview
|
||||||
|
@ -453,14 +453,15 @@ class Date
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.jd_to_weeknum(jd, k=0, sg=GREGORIAN) # :nodoc:
|
def self.jd_to_weeknum(jd, k=0, sg=GREGORIAN) # :nodoc:
|
||||||
y, m, d = jd_to_civil(jd, sg)
|
ns = fix_style(jd, sg)
|
||||||
a = civil_to_jd(y, 1, 1, sg) + 6
|
y, m, d = jd_to_civil(jd, ns)
|
||||||
|
a = civil_to_jd(y, 1, 1, ns) + 6
|
||||||
w, d = (jd - (a - ((a - k) + 1) % 7) + 7).divmod(7)
|
w, d = (jd - (a - ((a - k) + 1) % 7) + 7).divmod(7)
|
||||||
return y, w, d
|
return y, w, d
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.weeknum_to_jd(y, w, d, k=0, sg=GREGORIAN) # :nodoc:
|
def self.weeknum_to_jd(y, w, d, k=0, ns=GREGORIAN) # :nodoc:
|
||||||
a = civil_to_jd(y, 1, 1, sg) + 6
|
a = civil_to_jd(y, 1, 1, ns) + 6
|
||||||
(a - ((a - k) + 1) % 7 - 7) + 7 * w + d
|
(a - ((a - k) + 1) % 7 - 7) + 7 * w + d
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -730,12 +731,78 @@ class Date
|
||||||
elem[:hour] = h
|
elem[:hour] = h
|
||||||
elem[:min] = min
|
elem[:min] = min
|
||||||
elem[:sec] = s
|
elem[:sec] = s
|
||||||
|
elem[:sec_fraction] = fr
|
||||||
elem.delete(:seconds)
|
elem.delete(:seconds)
|
||||||
elem.delete(:offset)
|
elem.delete(:offset)
|
||||||
end
|
end
|
||||||
elem
|
elem
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def self.complete_hash(elem) # :nodoc:
|
||||||
|
i = 0
|
||||||
|
g = [[:jd, [:jd]],
|
||||||
|
[:ordinal, [:year, :yday]],
|
||||||
|
[:civil, [:year, :mon, :mday]],
|
||||||
|
[:commercial, [:cwyear, :cweek, :cwday]],
|
||||||
|
[nil, [:wday]],
|
||||||
|
[:wnum0, [:year, :wnum0, :wday]],
|
||||||
|
[:wnum1, [:year, :wnum1, :wday]],
|
||||||
|
[:time, [:hour, :min, :sec]],
|
||||||
|
[nil, [:cwyear, :cweek, :wday]],
|
||||||
|
[nil, [:year, :wnum0, :cwday]],
|
||||||
|
[nil, [:year, :wnum1, :cwday]]].
|
||||||
|
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
|
||||||
|
|
||||||
|
if g && g[0] && (g[1].size - g[2].size) != 0
|
||||||
|
d = Date.today
|
||||||
|
|
||||||
|
case g[0]
|
||||||
|
when :ordinal
|
||||||
|
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
|
||||||
|
when :commercial
|
||||||
|
g[1].each do |e|
|
||||||
|
break if elem[e]
|
||||||
|
elem[e] = d.__send__(e)
|
||||||
|
end
|
||||||
|
elem[:cweek] ||= 1
|
||||||
|
elem[:cwday] ||= 1
|
||||||
|
when :wnum0
|
||||||
|
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] ||= 0
|
||||||
|
when :time
|
||||||
|
elem[:jd] ||= d.jd
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
elem[:hour] ||= 0
|
||||||
|
elem[:min] ||= 0
|
||||||
|
elem[:sec] ||= 0
|
||||||
|
elem[:sec] = [elem[:sec], 59].min
|
||||||
|
|
||||||
|
elem
|
||||||
|
end
|
||||||
|
|
||||||
def self.valid_date_with_hash?(elem, sg) # :nodoc:
|
def self.valid_date_with_hash?(elem, sg) # :nodoc:
|
||||||
catch :jd do
|
catch :jd do
|
||||||
a = elem.values_at(:jd)
|
a = elem.values_at(:jd)
|
||||||
|
@ -796,13 +863,15 @@ class Date
|
||||||
|
|
||||||
def self.new_with_hash(elem, sg) # :nodoc:
|
def self.new_with_hash(elem, sg) # :nodoc:
|
||||||
elem = rewrite_hash(elem)
|
elem = rewrite_hash(elem)
|
||||||
|
elem = complete_hash(elem)
|
||||||
unless jd = valid_date_with_hash?(elem, sg)
|
unless jd = valid_date_with_hash?(elem, sg)
|
||||||
raise ArgumentError, 'invalid date'
|
raise ArgumentError, 'invalid date'
|
||||||
end
|
end
|
||||||
new0(jd_to_ajd(jd, 0, 0), 0, sg)
|
new0(jd_to_ajd(jd, 0, 0), 0, sg)
|
||||||
end
|
end
|
||||||
|
|
||||||
private_class_method :rewrite_hash, :valid_date_with_hash?, :new_with_hash
|
private_class_method :rewrite_hash, :complete_hash,
|
||||||
|
:valid_date_with_hash?, :new_with_hash
|
||||||
|
|
||||||
# Create a new Date object by parsing from a String
|
# Create a new Date object by parsing from a String
|
||||||
# according to a specified format.
|
# according to a specified format.
|
||||||
|
@ -915,8 +984,11 @@ class Date
|
||||||
# Get the date as a Commercial Date, [year, week_of_year, day_of_week]
|
# Get the date as a Commercial Date, [year, week_of_year, day_of_week]
|
||||||
def commercial() self.class.jd_to_commercial(jd, @sg) end # :nodoc:
|
def commercial() self.class.jd_to_commercial(jd, @sg) end # :nodoc:
|
||||||
|
|
||||||
once :civil, :ordinal, :commercial
|
def weeknum0() self.class.jd_to_weeknum(jd, 0, @sg) end # :nodoc:
|
||||||
private :civil, :ordinal, :commercial
|
def weeknum1() self.class.jd_to_weeknum(jd, 1, @sg) end # :nodoc:
|
||||||
|
|
||||||
|
once :civil, :ordinal, :commercial, :weeknum0, :weeknum1
|
||||||
|
private :civil, :ordinal, :commercial, :weeknum0, :weeknum1
|
||||||
|
|
||||||
# Get the year of this date.
|
# Get the year of this date.
|
||||||
def year() civil[0] end
|
def year() civil[0] end
|
||||||
|
@ -937,6 +1009,11 @@ class Date
|
||||||
alias_method :month, :mon
|
alias_method :month, :mon
|
||||||
alias_method :day, :mday
|
alias_method :day, :mday
|
||||||
|
|
||||||
|
def wnum0() weeknum0[1] end # :nodoc:
|
||||||
|
def wnum1() weeknum1[1] end # :nodoc:
|
||||||
|
|
||||||
|
private :wnum0, :wnum1
|
||||||
|
|
||||||
# Get the time of this date as [hours, minutes, seconds,
|
# Get the time of this date as [hours, minutes, seconds,
|
||||||
# fraction_of_a_second]
|
# fraction_of_a_second]
|
||||||
def time() self.class.day_fraction_to_time(day_fraction) end # :nodoc:
|
def time() self.class.day_fraction_to_time(day_fraction) end # :nodoc:
|
||||||
|
@ -1139,7 +1216,7 @@ class Date
|
||||||
# time (or backward, if +step+ is negative) until
|
# time (or backward, if +step+ is negative) until
|
||||||
# we reach +limit+ (inclusive), yielding the resultant
|
# we reach +limit+ (inclusive), yielding the resultant
|
||||||
# date at each step.
|
# date at each step.
|
||||||
def step(limit, step=1) # :yield: date
|
def step(limit, step=1) # :yield: date
|
||||||
da = self
|
da = self
|
||||||
op = %w(- <= >=)[step <=> 0]
|
op = %w(- <= >=)[step <=> 0]
|
||||||
while da.__send__(op, limit)
|
while da.__send__(op, limit)
|
||||||
|
@ -1151,7 +1228,7 @@ class Date
|
||||||
|
|
||||||
# Step forward one day at a time until we reach +max+
|
# Step forward one day at a time until we reach +max+
|
||||||
# (inclusive), yielding each date as we go.
|
# (inclusive), yielding each date as we go.
|
||||||
def upto(max, &block) # :yield: date
|
def upto(max, &block) # :yield: date
|
||||||
step(max, +1, &block)
|
step(max, +1, &block)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -1271,10 +1348,6 @@ class DateTime < Date
|
||||||
|
|
||||||
def self.valid_time_with_hash? (elem) # :nodoc:
|
def self.valid_time_with_hash? (elem) # :nodoc:
|
||||||
h, min, s = elem.values_at(:hour, :min, :sec)
|
h, min, s = elem.values_at(:hour, :min, :sec)
|
||||||
h ||= 0
|
|
||||||
min ||= 0
|
|
||||||
s ||= 0
|
|
||||||
s = [s, 59].min
|
|
||||||
valid_time?(h, min, s)
|
valid_time?(h, min, s)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -1372,6 +1445,7 @@ class DateTime < Date
|
||||||
|
|
||||||
def self.new_with_hash(elem, sg) # :nodoc:
|
def self.new_with_hash(elem, sg) # :nodoc:
|
||||||
elem = rewrite_hash(elem)
|
elem = rewrite_hash(elem)
|
||||||
|
elem = complete_hash(elem)
|
||||||
unless (jd = valid_date_with_hash?(elem, sg)) and
|
unless (jd = valid_date_with_hash?(elem, sg)) and
|
||||||
(fr = valid_time_with_hash?(elem))
|
(fr = valid_time_with_hash?(elem))
|
||||||
raise ArgumentError, 'invalid date'
|
raise ArgumentError, 'invalid date'
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
# format.rb: Written by Tadayoshi Funaba 1999-2006
|
# format.rb: Written by Tadayoshi Funaba 1999-2006
|
||||||
# $Id: format.rb,v 2.24 2006-09-22 23:26:52+09 tadf Exp $
|
# $Id: format.rb,v 2.25 2006-09-24 07:56:58+09 tadf Exp $
|
||||||
|
|
||||||
require 'rational'
|
require 'rational'
|
||||||
|
|
||||||
|
@ -219,7 +219,7 @@ class Date
|
||||||
return unless str.sub!(/\A([ap])(?:m\b|\.m\.)/i, '')
|
return unless str.sub!(/\A([ap])(?:m\b|\.m\.)/i, '')
|
||||||
e._merid = if $1.downcase == 'a' then 0 else 12 end
|
e._merid = if $1.downcase == 'a' then 0 else 12 end
|
||||||
when 'Q'
|
when 'Q'
|
||||||
return unless str.sub!(/\A(\d{1,})/, '')
|
return unless str.sub!(/\A(-?\d{1,})/, '')
|
||||||
val = $1.to_i.to_r / 10**3
|
val = $1.to_i.to_r / 10**3
|
||||||
e.seconds = val
|
e.seconds = val
|
||||||
when 'R'
|
when 'R'
|
||||||
|
@ -232,7 +232,7 @@ class Date
|
||||||
return unless (0..60) === val
|
return unless (0..60) === val
|
||||||
e.sec = val
|
e.sec = val
|
||||||
when 's'
|
when 's'
|
||||||
return unless str.sub!(/\A(\d{1,})/, '')
|
return unless str.sub!(/\A(-?\d{1,})/, '')
|
||||||
val = $1.to_i
|
val = $1.to_i
|
||||||
e.seconds = val
|
e.seconds = val
|
||||||
when 'T'
|
when 'T'
|
||||||
|
@ -555,7 +555,7 @@ class Date
|
||||||
end
|
end
|
||||||
|
|
||||||
def self._parse_sla_ja(str, e) # :nodoc:
|
def self._parse_sla_ja(str, e) # :nodoc:
|
||||||
if str.sub!(%r|(-?\d+)[/.](\d+)(?:[/.](-?\d+))?|n, ' ')
|
if str.sub!(%r|(-?\d+)[/.](\d+)(?:[^\d](-?\d+))?|n, ' ')
|
||||||
if $3
|
if $3
|
||||||
e.year = $1.to_i
|
e.year = $1.to_i
|
||||||
e.mon = $2.to_i
|
e.mon = $2.to_i
|
||||||
|
@ -585,7 +585,7 @@ class Date
|
||||||
end
|
end
|
||||||
|
|
||||||
def self._parse_sla_eu(str, e) # :nodoc:
|
def self._parse_sla_eu(str, e) # :nodoc:
|
||||||
if str.sub!(%r|(-?\d+)[/.](\d+)(?:[/.](-?\d+))?|n, ' ')
|
if str.sub!(%r|(-?\d+)[/.](\d+)(?:[^\d](-?\d+))?|n, ' ')
|
||||||
if $3
|
if $3
|
||||||
e.mday = $1.to_i
|
e.mday = $1.to_i
|
||||||
e.mon = $2.to_i
|
e.mon = $2.to_i
|
||||||
|
@ -615,7 +615,7 @@ class Date
|
||||||
end
|
end
|
||||||
|
|
||||||
def self._parse_sla_us(str, e) # :nodoc:
|
def self._parse_sla_us(str, e) # :nodoc:
|
||||||
if str.sub!(%r|(-?\d+)[/.](\d+)(?:[/.](-?\d+))?|n, ' ')
|
if str.sub!(%r|(-?\d+)[/.](\d+)(?:[^\d](-?\d+))?|n, ' ')
|
||||||
if $3
|
if $3
|
||||||
e.mon = $1.to_i
|
e.mon = $1.to_i
|
||||||
e.mday = $2.to_i
|
e.mday = $2.to_i
|
||||||
|
@ -1057,14 +1057,14 @@ class Date
|
||||||
when 'Q'
|
when 'Q'
|
||||||
d = ajd - self.class.jd_to_ajd(self.class::UNIXEPOCH, 0)
|
d = ajd - self.class.jd_to_ajd(self.class::UNIXEPOCH, 0)
|
||||||
s = (d * 86400*10**3).to_i
|
s = (d * 86400*10**3).to_i
|
||||||
emit_n(s, 1, f)
|
emit_sn(s, 1, f)
|
||||||
when 'R'; emit_a(strftime('%H:%M'), 0, f)
|
when 'R'; emit_a(strftime('%H:%M'), 0, f)
|
||||||
when 'r'; emit_a(strftime('%I:%M:%S %p'), 0, f)
|
when 'r'; emit_a(strftime('%I:%M:%S %p'), 0, f)
|
||||||
when 'S'; emit_n(sec, 2, f)
|
when 'S'; emit_n(sec, 2, f)
|
||||||
when 's'
|
when 's'
|
||||||
d = ajd - self.class.jd_to_ajd(self.class::UNIXEPOCH, 0)
|
d = ajd - self.class.jd_to_ajd(self.class::UNIXEPOCH, 0)
|
||||||
s = (d * 86400).to_i
|
s = (d * 86400).to_i
|
||||||
emit_n(s, 1, f)
|
emit_sn(s, 1, f)
|
||||||
when 'T'
|
when 'T'
|
||||||
if m == '%T'
|
if m == '%T'
|
||||||
format('%02d:%02d:%02d', hour, min, sec) # 4p
|
format('%02d:%02d:%02d', hour, min, sec) # 4p
|
||||||
|
@ -1073,8 +1073,7 @@ class Date
|
||||||
end
|
end
|
||||||
when 't'; "\t"
|
when 't'; "\t"
|
||||||
when 'U', 'W'
|
when 'U', 'W'
|
||||||
k = if c == 'U' then 0 else 1 end
|
emit_n(if c == 'U' then wnum0 else wnum1 end, 2, f)
|
||||||
emit_n(self.class.jd_to_weeknum(jd, k, fix_style)[1], 2, f)
|
|
||||||
when 'u'; emit_n(cwday, 1, f)
|
when 'u'; emit_n(cwday, 1, f)
|
||||||
when 'V'; emit_n(cweek, 2, f)
|
when 'V'; emit_n(cweek, 2, f)
|
||||||
when 'v'; emit_a(strftime('%e-%b-%Y'), 0, f)
|
when 'v'; emit_a(strftime('%e-%b-%Y'), 0, f)
|
||||||
|
|
Loading…
Reference in a new issue