From af3f253ffdce66d46b696b18175c0ea7544b297f Mon Sep 17 00:00:00 2001 From: Eric Mertens Date: Fri, 31 May 2024 14:49:13 -0700 Subject: [PATCH 1/2] Add FromValue and ToValue instances for UTCTime This encoding automatically translates times into and out of UTC Fixes #19 --- ChangeLog.md | 5 +++++ src/Toml/Schema/FromValue.hs | 7 ++++++- src/Toml/Schema/ToValue.hs | 3 ++- toml-parser.cabal | 2 +- 4 files changed, 14 insertions(+), 3 deletions(-) diff --git a/ChangeLog.md b/ChangeLog.md index 854274b..48383a2 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -1,5 +1,10 @@ # Revision history for toml-parser +## 2.0.1.0 + +* Added `ToValue UTCTime` and `FromValue UTCTime`. These correspond + to offset data-times with the timezone translated to UTC. + ## 2.0.0.0 * Pervasive annotations on the values added to allow for detailed diff --git a/src/Toml/Schema/FromValue.hs b/src/Toml/Schema/FromValue.hs index d692d5b..9c87a38 100644 --- a/src/Toml/Schema/FromValue.hs +++ b/src/Toml/Schema/FromValue.hs @@ -55,7 +55,7 @@ import Data.Sequence qualified as Seq import Data.Text (Text) import Data.Text qualified as Text import Data.Text.Lazy qualified -import Data.Time (ZonedTime, LocalTime, Day, TimeOfDay) +import Data.Time (ZonedTime, LocalTime, Day, TimeOfDay, UTCTime, zonedTimeToUTC) import Data.Word (Word8, Word16, Word32, Word64) import Numeric.Natural (Natural) import Toml.Schema.Matcher @@ -245,6 +245,11 @@ instance FromValue ZonedTime where fromValue (ZonedTime' _ x) = pure x fromValue v = typeError "offset date-time" v +-- | Matches offset date-time literals and converts to UTC +instance FromValue UTCTime where + fromValue (ZonedTime' _ x) = pure (zonedTimeToUTC x) + fromValue v = typeError "offset date-time" v + -- | Matches local date-time literals instance FromValue LocalTime where fromValue (LocalTime' _ x) = pure x diff --git a/src/Toml/Schema/ToValue.hs b/src/Toml/Schema/ToValue.hs index b2a9e7c..99db1e8 100644 --- a/src/Toml/Schema/ToValue.hs +++ b/src/Toml/Schema/ToValue.hs @@ -39,7 +39,7 @@ import Data.Sequence (Seq) import Data.Text (Text) import Data.Text qualified as Text import Data.Text.Lazy qualified -import Data.Time (Day, TimeOfDay, LocalTime, ZonedTime) +import Data.Time (Day, TimeOfDay, LocalTime, ZonedTime, UTCTime, utcToZonedTime, utc) import Data.Word (Word8, Word16, Word32, Word64) import Numeric.Natural (Natural) import Toml.Semantics @@ -163,6 +163,7 @@ instance ToValue Bool where toValue = Bool instance ToValue TimeOfDay where toValue = TimeOfDay instance ToValue LocalTime where toValue = LocalTime instance ToValue ZonedTime where toValue = ZonedTime +instance ToValue UTCTime where toValue = ZonedTime . utcToZonedTime utc instance ToValue Day where toValue = Day instance ToValue Integer where toValue = Integer instance ToValue Natural where toValue = Integer . fromIntegral diff --git a/toml-parser.cabal b/toml-parser.cabal index da307e3..952dacd 100644 --- a/toml-parser.cabal +++ b/toml-parser.cabal @@ -1,6 +1,6 @@ cabal-version: 3.0 name: toml-parser -version: 2.0.0.0 +version: 2.0.1.0 synopsis: TOML 1.0.0 parser description: TOML parser using generated lexers and parsers with From 81a5571f11b195fcc8606a6fd431fc0b3d97ff48 Mon Sep 17 00:00:00 2001 From: Eric Mertens Date: Fri, 31 May 2024 14:50:43 -0700 Subject: [PATCH 2/2] bump dependency on test-drivers --- test-drivers/toml-test-drivers.cabal | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test-drivers/toml-test-drivers.cabal b/test-drivers/toml-test-drivers.cabal index 2d526cd..70dbc30 100644 --- a/test-drivers/toml-test-drivers.cabal +++ b/test-drivers/toml-test-drivers.cabal @@ -30,7 +30,7 @@ common shared ViewPatterns build-depends: base ^>= {4.14, 4.15, 4.16, 4.17, 4.18, 4.19, 4.20}, - toml-parser ^>= 2.0.0.0, + toml-parser ^>= 2.0.1.0, executable TomlDecoder import: shared