Skip to content

Commit

Permalink
Time#to_json: don't convert to utc before encoding. References #175
Browse files Browse the repository at this point in the history
  • Loading branch information
gbuesing committed May 18, 2008
1 parent 0f33344 commit be85868
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
2 changes: 2 additions & 0 deletions activesupport/CHANGELOG
@@ -1,3 +1,5 @@
* Time#to_json: don't convert to utc before encoding. References #175 [Geoff Buesing]

*2.1.0 RC1 (May 11th, 2008)*

* Remove unused JSON::RESERVED_WORDS, JSON.valid_identifier? and JSON.reserved_word? methods. Resolves #164. [Cheah Chu Yeow]
Expand Down
2 changes: 1 addition & 1 deletion activesupport/lib/active_support/json/encoders/time.rb
Expand Up @@ -6,7 +6,7 @@ class Time
# # => 2005/02/01 15:15:10 +0000"
def to_json(options = nil)
if ActiveSupport.use_standard_json_time_format
utc.xmlschema.inspect
xmlschema.inspect
else
%("#{strftime("%Y/%m/%d %H:%M:%S")} #{formatted_offset(false)}")
end
Expand Down
16 changes: 16 additions & 0 deletions activesupport/test/json/encoding_test.rb
Expand Up @@ -90,6 +90,15 @@ def test_hash_should_allow_key_filtering_with_only
def test_hash_should_allow_key_filtering_with_except
assert_equal %({"b": 2}), { 'foo' => 'bar', :b => 2, :c => 3 }.to_json(:except => ['foo', :c])
end

def test_time_to_json_includes_local_offset
ActiveSupport.use_standard_json_time_format = true
with_env_tz 'US/Eastern' do
assert_equal %("2005-02-01T15:15:10-05:00"), Time.local(2005,2,1,15,15,10).to_json
end
ensure
ActiveSupport.use_standard_json_time_format = false
end

protected
def with_kcode(code)
Expand All @@ -108,6 +117,13 @@ def with_kcode(code)
def object_keys(json_object)
json_object[1..-2].scan(/([^{}:,\s]+):/).flatten.sort
end

def with_env_tz(new_tz = 'US/Eastern')
old_tz, ENV['TZ'] = ENV['TZ'], new_tz
yield
ensure
old_tz ? ENV['TZ'] = old_tz : ENV.delete('TZ')
end
end

uses_mocha 'JsonOptionsTests' do
Expand Down

0 comments on commit be85868

Please sign in to comment.