diff options
author | tadf <tadf@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2008-01-06 00:39:44 +0000 |
---|---|---|
committer | tadf <tadf@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2008-01-06 00:39:44 +0000 |
commit | 5781f2b44c1b967903016b08c865f066736672c6 (patch) | |
tree | 76ff79ab4116ca57f936423dd39cceadfd1a4cef /lib | |
parent | 636089ddbebe54616d010f2296ae1182b195593d (diff) |
* lib/date.rb, lib/date/format.rb: introduced some constants
(for internal use) and aliases (minute and second).
* sample/cal.rb: trivial adjustments.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_8@14904 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'lib')
-rw-r--r-- | lib/date.rb | 85 | ||||
-rw-r--r-- | lib/date/format.rb | 48 |
2 files changed, 79 insertions, 54 deletions
diff --git a/lib/date.rb b/lib/date.rb index ce24802371..bc75b198af 100644 --- a/lib/date.rb +++ b/lib/date.rb @@ -1,12 +1,12 @@ # # date.rb - date and time library # -# Author: Tadayoshi Funaba 1998-2007 +# Author: Tadayoshi Funaba 1998-2008 # # Documentation: William Webber <william@williamwebber.com> # #-- -# $Id: date.rb,v 2.34 2007-12-30 21:45:27+09 tadf Exp $ +# $Id: date.rb,v 2.35 2008-01-06 08:42:17+09 tadf Exp $ #++ # # == Overview @@ -313,7 +313,20 @@ class Date # Gregorian calendar. GREGORIAN = -Infinity.new - UNIXEPOCH = 2440588 # 1970-01-01 :nodoc: + HALF_DAYS_IN_DAY = Rational(1, 2) # :nodoc: + HOURS_IN_DAY = Rational(1, 24) # :nodoc: + MINUTES_IN_DAY = Rational(1, 1440) # :nodoc: + SECONDS_IN_DAY = Rational(1, 86400) # :nodoc: + MILLISECONDS_IN_DAY = Rational(1, 86400*10**3) # :nodoc: + NANOSECONDS_IN_DAY = Rational(1, 86400*10**9) # :nodoc: + MILLISECONDS_IN_SECOND = Rational(1, 10**3) # :nodoc: + NANOSECONDS_IN_SECOND = Rational(1, 10**9) # :nodoc: + + MJD_EPOCH_IN_AJD = Rational(4800001, 2) # 1858-11-17 # :nodoc: + UNIX_EPOCH_IN_AJD = Rational(4881175, 2) # 1970-01-01 # :nodoc: + MJD_EPOCH_IN_CJD = 2400001 # :nodoc: + UNIX_EPOCH_IN_CJD = 2440588 # :nodoc: + LD_EPOCH_IN_CJD = 2299160 # :nodoc: # Does a given Julian Day Number fall inside the old-style (Julian) # calendar? @@ -476,7 +489,7 @@ class Date # # Returns the (civil) Julian Day Number as [day_number, # fraction] where +fraction+ is always 1/2. - def self.ajd_to_jd(ajd, of=0) (ajd + of + 1.to_r/2).divmod(1) end + def self.ajd_to_jd(ajd, of=0) (ajd + of + HALF_DAYS_IN_DAY).divmod(1) end # Convert a (civil) Julian Day Number to an Astronomical Julian # Day Number. @@ -487,46 +500,54 @@ class Date # # Returns the Astronomical Julian Day Number as a single # numeric value. - def self.jd_to_ajd(jd, fr, of=0) jd + fr - of - 1.to_r/2 end + def self.jd_to_ajd(jd, fr, of=0) jd + fr - of - HALF_DAYS_IN_DAY end # Convert a fractional day +fr+ to [hours, minutes, seconds, # fraction_of_a_second] def self.day_fraction_to_time(fr) - h, fr = fr.divmod(1.to_r/24) - min, fr = fr.divmod(1.to_r/1440) - s, fr = fr.divmod(1.to_r/86400) + h, fr = fr.divmod(HOURS_IN_DAY) + min, fr = fr.divmod(MINUTES_IN_DAY) + s, fr = fr.divmod(SECONDS_IN_DAY) return h, min, s, fr end # Convert an +h+ hour, +min+ minutes, +s+ seconds period # to a fractional day. - def self.time_to_day_fraction(h, min, s) - h.to_r/24 + min.to_r/1440 + s.to_r/86400 + begin + Rational(Rational(1, 2), 2) # a challenge + + def self.time_to_day_fraction(h, min, s) + Rational(h, 24) + Rational(min, 1440) + Rational(s, 86400) + end + rescue + def self.time_to_day_fraction(h, min, s) + h.to_r/24 + min.to_r/1440 + s.to_r/86400 + end end # Convert an Astronomical Modified Julian Day Number to an # Astronomical Julian Day Number. - def self.amjd_to_ajd(amjd) amjd + 4800001.to_r/2 end + def self.amjd_to_ajd(amjd) amjd + MJD_EPOCH_IN_AJD end # Convert an Astronomical Julian Day Number to an # Astronomical Modified Julian Day Number. - def self.ajd_to_amjd(ajd) ajd - 4800001.to_r/2 end + def self.ajd_to_amjd(ajd) ajd - MJD_EPOCH_IN_AJD end # Convert a Modified Julian Day Number to a Julian # Day Number. - def self.mjd_to_jd(mjd) mjd + 2400001 end + def self.mjd_to_jd(mjd) mjd + MJD_EPOCH_IN_CJD end # Convert a Julian Day Number to a Modified Julian Day # Number. - def self.jd_to_mjd(jd) jd - 2400001 end + def self.jd_to_mjd(jd) jd - MJD_EPOCH_IN_CJD end # Convert a count of the number of days since the adoption # of the Gregorian Calendar (in Italy) to a Julian Day Number. - def self.ld_to_jd(ld) ld + 2299160 end + def self.ld_to_jd(ld) ld + LD_EPOCH_IN_CJD end # Convert a Julian Day Number to the number of days since # the adoption of the Gregorian Calendar (in Italy). - def self.jd_to_ld(jd) jd - 2299160 end + def self.jd_to_ld(jd) jd - LD_EPOCH_IN_CJD end # Convert a Julian Day Number to the day of the week. # @@ -768,7 +789,7 @@ class Date h, fr = fr.divmod(3600) min, fr = fr.divmod(60) s, fr = fr.divmod(1) - elem[:jd] = UNIXEPOCH + d + elem[:jd] = UNIX_EPOCH_IN_CJD + d elem[:hour] = h elem[:min] = min elem[:sec] = s @@ -1096,7 +1117,14 @@ class Date # I do NOT recommend you to use this method. def sec_fraction() time[3] end +=begin + alias_method :minute, :min + alias_method :second, :sec + alias_method :second_fraction, :sec_fraction +=end + private :hour, :min, :sec, :sec_fraction +# :minute, :second, :second_fraction def zone() strftime('%:z') end @@ -1181,7 +1209,7 @@ class Date def new_offset(of=0) if String === of - of = (self.class.zone_to_diff(of) || 0).to_r/86400 + of = Rational(zone_to_diff(of) || 0, 86400) end self.class.new!(@ajd, of, @sg) end @@ -1437,7 +1465,7 @@ class DateTime < Date raise ArgumentError, 'invalid date' end if String === of - of = (zone_to_diff(of) || 0).to_r/86400 + of = Rational(zone_to_diff(of) || 0, 86400) end new!(jd_to_ajd(jd, fr, of), of, sg) end @@ -1462,7 +1490,7 @@ class DateTime < Date raise ArgumentError, 'invalid date' end if String === of - of = (zone_to_diff(of) || 0).to_r/86400 + of = Rational(zone_to_diff(of) || 0, 86400) end new!(jd_to_ajd(jd, fr, of), of, sg) end @@ -1487,7 +1515,7 @@ class DateTime < Date raise ArgumentError, 'invalid date' end if String === of - of = (zone_to_diff(of) || 0).to_r/86400 + of = Rational(zone_to_diff(of) || 0, 86400) end new!(jd_to_ajd(jd, fr, of), of, sg) end @@ -1515,7 +1543,7 @@ class DateTime < Date raise ArgumentError, 'invalid date' end if String === of - of = (zone_to_diff(of) || 0).to_r/86400 + of = Rational(zone_to_diff(of) || 0, 86400) end new!(jd_to_ajd(jd, fr, of), of, sg) end @@ -1526,7 +1554,7 @@ class DateTime < Date raise ArgumentError, 'invalid date' end if String === of - of = (zone_to_diff(of) || 0).to_r/86400 + of = Rational(zone_to_diff(of) || 0, 86400) end new!(jd_to_ajd(jd, fr, of), of, sg) end @@ -1540,10 +1568,8 @@ class DateTime < Date (fr = valid_time_frags?(elem)) raise ArgumentError, 'invalid date' end - sf = (elem[:sec_fraction] || 0) - fr += sf/86400 - of = (elem[:offset] || 0) - of = of.to_r/86400 + fr += (elem[:sec_fraction] || 0) / 86400 + of = Rational(elem[:offset] || 0, 86400) new!(jd_to_ajd(jd, fr, of), of, sg) end @@ -1589,6 +1615,7 @@ class DateTime < Date end public :hour, :min, :sec, :sec_fraction, :zone, :offset, :new_offset +# :minute, :second, :second_fraction end @@ -1604,8 +1631,8 @@ class Time 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 + Rational(usec, 86400_000_000) + of = Rational(utc_offset, 86400) DateTime.new!(DateTime.jd_to_ajd(jd, fr, of), of, DateTime::ITALY) end diff --git a/lib/date/format.rb b/lib/date/format.rb index c7efb0e3bc..db59cfa25f 100644 --- a/lib/date/format.rb +++ b/lib/date/format.rb @@ -1,5 +1,5 @@ -# format.rb: Written by Tadayoshi Funaba 1999-2007 -# $Id: format.rb,v 2.40 2007-09-09 08:28:03+09 tadf Exp $ +# format.rb: Written by Tadayoshi Funaba 1999-2008 +# $Id: format.rb,v 2.41 2008-01-06 08:42:17+09 tadf Exp $ require 'rational' @@ -257,25 +257,23 @@ class Date when 'j'; emit_n(yday, 3, f) when 'k'; emit_a(hour, 2, f) when 'L' - emit_n((sec_fraction / (1.to_r/86400/(10**3))).round, 3, f) + emit_n((sec_fraction / MILLISECONDS_IN_DAY).round, 3, f) when 'l'; emit_a((hour % 12).nonzero? || 12, 2, f) when 'M', 'OM'; emit_n(min, 2, f) when 'm', 'Om'; emit_n(mon, 2, f) when 'N' - emit_n((sec_fraction / (1.to_r/86400/(10**9))).round, 9, f) + emit_n((sec_fraction / NANOSECONDS_IN_DAY).round, 9, f) when 'n'; "\n" when 'P'; emit_ad(strftime('%p').downcase, 0, f) when 'p'; emit_au(if hour < 12 then 'AM' else 'PM' end, 0, f) when 'Q' - d = ajd - self.class.jd_to_ajd(self.class::UNIXEPOCH, 0) - s = (d * 86400*10**3).to_i + s = ((ajd - UNIX_EPOCH_IN_AJD) / MILLISECONDS_IN_DAY).round 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', 'OS'; emit_n(sec, 2, f) when 's' - d = ajd - self.class.jd_to_ajd(self.class::UNIXEPOCH, 0) - s = (d * 86400).to_i + s = ((ajd - UNIX_EPOCH_IN_AJD) / SECONDS_IN_DAY).round emit_sn(s, 1, f) when 'T' if m == '%T' @@ -299,9 +297,9 @@ class Date t = $1.size sign = if offset < 0 then -1 else +1 end fr = offset.abs - hh, fr = fr.divmod(1.to_r/24) - mm, fr = fr.divmod(1.to_r/1440) - ss, fr = fr.divmod(1.to_r/86400) + hh, fr = fr.divmod(HOURS_IN_DAY) + mm, fr = fr.divmod(MINUTES_IN_DAY) + ss, fr = fr.divmod(SECONDS_IN_DAY) if t == 3 if ss.nonzero? then t = 2 elsif mm.nonzero? then t = 1 @@ -383,12 +381,12 @@ class Date end def beat(n=0) - i, f = (new_offset(1.to_r/24).day_fraction * 1000).divmod(1) + i, f = (new_offset(HOURS_IN_DAY).day_fraction * 1000).divmod(1) ('@%03d' % i) + if n < 1 '' else - '.%0*d' % [n, (f / (1.to_r/(10**n))).round] + '.%0*d' % [n, (f / Rational(1, 10**n)).round] end end =end @@ -467,8 +465,8 @@ class Date then /\A([-+]?\d{1,3})/ else /\A([-+]?\d{1,})/ end, '') -# val = $1.to_i.to_r / (10**3) - val = $1.to_i.to_r / (10**$1.size) +# val = Rational($1.to_i, 10**3) + val = Rational($1.to_i, 10**$1.size) e.sec_fraction = val when 'M', 'OM' return unless str.sub!(/\A(\d{1,2})/, '') @@ -485,8 +483,8 @@ class Date then /\A([-+]?\d{1,9})/ else /\A([-+]?\d{1,})/ end, '') -# val = $1.to_i.to_r / (10**9) - val = $1.to_i.to_r / (10**$1.size) +# val = Rational($1.to_i, 10**9) + val = Rational($1.to_i, 10**$1.size) e.sec_fraction = val when 'n', 't' return unless _strptime_i(str, "\s", e) @@ -495,7 +493,7 @@ class Date e._merid = if $1.downcase == 'a' then 0 else 12 end when 'Q' return unless str.sub!(/\A(-?\d{1,})/, '') - val = $1.to_i.to_r / 10**3 + val = Rational($1.to_i, 10**3) e.seconds = val when 'R' return unless _strptime_i(str, '%H:%M', e) @@ -751,7 +749,7 @@ class Date e.hour = $1.to_i e.min = $2.to_i if $2 e.sec = $3.to_i if $3 - e.sec_fraction = $4.to_i.to_r / (10**$4.size) if $4 + e.sec_fraction = Rational($4.to_i, 10**$4.size) if $4 if $5 e.hour %= 12 @@ -766,9 +764,9 @@ class Date =begin def self._parse_beat(str, e) # :nodoc: if str.sub!(/@\s*(\d+)(?:[,.](\d*))?/, ' ') - beat = $1.to_i.to_r - beat += $2.to_i.to_r / (10**$2.size) if $2 - secs = beat.to_r / 1000 + beat = Rational($1.to_i) + beat += Rational($2.to_i, 10**$2.size) if $2 + secs = Rational(beat, 1000) h, min, s, fr = self.day_fraction_to_time(secs) e.hour = h e.min = min @@ -1031,7 +1029,7 @@ class Date end end if $4 - e.sec_fraction = $4.to_i.to_r / (10**$4.size) + e.sec_fraction = Rational($4.to_i, 10**$4.size) end if $5 e.zone = $5 @@ -1135,7 +1133,7 @@ class Date hour, min, sec, = zone.split(':') elsif zone.include?(',') || zone.include?('.') hour, fr, = zone.split(/[,.]/) - min = fr.to_i.to_r / (10**fr.size) * 60 + min = Rational(fr.to_i, 10**fr.size) * 60 else case zone.size when 3 @@ -1171,7 +1169,7 @@ class DateTime < Date if n < 1 '' else - '.%0*d' % [n, (sec_fraction / (1.to_r/86400/(10**n))).round] + '.%0*d' % [n, (sec_fraction / SECONDS_IN_DAY / (10**n)).round] end + '%:z') end |