Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Changeset 9203

Show
Ignore:
Timestamp:
04/01/08 20:25:26 (3 months ago)
Author:
rick
Message:

Add config.active_support.use_standard_json_time_format setting so that Times and Dates export to ISO 8601 dates. [rick]

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/activesupport/CHANGELOG

    r9153 r9203  
    11*SVN* 
     2 
     3* Add config.active_support.use_standard_json_time_format setting so that Times and Dates export to ISO 8601 dates.  [rick] 
    24 
    35* TZInfo: Removing unneeded TimezoneProxy class [Geoff Buesing] 
  • trunk/activesupport/lib/active_support/json.rb

    r6443 r9203  
    33 
    44module ActiveSupport 
     5  mattr_accessor :use_standard_json_time_format 
     6 
    57  module JSON 
    68    RESERVED_WORDS = %w( 
  • trunk/activesupport/lib/active_support/json/encoders/date_time.rb

    r9093 r9203  
    66  #   # => "2005/02/01 15:15:10 +0000" 
    77  def to_json(options = nil) 
    8     %("#{strftime("%Y/%m/%d %H:%M:%S %z")}") 
     8    if ActiveSupport.use_standard_json_time_format 
     9      xmlschema.inspect 
     10    else 
     11      %("#{strftime("%Y/%m/%d %H:%M:%S %z")}") 
     12    end 
    913  end 
    1014end 
  • trunk/activesupport/lib/active_support/json/encoders/date.rb

    r9093 r9203  
    66  #   # => "2005/02/01" 
    77  def to_json(options = nil) 
    8     %("#{strftime("%Y/%m/%d")}") 
     8    if ActiveSupport.use_standard_json_time_format 
     9      %("#{strftime("%Y-%m-%d")}") 
     10    else 
     11      %("#{strftime("%Y/%m/%d")}") 
     12    end 
    913  end 
    1014end 
  • trunk/activesupport/lib/active_support/json/encoders/time.rb

    r9093 r9203  
    66  #   # => 2005/02/01 15:15:10 +0000" 
    77  def to_json(options = nil) 
    8     %("#{strftime("%Y/%m/%d %H:%M:%S")} #{formatted_offset(false)}") 
     8    if ActiveSupport.use_standard_json_time_format 
     9      utc.xmlschema.inspect 
     10    else 
     11      %("#{strftime("%Y/%m/%d %H:%M:%S")} #{formatted_offset(false)}") 
     12    end 
    913  end 
    1014end 
  • trunk/activesupport/test/json/encoding_test.rb

    r8789 r9203  
    3636  DateTimeTests = [[ DateTime.civil(2005,2,1,15,15,10), %("2005/02/01 15:15:10 +0000") ]] 
    3737 
     38  StandardDateTests     = [[ Date.new(2005,2,1), %("2005-02-01") ]] 
     39  StandardTimeTests     = [[ Time.utc(2005,2,1,15,15,10), %("2005-02-01T15:15:10Z") ]] 
     40  StandardDateTimeTests = [[ DateTime.civil(2005,2,1,15,15,10), %("2005-02-01T15:15:10+00:00") ]] 
     41 
    3842  constants.grep(/Tests$/).each do |class_tests| 
    39     define_method("test_#{class_tests[0..-6].downcase}") do 
    40       self.class.const_get(class_tests).each do |pair| 
    41         assert_equal pair.last, pair.first.to_json 
     43    define_method("test_#{class_tests[0..-6].underscore}") do 
     44      begin 
     45        ActiveSupport.use_standard_json_time_format = class_tests =~ /^Standard/ 
     46        self.class.const_get(class_tests).each do |pair| 
     47          assert_equal pair.last, pair.first.to_json 
     48        end 
     49      ensure 
     50        ActiveSupport.use_standard_json_time_format = false 
    4251      end 
    4352    end