Skip to content

Commit

Permalink
Merge pull request dewski#19 from barsoom/timezones-rails
Browse files Browse the repository at this point in the history
Get the right time zone from Rails' TimeWithZone.
  • Loading branch information
Garrett Bjerkhoel committed Feb 10, 2012
2 parents 61361e4 + 6ef98cd commit 0e1f1bb
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 2 deletions.
2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ PATH
specs:
json_builder (3.0.7)
activesupport (>= 2.0.0)
tzinfo

GEM
remote: http://rubygems.org/
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions json_builder.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 6 additions & 0 deletions lib/json_builder/extensions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion test/compiler_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion test/extensions_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -44,4 +48,4 @@ def test_bson_objectid_value
def test_custom_class
assert_respond_to Dozer.new('hello'), :to_builder
end
end
end
5 changes: 5 additions & 0 deletions test/value_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'))
Expand Down

0 comments on commit 0e1f1bb

Please sign in to comment.