{-# LANGUAGE CPP #-}
module Test.Sandwich.Formatters.Common.Util (
formatNominalDiffTime
) where
import Data.Fixed
import Data.Time.Clock
import Text.Printf
formatNominalDiffTime :: NominalDiffTime -> String
formatNominalDiffTime :: NominalDiffTime -> String
formatNominalDiffTime NominalDiffTime
diff | NominalDiffTime
diff NominalDiffTime -> NominalDiffTime -> Bool
forall a. Ord a => a -> a -> Bool
< NominalDiffTime
ps = (Fixed E12 -> String
roundFixed ((NominalDiffTime -> Fixed E12
nominalDiffTimeToSeconds NominalDiffTime
diff) Fixed E12 -> Fixed E12 -> Fixed E12
forall a. Num a => a -> a -> a
* Fixed E12
10Fixed E12 -> Integer -> Fixed E12
forall a b. (Num a, Integral b) => a -> b -> a
^Integer
15)) String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
" ps"
formatNominalDiffTime NominalDiffTime
diff | NominalDiffTime
diff NominalDiffTime -> NominalDiffTime -> Bool
forall a. Ord a => a -> a -> Bool
< NominalDiffTime
ns = (Fixed E12 -> String
roundFixed ((NominalDiffTime -> Fixed E12
nominalDiffTimeToSeconds NominalDiffTime
diff) Fixed E12 -> Fixed E12 -> Fixed E12
forall a. Num a => a -> a -> a
* Fixed E12
10Fixed E12 -> Integer -> Fixed E12
forall a b. (Num a, Integral b) => a -> b -> a
^Integer
12)) String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
" ns"
formatNominalDiffTime NominalDiffTime
diff | NominalDiffTime
diff NominalDiffTime -> NominalDiffTime -> Bool
forall a. Ord a => a -> a -> Bool
< NominalDiffTime
us = (Fixed E12 -> String
roundFixed ((NominalDiffTime -> Fixed E12
nominalDiffTimeToSeconds NominalDiffTime
diff) Fixed E12 -> Fixed E12 -> Fixed E12
forall a. Num a => a -> a -> a
* Fixed E12
10Fixed E12 -> Integer -> Fixed E12
forall a b. (Num a, Integral b) => a -> b -> a
^Integer
9)) String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
" ns"
formatNominalDiffTime NominalDiffTime
diff | NominalDiffTime
diff NominalDiffTime -> NominalDiffTime -> Bool
forall a. Ord a => a -> a -> Bool
< NominalDiffTime
ms = (Fixed E12 -> String
roundFixed ((NominalDiffTime -> Fixed E12
nominalDiffTimeToSeconds NominalDiffTime
diff) Fixed E12 -> Fixed E12 -> Fixed E12
forall a. Num a => a -> a -> a
* Fixed E12
10Fixed E12 -> Integer -> Fixed E12
forall a b. (Num a, Integral b) => a -> b -> a
^Integer
6)) String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
" us"
formatNominalDiffTime NominalDiffTime
diff | NominalDiffTime
diff NominalDiffTime -> NominalDiffTime -> Bool
forall a. Ord a => a -> a -> Bool
< NominalDiffTime
second = (Fixed E12 -> String
roundFixed ((NominalDiffTime -> Fixed E12
nominalDiffTimeToSeconds NominalDiffTime
diff) Fixed E12 -> Fixed E12 -> Fixed E12
forall a. Num a => a -> a -> a
* Fixed E12
10Fixed E12 -> Integer -> Fixed E12
forall a b. (Num a, Integral b) => a -> b -> a
^Integer
3)) String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
" ms"
formatNominalDiffTime NominalDiffTime
diff = (Fixed E12 -> String
roundFixed (NominalDiffTime -> Fixed E12
nominalDiffTimeToSeconds NominalDiffTime
diff)) String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
" s"
second :: NominalDiffTime
second = Fixed E12 -> NominalDiffTime
secondsToNominalDiffTime Fixed E12
1
ms :: NominalDiffTime
ms = Fixed E12 -> NominalDiffTime
secondsToNominalDiffTime Fixed E12
0.001
us :: NominalDiffTime
us = Fixed E12 -> NominalDiffTime
secondsToNominalDiffTime Fixed E12
0.000001
ns :: NominalDiffTime
ns = Fixed E12 -> NominalDiffTime
secondsToNominalDiffTime Fixed E12
0.000000001
ps :: NominalDiffTime
ps = Fixed E12 -> NominalDiffTime
secondsToNominalDiffTime Fixed E12
0.000000000001
roundFixed :: Fixed E12 -> String
roundFixed :: Fixed E12 -> String
roundFixed Fixed E12
f = String -> Double -> String
forall r. PrintfType r => String -> r
printf String
"%.1f" ((Fixed E12 -> Double
forall a b. (Real a, Fractional b) => a -> b
realToFrac Fixed E12
f) :: Double)
#if !MIN_VERSION_time(1,9,1)
secondsToNominalDiffTime :: Pico -> NominalDiffTime
secondsToNominalDiffTime = realToFrac
nominalDiffTimeToSeconds :: NominalDiffTime -> Pico
nominalDiffTimeToSeconds = realToFrac
#endif