git.iko.soy/typeable/http-api-data
on master help sign in
Add instances for TimeOfDay and include second fractions in examples
a879adfNickolay Kudasov
src/Web/Internal/HttpApiData.hs+17−5
@@ -456,15 +456,22 @@ timeToUrlPiece :: FormatTime t => String -> t -> Text
456timeToUrlPiece fmt = T.pack . formatTime defaultTimeLocale (iso8601DateFormat (Just fmt))
456timeToUrlPiece fmt = T.pack . formatTime defaultTimeLocale (iso8601DateFormat (Just fmt))
457
457
458-- |
458-- |
459+-- >>> toUrlPiece $ TimeOfDay 14 55 23.1
460+-- "14:55:23.1"
461+instance ToHttpApiData TimeOfDay where
462+ toUrlPiece = T.pack . formatTime defaultTimeLocale "%H:%M:%S%Q"
463+ toEncodedUrlPiece = unsafeToEncodedUrlPiece
464+
465+-- |
459−-- >>> toUrlPiece $ LocalTime (fromGregorian 2015 10 03) (TimeOfDay 14 55 01)
466+-- >>> toUrlPiece $ LocalTime (fromGregorian 2015 10 03) (TimeOfDay 14 55 21.687)
460−-- "2015-10-03T14:55:01"
467+-- "2015-10-03T14:55:21.687"
461instance ToHttpApiData LocalTime where
468instance ToHttpApiData LocalTime where
462 toUrlPiece = timeToUrlPiece "%H:%M:%S%Q"
469 toUrlPiece = timeToUrlPiece "%H:%M:%S%Q"
463 toEncodedUrlPiece = unsafeToEncodedUrlPiece
470 toEncodedUrlPiece = unsafeToEncodedUrlPiece
464
471
465-- |
472-- |
466−-- >>> toUrlPiece $ ZonedTime (LocalTime (fromGregorian 2015 10 03) (TimeOfDay 14 55 01)) utc
473+-- >>> toUrlPiece $ ZonedTime (LocalTime (fromGregorian 2015 10 03) (TimeOfDay 14 55 51.001)) utc
467−-- "2015-10-03T14:55:01+0000"
474+-- "2015-10-03T14:55:51.001+0000"
468instance ToHttpApiData ZonedTime where
475instance ToHttpApiData ZonedTime where
469 toUrlPiece = timeToUrlPiece "%H:%M:%S%Q%z"
476 toUrlPiece = timeToUrlPiece "%H:%M:%S%Q%z"
470 toEncodedUrlPiece = unsafeToEncodedUrlPiece
477 toEncodedUrlPiece = unsafeToEncodedUrlPiece
@@ -580,7 +587,12 @@ instance FromHttpApiData L.Text where parseUrlPiece = Right . L.fromStrict
580-- |
587-- |
581-- >>> toGregorian <$> parseUrlPiece "2016-12-01"
588-- >>> toGregorian <$> parseUrlPiece "2016-12-01"
582-- Right (2016,12,1)
589-- Right (2016,12,1)
583−instance FromHttpApiData Day where parseUrlPiece = runAtto Atto.day
590+instance FromHttpApiData Day where parseUrlPiece = runAtto Atto.day
591+
592+-- |
593+-- >>> parseUrlPiece "14:55:01.333" :: Either Text TimeOfDay
594+-- Right 14:55:01.333
595+instance FromHttpApiData TimeOfDay where parseUrlPiece = runAtto Atto.timeOfDay
584
596
585-- |
597-- |
586-- >>> parseUrlPiece "2015-10-03T14:55:01" :: Either Text LocalTime
598-- >>> parseUrlPiece "2015-10-03T14:55:01" :: Either Text LocalTime
test/Web/Internal/HttpApiDataSpec.hs+9−3
@@ -76,6 +76,7 @@ spec = do
76 checkUrlPiece (Proxy :: Proxy T.Text) "Text.Strict"
76 checkUrlPiece (Proxy :: Proxy T.Text) "Text.Strict"
77 checkUrlPiece (Proxy :: Proxy L.Text) "Text.Lazy"
77 checkUrlPiece (Proxy :: Proxy L.Text) "Text.Lazy"
78 checkUrlPiece (Proxy :: Proxy Day) "Day"
78 checkUrlPiece (Proxy :: Proxy Day) "Day"
79+ checkUrlPiece' timeOfDayGen "TimeOfDay"
79 checkUrlPiece' localTimeGen "LocalTime"
80 checkUrlPiece' localTimeGen "LocalTime"
80 checkUrlPiece' zonedTimeGen "ZonedTime"
81 checkUrlPiece' zonedTimeGen "ZonedTime"
81 checkUrlPiece' utcTimeGen "UTCTime"
82 checkUrlPiece' utcTimeGen "UTCTime"
@@ -112,6 +113,7 @@ spec = do
112 checkEncodedUrlPiece (Proxy :: Proxy T.Text) "Text.Strict"
113 checkEncodedUrlPiece (Proxy :: Proxy T.Text) "Text.Strict"
113 checkEncodedUrlPiece (Proxy :: Proxy L.Text) "Text.Lazy"
114 checkEncodedUrlPiece (Proxy :: Proxy L.Text) "Text.Lazy"
114 checkEncodedUrlPiece (Proxy :: Proxy Day) "Day"
115 checkEncodedUrlPiece (Proxy :: Proxy Day) "Day"
116+ checkEncodedUrlPiece' timeOfDayGen "TimeOfDay"
115 checkEncodedUrlPiece' localTimeGen "LocalTime"
117 checkEncodedUrlPiece' localTimeGen "LocalTime"
116 checkEncodedUrlPiece' zonedTimeGen "ZonedTime"
118 checkEncodedUrlPiece' zonedTimeGen "ZonedTime"
117 checkEncodedUrlPiece' utcTimeGen "UTCTime"
119 checkEncodedUrlPiece' utcTimeGen "UTCTime"
@@ -144,9 +146,13 @@ uuidGen = UUID.fromWords <$> arbitrary <*> arbitrary <*> arbitrary <*> arbitrary
144
146
145-- TODO: this generators don't generate full range items
147-- TODO: this generators don't generate full range items
146localTimeGen :: Gen LocalTime
148localTimeGen :: Gen LocalTime
147−localTimeGen = LocalTime
149+localTimeGen = LocalTime <$> arbitrary <*> timeOfDayGen
148− <$> arbitrary
150+
151+timeOfDayGen :: Gen TimeOfDay
152+timeOfDayGen = TimeOfDay
153+ <$> choose (0, 23)
154+ <*> choose (0, 59)
149− <*> liftA3 TimeOfDay (choose (0, 23)) (choose (0, 59)) (fromInteger <$> choose (0, 60))
155+ <*> fmap (\x -> 0.1 * fromInteger x) (choose (0, 600))
150
156
151zonedTimeGen :: Gen ZonedTime
157zonedTimeGen :: Gen ZonedTime
152zonedTimeGen = ZonedTime
158zonedTimeGen = ZonedTime