Merge pull request #10428 from 9999years/add-validate-tasty-arg
[cabal.git] / cabal-validate / src / ClockUtil.hs
blob2df7cdd9866e273c91df5869d24b1feeb1f9749e
1 -- | Utilities for dealing with times and durations.
2 module ClockUtil
3 ( DiffTime
4 , AbsoluteTime
5 , diffAbsoluteTime
6 , getAbsoluteTime
7 , formatDiffTime
8 ) where
10 import Data.Time.Clock (DiffTime, secondsToDiffTime)
11 import Data.Time.Clock.System (getSystemTime, systemToTAITime)
12 import Data.Time.Clock.TAI (AbsoluteTime, diffAbsoluteTime)
13 import Data.Time.Format (defaultTimeLocale, formatTime)
15 -- | Get the current time as an `AbsoluteTime`.
16 getAbsoluteTime :: IO AbsoluteTime
17 getAbsoluteTime = systemToTAITime <$> getSystemTime
19 -- | Format a `DiffTime` nicely.
21 -- Short durations are formatted like @16.34s@, durations longer than a minute
22 -- are formatted like @22:34.68@, durations longer than an hour are formatted
23 -- like @1:32:04.68@.
24 formatDiffTime :: DiffTime -> String
25 formatDiffTime delta =
26 let minute = secondsToDiffTime 60
27 hour = 60 * minute
28 in if delta >= hour
29 then formatTime defaultTimeLocale "%h:%02M:%02ES" delta
30 else
31 if delta >= minute
32 then formatTime defaultTimeLocale "%m:%2ES" delta
33 else formatTime defaultTimeLocale "%2Ess" delta