1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
|
{-
Copyright (C) 2009-2011 John Goerzen <jgoerzen@complete.org>
All rights reserved.
For license and copyright information, see the file LICENSE
-}
module TestTime where
import TestInfrastructure
import Data.Convertible
import Test.QuickCheck
import Test.QuickCheck.Tools
import Test.QuickCheck.Instances
import qualified Test.QuickCheck.Property as P
import qualified System.Time as ST
import Data.Time
import Data.Time.Clock.POSIX
import Data.Ratio
import Foreign.C.Types
instance Arbitrary ST.ClockTime where
arbitrary = do r1 <- arbitrary
r2 <- sized $ \n -> choose (0, 1000000000000 - 1)
return (ST.TOD r1 r2)
-- coarbitrary (ST.TOD a b) = coarbitrary a . coarbitrary b
instance Arbitrary ST.CalendarTime where
arbitrary = do r <- arbitrary
return $ convert (r::POSIXTime)
instance Arbitrary NominalDiffTime where
arbitrary = do r <- arbitrary
return $ convert (r::ST.ClockTime)
instance Arbitrary UTCTime where
arbitrary = do r <- arbitrary
return $ convert (r::POSIXTime)
instance Arbitrary ZonedTime where
arbitrary = do r <- arbitrary
return $ convert (r::POSIXTime)
instance Eq ZonedTime where
a == b = zonedTimeToUTC a == zonedTimeToUTC b
propCltCalt :: ST.ClockTime -> P.Result
propCltCalt x =
safeConvert x @?= Right (ST.toUTCTime x)
propCltCaltClt :: ST.ClockTime -> P.Result
propCltCaltClt x =
Right x @=? do r1 <- ((safeConvert x)::ConvertResult ST.CalendarTime)
safeConvert r1
propCltPT :: ST.ClockTime -> P.Result
propCltPT x@(ST.TOD y z) =
safeConvert x @?= Right (r::POSIXTime)
where r = fromRational $ fromInteger y + fromRational (z % 1000000000000)
propPTClt :: POSIXTime -> P.Result
propPTClt x =
safeConvert x @?= Right (r::ST.ClockTime)
where r = ST.TOD rsecs rpico
rsecs = floor x
rpico = truncate $ abs $ 1000000000000 * (x - (fromIntegral rsecs))
propCaltPT :: ST.CalendarTime -> P.Result
propCaltPT x =
safeConvert x @?= expected
where expected = do r <- safeConvert x
(safeConvert (r :: ST.ClockTime))::ConvertResult POSIXTime
propCltPTClt :: ST.ClockTime -> P.Result
propCltPTClt x =
Right (toTOD x) @=? case do r1 <- (safeConvert x)::ConvertResult POSIXTime
safeConvert r1
of Left x -> Left x
Right y -> Right $ toTOD y
where toTOD (ST.TOD x y) = (x, y)
{-
Right x @=? do r1 <- (safeConvert x)::ConvertResult POSIXTime
safeConvert r1
-}
propPTZTPT :: POSIXTime -> P.Result
propPTZTPT x =
Right x @=? do r1 <- safeConvert x
safeConvert (r1 :: ZonedTime)
propPTCltPT :: POSIXTime -> P.Result
propPTCltPT x =
Right x @=? do r1 <- (safeConvert x)::ConvertResult ST.ClockTime
safeConvert r1
propPTCalPT :: POSIXTime -> P.Result
propPTCalPT x =
Right x @=? do r1 <- safeConvert x
safeConvert (r1::ST.CalendarTime)
propUTCCaltUTC :: UTCTime -> P.Result
propUTCCaltUTC x =
Right x @=? do r1 <- safeConvert x
safeConvert (r1::ST.CalendarTime)
propPTUTC :: POSIXTime -> P.Result
propPTUTC x =
safeConvert x @?= Right (posixSecondsToUTCTime x)
propUTCPT :: UTCTime -> P.Result
propUTCPT x =
safeConvert x @?= Right (utcTimeToPOSIXSeconds x)
propCltUTC :: ST.ClockTime -> P.Result
propCltUTC x =
safeConvert x @?= Right (posixSecondsToUTCTime . convert $ x)
propZTCTeqZTCaltCt :: ZonedTime -> P.Result
propZTCTeqZTCaltCt x =
route1 @=? route2
where route1 = (safeConvert x)::ConvertResult ST.ClockTime
route2 = do calt <- safeConvert x
safeConvert (calt :: ST.CalendarTime)
propCaltZTCalt :: ST.ClockTime -> P.Result
propCaltZTCalt x =
Right x @=? do zt <- ((safeConvert calt)::ConvertResult ZonedTime)
calt' <- ((safeConvert zt)::ConvertResult ST.CalendarTime)
return (ST.toClockTime calt')
where calt = ST.toUTCTime x
propCaltZTCalt2 :: ST.CalendarTime -> P.Result
propCaltZTCalt2 x =
Right x @=? do zt <- safeConvert x
safeConvert (zt :: ZonedTime)
propZTCaltCtZT :: ZonedTime -> P.Result
propZTCaltCtZT x =
Right x @=? do calt <- safeConvert x
ct <- safeConvert (calt :: ST.CalendarTime)
safeConvert (ct :: ST.ClockTime)
propZTCtCaltZT :: ZonedTime -> P.Result
propZTCtCaltZT x =
Right x @=? do ct <- safeConvert x
calt <- safeConvert (ct :: ST.ClockTime)
safeConvert (calt :: ST.CalendarTime)
propZTCaltZT :: ZonedTime -> P.Result
propZTCaltZT x =
Right x @=? do calt <- safeConvert x
safeConvert (calt :: ST.CalendarTime)
propZTCtCaltCtZT :: ZonedTime -> P.Result
propZTCtCaltCtZT x =
Right x @=? do ct <- safeConvert x
calt <- safeConvert (ct :: ST.ClockTime)
ct' <- safeConvert (calt :: ST.CalendarTime)
safeConvert (ct' :: ST.ClockTime)
propUTCZT :: UTCTime -> Bool
propUTCZT x =
x == zonedTimeToUTC (convert x)
propUTCZTUTC :: UTCTime -> P.Result
propUTCZTUTC x =
Right x @=? do r1 <- ((safeConvert x)::ConvertResult ZonedTime)
safeConvert r1
propNdtTdNdt :: NominalDiffTime -> P.Result
propNdtTdNdt x =
Right x @=? do r1 <- ((safeConvert x)::ConvertResult ST.TimeDiff)
safeConvert r1
propPTCPT :: POSIXTime -> P.Result
propPTCPT x =
Right testval @=? do r1 <- safeConvert testval
safeConvert (r1 :: CTime)
where testval = (convert ((truncate x)::Integer))::POSIXTime -- CTime doesn't support picosecs
allt = [q "ClockTime -> CalendarTime" propCltCalt,
q "ClockTime -> CalendarTime -> ClockTime" propCltCaltClt,
q "ClockTime -> POSIXTime" propCltPT,
q "POSIXTime -> ClockTime" propPTClt,
q "CalendarTime -> POSIXTime" propCaltPT,
q "identity ClockTime -> POSIXTime -> ClockTime" propCltPTClt,
q "identity POSIXTime -> ClockTime -> POSIXTime" propPTCltPT,
q "identity POSIXTime -> ZonedTime -> POSIXTime" propPTZTPT,
q "identity POSIXTime -> CalendarTime -> POSIXTime" propPTCalPT,
q "identity UTCTime -> CalendarTime -> UTCTime" propUTCCaltUTC,
q "POSIXTime -> UTCTime" propPTUTC,
q "UTCTime -> POSIXTime" propUTCPT,
q "ClockTime -> UTCTime" propCltUTC,
q "ZonedTime -> ClockTime == ZonedTime -> CalendarTime -> ClockTime" propZTCTeqZTCaltCt,
q "identity CalendarTime -> ZonedTime -> CalendarTime" propCaltZTCalt,
q "identity CalendarTime -> ZonedTime -> CalenderTime, test 2" propCaltZTCalt2,
q "identity ZonedTime -> CalendarTime -> ZonedTime" propZTCaltZT,
q "ZonedTime -> CalendarTime -> ClockTime -> ZonedTime" propZTCaltCtZT,
q "ZonedTime -> ClockTime -> CalendarTime -> ZonedTime" propZTCtCaltZT,
q "ZonedTime -> ColckTime -> CalendarTime -> ClockTime -> ZonedTime" propZTCtCaltCtZT,
q "UTCTime -> ZonedTime" propUTCZT,
q "UTCTime -> ZonedTime -> UTCTime" propUTCZTUTC,
q "identity NominalDiffTime -> TimeDiff -> NominalDiffTime" propNdtTdNdt,
q "identity POSIXTime -> CTime -> POSIXTime" propPTCPT
]
|