2012-09-25 16:48:24 -04:00
|
|
|
{- Time for humans.
|
|
|
|
-
|
2015-01-21 12:50:09 -04:00
|
|
|
- Copyright 2012-2013 Joey Hess <id@joeyh.name>
|
2012-09-25 16:48:24 -04:00
|
|
|
-
|
2014-05-10 11:01:27 -03:00
|
|
|
- License: BSD-2-clause
|
2012-09-25 16:48:24 -04:00
|
|
|
-}
|
|
|
|
|
2013-10-08 17:12:38 -04:00
|
|
|
module Utility.HumanTime (
|
|
|
|
Duration(..),
|
2014-01-23 15:09:43 -04:00
|
|
|
durationSince,
|
2013-10-08 17:12:38 -04:00
|
|
|
durationToPOSIXTime,
|
2014-01-23 15:09:43 -04:00
|
|
|
durationToDays,
|
|
|
|
daysToDuration,
|
2013-10-08 17:12:38 -04:00
|
|
|
parseDuration,
|
|
|
|
fromDuration,
|
2013-10-08 17:35:25 -04:00
|
|
|
prop_duration_roundtrips
|
2013-10-08 17:12:38 -04:00
|
|
|
) where
|
2012-09-25 16:48:24 -04:00
|
|
|
|
|
|
|
import Utility.PartialPrelude
|
2013-10-08 17:35:25 -04:00
|
|
|
import Utility.QuickCheck
|
2012-09-25 16:48:24 -04:00
|
|
|
|
2015-05-10 16:19:56 -04:00
|
|
|
import qualified Data.Map as M
|
2014-01-23 15:09:43 -04:00
|
|
|
import Data.Time.Clock
|
2012-09-25 16:48:24 -04:00
|
|
|
import Data.Time.Clock.POSIX (POSIXTime)
|
2013-10-08 17:12:38 -04:00
|
|
|
import Data.Char
|
2013-10-08 17:35:25 -04:00
|
|
|
import Control.Applicative
|
2015-05-10 16:19:56 -04:00
|
|
|
import Prelude
|
2012-09-25 16:48:24 -04:00
|
|
|
|
2013-10-08 17:12:38 -04:00
|
|
|
newtype Duration = Duration { durationSeconds :: Integer }
|
2014-10-09 15:09:26 -04:00
|
|
|
deriving (Eq, Ord, Read, Show)
|
2013-10-08 17:12:38 -04:00
|
|
|
|
2014-01-23 15:09:43 -04:00
|
|
|
durationSince :: UTCTime -> IO Duration
|
|
|
|
durationSince pasttime = do
|
|
|
|
now <- getCurrentTime
|
|
|
|
return $ Duration $ round $ diffUTCTime now pasttime
|
|
|
|
|
2013-10-08 17:12:38 -04:00
|
|
|
durationToPOSIXTime :: Duration -> POSIXTime
|
|
|
|
durationToPOSIXTime = fromIntegral . durationSeconds
|
|
|
|
|
2014-01-23 15:09:43 -04:00
|
|
|
durationToDays :: Duration -> Integer
|
|
|
|
durationToDays d = durationSeconds d `div` dsecs
|
|
|
|
|
|
|
|
daysToDuration :: Integer -> Duration
|
|
|
|
daysToDuration i = Duration $ i * dsecs
|
|
|
|
|
2013-10-08 17:12:38 -04:00
|
|
|
{- Parses a human-input time duration, of the form "5h", "1m", "5h1m", etc -}
|
2020-08-15 15:53:35 -04:00
|
|
|
parseDuration :: String -> Either String Duration
|
2020-12-08 12:51:56 -04:00
|
|
|
parseDuration d
|
|
|
|
| null d = parsefail
|
|
|
|
| otherwise = maybe parsefail (Right . Duration) $ go 0 d
|
2012-12-13 00:24:19 -04:00
|
|
|
where
|
2014-10-09 14:53:13 -04:00
|
|
|
go n [] = return n
|
|
|
|
go n s = do
|
2013-10-08 17:12:38 -04:00
|
|
|
num <- readish s :: Maybe Integer
|
2013-10-26 12:07:00 -04:00
|
|
|
case dropWhile isDigit s of
|
|
|
|
(c:rest) -> do
|
|
|
|
u <- M.lookup c unitmap
|
|
|
|
go (n + num * u) rest
|
|
|
|
_ -> return $ n + num
|
2020-08-15 15:53:35 -04:00
|
|
|
parsefail = Left $ "failed to parse duration \"" ++ d ++ "\" (expected eg \"5m\" or \"1h5m\")"
|
2013-10-08 17:12:38 -04:00
|
|
|
|
|
|
|
fromDuration :: Duration -> String
|
2013-10-08 17:35:25 -04:00
|
|
|
fromDuration Duration { durationSeconds = d }
|
|
|
|
| d == 0 = "0s"
|
2019-01-20 20:00:05 -04:00
|
|
|
| otherwise = concatMap showunit $ take 2 $ go [] units d
|
2013-10-08 17:12:38 -04:00
|
|
|
where
|
2019-01-20 20:00:05 -04:00
|
|
|
showunit (u, n) = show n ++ [u]
|
2013-10-08 17:12:38 -04:00
|
|
|
go c [] _ = reverse c
|
2013-10-08 17:35:25 -04:00
|
|
|
go c ((u, n):us) v =
|
|
|
|
let (q,r) = v `quotRem` n
|
2019-01-20 20:00:05 -04:00
|
|
|
in if q > 0
|
|
|
|
then go ((u, q):c) us r
|
|
|
|
else if null c
|
|
|
|
then go c us r
|
|
|
|
else reverse c
|
2013-10-08 17:12:38 -04:00
|
|
|
|
|
|
|
units :: [(Char, Integer)]
|
|
|
|
units =
|
|
|
|
[ ('y', ysecs)
|
|
|
|
, ('d', dsecs)
|
|
|
|
, ('h', hsecs)
|
|
|
|
, ('m', msecs)
|
|
|
|
, ('s', 1)
|
|
|
|
]
|
|
|
|
|
|
|
|
unitmap :: M.Map Char Integer
|
|
|
|
unitmap = M.fromList units
|
|
|
|
|
|
|
|
ysecs :: Integer
|
|
|
|
ysecs = dsecs * 365
|
|
|
|
|
|
|
|
dsecs :: Integer
|
|
|
|
dsecs = hsecs * 24
|
|
|
|
|
|
|
|
hsecs :: Integer
|
|
|
|
hsecs = msecs * 60
|
|
|
|
|
|
|
|
msecs :: Integer
|
|
|
|
msecs = 60
|
2013-10-08 17:35:25 -04:00
|
|
|
|
|
|
|
-- Durations cannot be negative.
|
|
|
|
instance Arbitrary Duration where
|
|
|
|
arbitrary = Duration <$> nonNegative arbitrary
|
|
|
|
|
|
|
|
prop_duration_roundtrips :: Duration -> Bool
|
2020-08-15 15:53:35 -04:00
|
|
|
prop_duration_roundtrips d = parseDuration (fromDuration d) == Right d
|