From ea78055b081804bc3c3e1053bfcdffe02f1d2ba9 Mon Sep 17 00:00:00 2001 From: tadf Date: Sat, 12 Jan 2008 03:03:31 +0000 Subject: * lib/date.rb, lib/date/format.rb: tuning for performance. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@15001 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- lib/date.rb | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) (limited to 'lib/date.rb') diff --git a/lib/date.rb b/lib/date.rb index 8ef1b1fe59..61ba06c79d 100644 --- a/lib/date.rb +++ b/lib/date.rb @@ -6,7 +6,7 @@ # Documentation: William Webber # #-- -# $Id: date.rb,v 2.35 2008-01-06 08:42:17+09 tadf Exp $ +# $Id: date.rb,v 2.36 2008-01-12 10:54:29+09 tadf Exp $ #++ # # == Overview @@ -516,9 +516,9 @@ class Date # Convert a fractional day +fr+ to [hours, minutes, seconds, # fraction_of_a_second] def day_fraction_to_time(fr) # :nodoc: - h, fr = fr.divmod(HOURS_IN_DAY) - min, fr = fr.divmod(MINUTES_IN_DAY) - s, fr = fr.divmod(SECONDS_IN_DAY) + ss, fr = fr.divmod(SECONDS_IN_DAY) # 4p + h, ss = ss.divmod(3600) + min, s = ss.divmod(60) return h, min, s, fr * 86400 end @@ -527,12 +527,16 @@ class Date begin Rational(Rational(1, 2), 2) # a challenge - def time_to_day_fraction(h, min, s) # :nodoc: - Rational(h, 24) + Rational(min, 1440) + Rational(s, 86400) + def time_to_day_fraction(h, min, s) + Rational(h * 3600 + min * 60 + s, 86400) # 4p end rescue - def time_to_day_fraction(h, min, s) # :nodoc: - h.to_r/24 + min.to_r/1440 + s.to_r/86400 + def time_to_day_fraction(h, min, s) + if Integer === h && Integer === min && Integer === s + Rational(h * 3600 + min * 60 + s, 86400) # 4p + else + h.to_r/24 + min.to_r/1440 + s.to_r/86400 + end end end @@ -1469,7 +1473,7 @@ class Date # Return the date as a human-readable string. # # The format used is YYYY-MM-DD. - def to_s() strftime end + def to_s() format('%.4d-%02d-%02d', year, mon, mday) end # 4p # Dump to Marshal format. def marshal_dump() [@ajd, @of, @sg] end @@ -1745,6 +1749,11 @@ class DateTime < Date public :hour, :min, :sec, :sec_fraction, :zone, :offset, :new_offset, :minute, :second, :second_fraction + def to_s # 4p + format('%.4d-%02d-%02dT%02d:%02d:%02d%s', + year, mon, mday, hour, min, sec, zone) + end + end class Time -- cgit v1.2.3