Skip to content

Commit

Permalink
encode timestamp with canonical format (iso8601) see #87
Browse files Browse the repository at this point in the history
  • Loading branch information
jlgeering committed Aug 2, 2022
1 parent 9236d94 commit 3297ec1
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
4 changes: 2 additions & 2 deletions lib/ymlr/encoder.ex
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,10 @@ defmodule Ymlr.Encoder do
"{}"
end

defp encode_as_io_list(%Date{} = data, _), do: Calendar.strftime(data, "%Y-%m-%d")
defp encode_as_io_list(%Date{} = data, _), do: Date.to_iso8601(data)

defp encode_as_io_list(%DateTime{} = data, _) do
data |> DateTime.shift_zone!("Etc/UTC") |> Calendar.strftime("%Y-%m-%d %H:%M:%S.000000000 Z")
data |> DateTime.shift_zone!("Etc/UTC") |> DateTime.to_iso8601()
end

defp encode_as_io_list(data, level) when is_map(data) do
Expand Down
6 changes: 5 additions & 1 deletion lib/ymlr/encoder_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,11 @@ defmodule Ymlr.EncoderTest do
end

test "datetime" do
assert MUT.to_s!(DateTime.from_naive!(~N[2016-05-24 13:26:08.003], "Etc/UTC")) == "2016-05-24 13:26:08.000000000 Z"
assert MUT.to_s!(~U[2016-05-24 13:26:08Z]) == "2016-05-24T13:26:08Z"
assert MUT.to_s!(~U[2016-05-24 13:26:08.1Z]) == "2016-05-24T13:26:08.1Z"
assert MUT.to_s!(~U[2016-05-24 13:26:08.02Z]) == "2016-05-24T13:26:08.02Z"
assert MUT.to_s!(~U[2016-05-24 13:26:08.003Z]) == "2016-05-24T13:26:08.003Z"
assert MUT.to_s!(~U[2016-05-24 13:26:08.0004Z]) == "2016-05-24T13:26:08.0004Z"
end
end

Expand Down

0 comments on commit 3297ec1

Please sign in to comment.