Skip to content

Commit

Permalink
Add FromValue and ToValue instances for UTCTime (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
glguy authored May 31, 2024
1 parent 91112d1 commit 4a29178
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 4 deletions.
5 changes: 5 additions & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
7 changes: 6 additions & 1 deletion src/Toml/Schema/FromValue.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion src/Toml/Schema/ToValue.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion test-drivers/toml-test-drivers.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion toml-parser.cabal
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit 4a29178

Please sign in to comment.