Skip to content
This repository was archived by the owner on May 27, 2026. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion lib/tjson/datatype/timestamp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ def tag

def convert(str)
raise TJSON::TypeError, "expected String, got #{str.class}: #{str.inspect}" unless str.is_a?(::String)
raise TJSON::ParseError, "invalid timestamp: #{str.inspect}" unless str =~ /\A\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z\z/
unless str =~ /\A\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d+)?Z\z/
raise TJSON::ParseError, "invalid timestamp: #{str.inspect}"
end

::Time.iso8601(str)
end
Expand Down
16 changes: 16 additions & 0 deletions spec/tjson/datatype/timestamp_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,20 @@
expect { subject.convert(invalid_timestamp) }.to raise_error(TJSON::ParseError)
end
end

context "valid UTC RFC3339 timestamp with fractional seconds" do
let(:example_timestamp) { "2016-10-02T07:31:51.42Z" }

it "parses successfully" do
expect(subject.convert(example_timestamp)).to be_a Time
end
end

context "valid UTC RFC3339 timestamp with comma separated fractional seconds" do
let(:invalid_timestamp) { "2016-10-02T07:31:51,42Z" }

it "raises TJSON::ParseError" do
expect { subject.convert(invalid_timestamp) }.to raise_error(TJSON::ParseError)
end
end
end