git.iko.soy/typeable/http-api-data
on master help sign in
Merge pull request #53 from fizruk/fix-time

Fix ToHttpApiData instances for time types

commite5b2414f912dc36abbdacbb68a967a459e7fe25f authorNickolay Kudasov <fizruk@users.noreply.github.com> dateFri 21 Apr 2017, 10:03 (9 years ago) parentf470704a879adf
2 files changed+31−13
src/Web/Internal/HttpApiData.hs+22−10 test/Web/Internal/HttpApiDataSpec.hs+9−3
src/Web/Internal/HttpApiData.hs+22−10
@@ -456,24 +456,31 @@ timeToUrlPiece :: FormatTime t => String -> t -> Text
456456timeToUrlPiece fmt = T.pack . formatTime defaultTimeLocale (iso8601DateFormat (Just fmt))
457457
458458-- |
459−-- >>> toUrlPiece $ LocalTime (fromGregorian 2015 10 03) (TimeOfDay 14 55 01)
460−-- "2015-10-03T14:55:01"
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+-- |
466+-- >>> toUrlPiece $ LocalTime (fromGregorian 2015 10 03) (TimeOfDay 14 55 21.687)
467+-- "2015-10-03T14:55:21.687"
461468instance ToHttpApiData LocalTime where
462− toUrlPiece = timeToUrlPiece "%H:%M:%S"
469+ toUrlPiece = timeToUrlPiece "%H:%M:%S%Q"
463470 toEncodedUrlPiece = unsafeToEncodedUrlPiece
464471
465472-- |
466−-- >>> toUrlPiece $ ZonedTime (LocalTime (fromGregorian 2015 10 03) (TimeOfDay 14 55 01)) utc
467−-- "2015-10-03T14:55:01+0000"
473+-- >>> toUrlPiece $ ZonedTime (LocalTime (fromGregorian 2015 10 03) (TimeOfDay 14 55 51.001)) utc
474+-- "2015-10-03T14:55:51.001+0000"
468475instance ToHttpApiData ZonedTime where
469− toUrlPiece = timeToUrlPiece "%H:%M:%S%z"
476+ toUrlPiece = timeToUrlPiece "%H:%M:%S%Q%z"
470477 toEncodedUrlPiece = unsafeToEncodedUrlPiece
471478
472479-- |
473−-- >>> toUrlPiece $ UTCTime (fromGregorian 2015 10 03) 864
474−-- "2015-10-03T00:14:24Z"
480+-- >>> toUrlPiece $ UTCTime (fromGregorian 2015 10 03) 864.5
481+-- "2015-10-03T00:14:24.5Z"
475482instance ToHttpApiData UTCTime where
476− toUrlPiece = timeToUrlPiece "%H:%M:%SZ"
483+ toUrlPiece = timeToUrlPiece "%H:%M:%S%QZ"
477484 toEncodedUrlPiece = unsafeToEncodedUrlPiece
478485
479486instance ToHttpApiData NominalDiffTime where
@@ -580,7 +587,12 @@ instance FromHttpApiData L.Text where parseUrlPiece = Right . L.fromStrict
580587-- |
581588-- >>> toGregorian <$> parseUrlPiece "2016-12-01"
582589-- 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
584596
585597-- |
586598-- >>> parseUrlPiece "2015-10-03T14:55:01" :: Either Text LocalTime
test/Web/Internal/HttpApiDataSpec.hs+9−3
@@ -76,6 +76,7 @@ spec = do
7676 checkUrlPiece (Proxy :: Proxy T.Text) "Text.Strict"
7777 checkUrlPiece (Proxy :: Proxy L.Text) "Text.Lazy"
7878 checkUrlPiece (Proxy :: Proxy Day) "Day"
79+ checkUrlPiece' timeOfDayGen "TimeOfDay"
7980 checkUrlPiece' localTimeGen "LocalTime"
8081 checkUrlPiece' zonedTimeGen "ZonedTime"
8182 checkUrlPiece' utcTimeGen "UTCTime"
@@ -112,6 +113,7 @@ spec = do
112113 checkEncodedUrlPiece (Proxy :: Proxy T.Text) "Text.Strict"
113114 checkEncodedUrlPiece (Proxy :: Proxy L.Text) "Text.Lazy"
114115 checkEncodedUrlPiece (Proxy :: Proxy Day) "Day"
116+ checkEncodedUrlPiece' timeOfDayGen "TimeOfDay"
115117 checkEncodedUrlPiece' localTimeGen "LocalTime"
116118 checkEncodedUrlPiece' zonedTimeGen "ZonedTime"
117119 checkEncodedUrlPiece' utcTimeGen "UTCTime"
@@ -144,9 +146,13 @@ uuidGen = UUID.fromWords <$> arbitrary <*> arbitrary <*> arbitrary <*> arbitrary
144146
145147-- TODO: this generators don't generate full range items
146148localTimeGen :: Gen LocalTime
147−localTimeGen = LocalTime
148− <$> arbitrary
149− <*> liftA3 TimeOfDay (choose (0, 23)) (choose (0, 59)) (fromInteger <$> choose (0, 60))
149+localTimeGen = LocalTime <$> arbitrary <*> timeOfDayGen
150+
151+timeOfDayGen :: Gen TimeOfDay
152+timeOfDayGen = TimeOfDay
153+ <$> choose (0, 23)
154+ <*> choose (0, 59)
155+ <*> fmap (\x -> 0.1 * fromInteger x) (choose (0, 600))
150156
151157zonedTimeGen :: Gen ZonedTime
152158zonedTimeGen = ZonedTime