git-annex/Logs/TimeStamp.hs

40 lines
940 B
Haskell
Raw Normal View History

2015-05-10 19:23:38 +00:00
{- log timestamp parsing
-
- Copyright 2015-2016 Joey Hess <id@joeyh.name>
2015-05-10 19:23:38 +00:00
-
- Licensed under the GNU GPL version 3 or higher.
-}
{-# LANGUAGE CPP #-}
module Logs.TimeStamp where
import Utility.PartialPrelude
import Utility.Misc
2015-05-10 19:23:38 +00:00
import Data.Time.Clock.POSIX
import Data.Time
import Data.Ratio
2015-05-10 19:23:38 +00:00
#if ! MIN_VERSION_time(1,5,0)
import System.Locale
#endif
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
parsePOSIXTime s = do
let (sn, sd) = separate (== '.') s
n <- readi sn
if null sd
then return (fromIntegral n)
else do
d <- readi sd
let r = d % (10 ^ (length sd - 1))
return (fromIntegral n + fromRational r)
where
readi :: String -> Maybe Integer
readi = readish
2015-05-10 19:36:58 +00:00
formatPOSIXTime :: String -> POSIXTime -> String
formatPOSIXTime fmt t = formatTime defaultTimeLocale fmt (posixSecondsToUTCTime t)