Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add config.active_support.use_standard_json_time_format setting so th…

…at 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
  • Loading branch information...
commit 1eb57a6870cbaa0f965f1c1cf7681a67785e71b0 1 parent d450ac4
@technoweenie technoweenie authored
View
2  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]
View
2  activesupport/lib/active_support/json.rb
@@ -2,6 +2,8 @@
require 'active_support/json/decoding'
module ActiveSupport
+ mattr_accessor :use_standard_json_time_format
+
module JSON
RESERVED_WORDS = %w(
abstract delete goto private transient
View
6 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
View
6 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
View
6 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
View
15 activesupport/test/json/encoding_test.rb
@@ -35,10 +35,19 @@ def initialize(a, b)
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
Please sign in to comment.
Something went wrong with that request. Please try again.