diff options
-rw-r--r-- | activerecord/CHANGELOG.md | 1 | ||||
-rw-r--r-- | activerecord/test/cases/base_test.rb | 4 | ||||
-rw-r--r-- | activesupport/lib/active_support/core_ext/time/conversions.rb | 3 | ||||
-rw-r--r-- | activesupport/test/core_ext/time_ext_test.rb | 18 |
4 files changed, 16 insertions, 10 deletions
diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md index 50bbf9dd9b..9e65cc0145 100644 --- a/activerecord/CHANGELOG.md +++ b/activerecord/CHANGELOG.md @@ -8,6 +8,7 @@ *Rafael Mendonça França* * Add `:nsec` date format. This can be used to improve the precision of cache key. + Please note that this format only works with Ruby 1.9, Ruby 1.8 will ignore it completely. *Jamie Gaskins* diff --git a/activerecord/test/cases/base_test.rb b/activerecord/test/cases/base_test.rb index 5631ac9ce2..2324704e15 100644 --- a/activerecord/test/cases/base_test.rb +++ b/activerecord/test/cases/base_test.rb @@ -2172,11 +2172,15 @@ class BasicsTest < ActiveRecord::TestCase end def test_cache_key_format_for_existing_record_with_updated_at_1 + return skip "Only in Ruby 1.9" if RUBY_VERSION < '1.9' + dev = CachedDeveloper.first assert_equal "cached_developers/#{dev.id}-#{dev.updated_at.utc.to_s(:nsec)}", dev.cache_key end def test_cache_key_changes_when_child_touched + return skip "Only in Ruby 1.9" if RUBY_VERSION < '1.9' + old_timestamp_format = Car.cache_timestamp_format Car.cache_timestamp_format = :nsec car = Car.create diff --git a/activesupport/lib/active_support/core_ext/time/conversions.rb b/activesupport/lib/active_support/core_ext/time/conversions.rb index cb175e8751..d180e1e588 100644 --- a/activesupport/lib/active_support/core_ext/time/conversions.rb +++ b/activesupport/lib/active_support/core_ext/time/conversions.rb @@ -6,7 +6,6 @@ class Time DATE_FORMATS = { :db => "%Y-%m-%d %H:%M:%S", :number => "%Y%m%d%H%M%S", - :nsec => '%Y%m%d%H%M%S%9N', :time => "%H:%M", :short => "%d %b %H:%M", :long => "%B %d, %Y %H:%M", @@ -14,6 +13,8 @@ class Time :rfc822 => lambda { |time| time.strftime("%a, %d %b %Y %H:%M:%S #{time.formatted_offset(false)}") } } + DATE_FORMATS[:nsec] = '%Y%m%d%H%M%S%9N' if RUBY_VERSION >= '1.9' + # Converts to a formatted string. See DATE_FORMATS for builtin formats. # # This method is aliased to <tt>to_s</tt>. diff --git a/activesupport/test/core_ext/time_ext_test.rb b/activesupport/test/core_ext/time_ext_test.rb index 1c71c8e597..7a818411d0 100644 --- a/activesupport/test/core_ext/time_ext_test.rb +++ b/activesupport/test/core_ext/time_ext_test.rb @@ -539,15 +539,15 @@ class TimeExtCalculationsTest < ActiveSupport::TestCase def test_to_s time = Time.utc(2005, 2, 21, 17, 44, 30.12345678901) - 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) - assert_equal "20050221174430", time.to_s(:number) - assert_equal "20050221174430123456789", time.to_s(:nsec) - assert_equal "February 21, 2005 17:44", time.to_s(:long) - assert_equal "February 21st, 2005 17:44", time.to_s(:long_ordinal) + 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) + assert_equal "20050221174430", time.to_s(:number) + assert_equal "20050221174430123456789", time.to_s(:nsec) if RUBY_VERSION >= '1.9' + assert_equal "February 21, 2005 17:44", time.to_s(:long) + assert_equal "February 21st, 2005 17:44", time.to_s(:long_ordinal) with_env_tz "UTC" do assert_equal "Mon, 21 Feb 2005 17:44:30 +0000", time.to_s(:rfc822) end |