From dd10d43da1d9d9af607b94293796541d7d4b62ab Mon Sep 17 00:00:00 2001 From: Bram Date: Fri, 15 Dec 2023 23:55:03 +0100 Subject: [PATCH] Add Timestamp data type --- elm.json | 2 ++ src/Internal/Tools/Timestamp.elm | 43 ++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 src/Internal/Tools/Timestamp.elm diff --git a/elm.json b/elm.json index 869ba7a..0b6d564 100644 --- a/elm.json +++ b/elm.json @@ -8,12 +8,14 @@ "Matrix", "Internal.Tools.Hashdict", "Internal.Tools.Iddict", + "Internal.Tools.Timestamp", "Internal.Tools.VersionControl" ], "elm-version": "0.19.0 <= v < 0.20.0", "dependencies": { "elm/core": "1.0.0 <= v < 2.0.0", "elm/json": "1.0.0 <= v < 2.0.0", + "elm/time": "1.0.0 <= v < 2.0.0", "miniBill/elm-fast-dict": "1.0.0 <= v < 2.0.0" }, "test-dependencies": {} diff --git a/src/Internal/Tools/Timestamp.elm b/src/Internal/Tools/Timestamp.elm new file mode 100644 index 0000000..8eddec6 --- /dev/null +++ b/src/Internal/Tools/Timestamp.elm @@ -0,0 +1,43 @@ +module Internal.Tools.Timestamp exposing + ( Timestamp + , encode, decoder + ) + +{-| The Timestamp module is a simplification of the Timetsamp as delivered by +elm/time. This module offers ways to work with the timestamp in meaningful ways. + + +## Timetstamp + +@docs Timestamp + + +## JSON coders + +@docs encode, decoder + +-} + +import Json.Decode as D +import Json.Encode as E +import Time + + +{-| The Timetstamp data type representing a moment in time. +-} +type alias Timestamp = + Time.Posix + + +{-| Encode a timestamp into a JSON value. +-} +encode : Timestamp -> E.Value +encode = + Time.posixToMillis >> E.int + + +{-| Decode a timestamp from a JSON value. +-} +decoder : D.Decoder Timestamp +decoder = + D.map Time.millisToPosix D.int