2020-01-16 04:56:57 +03:00
|
|
|
-- | Time-related properties we care about.
|
2021-08-17 01:19:24 +03:00
|
|
|
module Data.TimeSpec (spec) where
|
2020-01-16 04:56:57 +03:00
|
|
|
|
2021-09-24 01:56:37 +03:00
|
|
|
import Data.Aeson
|
|
|
|
import Data.Time
|
|
|
|
import Data.Time.Clock.Units
|
|
|
|
import Test.Hspec
|
|
|
|
import Prelude
|
2020-01-16 04:56:57 +03:00
|
|
|
|
|
|
|
spec :: Spec
|
|
|
|
spec = do
|
|
|
|
timeUnitsSpec
|
|
|
|
diffTimeSpec
|
|
|
|
|
|
|
|
timeUnitsSpec :: Spec
|
|
|
|
timeUnitsSpec =
|
|
|
|
describe "time units" $ do
|
|
|
|
it "converts correctly" $ do
|
|
|
|
seconds 123 `shouldBe` 123
|
|
|
|
milliseconds 123 `shouldBe` 0.123
|
|
|
|
microseconds 123 `shouldBe` 0.000123
|
|
|
|
nanoseconds 123 `shouldBe` 0.000000123
|
|
|
|
|
|
|
|
it "has a correct Read instance" $ do
|
|
|
|
seconds (read "123") `shouldBe` 123
|
|
|
|
milliseconds (read "123") `shouldBe` 0.123
|
|
|
|
microseconds (read "123") `shouldBe` 0.000123
|
|
|
|
nanoseconds (read "123") `shouldBe` 0.000000123
|
|
|
|
|
|
|
|
it "JSON serializes as proper units" $ do
|
|
|
|
toJSON (1 :: Seconds) `shouldBe` Number 1
|
|
|
|
decode "1.0" `shouldBe` Just (1 :: Seconds)
|
|
|
|
|
2020-05-13 15:33:16 +03:00
|
|
|
it "converts with convertDuration" $ do
|
|
|
|
convertDuration (2 :: Minutes) `shouldBe` (120 :: NominalDiffTime)
|
|
|
|
convertDuration (60 :: Seconds) `shouldBe` (1 :: Minutes)
|
2020-01-16 04:56:57 +03:00
|
|
|
|
|
|
|
diffTimeSpec :: Spec
|
|
|
|
diffTimeSpec =
|
|
|
|
describe "DiffTime" $ do
|
|
|
|
it "JSON serializes as seconds" $ do
|
2021-09-24 01:56:37 +03:00
|
|
|
-- although we would prefer to use Seconds instead...
|
2020-01-16 04:56:57 +03:00
|
|
|
toJSON (1 :: DiffTime) `shouldBe` Number 1
|
|
|
|
decode "1.0" `shouldBe` Just (1 :: DiffTime)
|