Skip to content

Commit

Permalink
Merge pull request #90 from ufirstgroup/canonical-timestamp-format
Browse files Browse the repository at this point in the history
encode timestamp with canonical format (iso8601) see #87
  • Loading branch information
Michael Ruoss authored Aug 5, 2022
2 parents 45bbb72 + e8982c2 commit bc5948b
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 8 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
name: CI
name: CI checks

on:
push:
branches: [main]
Expand Down
14 changes: 11 additions & 3 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Run tests on versions according to .tool-versions
name: mix test for [version matrix]

on:
push:
Expand All @@ -11,9 +11,17 @@ jobs:
runs-on: ubuntu-latest
name: Test - OTP ${{matrix.otp}} / Elixir ${{matrix.elixir}}
strategy:
# see https://hexdocs.pm/elixir/1.14.0-rc.0/compatibility-and-deprecations.html#compatibility-between-elixir-and-erlang-otp
matrix:
elixir: [1.11.x, 1.12.x]
otp: [22.x, 23.x, 24.x]
elixir: ['1.11.x', '1.12.x', '1.13.x']
otp: ['22.x', '23.x', '24.x']
include:
- elixir: '1.10.x'
otp: '22.x'
- elixir: '>1.10.3'
otp: '23.x'
- elixir: '1.13.4'
otp: '25.x'
steps:
- uses: actions/checkout@v1.0.0

Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
### Changed

- use `Enum.map_join/3` indead of `Enum.map/2` and `Enum.join/2` as it's more efficient according to credo recommendations
- fix #87 - change the serialization of timestamps to use the canonical (iso8601) format, i.e. before: `2022-07-31 14:48:48.000000000 Z` and now: `"2022-07-31T14:48:48Z"`

## [2.0.0] - 2021-04-02

Expand Down
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
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ defmodule Ymlr.MixProject do
app: :ymlr,
description: "A YAML encoder for Elixir",
version: @version,
elixir: "~> 1.11",
elixir: "~> 1.10",
deps: deps(),
dialyzer: dialyzer(),
package: package(),
Expand Down

0 comments on commit bc5948b

Please sign in to comment.