From ffc6df2b67a2e84116888efb108a43a1d9c2afae Mon Sep 17 00:00:00 2001 From: tadf Date: Sun, 24 Sep 2006 03:11:56 +0000 Subject: updated based on date2 3.9.3. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@11005 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- lib/date.rb | 102 +++++++++++++++++++++++++++++++++++++++++++++-------- lib/date/format.rb | 19 +++++----- 2 files changed, 97 insertions(+), 24 deletions(-) (limited to 'lib') diff --git a/lib/date.rb b/lib/date.rb index 0ec940ed23..86a657f682 100644 --- a/lib/date.rb +++ b/lib/date.rb @@ -6,7 +6,7 @@ # Documentation: William Webber # #-- -# $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 @@ -453,14 +453,15 @@ class Date end def self.jd_to_weeknum(jd, k=0, sg=GREGORIAN) # :nodoc: - y, m, d = jd_to_civil(jd, sg) - a = civil_to_jd(y, 1, 1, sg) + 6 + ns = fix_style(jd, sg) + 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) return y, w, d end - def self.weeknum_to_jd(y, w, d, k=0, sg=GREGORIAN) # :nodoc: - a = civil_to_jd(y, 1, 1, sg) + 6 + def self.weeknum_to_jd(y, w, d, k=0, ns=GREGORIAN) # :nodoc: + a = civil_to_jd(y, 1, 1, ns) + 6 (a - ((a - k) + 1) % 7 - 7) + 7 * w + d end @@ -730,12 +731,78 @@ class Date elem[:hour] = h elem[:min] = min elem[:sec] = s + elem[:sec_fraction] = fr elem.delete(:seconds) elem.delete(:offset) end elem 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: catch :jd do a = elem.values_at(:jd) @@ -796,13 +863,15 @@ class Date def self.new_with_hash(elem, sg) # :nodoc: elem = rewrite_hash(elem) + elem = complete_hash(elem) unless jd = valid_date_with_hash?(elem, sg) raise ArgumentError, 'invalid date' end new0(jd_to_ajd(jd, 0, 0), 0, sg) 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 # 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] def commercial() self.class.jd_to_commercial(jd, @sg) end # :nodoc: - once :civil, :ordinal, :commercial - private :civil, :ordinal, :commercial + def weeknum0() self.class.jd_to_weeknum(jd, 0, @sg) end # :nodoc: + 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. def year() civil[0] end @@ -937,6 +1009,11 @@ class Date alias_method :month, :mon 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, # fraction_of_a_second] 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 # we reach +limit+ (inclusive), yielding the resultant # date at each step. - def step(limit, step=1) # :yield: date + def step(limit, step=1) # :yield: date da = self op = %w(- <= >=)[step <=> 0] while da.__send__(op, limit) @@ -1151,7 +1228,7 @@ class Date # Step forward one day at a time until we reach +max+ # (inclusive), yielding each date as we go. - def upto(max, &block) # :yield: date + def upto(max, &block) # :yield: date step(max, +1, &block) end @@ -1271,10 +1348,6 @@ class DateTime < Date def self.valid_time_with_hash? (elem) # :nodoc: h, min, s = elem.values_at(:hour, :min, :sec) - h ||= 0 - min ||= 0 - s ||= 0 - s = [s, 59].min valid_time?(h, min, s) end @@ -1372,6 +1445,7 @@ class DateTime < Date def self.new_with_hash(elem, sg) # :nodoc: elem = rewrite_hash(elem) + elem = complete_hash(elem) unless (jd = valid_date_with_hash?(elem, sg)) and (fr = valid_time_with_hash?(elem)) raise ArgumentError, 'invalid date' diff --git a/lib/date/format.rb b/lib/date/format.rb index fe20a84ef2..67e216039d 100644 --- a/lib/date/format.rb +++ b/lib/date/format.rb @@ -1,5 +1,5 @@ # 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' @@ -219,7 +219,7 @@ class Date return unless str.sub!(/\A([ap])(?:m\b|\.m\.)/i, '') e._merid = if $1.downcase == 'a' then 0 else 12 end when 'Q' - return unless str.sub!(/\A(\d{1,})/, '') + return unless str.sub!(/\A(-?\d{1,})/, '') val = $1.to_i.to_r / 10**3 e.seconds = val when 'R' @@ -232,7 +232,7 @@ class Date return unless (0..60) === val e.sec = val when 's' - return unless str.sub!(/\A(\d{1,})/, '') + return unless str.sub!(/\A(-?\d{1,})/, '') val = $1.to_i e.seconds = val when 'T' @@ -555,7 +555,7 @@ class Date end 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 e.year = $1.to_i e.mon = $2.to_i @@ -585,7 +585,7 @@ class Date end 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 e.mday = $1.to_i e.mon = $2.to_i @@ -615,7 +615,7 @@ class Date end 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 e.mon = $1.to_i e.mday = $2.to_i @@ -1057,14 +1057,14 @@ class Date when 'Q' d = ajd - self.class.jd_to_ajd(self.class::UNIXEPOCH, 0) 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('%I:%M:%S %p'), 0, f) when 'S'; emit_n(sec, 2, f) when 's' d = ajd - self.class.jd_to_ajd(self.class::UNIXEPOCH, 0) s = (d * 86400).to_i - emit_n(s, 1, f) + emit_sn(s, 1, f) when 'T' if m == '%T' format('%02d:%02d:%02d', hour, min, sec) # 4p @@ -1073,8 +1073,7 @@ class Date end when 't'; "\t" when 'U', 'W' - k = if c == 'U' then 0 else 1 end - emit_n(self.class.jd_to_weeknum(jd, k, fix_style)[1], 2, f) + emit_n(if c == 'U' then wnum0 else wnum1 end, 2, f) when 'u'; emit_n(cwday, 1, f) when 'V'; emit_n(cweek, 2, f) when 'v'; emit_a(strftime('%e-%b-%Y'), 0, f) -- cgit v1.2.3