2018-10-30 03:13:36 +00:00
|
|
|
{- timestamp parsing and formatting
|
2015-05-10 19:23:38 +00:00
|
|
|
-
|
2016-09-29 18:04:53 +00:00
|
|
|
- Copyright 2015-2016 Joey Hess <id@joeyh.name>
|
2015-05-10 19:23:38 +00:00
|
|
|
-
|
2018-10-30 03:13:36 +00:00
|
|
|
- License: BSD-2-clause
|
2015-05-10 19:23:38 +00:00
|
|
|
-}
|
|
|
|
|
|
|
|
{-# LANGUAGE CPP #-}
|
|
|
|
|
2018-10-30 03:13:36 +00:00
|
|
|
module Utility.TimeStamp where
|
2015-05-10 19:23:38 +00:00
|
|
|
|
2016-09-29 18:04:53 +00:00
|
|
|
import Utility.PartialPrelude
|
|
|
|
import Utility.Misc
|
|
|
|
|
2015-05-10 19:23:38 +00:00
|
|
|
import Data.Time.Clock.POSIX
|
|
|
|
import Data.Time
|
2016-09-29 18:04:53 +00:00
|
|
|
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
|
2018-10-30 03:33:56 +00:00
|
|
|
parsePOSIXTime = uncurry parsePOSIXTime' . separate (== '.')
|
|
|
|
|
|
|
|
{- Parses the integral and decimal part of a POSIXTime -}
|
|
|
|
parsePOSIXTime' :: String -> String -> Maybe POSIXTime
|
|
|
|
parsePOSIXTime' sn sd = do
|
2018-10-30 03:40:34 +00:00
|
|
|
n <- fromIntegral <$> readi sn
|
2018-10-30 03:36:47 +00:00
|
|
|
let sd' = takeWhile (/= 's') sd
|
|
|
|
if null sd'
|
2018-10-30 03:40:34 +00:00
|
|
|
then return n
|
2016-09-29 18:04:53 +00:00
|
|
|
else do
|
2018-10-30 03:36:47 +00:00
|
|
|
d <- readi sd'
|
|
|
|
let r = d % (10 ^ (length sd'))
|
2018-10-30 03:40:34 +00:00
|
|
|
return $ if n < 0
|
|
|
|
then n - fromRational r
|
|
|
|
else n + fromRational r
|
2016-09-29 18:04:53 +00:00
|
|
|
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)
|