{-# LANGUAGE CPP              #-}
{-# LANGUAGE FlexibleContexts #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}
module Test.QuickCheck.Instances.Time () where

import Prelude ()
import Test.QuickCheck.Instances.CustomPrelude

import Test.QuickCheck

import qualified Data.Time.Calendar.Compat         as Time
import qualified Data.Time.Calendar.Month.Compat   as Time
import qualified Data.Time.Calendar.Quarter.Compat as Time
import qualified Data.Time.Clock.Compat            as Time
import qualified Data.Time.Clock.System.Compat     as Time
import qualified Data.Time.Clock.TAI.Compat        as Time
import qualified Data.Time.LocalTime.Compat        as Time

-------------------------------------------------------------------------------
-- time
-------------------------------------------------------------------------------

instance Arbitrary Time.Day where
    arbitrary :: Gen Day
arbitrary = Integer -> Day
Time.ModifiedJulianDay (Integer -> Day) -> (Integer -> Integer) -> Integer -> Day
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Integer
2000 Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+) (Integer -> Day) -> Gen Integer -> Gen Day
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Gen Integer
forall a. Arbitrary a => Gen a
arbitrary
    shrink :: Day -> [Day]
shrink    = (Integer -> Day
Time.ModifiedJulianDay (Integer -> Day) -> [Integer] -> [Day]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$>) ([Integer] -> [Day]) -> (Day -> [Integer]) -> Day -> [Day]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Integer -> [Integer]
forall a. Arbitrary a => a -> [a]
shrink (Integer -> [Integer]) -> (Day -> Integer) -> Day -> [Integer]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Day -> Integer
Time.toModifiedJulianDay

instance CoArbitrary Time.Day where
    coarbitrary :: Day -> Gen b -> Gen b
coarbitrary = Integer -> Gen b -> Gen b
forall a b. CoArbitrary a => a -> Gen b -> Gen b
coarbitrary (Integer -> Gen b -> Gen b)
-> (Day -> Integer) -> Day -> Gen b -> Gen b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Day -> Integer
Time.toModifiedJulianDay

instance Function Time.Day where
    function :: (Day -> b) -> Day :-> b
function = (Day -> Integer) -> (Integer -> Day) -> (Day -> b) -> Day :-> b
forall b a c.
Function b =>
(a -> b) -> (b -> a) -> (a -> c) -> a :-> c
functionMap Day -> Integer
Time.toModifiedJulianDay Integer -> Day
Time.ModifiedJulianDay

instance Arbitrary Time.UniversalTime where
    arbitrary :: Gen UniversalTime
arbitrary = Rational -> UniversalTime
Time.ModJulianDate (Rational -> UniversalTime)
-> (Rational -> Rational) -> Rational -> UniversalTime
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Rational
2000 Rational -> Rational -> Rational
forall a. Num a => a -> a -> a
+) (Rational -> UniversalTime) -> Gen Rational -> Gen UniversalTime
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Gen Rational
forall a. Arbitrary a => Gen a
arbitrary
    shrink :: UniversalTime -> [UniversalTime]
shrink    = (Rational -> UniversalTime
Time.ModJulianDate (Rational -> UniversalTime) -> [Rational] -> [UniversalTime]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$>) ([Rational] -> [UniversalTime])
-> (UniversalTime -> [Rational])
-> UniversalTime
-> [UniversalTime]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Rational -> [Rational]
forall a. Arbitrary a => a -> [a]
shrink (Rational -> [Rational])
-> (UniversalTime -> Rational) -> UniversalTime -> [Rational]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. UniversalTime -> Rational
Time.getModJulianDate

instance CoArbitrary Time.UniversalTime where
    coarbitrary :: UniversalTime -> Gen b -> Gen b
coarbitrary = Rational -> Gen b -> Gen b
forall a b. CoArbitrary a => a -> Gen b -> Gen b
coarbitrary (Rational -> Gen b -> Gen b)
-> (UniversalTime -> Rational) -> UniversalTime -> Gen b -> Gen b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. UniversalTime -> Rational
Time.getModJulianDate

instance Arbitrary Time.DiffTime where
    arbitrary :: Gen DiffTime
arbitrary = Gen DiffTime
forall a. Fractional a => Gen a
arbitrarySizedFractional
#if MIN_VERSION_time(1,3,0)
    shrink :: DiffTime -> [DiffTime]
shrink    = DiffTime -> [DiffTime]
forall a. RealFrac a => a -> [a]
shrinkRealFrac
#else
    shrink    = (fromRational <$>) . shrink . toRational
#endif

instance CoArbitrary Time.DiffTime where
    coarbitrary :: DiffTime -> Gen b -> Gen b
coarbitrary = DiffTime -> Gen b -> Gen b
forall a b. Real a => a -> Gen b -> Gen b
coarbitraryReal

instance Function Time.DiffTime where
    function :: (DiffTime -> b) -> DiffTime :-> b
function = (DiffTime -> Rational)
-> (Rational -> DiffTime) -> (DiffTime -> b) -> DiffTime :-> b
forall b a c.
Function b =>
(a -> b) -> (b -> a) -> (a -> c) -> a :-> c
functionMap DiffTime -> Rational
forall a. Real a => a -> Rational
toRational Rational -> DiffTime
forall a. Fractional a => Rational -> a
fromRational

instance Arbitrary Time.UTCTime where
    arbitrary :: Gen UTCTime
arbitrary =
        Day -> DiffTime -> UTCTime
Time.UTCTime
        (Day -> DiffTime -> UTCTime)
-> Gen Day -> Gen (DiffTime -> UTCTime)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Gen Day
forall a. Arbitrary a => Gen a
arbitrary
        Gen (DiffTime -> UTCTime) -> Gen DiffTime -> Gen UTCTime
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> (Rational -> DiffTime
forall a. Fractional a => Rational -> a
fromRational (Rational -> DiffTime)
-> (Double -> Rational) -> Double -> DiffTime
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Double -> Rational
forall a. Real a => a -> Rational
toRational (Double -> DiffTime) -> Gen Double -> Gen DiffTime
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Double, Double) -> Gen Double
forall a. Random a => (a, a) -> Gen a
choose (Double
0::Double, Double
86400))
    shrink :: UTCTime -> [UTCTime]
shrink ut :: UTCTime
ut@(Time.UTCTime Day
day DiffTime
dayTime) =
        [ UTCTime
ut { utctDay :: Day
Time.utctDay     = Day
d' } | Day
d' <- Day -> [Day]
forall a. Arbitrary a => a -> [a]
shrink Day
day     ] [UTCTime] -> [UTCTime] -> [UTCTime]
forall a. [a] -> [a] -> [a]
++
        [ UTCTime
ut { utctDayTime :: DiffTime
Time.utctDayTime = DiffTime
t' } | DiffTime
t' <- DiffTime -> [DiffTime]
forall a. Arbitrary a => a -> [a]
shrink DiffTime
dayTime ]

instance CoArbitrary Time.UTCTime where
    coarbitrary :: UTCTime -> Gen b -> Gen b
coarbitrary (Time.UTCTime Day
day DiffTime
dayTime) =
        Day -> Gen b -> Gen b
forall a b. CoArbitrary a => a -> Gen b -> Gen b
coarbitrary Day
day (Gen b -> Gen b) -> (Gen b -> Gen b) -> Gen b -> Gen b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. DiffTime -> Gen b -> Gen b
forall a b. CoArbitrary a => a -> Gen b -> Gen b
coarbitrary DiffTime
dayTime

instance Function Time.UTCTime where
    function :: (UTCTime -> b) -> UTCTime :-> b
function = (UTCTime -> (Day, DiffTime))
-> ((Day, DiffTime) -> UTCTime) -> (UTCTime -> b) -> UTCTime :-> b
forall b a c.
Function b =>
(a -> b) -> (b -> a) -> (a -> c) -> a :-> c
functionMap (\(Time.UTCTime Day
day DiffTime
dt) -> (Day
day,DiffTime
dt))
                           ((Day -> DiffTime -> UTCTime) -> (Day, DiffTime) -> UTCTime
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry Day -> DiffTime -> UTCTime
Time.UTCTime)

instance Arbitrary Time.NominalDiffTime where
    arbitrary :: Gen NominalDiffTime
arbitrary = Gen NominalDiffTime
forall a. Fractional a => Gen a
arbitrarySizedFractional
    shrink :: NominalDiffTime -> [NominalDiffTime]
shrink    = NominalDiffTime -> [NominalDiffTime]
forall a. RealFrac a => a -> [a]
shrinkRealFrac

instance CoArbitrary Time.NominalDiffTime where
    coarbitrary :: NominalDiffTime -> Gen b -> Gen b
coarbitrary = NominalDiffTime -> Gen b -> Gen b
forall a b. Real a => a -> Gen b -> Gen b
coarbitraryReal

instance Function Time.NominalDiffTime where
    function :: (NominalDiffTime -> b) -> NominalDiffTime :-> b
function = (NominalDiffTime -> Rational)
-> (Rational -> NominalDiffTime)
-> (NominalDiffTime -> b)
-> NominalDiffTime :-> b
forall b a c.
Function b =>
(a -> b) -> (b -> a) -> (a -> c) -> a :-> c
functionMap NominalDiffTime -> Rational
forall a. Real a => a -> Rational
toRational Rational -> NominalDiffTime
forall a. Fractional a => Rational -> a
fromRational

instance Arbitrary Time.TimeZone where
    arbitrary :: Gen TimeZone
arbitrary =
        Int -> Bool -> String -> TimeZone
Time.TimeZone
         (Int -> Bool -> String -> TimeZone)
-> Gen Int -> Gen (Bool -> String -> TimeZone)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Int, Int) -> Gen Int
forall a. Random a => (a, a) -> Gen a
choose (-Int
12Int -> Int -> Int
forall a. Num a => a -> a -> a
*Int
60,Int
14Int -> Int -> Int
forall a. Num a => a -> a -> a
*Int
60) -- utc offset (m)
         Gen (Bool -> String -> TimeZone)
-> Gen Bool -> Gen (String -> TimeZone)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Gen Bool
forall a. Arbitrary a => Gen a
arbitrary -- is summer time
         Gen (String -> TimeZone) -> Gen String -> Gen TimeZone
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ([Gen Char] -> Gen String
forall (t :: * -> *) (m :: * -> *) a.
(Traversable t, Monad m) =>
t (m a) -> m (t a)
sequence ([Gen Char] -> Gen String)
-> (Gen Char -> [Gen Char]) -> Gen Char -> Gen String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Gen Char -> [Gen Char]
forall a. Int -> a -> [a]
replicate Int
4 (Gen Char -> Gen String) -> Gen Char -> Gen String
forall a b. (a -> b) -> a -> b
$ (Char, Char) -> Gen Char
forall a. Random a => (a, a) -> Gen a
choose (Char
'A',Char
'Z'))
    shrink :: TimeZone -> [TimeZone]
shrink tz :: TimeZone
tz@(Time.TimeZone Int
minutes Bool
summerOnly String
name) =
        [ TimeZone
tz { timeZoneMinutes :: Int
Time.timeZoneMinutes    = Int
m' } | Int
m' <- Int -> [Int]
forall a. Arbitrary a => a -> [a]
shrink Int
minutes    ] [TimeZone] -> [TimeZone] -> [TimeZone]
forall a. [a] -> [a] -> [a]
++
        [ TimeZone
tz { timeZoneSummerOnly :: Bool
Time.timeZoneSummerOnly = Bool
s' } | Bool
s' <- Bool -> [Bool]
forall a. Arbitrary a => a -> [a]
shrink Bool
summerOnly ] [TimeZone] -> [TimeZone] -> [TimeZone]
forall a. [a] -> [a] -> [a]
++
        [ TimeZone
tz { timeZoneName :: String
Time.timeZoneName       = String
n' } | String
n' <- String -> [String]
forall a. Arbitrary a => a -> [a]
shrink String
name       ]

instance CoArbitrary Time.TimeZone where
    coarbitrary :: TimeZone -> Gen b -> Gen b
coarbitrary (Time.TimeZone Int
minutes Bool
summerOnly String
name) =
        Int -> Gen b -> Gen b
forall a b. CoArbitrary a => a -> Gen b -> Gen b
coarbitrary Int
minutes (Gen b -> Gen b) -> (Gen b -> Gen b) -> Gen b -> Gen b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Bool -> Gen b -> Gen b
forall a b. CoArbitrary a => a -> Gen b -> Gen b
coarbitrary Bool
summerOnly (Gen b -> Gen b) -> (Gen b -> Gen b) -> Gen b -> Gen b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Gen b -> Gen b
forall a b. CoArbitrary a => a -> Gen b -> Gen b
coarbitrary String
name

instance Arbitrary Time.TimeOfDay where
    arbitrary :: Gen TimeOfDay
arbitrary =
        Int -> Int -> Pico -> TimeOfDay
Time.TimeOfDay
         (Int -> Int -> Pico -> TimeOfDay)
-> Gen Int -> Gen (Int -> Pico -> TimeOfDay)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Int, Int) -> Gen Int
forall a. Random a => (a, a) -> Gen a
choose (Int
0, Int
23) -- hour
         Gen (Int -> Pico -> TimeOfDay)
-> Gen Int -> Gen (Pico -> TimeOfDay)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> (Int, Int) -> Gen Int
forall a. Random a => (a, a) -> Gen a
choose (Int
0, Int
59) -- minute
         Gen (Pico -> TimeOfDay) -> Gen Pico -> Gen TimeOfDay
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> (Rational -> Pico
forall a. Fractional a => Rational -> a
fromRational (Rational -> Pico) -> (Double -> Rational) -> Double -> Pico
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Double -> Rational
forall a. Real a => a -> Rational
toRational (Double -> Pico) -> Gen Double -> Gen Pico
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Double, Double) -> Gen Double
forall a. Random a => (a, a) -> Gen a
choose (Double
0::Double, Double
60)) -- picoseconds, via double
    shrink :: TimeOfDay -> [TimeOfDay]
shrink tod :: TimeOfDay
tod@(Time.TimeOfDay Int
hour Int
minute Pico
sec) =
        [ TimeOfDay
tod { todHour :: Int
Time.todHour = Int
h' } | Int
h' <- Int -> [Int]
forall a. Arbitrary a => a -> [a]
shrink Int
hour   ] [TimeOfDay] -> [TimeOfDay] -> [TimeOfDay]
forall a. [a] -> [a] -> [a]
++
        [ TimeOfDay
tod { todMin :: Int
Time.todMin  = Int
m' } | Int
m' <- Int -> [Int]
forall a. Arbitrary a => a -> [a]
shrink Int
minute ] [TimeOfDay] -> [TimeOfDay] -> [TimeOfDay]
forall a. [a] -> [a] -> [a]
++
        [ TimeOfDay
tod { todSec :: Pico
Time.todSec  = Pico
s' } | Pico
s' <- Pico -> [Pico]
forall a. Arbitrary a => a -> [a]
shrink Pico
sec    ]

instance CoArbitrary Time.TimeOfDay where
    coarbitrary :: TimeOfDay -> Gen b -> Gen b
coarbitrary (Time.TimeOfDay Int
hour Int
minute Pico
sec) =
        Int -> Gen b -> Gen b
forall a b. CoArbitrary a => a -> Gen b -> Gen b
coarbitrary Int
hour (Gen b -> Gen b) -> (Gen b -> Gen b) -> Gen b -> Gen b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Gen b -> Gen b
forall a b. CoArbitrary a => a -> Gen b -> Gen b
coarbitrary Int
minute (Gen b -> Gen b) -> (Gen b -> Gen b) -> Gen b -> Gen b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Pico -> Gen b -> Gen b
forall a b. CoArbitrary a => a -> Gen b -> Gen b
coarbitrary Pico
sec

instance Arbitrary Time.LocalTime where
    arbitrary :: Gen LocalTime
arbitrary =
        Day -> TimeOfDay -> LocalTime
Time.LocalTime
         (Day -> TimeOfDay -> LocalTime)
-> Gen Day -> Gen (TimeOfDay -> LocalTime)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Gen Day
forall a. Arbitrary a => Gen a
arbitrary
         Gen (TimeOfDay -> LocalTime) -> Gen TimeOfDay -> Gen LocalTime
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Gen TimeOfDay
forall a. Arbitrary a => Gen a
arbitrary
    shrink :: LocalTime -> [LocalTime]
shrink lt :: LocalTime
lt@(Time.LocalTime Day
day TimeOfDay
tod) =
        [ LocalTime
lt { localDay :: Day
Time.localDay       = Day
d' } | Day
d' <- Day -> [Day]
forall a. Arbitrary a => a -> [a]
shrink Day
day ] [LocalTime] -> [LocalTime] -> [LocalTime]
forall a. [a] -> [a] -> [a]
++
        [ LocalTime
lt { localTimeOfDay :: TimeOfDay
Time.localTimeOfDay = TimeOfDay
t' } | TimeOfDay
t' <- TimeOfDay -> [TimeOfDay]
forall a. Arbitrary a => a -> [a]
shrink TimeOfDay
tod ]

instance CoArbitrary Time.LocalTime where
    coarbitrary :: LocalTime -> Gen b -> Gen b
coarbitrary (Time.LocalTime Day
day TimeOfDay
tod) =
        Day -> Gen b -> Gen b
forall a b. CoArbitrary a => a -> Gen b -> Gen b
coarbitrary Day
day (Gen b -> Gen b) -> (Gen b -> Gen b) -> Gen b -> Gen b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TimeOfDay -> Gen b -> Gen b
forall a b. CoArbitrary a => a -> Gen b -> Gen b
coarbitrary TimeOfDay
tod

instance Arbitrary Time.ZonedTime where
    arbitrary :: Gen ZonedTime
arbitrary =
        LocalTime -> TimeZone -> ZonedTime
Time.ZonedTime
         (LocalTime -> TimeZone -> ZonedTime)
-> Gen LocalTime -> Gen (TimeZone -> ZonedTime)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Gen LocalTime
forall a. Arbitrary a => Gen a
arbitrary
         Gen (TimeZone -> ZonedTime) -> Gen TimeZone -> Gen ZonedTime
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Gen TimeZone
forall a. Arbitrary a => Gen a
arbitrary
    shrink :: ZonedTime -> [ZonedTime]
shrink zt :: ZonedTime
zt@(Time.ZonedTime LocalTime
lt TimeZone
zone) =
        [ ZonedTime
zt { zonedTimeToLocalTime :: LocalTime
Time.zonedTimeToLocalTime = LocalTime
l' } | LocalTime
l' <- LocalTime -> [LocalTime]
forall a. Arbitrary a => a -> [a]
shrink LocalTime
lt   ] [ZonedTime] -> [ZonedTime] -> [ZonedTime]
forall a. [a] -> [a] -> [a]
++
        [ ZonedTime
zt { zonedTimeZone :: TimeZone
Time.zonedTimeZone        = TimeZone
z' } | TimeZone
z' <- TimeZone -> [TimeZone]
forall a. Arbitrary a => a -> [a]
shrink TimeZone
zone ]

instance CoArbitrary Time.ZonedTime where
    coarbitrary :: ZonedTime -> Gen b -> Gen b
coarbitrary (Time.ZonedTime LocalTime
lt TimeZone
zone) =
        LocalTime -> Gen b -> Gen b
forall a b. CoArbitrary a => a -> Gen b -> Gen b
coarbitrary LocalTime
lt (Gen b -> Gen b) -> (Gen b -> Gen b) -> Gen b -> Gen b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TimeZone -> Gen b -> Gen b
forall a b. CoArbitrary a => a -> Gen b -> Gen b
coarbitrary TimeZone
zone

instance Arbitrary Time.AbsoluteTime where
    arbitrary :: Gen AbsoluteTime
arbitrary =
        DiffTime -> AbsoluteTime -> AbsoluteTime
Time.addAbsoluteTime
         (DiffTime -> AbsoluteTime -> AbsoluteTime)
-> Gen DiffTime -> Gen (AbsoluteTime -> AbsoluteTime)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Gen DiffTime
forall a. Arbitrary a => Gen a
arbitrary
         Gen (AbsoluteTime -> AbsoluteTime)
-> Gen AbsoluteTime -> Gen AbsoluteTime
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> AbsoluteTime -> Gen AbsoluteTime
forall (m :: * -> *) a. Monad m => a -> m a
return AbsoluteTime
Time.taiEpoch
    shrink :: AbsoluteTime -> [AbsoluteTime]
shrink AbsoluteTime
at =
        (DiffTime -> AbsoluteTime -> AbsoluteTime
`Time.addAbsoluteTime` AbsoluteTime
at) (DiffTime -> AbsoluteTime) -> [DiffTime] -> [AbsoluteTime]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> DiffTime -> [DiffTime]
forall a. Arbitrary a => a -> [a]
shrink (AbsoluteTime -> AbsoluteTime -> DiffTime
Time.diffAbsoluteTime AbsoluteTime
at AbsoluteTime
Time.taiEpoch)

instance CoArbitrary Time.AbsoluteTime where
    coarbitrary :: AbsoluteTime -> Gen b -> Gen b
coarbitrary = DiffTime -> Gen b -> Gen b
forall a b. CoArbitrary a => a -> Gen b -> Gen b
coarbitrary (DiffTime -> Gen b -> Gen b)
-> (AbsoluteTime -> DiffTime) -> AbsoluteTime -> Gen b -> Gen b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (AbsoluteTime -> AbsoluteTime -> DiffTime)
-> AbsoluteTime -> AbsoluteTime -> DiffTime
forall a b c. (a -> b -> c) -> b -> a -> c
flip AbsoluteTime -> AbsoluteTime -> DiffTime
Time.diffAbsoluteTime AbsoluteTime
Time.taiEpoch

instance Arbitrary Time.DayOfWeek where
    arbitrary :: Gen DayOfWeek
arbitrary = [DayOfWeek] -> Gen DayOfWeek
forall a. [a] -> Gen a
elements [DayOfWeek
Time.Monday .. DayOfWeek
Time.Sunday]
instance CoArbitrary Time.DayOfWeek where
    coarbitrary :: DayOfWeek -> Gen b -> Gen b
coarbitrary = Int -> Gen b -> Gen b
forall a b. CoArbitrary a => a -> Gen b -> Gen b
coarbitrary (Int -> Gen b -> Gen b)
-> (DayOfWeek -> Int) -> DayOfWeek -> Gen b -> Gen b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. DayOfWeek -> Int
forall a. Enum a => a -> Int
fromEnum
instance Function Time.DayOfWeek where
    function :: (DayOfWeek -> b) -> DayOfWeek :-> b
function = (DayOfWeek -> Int)
-> (Int -> DayOfWeek) -> (DayOfWeek -> b) -> DayOfWeek :-> b
forall b a c.
Function b =>
(a -> b) -> (b -> a) -> (a -> c) -> a :-> c
functionMap DayOfWeek -> Int
forall a. Enum a => a -> Int
fromEnum Int -> DayOfWeek
forall a. Enum a => Int -> a
toEnum

instance Arbitrary Time.SystemTime where
    arbitrary :: Gen SystemTime
arbitrary = Int64 -> Word32 -> SystemTime
Time.MkSystemTime (Int64 -> Word32 -> SystemTime)
-> Gen Int64 -> Gen (Word32 -> SystemTime)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Gen Int64
forall a. Arbitrary a => Gen a
arbitrary Gen (Word32 -> SystemTime) -> Gen Word32 -> Gen SystemTime
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Gen Word32
nano where
        -- generate 0 often.
        nano :: Gen Word32
nano = [(Int, Gen Word32)] -> Gen Word32
forall a. [(Int, Gen a)] -> Gen a
frequency
            [ (Int
1, Word32 -> Gen Word32
forall (f :: * -> *) a. Applicative f => a -> f a
pure Word32
0)
            , (Int
15, (Word32, Word32) -> Gen Word32
forall a. Random a => (a, a) -> Gen a
choose (Word32
0, Word32
999999999))
            ]
    shrink :: SystemTime -> [SystemTime]
shrink (Time.MkSystemTime Int64
s Word32
n) = ((Int64, Word32) -> SystemTime)
-> [(Int64, Word32)] -> [SystemTime]
forall a b. (a -> b) -> [a] -> [b]
map ((Int64 -> Word32 -> SystemTime) -> (Int64, Word32) -> SystemTime
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry Int64 -> Word32 -> SystemTime
Time.MkSystemTime) ((Int64, Word32) -> [(Int64, Word32)]
forall a. Arbitrary a => a -> [a]
shrink (Int64
s, Word32
n))
instance CoArbitrary Time.SystemTime where
    coarbitrary :: SystemTime -> Gen b -> Gen b
coarbitrary (Time.MkSystemTime Int64
s Word32
n) = (Int64, Word32) -> Gen b -> Gen b
forall a b. CoArbitrary a => a -> Gen b -> Gen b
coarbitrary (Int64
s, Word32
n)
instance Function Time.SystemTime where
    function :: (SystemTime -> b) -> SystemTime :-> b
function = (SystemTime -> (Int64, Word32))
-> ((Int64, Word32) -> SystemTime)
-> (SystemTime -> b)
-> SystemTime :-> b
forall b a c.
Function b =>
(a -> b) -> (b -> a) -> (a -> c) -> a :-> c
functionMap
        (\(Time.MkSystemTime Int64
s Word32
n) -> (Int64
s, Word32
n))
        ((Int64 -> Word32 -> SystemTime) -> (Int64, Word32) -> SystemTime
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry Int64 -> Word32 -> SystemTime
Time.MkSystemTime)

instance Arbitrary Time.CalendarDiffDays where
    arbitrary :: Gen CalendarDiffDays
arbitrary = Integer -> Integer -> CalendarDiffDays
Time.CalendarDiffDays (Integer -> Integer -> CalendarDiffDays)
-> Gen Integer -> Gen (Integer -> CalendarDiffDays)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Gen Integer
forall a. Arbitrary a => Gen a
arbitrary Gen (Integer -> CalendarDiffDays)
-> Gen Integer -> Gen CalendarDiffDays
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Gen Integer
forall a. Arbitrary a => Gen a
arbitrary
    shrink :: CalendarDiffDays -> [CalendarDiffDays]
shrink (Time.CalendarDiffDays Integer
m Integer
d) = ((Integer, Integer) -> CalendarDiffDays)
-> [(Integer, Integer)] -> [CalendarDiffDays]
forall a b. (a -> b) -> [a] -> [b]
map ((Integer -> Integer -> CalendarDiffDays)
-> (Integer, Integer) -> CalendarDiffDays
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry Integer -> Integer -> CalendarDiffDays
Time.CalendarDiffDays) ((Integer, Integer) -> [(Integer, Integer)]
forall a. Arbitrary a => a -> [a]
shrink (Integer
m, Integer
d))
instance CoArbitrary Time.CalendarDiffDays where
    coarbitrary :: CalendarDiffDays -> Gen b -> Gen b
coarbitrary (Time.CalendarDiffDays Integer
m Integer
d) = (Integer, Integer) -> Gen b -> Gen b
forall a b. CoArbitrary a => a -> Gen b -> Gen b
coarbitrary (Integer
m, Integer
d)
instance Function Time.CalendarDiffDays where
    function :: (CalendarDiffDays -> b) -> CalendarDiffDays :-> b
function = (CalendarDiffDays -> (Integer, Integer))
-> ((Integer, Integer) -> CalendarDiffDays)
-> (CalendarDiffDays -> b)
-> CalendarDiffDays :-> b
forall b a c.
Function b =>
(a -> b) -> (b -> a) -> (a -> c) -> a :-> c
functionMap
        (\(Time.CalendarDiffDays Integer
m Integer
d) -> (Integer
m, Integer
d))
        ((Integer -> Integer -> CalendarDiffDays)
-> (Integer, Integer) -> CalendarDiffDays
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry Integer -> Integer -> CalendarDiffDays
Time.CalendarDiffDays)

instance Arbitrary Time.CalendarDiffTime where
    arbitrary :: Gen CalendarDiffTime
arbitrary = Integer -> NominalDiffTime -> CalendarDiffTime
Time.CalendarDiffTime (Integer -> NominalDiffTime -> CalendarDiffTime)
-> Gen Integer -> Gen (NominalDiffTime -> CalendarDiffTime)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Gen Integer
forall a. Arbitrary a => Gen a
arbitrary Gen (NominalDiffTime -> CalendarDiffTime)
-> Gen NominalDiffTime -> Gen CalendarDiffTime
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Gen NominalDiffTime
forall a. Arbitrary a => Gen a
arbitrary
    shrink :: CalendarDiffTime -> [CalendarDiffTime]
shrink (Time.CalendarDiffTime Integer
m NominalDiffTime
d) = ((Integer, NominalDiffTime) -> CalendarDiffTime)
-> [(Integer, NominalDiffTime)] -> [CalendarDiffTime]
forall a b. (a -> b) -> [a] -> [b]
map ((Integer -> NominalDiffTime -> CalendarDiffTime)
-> (Integer, NominalDiffTime) -> CalendarDiffTime
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry Integer -> NominalDiffTime -> CalendarDiffTime
Time.CalendarDiffTime) ((Integer, NominalDiffTime) -> [(Integer, NominalDiffTime)]
forall a. Arbitrary a => a -> [a]
shrink (Integer
m, NominalDiffTime
d))
instance CoArbitrary Time.CalendarDiffTime where
    coarbitrary :: CalendarDiffTime -> Gen b -> Gen b
coarbitrary (Time.CalendarDiffTime Integer
m NominalDiffTime
nt) = (Integer, NominalDiffTime) -> Gen b -> Gen b
forall a b. CoArbitrary a => a -> Gen b -> Gen b
coarbitrary (Integer
m, NominalDiffTime
nt)
instance Function Time.CalendarDiffTime where
    function :: (CalendarDiffTime -> b) -> CalendarDiffTime :-> b
function = (CalendarDiffTime -> (Integer, NominalDiffTime))
-> ((Integer, NominalDiffTime) -> CalendarDiffTime)
-> (CalendarDiffTime -> b)
-> CalendarDiffTime :-> b
forall b a c.
Function b =>
(a -> b) -> (b -> a) -> (a -> c) -> a :-> c
functionMap
        (\(Time.CalendarDiffTime Integer
m NominalDiffTime
nt) -> (Integer
m, NominalDiffTime
nt))
        ((Integer -> NominalDiffTime -> CalendarDiffTime)
-> (Integer, NominalDiffTime) -> CalendarDiffTime
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry Integer -> NominalDiffTime -> CalendarDiffTime
Time.CalendarDiffTime)

instance Arbitrary Time.Month where
    arbitrary :: Gen Month
arbitrary = do
        Integer
y <- Gen Integer
forall a. Arbitrary a => Gen a
arbitrary
        Int
m <- (Int, Int) -> Gen Int
chooseInt (Int
1,Int
12)
        Month -> Gen Month
forall (m :: * -> *) a. Monad m => a -> m a
return (Integer -> Int -> Month
Time.fromYearMonth (Integer
y Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Integer
2000) Int
m)
    shrink :: Month -> [Month]
shrink Month
mm = case Month -> (Integer, Int)
Time.toYearMonth Month
mm of
        (Integer
y, Int
m) -> do
            (Integer
y', Int
m') <- (Integer, Int) -> [(Integer, Int)]
forall a. Arbitrary a => a -> [a]
shrink (Integer
y Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
- Integer
2000, Int
m)
            Month -> [Month]
forall (m :: * -> *) a. Monad m => a -> m a
return (Integer -> Int -> Month
Time.fromYearMonth (Integer
y' Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Integer
2000) Int
m')
instance CoArbitrary Time.Month where
    coarbitrary :: Month -> Gen b -> Gen b
coarbitrary (Time.MkMonth Integer
m) = Integer -> Gen b -> Gen b
forall a b. CoArbitrary a => a -> Gen b -> Gen b
coarbitrary Integer
m
instance Function Time.Month where
    function :: (Month -> b) -> Month :-> b
function = (Month -> Integer)
-> (Integer -> Month) -> (Month -> b) -> Month :-> b
forall b a c.
Function b =>
(a -> b) -> (b -> a) -> (a -> c) -> a :-> c
functionMap
        (\(Time.MkMonth Integer
m) -> Integer
m)
        Integer -> Month
Time.MkMonth

instance Arbitrary Time.QuarterOfYear where
    arbitrary :: Gen QuarterOfYear
arbitrary = [QuarterOfYear] -> Gen QuarterOfYear
forall a. [a] -> Gen a
elements [ QuarterOfYear
Time.Q1 .. QuarterOfYear
Time.Q2 ]
instance CoArbitrary Time.QuarterOfYear where
    coarbitrary :: QuarterOfYear -> Gen b -> Gen b
coarbitrary = Int -> Gen b -> Gen b
forall a b. CoArbitrary a => a -> Gen b -> Gen b
coarbitrary (Int -> Gen b -> Gen b)
-> (QuarterOfYear -> Int) -> QuarterOfYear -> Gen b -> Gen b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. QuarterOfYear -> Int
forall a. Enum a => a -> Int
fromEnum
instance Function Time.QuarterOfYear where
    function :: (QuarterOfYear -> b) -> QuarterOfYear :-> b
function = (QuarterOfYear -> b) -> QuarterOfYear :-> b
forall a b. (Eq a, Bounded a, Enum a) => (a -> b) -> a :-> b
functionBoundedEnum

instance Arbitrary Time.Quarter where
    arbitrary :: Gen Quarter
arbitrary = do
        Integer
y <- Gen Integer
forall a. Arbitrary a => Gen a
arbitrary
        QuarterOfYear
q <- Gen QuarterOfYear
forall a. Arbitrary a => Gen a
arbitrary
        Quarter -> Gen Quarter
forall (m :: * -> *) a. Monad m => a -> m a
return (Integer -> QuarterOfYear -> Quarter
Time.fromYearQuarter (Integer
y Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Integer
2000) QuarterOfYear
q)
    shrink :: Quarter -> [Quarter]
shrink Quarter
qq = case Quarter -> (Integer, QuarterOfYear)
Time.toYearQuarter Quarter
qq of
        (Integer
y, QuarterOfYear
q) -> do
            (Integer
y', QuarterOfYear
q') <- (Integer, QuarterOfYear) -> [(Integer, QuarterOfYear)]
forall a. Arbitrary a => a -> [a]
shrink (Integer
y Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
- Integer
2000, QuarterOfYear
q)
            Quarter -> [Quarter]
forall (m :: * -> *) a. Monad m => a -> m a
return (Integer -> QuarterOfYear -> Quarter
Time.fromYearQuarter (Integer
y' Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Integer
2000) QuarterOfYear
q')
instance CoArbitrary Time.Quarter where
    coarbitrary :: Quarter -> Gen b -> Gen b
coarbitrary (Time.MkQuarter Integer
x) = Integer -> Gen b -> Gen b
forall a b. CoArbitrary a => a -> Gen b -> Gen b
coarbitrary Integer
x
instance Function Time.Quarter where
    function :: (Quarter -> b) -> Quarter :-> b
function = (Quarter -> Integer)
-> (Integer -> Quarter) -> (Quarter -> b) -> Quarter :-> b
forall b a c.
Function b =>
(a -> b) -> (b -> a) -> (a -> c) -> a :-> c
functionMap
        (\(Time.MkQuarter Integer
x) -> Integer
x)
        Integer -> Quarter
Time.MkQuarter