e213ef310f
* Fix minor FD leak in journal code. Closes: #754608 * direct: Fix handling of case where a work tree subdirectory cannot be written to due to permissions. * migrate: Avoid re-checksumming when migrating from hashE to hash backend. * uninit: Avoid failing final removal in some direct mode repositories due to file modes. * S3: Deal with AWS ACL configurations that do not allow creating or checking the location of a bucket, but only reading and writing content to it. * resolvemerge: New plumbing command that runs the automatic merge conflict resolver. * Deal with change in git 2.0 that made indirect mode merge conflict resolution leave behind old files. * sync: Fix git sync with local git remotes even when they don't have an annex.uuid set. (The assistant already did so.) * Set gcrypt-publish-participants when setting up a gcrypt repository, to avoid unncessary passphrase prompts. This is a security/usability tradeoff. To avoid exposing the gpg key ids who can decrypt the repository, users can unset gcrypt-publish-participants. * Install nautilus hooks even when ~/.local/share/nautilus/ does not yet exist, since it is not automatically created for Gnome 3 users. * Windows: Move .vbs files out of git\bin, to avoid that being in the PATH, which caused some weird breakage. (Thanks, divB) * Windows: Fix locking issue that prevented the webapp starting (since 5.20140707). # imported from the archive
101 lines
2.2 KiB
Haskell
101 lines
2.2 KiB
Haskell
{- Time for humans.
|
|
-
|
|
- Copyright 2012-2013 Joey Hess <joey@kitenet.net>
|
|
-
|
|
- License: BSD-2-clause
|
|
-}
|
|
|
|
module Utility.HumanTime (
|
|
Duration(..),
|
|
durationSince,
|
|
durationToPOSIXTime,
|
|
durationToDays,
|
|
daysToDuration,
|
|
parseDuration,
|
|
fromDuration,
|
|
prop_duration_roundtrips
|
|
) where
|
|
|
|
import Utility.PartialPrelude
|
|
import Utility.Applicative
|
|
import Utility.QuickCheck
|
|
|
|
import Data.Time.Clock
|
|
import Data.Time.Clock.POSIX (POSIXTime)
|
|
import Data.Char
|
|
import Control.Applicative
|
|
import qualified Data.Map as M
|
|
|
|
newtype Duration = Duration { durationSeconds :: Integer }
|
|
deriving (Eq, Ord, Read, Show)
|
|
|
|
durationSince :: UTCTime -> IO Duration
|
|
durationSince pasttime = do
|
|
now <- getCurrentTime
|
|
return $ Duration $ round $ diffUTCTime now pasttime
|
|
|
|
durationToPOSIXTime :: Duration -> POSIXTime
|
|
durationToPOSIXTime = fromIntegral . durationSeconds
|
|
|
|
durationToDays :: Duration -> Integer
|
|
durationToDays d = durationSeconds d `div` dsecs
|
|
|
|
daysToDuration :: Integer -> Duration
|
|
daysToDuration i = Duration $ i * dsecs
|
|
|
|
{- Parses a human-input time duration, of the form "5h", "1m", "5h1m", etc -}
|
|
parseDuration :: String -> Maybe Duration
|
|
parseDuration = Duration <$$> go 0
|
|
where
|
|
go n [] = return n
|
|
go n s = do
|
|
num <- readish s :: Maybe Integer
|
|
case dropWhile isDigit s of
|
|
(c:rest) -> do
|
|
u <- M.lookup c unitmap
|
|
go (n + num * u) rest
|
|
_ -> return $ n + num
|
|
|
|
fromDuration :: Duration -> String
|
|
fromDuration Duration { durationSeconds = d }
|
|
| d == 0 = "0s"
|
|
| otherwise = concatMap showunit $ go [] units d
|
|
where
|
|
showunit (u, n)
|
|
| n > 0 = show n ++ [u]
|
|
| otherwise = ""
|
|
go c [] _ = reverse c
|
|
go c ((u, n):us) v =
|
|
let (q,r) = v `quotRem` n
|
|
in go ((u, q):c) us r
|
|
|
|
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
|
|
|
|
-- Durations cannot be negative.
|
|
instance Arbitrary Duration where
|
|
arbitrary = Duration <$> nonNegative arbitrary
|
|
|
|
prop_duration_roundtrips :: Duration -> Bool
|
|
prop_duration_roundtrips d = parseDuration (fromDuration d) == Just d
|