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
|
{-# LANGUAGE CPP #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TemplateHaskell #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}
module UnixTimeSpec (main, spec) where
import Data.ByteString (ByteString)
import qualified Data.ByteString.Char8 as BS
import Data.Function (on)
import Data.Time
import Data.Time.Clock.POSIX (posixSecondsToUTCTime)
import Data.UnixTime
import Foreign.Marshal.Alloc (alloca)
import Foreign.Ptr (Ptr)
import Foreign.Storable (peek, poke)
import qualified Language.Haskell.TH as TH (runIO)
import Test.Hspec
import Test.Hspec.QuickCheck (prop)
import Test.QuickCheck hiding ((===))
#if !MIN_VERSION_time(1,5,0)
import System.Locale (defaultTimeLocale)
#endif
main :: IO ()
main = hspec spec
instance Arbitrary UnixTime where
arbitrary = do
a <- choose (0, 4294967295) :: Gen Int
b <- choose (0, 999999) :: Gen Int
let ut =
UnixTime
{ utSeconds = abs (fromIntegral a)
, utMicroSeconds = fromIntegral b
}
return ut
spec :: Spec
spec = do
describe "formatUnixTime" $
prop "behaves like the model" $ \ut -> do
let ours = formatUnixTime mailDateFormat ut
utcTime = toUTCTime ut
timeZone <- getTimeZone utcTime
let model = formatMailModel utcTime timeZone
ours `shouldReturn` model
describe "parseUnixTimeGMT & formatUnixTimeGMT" $ do
let (===) = (==) `on` utSeconds
prop "inverses the result" $ \ut ->
let dt = formatUnixTimeGMT webDateFormat ut
ut' = parseUnixTimeGMT webDateFormat dt
dt' = formatUnixTimeGMT webDateFormat ut'
in ut === ut' && dt == dt'
prop "inverses the result (2)" $ \ut ->
let str = formatUnixTimeGMT "%s" ut
ut' = parseUnixTimeGMT "%s" str
in ut === ut'
describe "addUnixDiffTime & diffUnixTime" $
prop "invrses the result" $ \(ut0, ut1) ->
let ut0' = addUnixDiffTime ut1 $ diffUnixTime ut0 ut1
ut1' = addUnixDiffTime ut0 $ diffUnixTime ut1 ut0
in ut0' == ut0 && ut1' == ut1
describe "UnixTime Storable instance" $
prop "peek . poke = id" $ \ut ->
let pokePeek :: Ptr UnixTime -> IO UnixTime
pokePeek ptr = poke ptr ut >> peek ptr
in shouldReturn (alloca pokePeek) ut
describe "getUnixTime" $ do
it "works well" $ do
x <- getUnixTime
x `shouldBe` x
it "should work in Template Haskell" $
$( do
time <- TH.runIO getUnixTime
let b = time == time
[|b|]
)
`shouldBe` True
formatMailModel :: UTCTime -> TimeZone -> ByteString
formatMailModel ut zone = BS.pack $ formatTime defaultTimeLocale fmt zt
where
zt = utcToZonedTime zone ut
fmt = BS.unpack mailDateFormat
toUTCTime :: UnixTime -> UTCTime
toUTCTime = posixSecondsToUTCTime . realToFrac . toEpochTime
|