packages feed

unix-time 0.1.3 → 0.1.4

raw patch · 3 files changed

+38/−41 lines, 3 filesdep +QuickCheckPVP ok

version bump matches the API change (PVP)

Dependencies added: QuickCheck

API changes (from Hackage documentation)

Files

Data/UnixTime/Conv.hs view
@@ -34,8 +34,9 @@  -- | -- Parsing 'ByteString' to 'UnixTime' interpreting as localtime.--- Zone in 'Format' (%Z or %z) would be ignored. -- This is a wrapper for strptime_l().+-- Many implementations of strptime_l() do not support %Z and+-- some implementations of strptime_l() do not support %z, either.  parseUnixTime :: Format -> ByteString -> UnixTime parseUnixTime fmt str = unsafePerformIO $@@ -45,7 +46,6 @@             return $ UnixTime sec 0 -- | -- Parsing 'ByteString' to 'UnixTime' interpreting as GMT.--- Zone in 'Format' (%Z or %z) would be ignored. -- This is a wrapper for strptime_l(). -- -- >>> parseUnixTimeGMT webDateFormat "Thu, 01 Jan 1970 00:00:00 GMT"
test/UnixTimeSpec.hs view
@@ -1,63 +1,58 @@-{-# LANGUAGE OverloadedStrings #-}+{-# LANGUAGE OverloadedStrings, FlexibleInstances #-}+{-# OPTIONS_GHC -fno-warn-orphans #-}  module UnixTimeSpec where -import Control.Applicative import Data.ByteString (ByteString) import qualified Data.ByteString.Char8 as BS import Data.Time+import Data.Time.Clock.POSIX (posixSecondsToUTCTime) import Data.UnixTime-import System.Locale+import System.IO.Unsafe (unsafePerformIO)+import System.Locale (defaultTimeLocale) import Test.Hspec--utcTime0 :: UTCTime-utcTime0 = UTCTime {-    utctDay = ModifiedJulianDay 40587-  , utctDayTime = secondsToDiffTime 0-  }+import Test.Hspec.QuickCheck (prop)+import Test.QuickCheck -unixTime0 :: UnixTime-unixTime0 = UnixTime 0 0+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 -{- timezone cannot be parsed by some strptime_l().-jst1970 :: ByteString-jst1970 = "Thu, 01 Jan 1970 09:00:00 +0900"--}+currentTimeZone :: TimeZone+currentTimeZone = unsafePerformIO getCurrentTimeZone  spec :: Spec spec = do     describe "formatUnixTime" $-        it "behaves like the model with utcTime0" $ do-            ans <- formatMailModel utcTime0-            formatUnixTime mailDateFormat unixTime0 `shouldBe` ans--{--    describe "parseUnixTime" $-        it "parses jst1970 properly" $-            parseUnixTime mailDateFormat jst1970 `shouldBe` unixTime0--}+        prop "behaves like the model" $ \ut ->+            let ours = formatUnixTime mailDateFormat ut+                model = formatMailModel (toUTCTime ut) currentTimeZone+            in ours == model      describe "parseUnixTimeGMT & formatUnixTimeGMT" $-        it "inverses the result" $ do-            ut@(UnixTime sec _) <- getUnixTime+        prop "inverses the result" $ \ut@(UnixTime sec _) ->             let dt  = formatUnixTimeGMT webDateFormat ut                 ut' = parseUnixTimeGMT  webDateFormat dt                 dt' = formatUnixTimeGMT webDateFormat ut'-            ut' `shouldBe` UnixTime sec 0-            dt `shouldBe` dt'+            in ut' == UnixTime sec 0 && dt == dt'      describe "addUnixDiffTime & diffUnixTime" $-        it "invrses the result" $ do-            ut0 <- getUnixTime-            ut1 <- getUnixTime+        prop "invrses the result" $ \(ut0, ut1) ->             let ut0' = addUnixDiffTime ut1 $ diffUnixTime ut0 ut1                 ut1' = addUnixDiffTime ut0 $ diffUnixTime ut1 ut0-            ut0' `shouldBe` ut0-            ut1' `shouldBe` ut1+            in ut0' == ut0 && ut1' == ut1 -formatMailModel :: UTCTime -> IO BS.ByteString-formatMailModel ut = ans <$> getCurrentTimeZone+formatMailModel :: UTCTime -> TimeZone -> ByteString+formatMailModel ut zone = BS.pack $ formatTime defaultTimeLocale fmt zt   where-   toZoneTime tz = utcToZonedTime tz ut+   zt = utcToZonedTime zone ut    fmt = BS.unpack mailDateFormat-   ans tz = BS.pack $ formatTime defaultTimeLocale fmt $ toZoneTime tz++toUTCTime :: UnixTime -> UTCTime+toUTCTime = posixSecondsToUTCTime . realToFrac . toEpochTime
unix-time.cabal view
@@ -1,5 +1,5 @@ Name:                   unix-time-Version:                0.1.3+Version:                0.1.4 Author:                 Kazu Yamamoto <kazu@iij.ad.jp> Maintainer:             Kazu Yamamoto <kazu@iij.ad.jp> License:                BSD3@@ -28,7 +28,7 @@ Test-Suite doctests   Type:                 exitcode-stdio-1.0   HS-Source-Dirs:       test-  Ghc-Options:          -threaded+  Ghc-Options:          -threaded -Wall   Main-Is:              doctests.hs   Build-Depends:        base                       , doctest >= 0.9.3@@ -37,6 +37,7 @@   Type:                 exitcode-stdio-1.0   Default-Language:     Haskell2010   Hs-Source-Dirs:       test+  Ghc-Options:          -Wall   Main-Is:              Spec.hs   Other-Modules:        UnixTimeSpec   Build-Depends:        base@@ -44,6 +45,7 @@                       , hspec                       , old-locale                       , old-time+                      , QuickCheck                       , time                       , unix-time