mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
updated based on date2 3.9.4.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_8@11055 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
2d9e29b19b
commit
6ac695abc5
3 changed files with 533 additions and 466 deletions
|
@ -1,3 +1,7 @@
|
||||||
|
Sat Sep 30 15:12:25 2006 Tadayoshi Funaba <tadf@dotrb.org>
|
||||||
|
|
||||||
|
* lib/date.rb, lib/date/format.rb: updated based on date2 3.9.4.
|
||||||
|
|
||||||
Fri Sep 29 12:11:04 2006 WATANABE Hirofumi <eban@ruby-lang.org>
|
Fri Sep 29 12:11:04 2006 WATANABE Hirofumi <eban@ruby-lang.org>
|
||||||
|
|
||||||
* jcode.rb (succ!): call original succ! if $KCODE == 'n'.
|
* jcode.rb (succ!): call original succ! if $KCODE == 'n'.
|
||||||
|
|
123
lib/date.rb
123
lib/date.rb
|
@ -6,7 +6,7 @@
|
||||||
# Documentation: William Webber <william@williamwebber.com>
|
# Documentation: William Webber <william@williamwebber.com>
|
||||||
#
|
#
|
||||||
#--
|
#--
|
||||||
# $Id: date.rb,v 2.25 2006-09-24 10:43:22+09 tadf Exp $
|
# $Id: date.rb,v 2.27 2006-09-30 13:10:32+09 tadf Exp $
|
||||||
#++
|
#++
|
||||||
#
|
#
|
||||||
# == Overview
|
# == Overview
|
||||||
|
@ -740,23 +740,25 @@ class Date
|
||||||
|
|
||||||
def self.complete_hash(elem) # :nodoc:
|
def self.complete_hash(elem) # :nodoc:
|
||||||
i = 0
|
i = 0
|
||||||
g = [[:jd, [:jd]],
|
g = [[:time, [:hour, :min, :sec]],
|
||||||
[:ordinal, [:year, :yday]],
|
[nil, [:jd]],
|
||||||
[:civil, [:year, :mon, :mday]],
|
[:ordinal, [:year, :yday, :hour, :min, :sec]],
|
||||||
[:commercial, [:cwyear, :cweek, :cwday]],
|
[:civil, [:year, :mon, :mday, :hour, :min, :sec]],
|
||||||
[nil, [:wday]],
|
[:commercial, [:cwyear, :cweek, :cwday, :hour, :min, :sec]],
|
||||||
[:wnum0, [:year, :wnum0, :wday]],
|
[:wday, [:wday, :hour, :min, :sec]],
|
||||||
[:wnum1, [:year, :wnum1, :wday]],
|
[:wnum0, [:year, :wnum0, :wday, :hour, :min, :sec]],
|
||||||
[:time, [:hour, :min, :sec]],
|
[:wnum1, [:year, :wnum1, :wday, :hour, :min, :sec]],
|
||||||
[nil, [:cwyear, :cweek, :wday]],
|
[nil, [:cwyear, :cweek, :wday, :hour, :min, :sec]],
|
||||||
[nil, [:year, :wnum0, :cwday]],
|
[nil, [:year, :wnum0, :cwday, :hour, :min, :sec]],
|
||||||
[nil, [:year, :wnum1, :cwday]]].
|
[nil, [:year, :wnum1, :cwday, :hour, :min, :sec]]].
|
||||||
collect{|k, a| e = elem.values_at(*a).compact; [k, a, e]}.
|
collect{|k, a| e = elem.values_at(*a).compact; [k, a, e]}.
|
||||||
select{|k, a, e| e.size > 0}.
|
select{|k, a, e| e.size > 0}.
|
||||||
sort_by{|k, a, e| [e.size, i -= 1]}.last
|
sort_by{|k, a, e| [e.size, i -= 1]}.last
|
||||||
|
|
||||||
|
d = nil
|
||||||
|
|
||||||
if g && g[0] && (g[1].size - g[2].size) != 0
|
if g && g[0] && (g[1].size - g[2].size) != 0
|
||||||
d = Date.today
|
d ||= Date.today
|
||||||
|
|
||||||
case g[0]
|
case g[0]
|
||||||
when :ordinal
|
when :ordinal
|
||||||
|
@ -776,6 +778,8 @@ class Date
|
||||||
end
|
end
|
||||||
elem[:cweek] ||= 1
|
elem[:cweek] ||= 1
|
||||||
elem[:cwday] ||= 1
|
elem[:cwday] ||= 1
|
||||||
|
when :wday
|
||||||
|
elem[:jd] ||= (d - d.wday + elem[:wday]).jd
|
||||||
when :wnum0
|
when :wnum0
|
||||||
g[1].each do |e|
|
g[1].each do |e|
|
||||||
break if elem[e]
|
break if elem[e]
|
||||||
|
@ -790,7 +794,12 @@ class Date
|
||||||
end
|
end
|
||||||
elem[:wnum1] ||= 0
|
elem[:wnum1] ||= 0
|
||||||
elem[:wday] ||= 0
|
elem[:wday] ||= 0
|
||||||
when :time
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
if g[0] == :time
|
||||||
|
if self <= DateTime
|
||||||
|
d ||= Date.today
|
||||||
elem[:jd] ||= d.jd
|
elem[:jd] ||= d.jd
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -1056,6 +1065,18 @@ class Date
|
||||||
|
|
||||||
once :wday
|
once :wday
|
||||||
|
|
||||||
|
=begin
|
||||||
|
MONTHNAMES.each_with_index do |n, i|
|
||||||
|
if n
|
||||||
|
define_method(n.downcase + '?'){mon == i}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
DAYNAMES.each_with_index do |n, i|
|
||||||
|
define_method(n.downcase + '?'){wday == i}
|
||||||
|
end
|
||||||
|
=end
|
||||||
|
|
||||||
# Is the current date old-style (Julian Calendar)?
|
# Is the current date old-style (Julian Calendar)?
|
||||||
def julian? () self.class.julian?(jd, @sg) end
|
def julian? () self.class.julian?(jd, @sg) end
|
||||||
|
|
||||||
|
@ -1172,10 +1193,15 @@ class Date
|
||||||
false
|
false
|
||||||
end
|
end
|
||||||
|
|
||||||
# Return a new Date one day after this one.
|
def next_day(n=1) self + n end
|
||||||
def succ() self + 1 end
|
# def prev_day(n=1) self - n end
|
||||||
|
|
||||||
alias_method :next, :succ
|
private :next_day
|
||||||
|
|
||||||
|
# Return a new Date one day after this one.
|
||||||
|
def next() next_day end
|
||||||
|
|
||||||
|
alias_method :succ, :next
|
||||||
|
|
||||||
# Return a new Date object that is +n+ months later than
|
# Return a new Date object that is +n+ months later than
|
||||||
# the current one.
|
# the current one.
|
||||||
|
@ -1199,6 +1225,14 @@ class Date
|
||||||
# of the returned Date will be the last day of the target month.
|
# of the returned Date will be the last day of the target month.
|
||||||
def << (n) self >> -n end
|
def << (n) self >> -n end
|
||||||
|
|
||||||
|
=begin
|
||||||
|
def next_month(n=1) self >> n end
|
||||||
|
def prev_month(n=1) self << n end
|
||||||
|
|
||||||
|
def next_year(n=1) self >> n * 12 end
|
||||||
|
def prev_year(n=1) self << n * 12 end
|
||||||
|
=end
|
||||||
|
|
||||||
# Step the current date forward +step+ days at a
|
# Step the current date forward +step+ days at a
|
||||||
# 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
|
||||||
|
@ -1489,33 +1523,64 @@ class DateTime < Date
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
class Time
|
||||||
|
|
||||||
|
# def to_time() getlocal end
|
||||||
|
|
||||||
|
def to_date
|
||||||
|
jd = Date.civil_to_jd(year, mon, mday, Date::ITALY)
|
||||||
|
Date.new0(Date.jd_to_ajd(jd, 0, 0), 0, Date::ITALY)
|
||||||
|
end
|
||||||
|
|
||||||
|
def to_datetime
|
||||||
|
jd = DateTime.civil_to_jd(year, mon, mday, DateTime::ITALY)
|
||||||
|
fr = DateTime.time_to_day_fraction(hour, min, [sec, 59].min) +
|
||||||
|
usec.to_r/86400000000
|
||||||
|
of = utc_offset.to_r/86400
|
||||||
|
DateTime.new0(DateTime.jd_to_ajd(jd, fr, of), of, DateTime::ITALY)
|
||||||
|
end
|
||||||
|
|
||||||
|
private :to_date, :to_datetime
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
class Date
|
class Date
|
||||||
|
|
||||||
|
=begin
|
||||||
|
def to_time() Time.local(year, mon, mday) end
|
||||||
|
def to_date() self end
|
||||||
|
def to_datetime() DateTime.new0(self.class.jd_to_ajd(jd, 0, 0), @of, @sg) end
|
||||||
|
=end
|
||||||
|
|
||||||
# Create a new Date object representing today.
|
# Create a new Date object representing today.
|
||||||
#
|
#
|
||||||
# +sg+ specifies the Day of Calendar Reform.
|
# +sg+ specifies the Day of Calendar Reform.
|
||||||
def self.today(sg=ITALY)
|
def self.today(sg=ITALY) Time.now.__send__(:to_date).new_start(sg) end
|
||||||
jd = civil_to_jd(*(Time.now.to_a[3..5].reverse << sg))
|
|
||||||
new0(jd_to_ajd(jd, 0, 0), 0, sg)
|
|
||||||
end
|
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
class DateTime < Date
|
class DateTime < Date
|
||||||
|
|
||||||
|
=begin
|
||||||
|
def to_time
|
||||||
|
d = new_offset(0)
|
||||||
|
d.instance_eval do
|
||||||
|
Time.utc(year, mon, mday, hour, min, sec,
|
||||||
|
(sec_fraction * 86400000000).to_i)
|
||||||
|
end.
|
||||||
|
getlocal
|
||||||
|
end
|
||||||
|
|
||||||
|
def to_date() Date.new0(self.class.jd_to_ajd(jd, 0, 0), 0, @sg) end
|
||||||
|
def to_datetime() self end
|
||||||
|
=end
|
||||||
|
|
||||||
class << self; undef_method :today end
|
class << self; undef_method :today end
|
||||||
|
|
||||||
# Create a new DateTime object representing the current time.
|
# Create a new DateTime object representing the current time.
|
||||||
#
|
#
|
||||||
# +sg+ specifies the Day of Calendar Reform.
|
# +sg+ specifies the Day of Calendar Reform.
|
||||||
def self.now(sg=ITALY)
|
def self.now(sg=ITALY) Time.now.__send__(:to_datetime).new_start(sg) end
|
||||||
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
|
|
||||||
of = i.utc_offset.to_r/86400
|
|
||||||
new0(jd_to_ajd(jd, fr, of), of, sg)
|
|
||||||
end
|
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
File diff suppressed because it is too large
Load diff
Loading…
Add table
Add a link
Reference in a new issue