packages feed

http-date 0.0.6.1 → 0.0.7

raw patch · 4 files changed

+58/−6 lines, 4 files

Files

Network/HTTP/Date.hs view
@@ -2,11 +2,11 @@   Fast parser and formatter for HTTP Date. -} module Network.HTTP.Date (-    module Network.HTTP.Date.Types+    module Network.HTTP.Date.Converter+  , module Network.HTTP.Date.Types   -- * Utility functions   , parseHTTPDate   , formatHTTPDate-  , epochTimeToHTTPDate   ) where  import Network.HTTP.Date.Converter
Network/HTTP/Date/Converter.hs view
@@ -1,8 +1,13 @@ {-# LANGUAGE BangPatterns #-}-module Network.HTTP.Date.Converter (epochTimeToHTTPDate) where+module Network.HTTP.Date.Converter ( epochTimeToHTTPDate+                                   , httpDateToUTC+                                   , utcToHTTPDate+                                   ) where  import Control.Applicative import Data.ByteString.Internal+import Data.Time+import Data.Time.Calendar.WeekDate import Data.Word import Foreign.Marshal.Array import Foreign.Ptr@@ -34,6 +39,40 @@     w = (days + 3) `rem` 7 + 1     (y,m,d) = toYYMMDD days     (h,n,s) = toHHMMSS secs++-- | Translating 'HTTPDate' to 'UTCTime'.+--+--   Since 0.0.7.+httpDateToUTC :: HTTPDate -> UTCTime+httpDateToUTC x = UTCTime (fromGregorian y m d) (secondsToDiffTime s)+  where+    y = fromIntegral $ hdYear x+    m = hdMonth x+    d = hdDay x+    s = fromIntegral $ (hdHour   x `rem` 24) * 3600+                     + (hdMinute x `rem` 60) * 60+                     + (hdSecond x `rem` 60)++-- | Translating 'UTCTime' to 'HTTPDate'.+--+--   Since 0.0.7.+utcToHTTPDate :: UTCTime -> HTTPDate+utcToHTTPDate x = defaultHTTPDate {+    hdYear   = fromIntegral y+  , hdMonth  = m+  , hdDay    = d+  , hdHour   = h+  , hdMinute = n+  , hdSecond = round s+  , hdWkday  = fromEnum (w :: Int)+  }+  where+    (y, m, d) = toGregorian day+    (h, n, s) = ((todHour tod), (todMin tod), (todSec tod))+    (_, _, w) = toWeekDate day+    day       = localDay time+    tod       = localTimeOfDay time+    time      = utcToLocalTime utc x  toYYMMDD :: Int -> (Int,Int,Int) toYYMMDD x = (yy, mm, dd)
http-date.cabal view
@@ -1,5 +1,5 @@ Name:                   http-date-Version:                0.0.6.1+Version:                0.0.7 Author:                 Kazu Yamamoto <kazu@iij.ad.jp> Maintainer:             Kazu Yamamoto <kazu@iij.ad.jp> License:                BSD3@@ -21,6 +21,7 @@                       , array                       , attoparsec                       , bytestring+                      , time  Test-Suite spec   Type:                 exitcode-stdio-1.0
test/DateSpec.hs view
@@ -9,7 +9,7 @@  spec :: Spec spec = do-    describe "formatHTTPDat" $ do+    describe "formatHTTPDate" $ do         it "behaves like the model" $              forM_ [0,100000..10000000000] $ \epochtime -> do                 let m = model epochtime@@ -17,9 +17,21 @@                     model = utcToDate . epochTimeToUtcTime                     ours  = formatHTTPDate . epochTimeToHTTPDate                 o `shouldBe` m-    describe "parseHTTPDate" $ do+    describe "httpDateToUTC" $ do         it "behaves like the model" $              forM_ [0,100000..10000000000] $ \epochtime -> do+                let m = epochTimeToUtcTime epochtime+                    o = httpDateToUTC $ epochTimeToHTTPDate epochtime+                o `shouldBe` m+    describe "parseHTTPDate" $ do+        it "behaves like the model" $+            forM_ [0,100000..10000000000] $ \epochtime -> do                 let m = epochTimeToHTTPDate epochtime                     Just o = parseHTTPDate $ formatHTTPDate m+                o `shouldBe` m+    describe "utcToHTTPDate" $ do+        it "behaves like the model" $+            forM_ [0,100000..10000000000] $ \epochtime -> do+                let m = epochTimeToHTTPDate epochtime+                    o = utcToHTTPDate $ epochTimeToUtcTime epochtime                 o `shouldBe` m