Date/Time/DateTime Ruby 1.9 compat

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@7647 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
Jeremy Kemper
2007-09-27 08:49:18 +00:00
parent 7faeb0db07
commit cd1f2d7b0e
5 changed files with 16 additions and 11 deletions

View File

@@ -17,6 +17,9 @@ module ActiveSupport #:nodoc:
alias_method :to_s, :to_formatted_s
alias_method :default_inspect, :inspect
alias_method :inspect, :readable_inspect
# Ruby 1.9 has Date#to_time which converts to localtime only.
remove_method :to_time if base.instance_methods.include?(:to_time)
end
end
@@ -25,13 +28,13 @@ module ActiveSupport #:nodoc:
if formatter.respond_to?(:call)
formatter.call(self).to_s
else
strftime(formatter).strip
strftime(formatter)
end
else
to_default_s
end
end
# Overrides the default inspect method with a human readable one, e.g., "Mon, 21 Feb 2005"
def readable_inspect
strftime("%a, %d %b %Y")
@@ -40,18 +43,18 @@ module ActiveSupport #:nodoc:
# To be able to keep Times, Dates and DateTimes interchangeable on conversions
def to_date
self
end
end if RUBY_VERSION < '1.9'
# Converts self to a Ruby Time object; time is set to beginning of day
# Timezone can either be :local or :utc (default :local)
def to_time(form = :local)
::Time.send("#{form}_time", year, month, day)
end
# Converts self to a Ruby DateTime object; time is set to beginning of day
def to_datetime
::DateTime.civil(year, month, day, 0, 0, 0, 0, 0)
end
end if RUBY_VERSION < '1.9'
def xmlschema
to_time.xmlschema

View File

@@ -17,13 +17,13 @@ module ActiveSupport #:nodoc:
if formatter.respond_to?(:call)
formatter.call(self).to_s
else
strftime(formatter).strip
strftime(formatter)
end
else
to_datetime_default_s
end
end
# Overrides the default inspect method with a human readable one, e.g., "Mon, 21 Feb 2005 14:30:00 +0000"
def readable_inspect
to_s(:rfc822)

View File

@@ -24,7 +24,7 @@ module ActiveSupport #:nodoc:
if formatter.respond_to?(:call)
formatter.call(self).to_s
else
strftime(formatter).strip
strftime(formatter)
end
else
to_default_s

View File

@@ -32,6 +32,7 @@ class DateTimeExtCalculationsTest < Test::Unit::TestCase
assert_equal DateTime.new(2005, 2, 21), DateTime.new(2005, 2, 21).to_datetime
end
# FIXME: ruby 1.9 compat
def test_to_time
assert_equal Time.utc(2005, 2, 21, 10, 11, 12), DateTime.new(2005, 2, 21, 10, 11, 12, 0, 0).to_time
assert_equal Time.local(2005, 2, 21, 10, 11, 12), DateTime.new(2005, 2, 21, 10, 11, 12, Rational(-5, 24), 0).to_time

View File

@@ -279,7 +279,8 @@ class TimeExtCalculationsTest < Test::Unit::TestCase
def test_to_s
time = Time.utc(2005, 2, 21, 17, 44, 30)
assert_equal "Mon Feb 21 17:44:30 UTC 2005", time.to_s
assert_equal time.to_default_s, time.to_s
assert_equal time.to_default_s, time.to_s(:doesnt_exist)
assert_equal "2005-02-21 17:44:30", time.to_s(:db)
assert_equal "21 Feb 17:44", time.to_s(:short)
assert_equal "17:44", time.to_s(:time)
@@ -381,10 +382,10 @@ class TimeExtCalculationsTest < Test::Unit::TestCase
end
protected
def with_timezone new_tz='US/Eastern'
def with_timezone(new_tz = 'US/Eastern')
old_tz, ENV['TZ'] = ENV['TZ'], new_tz
yield
ensure
ENV['TZ'] = old_tz
old_tz ? ENV['TZ'] = old_tz : ENV.delete('TZ')
end
end