File: Date.hs

package info (click to toggle)
haskell-wai-logger 0.1.4-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 68 kB
  • sloc: haskell: 197; makefile: 2
file content (63 lines) | stat: -rw-r--r-- 1,497 bytes parent folder | download
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
{-# LANGUAGE CPP #-}

module Network.Wai.Logger.Date (
    ZonedDate
  , DateRef
  , dateInit
  , getDate
  ) where

import Control.Applicative
import Data.ByteString (ByteString)
import qualified Data.ByteString.Char8 as BS
import Data.IORef
import Data.Time
import System.Locale
#if WINDOWS
#define TIME UTCTime
#define GETTIME getCurrentTime
#else
import System.Posix (EpochTime, epochTime)
import Data.Time.Clock.POSIX
#define TIME EpochTime
#define GETTIME epochTime
#endif

-- | A type for zoned date.
type ZonedDate = ByteString

data DateCache = DateCache {
    unixTime :: !TIME
  , zonedDate :: !ZonedDate
  }

-- | Reference to the 'ZonedDate' cache.
newtype DateRef = DateRef (IORef DateCache)

-- | Getting 'ZonedDate' from the 'ZonedDate' cache.
getDate :: DateRef -> IO ZonedDate
getDate (DateRef ref) = do
    newEt <- GETTIME
    cache <- readIORef ref
    let oldEt = unixTime cache
    if oldEt == newEt then
        return $ zonedDate cache
      else do
        newCache <- newDate newEt
        writeIORef ref newCache
        return $ zonedDate newCache

newDate :: TIME -> IO DateCache
newDate et = DateCache et . format <$> toZonedTime et
  where
    toZonedTime = utcToLocalZonedTime . toUTC
#if WINDOWS
    toUTC = id
#else
    toUTC = posixSecondsToUTCTime . realToFrac
#endif
    format = BS.pack . formatTime defaultTimeLocale "%d/%b/%Y:%T %z"

-- | Initializing the 'ZonedDate' cache.
dateInit :: IO DateRef
dateInit = DateRef <$> (GETTIME >>= newDate >>= newIORef)