2015-05-10 19:23:38 +00:00
|
|
|
{- log timestamp parsing
|
|
|
|
-
|
|
|
|
- Copyright 2015 Joey Hess <id@joeyh.name>
|
|
|
|
-
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
|
|
|
{-# LANGUAGE CPP #-}
|
|
|
|
|
|
|
|
module Logs.TimeStamp where
|
|
|
|
|
|
|
|
import Data.Time.Clock.POSIX
|
|
|
|
import Data.Time
|
|
|
|
#if ! MIN_VERSION_time(1,5,0)
|
|
|
|
import System.Locale
|
|
|
|
#endif
|
2015-05-10 20:50:46 +00:00
|
|
|
import Control.Applicative
|
|
|
|
import Prelude
|
2015-05-10 19:23:38 +00:00
|
|
|
|
2015-05-10 19:36:58 +00:00
|
|
|
{- Parses how POSIXTime shows itself: "1431286201.113452s"
|
|
|
|
- Also handles the format with no fractional seconds. -}
|
2015-05-10 19:23:38 +00:00
|
|
|
parsePOSIXTime :: String -> Maybe POSIXTime
|
2015-05-10 19:28:23 +00:00
|
|
|
#if MIN_VERSION_time(1,5,0)
|
|
|
|
parsePOSIXTime s = utcTimeToPOSIXSeconds <$> parseTimeM True defaultTimeLocale "%s%Qs" s
|
|
|
|
#else
|
2015-05-10 19:23:38 +00:00
|
|
|
parsePOSIXTime s = utcTimeToPOSIXSeconds <$> parseTime defaultTimeLocale "%s%Qs" s
|
2015-05-10 19:28:23 +00:00
|
|
|
#endif
|
2015-05-10 19:36:58 +00:00
|
|
|
|
|
|
|
formatPOSIXTime :: String -> POSIXTime -> String
|
|
|
|
formatPOSIXTime fmt t = formatTime defaultTimeLocale fmt (posixSecondsToUTCTime t)
|