Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Date/Time/DateTime Ruby 1.9 compat

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@7647 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
commit cd1f2d7b0ed7a25ff1c298732f921ce6d6d86f73 1 parent 7faeb0d
@jeremy jeremy authored
View
13 activesupport/lib/active_support/core_ext/date/conversions.rb
@@ -17,6 +17,9 @@ def self.included(base) #: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 @@ def to_formatted_s(format = :default)
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 @@ def readable_inspect
# 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
4 activesupport/lib/active_support/core_ext/date_time/conversions.rb
@@ -17,13 +17,13 @@ def to_formatted_s(format = :default)
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
2  activesupport/lib/active_support/core_ext/time/conversions.rb
@@ -24,7 +24,7 @@ def to_formatted_s(format = :default)
if formatter.respond_to?(:call)
formatter.call(self).to_s
else
- strftime(formatter).strip
+ strftime(formatter)
end
else
to_default_s
View
1  activesupport/test/core_ext/date_time_ext_test.rb
@@ -32,6 +32,7 @@ def test_to_datetime
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
7 activesupport/test/core_ext/time_ext_test.rb
@@ -279,7 +279,8 @@ def test_next_week_near_daylight_end
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 @@ def test_acts_like_time
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
Please sign in to comment.
Something went wrong with that request. Please try again.