module Wumpus.Core.Utils.Common
(
dtruncFmt
, truncateDouble
, roundup
, psTimeStamp
) where
import qualified Wumpus.Core.Utils.FormatCombinators as Fmt
import Data.Time
dtruncFmt :: Double -> Fmt.Doc
dtruncFmt = Fmt.text . truncateDouble
truncateDouble :: Double -> String
truncateDouble d | abs d < 0.0001 = "0.0"
| d < 0.0 = '-' : show (abs tx)
| otherwise = show tx
where
tx :: Double
tx = (realToFrac (roundi (d*1000000.0))) / 1000000.0
roundi :: RealFrac a => a -> Integer
roundi = round
roundup :: Double -> String
roundup = show . ceilingi
ceilingi :: RealFrac a => a -> Integer
ceilingi = ceiling
psTimeStamp :: ZonedTime -> ShowS
psTimeStamp zt = localTimeS . showChar ' ' . localDayS
where
local_tim = zonedTimeToLocalTime zt
localTimeS = timeOfDay $ localTimeOfDay $ local_tim
localDayS = showString $ showGregorian $ localDay local_tim
timeOfDay :: TimeOfDay -> ShowS
timeOfDay t =
fn todHour . showChar ':' . fn todMin . showChar ':' . fn (floori . todSec)
where
fn f = pad2 (f t)
pad2 :: Int -> ShowS
pad2 i | i < 10 = ('0':) . shows i
| otherwise = shows i
floori :: RealFrac a => a -> Int
floori = floor