module Extra.Time
( formatDebianDate
, myTimeDiffToString
) where
import Control.Exception
import Data.List
import Data.Time
import System.Locale
import System.Time
import Text.Printf
formatDebianDate t =
prefix ++ seconds ++ suffix
where prefix = formatTime defaultTimeLocale prefixFormat t
seconds = take 2 $ formatTime defaultTimeLocale secondsFormat t
suffix = formatTime defaultTimeLocale suffixFormat t
prefixFormat = "%a, %d %b %Y %H:%M:"
secondsFormat = "%S"
suffixFormat = " %Z"
format = "%a, %d %b %Y %H:%M:%S %Z"
_test = assert (format == prefixFormat ++ secondsFormat ++ suffixFormat)
_test=
do tz <- getCurrentTimeZone
let ut = localTimeToUTC tz testtime
return $ teststring == formatDebianDate ut
where testtime = LocalTime {localDay=fromGregorian testyear testmonth testday,
localTimeOfDay=TimeOfDay{todHour=testhour,todMin=testminute,todSec=testsecond}}
testyear = 2006
testmonth = 12
testday = 19
testhour = 12
testminute = 19
testsecond = 15.29
teststring = "Tue, 19 Dec 2006 17:19:15 UTC"
myTimeDiffToString diff =
do
case () of
_ | isPrefixOf "00:00:0" s -> drop 7 s ++ printf ".%03d" ms ++ " s."
_ | isPrefixOf "00:00:" s -> drop 6 s ++ printf ".%03d" ms ++ " s."
_ | isPrefixOf "00:" s -> drop 3 s
_ -> s
where
s = formatTimeDiff defaultTimeLocale "%T" diff
ms = ps2ms ps
ps2ms ps = quot (ps + 500000000) 1000000000
ps = tdPicosec diff