1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

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
This commit is contained in:
Rick Olson 2008-04-01 20:25:26 +00:00
parent d450ac4459
commit 1eb57a6870
6 changed files with 31 additions and 6 deletions

View file

@ -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 file

@ -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

View file

@ -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 file

@ -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 file

@ -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 file

@ -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