diff options
author | Rick Olson <technoweenie@gmail.com> | 2008-04-01 20:25:26 +0000 |
---|---|---|
committer | Rick Olson <technoweenie@gmail.com> | 2008-04-01 20:25:26 +0000 |
commit | 1eb57a6870cbaa0f965f1c1cf7681a67785e71b0 (patch) | |
tree | 329bceef9208beeae74d7f043f1a3fcb8aafb3aa /activesupport | |
parent | d450ac4459165e3af6e45798a6afeac1876f3bc4 (diff) | |
download | rails-1eb57a6870cbaa0f965f1c1cf7681a67785e71b0.tar.gz rails-1eb57a6870cbaa0f965f1c1cf7681a67785e71b0.tar.bz2 rails-1eb57a6870cbaa0f965f1c1cf7681a67785e71b0.zip |
Add config.active_support.use_standard_json_time_format setting so that Times and Dates export to ISO 8601 dates. [rick]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@9203 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activesupport')
-rw-r--r-- | activesupport/CHANGELOG | 2 | ||||
-rw-r--r-- | activesupport/lib/active_support/json.rb | 2 | ||||
-rw-r--r-- | activesupport/lib/active_support/json/encoders/date.rb | 6 | ||||
-rw-r--r-- | activesupport/lib/active_support/json/encoders/date_time.rb | 6 | ||||
-rw-r--r-- | activesupport/lib/active_support/json/encoders/time.rb | 6 | ||||
-rw-r--r-- | activesupport/test/json/encoding_test.rb | 15 |
6 files changed, 31 insertions, 6 deletions
diff --git a/activesupport/CHANGELOG b/activesupport/CHANGELOG index cd48b01c19..6ff50eab54 100644 --- a/activesupport/CHANGELOG +++ b/activesupport/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Add config.active_support.use_standard_json_time_format setting so that Times and Dates export to ISO 8601 dates. [rick] + * TZInfo: Removing unneeded TimezoneProxy class [Geoff Buesing] * TZInfo: Removing unneeded TimezoneIndexDefinition, since we're not including Indexes::Timezones [Geoff Buesing] diff --git a/activesupport/lib/active_support/json.rb b/activesupport/lib/active_support/json.rb index 6c828293e8..5acb02dbe9 100644 --- a/activesupport/lib/active_support/json.rb +++ b/activesupport/lib/active_support/json.rb @@ -2,6 +2,8 @@ require 'active_support/json/encoding' require 'active_support/json/decoding' module ActiveSupport + mattr_accessor :use_standard_json_time_format + module JSON RESERVED_WORDS = %w( abstract delete goto private transient diff --git a/activesupport/lib/active_support/json/encoders/date.rb b/activesupport/lib/active_support/json/encoders/date.rb index d55f17962b..cb9419d29d 100644 --- a/activesupport/lib/active_support/json/encoders/date.rb +++ b/activesupport/lib/active_support/json/encoders/date.rb @@ -5,6 +5,10 @@ class Date # Date.new(2005,2,1).to_json # # => "2005/02/01" def to_json(options = nil) - %("#{strftime("%Y/%m/%d")}") + if ActiveSupport.use_standard_json_time_format + %("#{strftime("%Y-%m-%d")}") + else + %("#{strftime("%Y/%m/%d")}") + end end end diff --git a/activesupport/lib/active_support/json/encoders/date_time.rb b/activesupport/lib/active_support/json/encoders/date_time.rb index 380361c360..d41c3e9786 100644 --- a/activesupport/lib/active_support/json/encoders/date_time.rb +++ b/activesupport/lib/active_support/json/encoders/date_time.rb @@ -5,6 +5,10 @@ class DateTime # DateTime.civil(2005,2,1,15,15,10).to_json # # => "2005/02/01 15:15:10 +0000" def to_json(options = nil) - %("#{strftime("%Y/%m/%d %H:%M:%S %z")}") + if ActiveSupport.use_standard_json_time_format + xmlschema.inspect + else + %("#{strftime("%Y/%m/%d %H:%M:%S %z")}") + end end end diff --git a/activesupport/lib/active_support/json/encoders/time.rb b/activesupport/lib/active_support/json/encoders/time.rb index b0c9189c78..3660d87c82 100644 --- a/activesupport/lib/active_support/json/encoders/time.rb +++ b/activesupport/lib/active_support/json/encoders/time.rb @@ -5,6 +5,10 @@ class Time # Time.utc(2005,2,1,15,15,10).to_json # # => 2005/02/01 15:15:10 +0000" def to_json(options = nil) - %("#{strftime("%Y/%m/%d %H:%M:%S")} #{formatted_offset(false)}") + if ActiveSupport.use_standard_json_time_format + utc.xmlschema.inspect + else + %("#{strftime("%Y/%m/%d %H:%M:%S")} #{formatted_offset(false)}") + end end end diff --git a/activesupport/test/json/encoding_test.rb b/activesupport/test/json/encoding_test.rb index 1cbd472a99..290232988a 100644 --- a/activesupport/test/json/encoding_test.rb +++ b/activesupport/test/json/encoding_test.rb @@ -35,10 +35,19 @@ class TestJSONEncoding < Test::Unit::TestCase TimeTests = [[ Time.utc(2005,2,1,15,15,10), %("2005/02/01 15:15:10 +0000") ]] DateTimeTests = [[ DateTime.civil(2005,2,1,15,15,10), %("2005/02/01 15:15:10 +0000") ]] + StandardDateTests = [[ Date.new(2005,2,1), %("2005-02-01") ]] + StandardTimeTests = [[ Time.utc(2005,2,1,15,15,10), %("2005-02-01T15:15:10Z") ]] + StandardDateTimeTests = [[ DateTime.civil(2005,2,1,15,15,10), %("2005-02-01T15:15:10+00:00") ]] + constants.grep(/Tests$/).each do |class_tests| - define_method("test_#{class_tests[0..-6].downcase}") do - self.class.const_get(class_tests).each do |pair| - assert_equal pair.last, pair.first.to_json + define_method("test_#{class_tests[0..-6].underscore}") do + begin + ActiveSupport.use_standard_json_time_format = class_tests =~ /^Standard/ + self.class.const_get(class_tests).each do |pair| + assert_equal pair.last, pair.first.to_json + end + ensure + ActiveSupport.use_standard_json_time_format = false end end end |