ruby-changes:3411
From: ko1@a...
Date: 6 Jan 2008 09:40:18 +0900
Subject: [ruby-changes:3411] tadf - Ruby:r14904 (ruby_1_8): * lib/date.rb, lib/date/format.rb: introduced some constants
tadf 2008-01-06 09:39:44 +0900 (Sun, 06 Jan 2008) New Revision: 14904 Modified files: branches/ruby_1_8/ChangeLog branches/ruby_1_8/lib/date/format.rb branches/ruby_1_8/lib/date.rb branches/ruby_1_8/sample/cal.rb Log: * lib/date.rb, lib/date/format.rb: introduced some constants (for internal use) and aliases (minute and second). * sample/cal.rb: trivial adjustments. http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/branches/ruby_1_8/ChangeLog?r1=14904&r2=14903&diff_format=u http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/branches/ruby_1_8/sample/cal.rb?r1=14904&r2=14903&diff_format=u http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/branches/ruby_1_8/lib/date.rb?r1=14904&r2=14903&diff_format=u http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/branches/ruby_1_8/lib/date/format.rb?r1=14904&r2=14903&diff_format=u Index: ruby_1_8/ChangeLog =================================================================== --- ruby_1_8/ChangeLog (revision 14903) +++ ruby_1_8/ChangeLog (revision 14904) @@ -1,3 +1,10 @@ +Sun Jan 6 09:39:02 2008 Tadayoshi Funaba <tadf@d...> + + * lib/date.rb, lib/date/format.rb: introduced some constants + (for internal use) and aliases (minute and second). + + * sample/cal.rb: trivial adjustments. + Fri Jan 4 23:08:48 2008 Nobuyoshi Nakada <nobu@r...> * time.c (time_arg): use converted object. [ruby-core:14759] Index: ruby_1_8/sample/cal.rb =================================================================== --- ruby_1_8/sample/cal.rb (revision 14903) +++ ruby_1_8/sample/cal.rb (revision 14904) @@ -1,7 +1,7 @@ #! /usr/bin/env ruby -# cal.rb: Written by Tadayoshi Funaba 1998-2004,2006 -# $Id: cal.rb,v 2.10 2006-12-30 21:44:44+09 tadf Exp $ +# cal.rb: Written by Tadayoshi Funaba 1998-2004,2006,2008 +# $Id: cal.rb,v 2.11 2008-01-06 08:42:17+09 tadf Exp $ require 'date' @@ -53,7 +53,7 @@ end def pict(y, m) - d = (1..31).detect{|d| Date.valid_date?(y, m, d, @start)} + d = (1..31).detect{|x| Date.valid_date?(y, m, x, @start)} fi = Date.new(y, m, d, @start) fi -= (fi.jd - @k + 1) % 7 @@ -162,3 +162,5 @@ print cal.print(y, m) end + +# See Bird & Wadler's Introduction to functional programming 4.5. Index: ruby_1_8/lib/date/format.rb =================================================================== --- ruby_1_8/lib/date/format.rb (revision 14903) +++ ruby_1_8/lib/date/format.rb (revision 14904) @@ -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 @@ 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 @@ 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 @@ 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 @@ 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 @@ 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 @@ 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 @@ 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 @@ =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 @@ 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 @@ 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 @@ 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 Index: ruby_1_8/lib/date.rb =================================================================== --- ruby_1_8/lib/date.rb (revision 14903) +++ ruby_1_8/lib/date.rb (revision 14904) @@ -1,12 +1,12 @@ # # date.rb - date and time library # -# Author: Tadayoshi Funaba 1998-2007 +# Author: Tadayoshi Funaba 1998-2008 # # Documentation: William Webber <william@w...> # #-- -# $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,8 +313,21 @@ # 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 @@ # # 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 @@ # # 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 @@ 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 @@ # 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 @@ 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 @@ 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 @@ 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 @@ 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 @@ 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 @@ 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 @@ (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 @@ end public :hour, :min, :sec, :sec_fraction, :zone, :offset, :new_offset +# :minute, :second, :second_fraction end @@ -1604,8 +1631,8 @@ 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 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml