Skip to content

Commit e8c6118

Browse files
p-mongop
andauthored
RUBY-2158 Return UTC times when parsing extended json (#192)
Co-authored-by: Oleg Pudeyev <[email protected]>
1 parent bc1240e commit e8c6118

File tree

2 files changed

+36
-4
lines changed

2 files changed

+36
-4
lines changed

lib/bson/ext_json.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -251,13 +251,13 @@ module ExtJSON
251251
when '$date'
252252
case value
253253
when String
254-
::Time.parse(value)
254+
::Time.parse(value).utc
255255
when Hash
256256
unless value.keys.sort == %w($numberLong)
257257
raise Error::ExtJSONParseError, "Invalid value for $date: #{value}"
258258
end
259259
sec, msec = value.values.first.to_i.divmod(1000)
260-
::Time.at(sec, msec*1000)
260+
::Time.at(sec, msec*1000).utc
261261
else
262262
raise Error::ExtJSONParseError, "Invalid value for $date: #{value}"
263263
end

spec/bson/ext_json_parse_spec.rb

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,46 @@
5050
end
5151
end
5252

53-
context 'when input is a timestamp' do
53+
context 'when input is a BSON timestamp' do
5454
let(:input) { {'$timestamp' => {'t' => 12345, 'i' => 42}} }
5555

56-
it 'returns a timestamp object' do
56+
it 'returns a BSON::Timestamp instance' do
5757
parsed.should == BSON::Timestamp.new(12345, 42)
5858
end
5959
end
6060

61+
context 'when input is an ISO time' do
62+
let(:input) { {'$date' => '1970-01-01T00:00:04Z'} }
63+
64+
it 'returns a Time instance ' do
65+
parsed.should be_a(Time)
66+
end
67+
68+
it 'returns a Time instance with correct value' do
69+
parsed.should == Time.at(4)
70+
end
71+
72+
it 'returns a Time instance in UTC' do
73+
parsed.zone.should == 'UTC'
74+
end
75+
end
76+
77+
context 'when input is a Unix timestamp' do
78+
let(:input) { {'$date' => {'$numberLong' => '4000'}} }
79+
80+
it 'returns a Time instance ' do
81+
parsed.should be_a(Time)
82+
end
83+
84+
it 'returns a Time instance with correct value' do
85+
parsed.should == Time.at(4)
86+
end
87+
88+
it 'returns a Time instance in UTC' do
89+
parsed.zone.should == 'UTC'
90+
end
91+
end
92+
6193
context 'when input is an int32' do
6294
let(:input) do
6395
{'$numberInt' => '42'}

0 commit comments

Comments
 (0)