module System.Log.FastLogger.Date (
TimeFormat
, FormattedTime
, newTimeCache
, simpleTimeFormat
, simpleTimeFormat'
) where
import Control.AutoUpdate (mkAutoUpdate, defaultUpdateSettings, updateAction)
import Data.ByteString (ByteString)
#if WINDOWS
import qualified Data.ByteString.Char8 as BS
import Data.Time (UTCTime, formatTime, getCurrentTime, utcToLocalZonedTime)
# if MIN_VERSION_time(1,5,0)
import Data.Time (defaultTimeLocale)
# else
import System.Locale (defaultTimeLocale)
# endif
#else
import Data.UnixTime (formatUnixTime, fromEpochTime)
import System.Posix (EpochTime, epochTime)
#endif
type FormattedTime = ByteString
type TimeFormat = ByteString
#if WINDOWS
getTime :: IO UTCTime
getTime = getCurrentTime
formatDate :: TimeFormat -> UTCTime -> IO FormattedTime
formatDate fmt ut = do
zt <- utcToLocalZonedTime ut
return $ BS.pack $ formatTime defaultTimeLocale (BS.unpack fmt) zt
#else
getTime :: IO EpochTime
getTime = epochTime
formatDate :: TimeFormat -> EpochTime -> IO FormattedTime
formatDate fmt = formatUnixTime fmt . fromEpochTime
#endif
newTimeCache :: TimeFormat -> IO (IO FormattedTime)
newTimeCache fmt = mkAutoUpdate defaultUpdateSettings{
updateAction = getTime >>= formatDate fmt
}
simpleTimeFormat :: TimeFormat
simpleTimeFormat = "%d/%b/%Y:%T %z"
simpleTimeFormat' :: TimeFormat
simpleTimeFormat' = "%d-%b-%Y %T"