Skip to content
Browse files

Merge pull request #19 from barsoom/timezones-rails

Get the right time zone from Rails' TimeWithZone.
  • Loading branch information...
2 parents 61361e4 + 6ef98cd commit 0e1f1bb3f302af74f80b1ecadee6d3b32749bf8b Garrett Bjerkhoel committed Feb 10, 2012
Showing with 22 additions and 2 deletions.
  1. +2 −0 Gemfile.lock
  2. +1 −0 json_builder.gemspec
  3. +6 −0 lib/json_builder/extensions.rb
  4. +3 −1 test/compiler_test.rb
  5. +5 −1 test/extensions_test.rb
  6. +5 −0 test/value_test.rb
View
2 Gemfile.lock
@@ -3,6 +3,7 @@ PATH
specs:
json_builder (3.0.7)
activesupport (>= 2.0.0)
+ tzinfo
GEM
remote: http://rubygems.org/
@@ -12,6 +13,7 @@ GEM
i18n (0.5.0)
multi_json (1.0.4)
rake (0.9.2.2)
+ tzinfo (0.3.30)
PLATFORMS
ruby
View
1 json_builder.gemspec
@@ -16,4 +16,5 @@ Gem::Specification.new do |s|
s.require_paths = ['lib']
s.add_dependency 'activesupport', '>= 2.0.0'
+ s.add_dependency 'tzinfo'
end
View
6 lib/json_builder/extensions.rb
@@ -30,6 +30,12 @@ def to_builder
end
end
+class ActiveSupport::TimeWithZone
+ def to_builder
+ iso8601.inspect
+ end
+end
+
class Time
def to_builder
iso8601.inspect
View
4 test/compiler_test.rb
@@ -22,9 +22,11 @@ def test_support_all_dates
date Date.new(2011, 11, 23)
date_time DateTime.new(2001, 2, 3, 4, 5, 6)
timed Time.utc(2012)
+ Time.zone = "CET"
+ zoned Time.zone.local(2012)
end
# The date will have the local time zone offset, hence the wildcard.
- assert_match(%r{\{"date": "2011-11-23T00:00:00.*", "date_time": "2001-02-03T04:05:06Z", "timed": "2012-01-01T00:00:00Z"\}}, actual)
+ assert_match(%r{\{"date": "2011-11-23T00:00:00.*", "date_time": "2001-02-03T04:05:06Z", "timed": "2012-01-01T00:00:00Z", "zoned": "2012-01-01T00:00:00\+01:00"\}}, actual)
end
def test_should_support_all_datatypes
View
6 test/extensions_test.rb
@@ -25,6 +25,10 @@ def test_nil_value
assert_respond_to nil, :to_builder
end
+ def test_time_with_zone_value
+ assert_respond_to Time.zone.now, :to_builder
+ end
+
def test_time_value
assert_respond_to Time.utc(2012), :to_builder
end
@@ -44,4 +48,4 @@ def test_bson_objectid_value
def test_custom_class
assert_respond_to Dozer.new('hello'), :to_builder
end
-end
+end
View
5 test/value_test.rb
@@ -41,6 +41,11 @@ def test_time_value
assert_equal '"2012-01-01T00:00:00Z"', value(Time.utc(2012))
end
+ def test_time_with_zone_value
+ Time.zone = "CET"
+ assert_equal '"2012-01-01T00:00:00+01:00"', value(Time.zone.local(2012))
+ end
+
def test_date_value
# This will be the local time zone offset, hence the wildcard.
assert_match /"2012-01-01T00:00:00.*/, value(Date.parse('2012-01-01'))

0 comments on commit 0e1f1bb

Please sign in to comment.
Something went wrong with that request. Please try again.