{-# LANGUAGE PatternGuards #-}
{-# LANGUAGE OverloadedStrings #-}
{-# OPTIONS_GHC -fno-warn-type-defaults #-}

-- | Formatters for high-res, real-time and timer clock values from "System.Clock".

module Formatting.Clock (timeSpecs) where

import Data.Text.Lazy.Builder
import Formatting
import Formatting.Internal
import System.Clock

fmt :: Integer -> Builder
fmt :: Integer -> Builder
fmt Integer
diff
  | Just Double
i <- Integer -> Maybe Double
scale ((Integer
10 forall a b. (Num a, Integral b) => a -> b -> a
^ Integer
9) forall a. Num a => a -> a -> a
* Integer
60 forall a. Num a => a -> a -> a
* Integer
60 forall a. Num a => a -> a -> a
* Integer
24) = forall a. Format Builder a -> a
bprint (forall a r. Real a => Int -> Format r (a -> r)
fixed Int
2 forall r a r'. Format r a -> Format r' r -> Format r' a
% Format Builder Builder
" d") Double
i
  | Just Double
i <- Integer -> Maybe Double
scale ((Integer
10 forall a b. (Num a, Integral b) => a -> b -> a
^ Integer
9) forall a. Num a => a -> a -> a
* Integer
60 forall a. Num a => a -> a -> a
* Integer
60) = forall a. Format Builder a -> a
bprint (forall a r. Real a => Int -> Format r (a -> r)
fixed Int
2 forall r a r'. Format r a -> Format r' r -> Format r' a
% Format Builder Builder
" h") Double
i
  | Just Double
i <- Integer -> Maybe Double
scale ((Integer
10 forall a b. (Num a, Integral b) => a -> b -> a
^ Integer
9) forall a. Num a => a -> a -> a
* Integer
60) = forall a. Format Builder a -> a
bprint (forall a r. Real a => Int -> Format r (a -> r)
fixed Int
2 forall r a r'. Format r a -> Format r' r -> Format r' a
% Format Builder Builder
" m") Double
i
  | Just Double
i <- Integer -> Maybe Double
scale (Integer
10 forall a b. (Num a, Integral b) => a -> b -> a
^ Integer
9) = forall a. Format Builder a -> a
bprint (forall a r. Real a => Int -> Format r (a -> r)
fixed Int
2 forall r a r'. Format r a -> Format r' r -> Format r' a
% Format Builder Builder
" s") Double
i
  | Just Double
i <- Integer -> Maybe Double
scale (Integer
10 forall a b. (Num a, Integral b) => a -> b -> a
^ Integer
6) = forall a. Format Builder a -> a
bprint (forall a r. Real a => Int -> Format r (a -> r)
fixed Int
2 forall r a r'. Format r a -> Format r' r -> Format r' a
% Format Builder Builder
" ms") Double
i
  | Just Double
i <- Integer -> Maybe Double
scale (Integer
10 forall a b. (Num a, Integral b) => a -> b -> a
^ Integer
3) = forall a. Format Builder a -> a
bprint (forall a r. Real a => Int -> Format r (a -> r)
fixed Int
2 forall r a r'. Format r a -> Format r' r -> Format r' a
% Format Builder Builder
" us") Double
i
  | Bool
otherwise = forall a. Format Builder a -> a
bprint (forall a r. Integral a => Format r (a -> r)
int forall r a r'. Format r a -> Format r' r -> Format r' a
% Format Builder Builder
" ns") Integer
diff
  where
    scale :: Integer -> Maybe Double
    scale :: Integer -> Maybe Double
scale Integer
i =
      if Integer
diff forall a. Ord a => a -> a -> Bool
>= Integer
i
        then forall a. a -> Maybe a
Just (forall a b. (Integral a, Num b) => a -> b
fromIntegral Integer
diff forall a. Fractional a => a -> a -> a
/ forall a b. (Integral a, Num b) => a -> b
fromIntegral Integer
i)
        else forall a. Maybe a
Nothing

-- | Same as @durationNS@ but works on `TimeSpec` from the clock package.
timeSpecs :: Format r (TimeSpec -> TimeSpec -> r)
timeSpecs :: forall r. Format r (TimeSpec -> TimeSpec -> r)
timeSpecs = forall r a. ((Builder -> r) -> a) -> Format r a
Format (\Builder -> r
g TimeSpec
x TimeSpec
y -> Builder -> r
g (TimeSpec -> TimeSpec -> Builder
fmt0 TimeSpec
x TimeSpec
y))
  where
    fmt0 :: TimeSpec -> TimeSpec -> Builder
fmt0 (TimeSpec Int64
s1 Int64
n1) (TimeSpec Int64
s2 Int64
n2) = Integer -> Builder
fmt Integer
diff
      where
        diff :: Integer
        diff :: Integer
diff = Integer
a2 forall a. Num a => a -> a -> a
- Integer
a1
        a1 :: Integer
a1 = (forall a b. (Integral a, Num b) => a -> b
fromIntegral Int64
s1 forall a. Num a => a -> a -> a
* Integer
10 forall a b. (Num a, Integral b) => a -> b -> a
^ Integer
9) forall a. Num a => a -> a -> a
+ forall a b. (Integral a, Num b) => a -> b
fromIntegral Int64
n1
        a2 :: Integer
a2 = (forall a b. (Integral a, Num b) => a -> b
fromIntegral Int64
s2 forall a. Num a => a -> a -> a
* Integer
10 forall a b. (Num a, Integral b) => a -> b -> a
^ Integer
9) forall a. Num a => a -> a -> a
+ forall a b. (Integral a, Num b) => a -> b
fromIntegral Int64
n2