{- Acknowledgements
~~~~~~~~~~~~~~~~~~~

This module has been largely copied off
    <https://hackage.haskell.org/package/formatting/docs/Formatting-Time.html>
Written by Chris Done
    <https://github.com/chrisdone>
-}

{-# LANGUAGE CPP                 #-}
{-# LANGUAGE ExplicitForAll      #-}
{-# LANGUAGE OverloadedStrings   #-}
{-# LANGUAGE ScopedTypeVariables #-}


{- | Formatters for various time types. This module copies the structure of
@<https://hackage.haskell.org/package/formatting/docs/Formatting-Time.html Formatting.Time>@
from the @<https://hackage.haskell.org/package/formatting formatting>@ package.

Most of the time you'll want to use one of these formatters (all of the
examples below use @"2018-02-14 16:20:45.5 CST"@):

* 'dateTimeF' – full date and time:

    >>> dateTimeF t
    "Wed Feb 14 16:20:45 CST 2018"

* 'hmF' – hours and minutes:

    >>> hmF t
    "16:20"

* 'hmsF' – hours, minutes and seconds:

    >>> hmsF t
    "16:20:45"

* 'dateDashF' – date in ISO 8601 format:

    >>> dateDashF t
    "2018-02-14"

* 'diffF' – either a time period or a point in time, in a convenient for
  humans format:

    >>> diffF False 130    -- time period (130 seconds)
    "2 minutes"
    >>> diffF True 130     -- point in time (130 seconds in the future)
    "in 2 minutes"

Note that two formatters from @Formatting.Time@ are called differently here:

@
pico     -> 'picosecondF'
decimals -> 'subsecondF'
@

-}
module Fmt.Time
(
  -- * Custom
  timeF,

  -- * For 'TimeZone' (and 'ZonedTime' and 'UTCTime')
  tzF,
  tzNameF,
  dateTimeF,

  -- * For 'TimeOfDay' (and 'LocalTime' and 'ZonedTime' and 'UTCTime')
  hmF,
  hmsF,
  hmsLF,
  hmsPLF,
  dayHalfF,
  dayHalfUF,
  hour24F,
  hour12F,
  hour24SF,
  hour12SF,
  minuteF,
  secondF,
  picosecondF,
  subsecondF,

  -- * For 'UTCTime' and 'ZonedTime'
  epochF,

  -- * For 'Day' (and 'LocalTime' and 'ZonedTime' and 'UTCTime')
  dateSlashF,
  dateDashF,
  dateSlashLF,
  yearF,
  yyF,
  centuryF,
  monthNameF,
  monthNameShortF,
  monthF,
  dayOfMonthF,
  dayOfMonthOrdF,
  dayOfMonthSF,
  dayF,
  weekYearF,
  weekYYF,
  weekCenturyF,
  weekF,
  dayOfWeekF,
  dayNameShortF,
  dayNameF,
  weekFromZeroF,
  dayOfWeekFromZeroF,
  weekOfYearMonF,

  -- * Time spans, diffs, 'NominalDiffTime', 'DiffTime', etc.
  diffF,
  yearsF,
  daysF,
  hoursF,
  minutesF,
  secondsF,
)
where


import           Data.List               (find)

#if !MIN_VERSION_base(4,9,0)
import           Data.Monoid             ((<>))
#endif

import           Data.Text               (Text)
import qualified Data.Text               as T
import           Formatting.Buildable    (build)
import           Data.Text.Lazy.Builder  (Builder)
import           Data.Time

#if !MIN_VERSION_time(1,5,0)
import           Data.Time.Locale.Compat
#endif

import           Fmt.Internal.Numeric    (fixedF, ordinalF)

-- $setup
-- >>> let t = read "2018-02-14 16:20:45.5 CST" :: ZonedTime

----------------------------------------------------------------------------
-- Custom
----------------------------------------------------------------------------

-- | Format time with an arbitrary formatting string. Other formatters in
-- this module are implemented using 'timeF'.
timeF :: FormatTime a => Text -> a -> Builder
timeF :: forall a. FormatTime a => Text -> a -> Builder
timeF Text
f = forall p. Buildable p => p -> Builder
build forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Text
T.pack forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall t. FormatTime t => TimeLocale -> String -> t -> String
formatTime TimeLocale
defaultTimeLocale (Text -> String
T.unpack Text
f)

----------------------------------------------------------------------------
-- For 'TimeZone' (and 'ZonedTime' and 'UTCTime')
----------------------------------------------------------------------------

-- | Timezone offset on the format @-HHMM@.
--
-- >>> t
-- 2018-02-14 16:20:45.5 CST
-- >>> tzF t
-- "-0600"
tzF :: FormatTime a => a -> Builder
tzF :: forall a. FormatTime a => a -> Builder
tzF = forall a. FormatTime a => Text -> a -> Builder
timeF Text
"%z"

-- | Timezone name.
--
-- >>> tzNameF t
-- "CST"
tzNameF :: FormatTime a => a -> Builder
tzNameF :: forall a. FormatTime a => a -> Builder
tzNameF = forall a. FormatTime a => Text -> a -> Builder
timeF Text
"%Z"

-- | As 'dateTimeFmt' @locale@ (e.g. @%a %b %e %H:%M:%S %Z %Y@).
--
-- >>> dateTimeF t
-- "Wed Feb 14 16:20:45 CST 2018"
dateTimeF :: FormatTime a => a -> Builder
dateTimeF :: forall a. FormatTime a => a -> Builder
dateTimeF = forall a. FormatTime a => Text -> a -> Builder
timeF Text
"%c"

----------------------------------------------------------------------------
-- For 'TimeOfDay' (and 'LocalTime' and 'ZonedTime' and 'UTCTime')
----------------------------------------------------------------------------

-- | Same as @%H:%M@.
--
-- >>> hmF t
-- "16:20"
hmF :: FormatTime a => a -> Builder
hmF :: forall a. FormatTime a => a -> Builder
hmF = forall a. FormatTime a => Text -> a -> Builder
timeF Text
"%R"

-- | Same as @%H:%M:%S@.
--
-- >>> hmsF t
-- "16:20:45"
hmsF :: FormatTime a => a -> Builder
hmsF :: forall a. FormatTime a => a -> Builder
hmsF = forall a. FormatTime a => Text -> a -> Builder
timeF Text
"%T"

-- | As 'timeFmt' @locale@ (e.g. @%H:%M:%S@).
--
-- >>> hmsLF t
-- "16:20:45"
hmsLF :: FormatTime a => a -> Builder
hmsLF :: forall a. FormatTime a => a -> Builder
hmsLF = forall a. FormatTime a => Text -> a -> Builder
timeF Text
"%X"

-- | As 'time12Fmt' @locale@ (e.g. @%I:%M:%S %p@).
--
-- >>> hmsPLF t
-- "04:20:45 PM"
hmsPLF :: FormatTime a => a -> Builder
hmsPLF :: forall a. FormatTime a => a -> Builder
hmsPLF = forall a. FormatTime a => Text -> a -> Builder
timeF Text
"%r"

-- | Day half from ('amPm' @locale@), converted to lowercase, @am@, @pm@.
--
-- >>> dayHalfF t
-- "pm"
dayHalfF :: FormatTime a => a -> Builder
dayHalfF :: forall a. FormatTime a => a -> Builder
dayHalfF = forall a. FormatTime a => Text -> a -> Builder
timeF Text
"%P"

-- | Day half from ('amPm' @locale@), @AM@, @PM@.
--
-- >>> dayHalfUF t
-- "PM"
dayHalfUF :: FormatTime a => a -> Builder
dayHalfUF :: forall a. FormatTime a => a -> Builder
dayHalfUF = forall a. FormatTime a => Text -> a -> Builder
timeF Text
"%p"

-- | Hour, 24-hour, leading 0 as needed, @00@ - @23@.
--
-- >>> hour24F t
-- "16"
-- >>> hour24F midnight
-- "00"
hour24F :: FormatTime a => a -> Builder
hour24F :: forall a. FormatTime a => a -> Builder
hour24F = forall a. FormatTime a => Text -> a -> Builder
timeF Text
"%H"

-- | Hour, 12-hour, leading 0 as needed, @01@ - @12@.
--
-- >>> hour12F t
-- "04"
-- >>> hour12F midnight
-- "12"
hour12F :: FormatTime a => a -> Builder
hour12F :: forall a. FormatTime a => a -> Builder
hour12F = forall a. FormatTime a => Text -> a -> Builder
timeF Text
"%I"

-- | Hour, 24-hour, leading space as needed, @ 0@ - @23@.
--
-- >>> hour24SF t
-- "16"
-- >>> hour24SF midnight
-- " 0"
hour24SF :: FormatTime a => a -> Builder
hour24SF :: forall a. FormatTime a => a -> Builder
hour24SF = forall a. FormatTime a => Text -> a -> Builder
timeF Text
"%k"

-- | Hour, 12-hour, leading space as needed, @ 1@ - @12@.
--
-- >>> hour12SF t
-- " 4"
-- >>> hour12SF midnight
-- "12"
hour12SF :: FormatTime a => a -> Builder
hour12SF :: forall a. FormatTime a => a -> Builder
hour12SF = forall a. FormatTime a => Text -> a -> Builder
timeF Text
"%l"

-- | Minute, @00@ - @59@.
--
-- >>> minuteF t
-- "20"
minuteF :: FormatTime a => a -> Builder
minuteF :: forall a. FormatTime a => a -> Builder
minuteF = forall a. FormatTime a => Text -> a -> Builder
timeF Text
"%M"

-- | Second, without decimal part, @00@ - @60@.
--
-- >>> secondF t
-- "45"
secondF :: FormatTime a => a -> Builder
secondF :: forall a. FormatTime a => a -> Builder
secondF = forall a. FormatTime a => Text -> a -> Builder
timeF Text
"%S"

-- | Picosecond, including trailing zeros, @000000000000@ - @999999999999@.
--
-- >>> picosecondF t
-- "500000000000"
picosecondF :: FormatTime a => a -> Builder
picosecondF :: forall a. FormatTime a => a -> Builder
picosecondF = forall a. FormatTime a => Text -> a -> Builder
timeF Text
"%q"

-- | Decimal point of the second. Up to 12 digits, without trailing zeros.
-- For a whole number of seconds, this produces an empty string.
--
-- >>> subsecondF t
-- ".5"
subsecondF :: FormatTime a => a -> Builder
subsecondF :: forall a. FormatTime a => a -> Builder
subsecondF = forall a. FormatTime a => Text -> a -> Builder
timeF Text
"%Q"

----------------------------------------------------------------------------
-- For 'UTCTime' and 'ZonedTime'
----------------------------------------------------------------------------

-- | Number of whole seconds since the Unix epoch. For times before the Unix
-- epoch, this is a negative number. Note that in @%s.%q@ and @%s%Q@ the
-- decimals are positive, not negative. For example, 0.9 seconds before the
-- Unix epoch is formatted as @-1.1@ with @%s%Q@.
--
-- >>> epochF t
-- "1518646845"
epochF :: FormatTime a => a -> Builder
epochF :: forall a. FormatTime a => a -> Builder
epochF = forall a. FormatTime a => Text -> a -> Builder
timeF Text
"%s"

----------------------------------------------------------------------------
-- For 'Day' (and 'LocalTime' and 'ZonedTime' and 'UTCTime')
----------------------------------------------------------------------------

-- | Same as @%m\/%d\/%y@.
--
-- >>> dateSlashF t
-- "02/14/18"
dateSlashF :: FormatTime a => a -> Builder
dateSlashF :: forall a. FormatTime a => a -> Builder
dateSlashF = forall a. FormatTime a => Text -> a -> Builder
timeF Text
"%D"

-- | Same as @%Y-%m-%d@.
--
-- >>> dateDashF t
-- "2018-02-14"
dateDashF :: FormatTime a => a -> Builder
dateDashF :: forall a. FormatTime a => a -> Builder
dateDashF = forall a. FormatTime a => Text -> a -> Builder
timeF Text
"%F"

-- | As 'dateFmt' @locale@ (e.g. @%m\/%d\/%y@).
--
-- >>> dateSlashLF t
-- "02/14/18"
dateSlashLF :: FormatTime a => a -> Builder
dateSlashLF :: forall a. FormatTime a => a -> Builder
dateSlashLF = forall a. FormatTime a => Text -> a -> Builder
timeF Text
"%x"

-- | Year.
--
-- >>> yearF t
-- "2018"
yearF :: FormatTime a => a -> Builder
yearF :: forall a. FormatTime a => a -> Builder
yearF = forall a. FormatTime a => Text -> a -> Builder
timeF Text
"%Y"

-- | Last two digits of year, @00@ - @99@.
--
-- >>> yyF t
-- "18"
yyF :: FormatTime a => a -> Builder
yyF :: forall a. FormatTime a => a -> Builder
yyF = forall a. FormatTime a => Text -> a -> Builder
timeF Text
"%y"

-- | Century (being the first two digits of the year), @00@ - @99@.
--
-- >>> centuryF t
-- "20"
centuryF :: FormatTime a => a -> Builder
centuryF :: forall a. FormatTime a => a -> Builder
centuryF = forall a. FormatTime a => Text -> a -> Builder
timeF Text
"%C"

-- | Month name, long form ('fst' from 'months' @locale@), @January@ -
-- @December@.
--
-- >>> monthNameF t
-- "February"
monthNameF :: FormatTime a => a -> Builder
monthNameF :: forall a. FormatTime a => a -> Builder
monthNameF = forall a. FormatTime a => Text -> a -> Builder
timeF Text
"%B"

-- | Month name, short form ('snd' from 'months' @locale@), @Jan@ - @Dec@.
--
-- >>> monthNameShortF t
-- "Feb"
monthNameShortF :: FormatTime a => a -> Builder
monthNameShortF :: forall a. FormatTime a => a -> Builder
monthNameShortF = forall a. FormatTime a => Text -> a -> Builder
timeF Text
"%b"

-- | Month of year, leading 0 as needed, @01@ - @12@.
--
-- >>> monthF t
-- "02"
monthF :: FormatTime a => a -> Builder
monthF :: forall a. FormatTime a => a -> Builder
monthF = forall a. FormatTime a => Text -> a -> Builder
timeF Text
"%m"

-- | Day of month, leading 0 as needed, @01@ - @31@.
--
-- >>> dayOfMonthF t
-- "14"
dayOfMonthF :: FormatTime a => a -> Builder
dayOfMonthF :: forall a. FormatTime a => a -> Builder
dayOfMonthF = forall a. FormatTime a => Text -> a -> Builder
timeF Text
"%d"

-- | Day of month, @1st@, @2nd@, @25th@, etc.
--
-- >>> dayOfMonthOrdF t
-- "14th"
dayOfMonthOrdF :: FormatTime a => a -> Builder
dayOfMonthOrdF :: forall a. FormatTime a => a -> Builder
dayOfMonthOrdF = forall a. (Buildable a, Integral a) => a -> Builder
ordinalF forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. FormatTime a => a -> Int
timeToInt
  where
    timeToInt :: FormatTime a => a -> Int
    timeToInt :: forall a. FormatTime a => a -> Int
timeToInt = forall a. Read a => String -> a
read forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall t. FormatTime t => TimeLocale -> String -> t -> String
formatTime TimeLocale
defaultTimeLocale String
"%d"

-- | Day of month, leading space as needed, @ 1@ - @31@.
dayOfMonthSF :: FormatTime a => a -> Builder
dayOfMonthSF :: forall a. FormatTime a => a -> Builder
dayOfMonthSF = forall a. FormatTime a => Text -> a -> Builder
timeF Text
"%e"

-- | Day of year for Ordinal Date format, @001@ - @366@.
--
-- >>> dayF t
-- "045"
dayF :: FormatTime a => a -> Builder
dayF :: forall a. FormatTime a => a -> Builder
dayF = forall a. FormatTime a => Text -> a -> Builder
timeF Text
"%j"

-- | Year for Week Date format e.g. @2013@.
--
-- >>> weekYearF t
-- "2018"
weekYearF :: FormatTime a => a -> Builder
weekYearF :: forall a. FormatTime a => a -> Builder
weekYearF = forall a. FormatTime a => Text -> a -> Builder
timeF Text
"%G"

-- | Last two digits of year for Week Date format, @00@ - @99@.
--
-- >>> weekYYF t
-- "18"
weekYYF :: FormatTime a => a -> Builder
weekYYF :: forall a. FormatTime a => a -> Builder
weekYYF = forall a. FormatTime a => Text -> a -> Builder
timeF Text
"%g"

-- | Century (first two digits of year) for Week Date format, @00@ - @99@.
--
-- >>> weekCenturyF t
-- "20"
weekCenturyF :: FormatTime a => a -> Builder
weekCenturyF :: forall a. FormatTime a => a -> Builder
weekCenturyF = forall a. FormatTime a => Text -> a -> Builder
timeF Text
"%f"

-- | Week for Week Date format, @01@ - @53@.
--
-- >>> weekF t
-- "07"
weekF :: FormatTime a => a -> Builder
weekF :: forall a. FormatTime a => a -> Builder
weekF = forall a. FormatTime a => Text -> a -> Builder
timeF Text
"%V"

-- | Day for Week Date format, @1@ - @7@.
--
-- >>> dayOfWeekF t
-- "3"
dayOfWeekF :: FormatTime a => a -> Builder
dayOfWeekF :: forall a. FormatTime a => a -> Builder
dayOfWeekF = forall a. FormatTime a => Text -> a -> Builder
timeF Text
"%u"

-- | Day of week, short form ('snd' from 'wDays' @locale@), @Sun@ - @Sat@.
--
-- >>> dayNameShortF t
-- "Wed"
dayNameShortF :: FormatTime a => a -> Builder
dayNameShortF :: forall a. FormatTime a => a -> Builder
dayNameShortF = forall a. FormatTime a => Text -> a -> Builder
timeF Text
"%a"

-- | Day of week, long form ('fst' from 'wDays' @locale@), @Sunday@ -
-- @Saturday@.
--
-- >>> dayNameF t
-- "Wednesday"
dayNameF :: FormatTime a => a -> Builder
dayNameF :: forall a. FormatTime a => a -> Builder
dayNameF = forall a. FormatTime a => Text -> a -> Builder
timeF Text
"%A"

-- | Week number of year, where weeks start on Sunday (as
-- 'sundayStartWeek'), @00@ - @53@.
--
-- >>> weekFromZeroF t
-- "06"
weekFromZeroF :: FormatTime a => a -> Builder
weekFromZeroF :: forall a. FormatTime a => a -> Builder
weekFromZeroF = forall a. FormatTime a => Text -> a -> Builder
timeF Text
"%U"

-- | Day of week number, @0@ (= Sunday) - @6@ (= Saturday).
--
-- >>> dayOfWeekFromZeroF t
-- "3"
dayOfWeekFromZeroF :: FormatTime a => a -> Builder
dayOfWeekFromZeroF :: forall a. FormatTime a => a -> Builder
dayOfWeekFromZeroF = forall a. FormatTime a => Text -> a -> Builder
timeF Text
"%w"

-- | Week number of year, where weeks start on Monday (as
-- 'mondayStartWeek'), @00@ - @53@.
--
-- >>> weekOfYearMonF t
-- "07"
weekOfYearMonF :: FormatTime a => a -> Builder
weekOfYearMonF :: forall a. FormatTime a => a -> Builder
weekOfYearMonF = forall a. FormatTime a => Text -> a -> Builder
timeF Text
"%W"

----------------------------------------------------------------------------
-- Time spans, diffs, 'NominalDiffTime', 'DiffTime', etc.
----------------------------------------------------------------------------

-- | Display a time span as one time relative to another. Input is assumed to
-- be seconds. Typical inputs are 'NominalDiffTime' and 'DiffTime'.
--
-- >>> diffF False 100
-- "a minute"
-- >>> diffF True 100
-- "in a minute"
diffF :: forall n . RealFrac n
      => Bool     -- ^ Whether to display the @in/ago@ prefix or not
      -> n        -- ^ Example: @3 seconds ago@, @in 2 days@
      -> Builder
diffF :: forall n. RealFrac n => Bool -> n -> Builder
diffF Bool
fix = RealFrac n => n -> Builder
diffed
  where
    diffed :: RealFrac n => n -> Builder
    diffed :: RealFrac n => n -> Builder
diffed n
ts =
      case forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Maybe a
find (\(n
s,Int -> Builder
_,n
_) -> forall a. Num a => a -> a
abs n
ts forall a. Ord a => a -> a -> Bool
>= n
s) (forall a. [a] -> [a]
reverse RealFrac n => [(n, Int -> Builder, n)]
ranges) of
        Maybe (n, Int -> Builder, n)
Nothing           -> Builder
"unknown"
        Just (n
_, Int -> Builder
f, n
base) -> Builder
prefix forall a. Semigroup a => a -> a -> a
<> Int -> Builder
f (RealFrac n => n -> n -> Int
toInt n
ts n
base) forall a. Semigroup a => a -> a -> a
<> Builder
suffix
      where
        prefix :: Builder
prefix = if Bool
fix Bool -> Bool -> Bool
&& n
ts forall a. Ord a => a -> a -> Bool
> n
0 then Builder
"in "  else Builder
""
        suffix :: Builder
suffix = if Bool
fix Bool -> Bool -> Bool
&& n
ts forall a. Ord a => a -> a -> Bool
< n
0 then Builder
" ago" else Builder
""

    toInt :: RealFrac n => n -> n -> Int
    toInt :: RealFrac n => n -> n -> Int
toInt n
ts n
base = forall a. Num a => a -> a
abs (forall a b. (RealFrac a, Integral b) => a -> b
round (n
ts forall a. Fractional a => a -> a -> a
/ n
base))

    intF :: Builder -> Int -> Builder
    intF :: Builder -> Int -> Builder
intF Builder
t Int
n = forall p. Buildable p => p -> Builder
build Int
n forall a. Semigroup a => a -> a -> a
<> Builder
t

    ranges :: RealFrac n => [(n, Int -> Builder, n)]
    ranges :: RealFrac n => [(n, Int -> Builder, n)]
ranges =
      [ (n
0           , Builder -> Int -> Builder
intF  Builder
" milliseconds" , n
0.001 )
      , (n
1           , Builder -> Int -> Builder
intF  Builder
" seconds"      , n
1     )
      , (n
minute      , forall a b. a -> b -> a
const Builder
"a minute"      , n
0     )
      , (n
minute forall a. Num a => a -> a -> a
* n
2  , Builder -> Int -> Builder
intF  Builder
" minutes"      , n
minute)
      , (n
minute forall a. Num a => a -> a -> a
* n
30 , forall a b. a -> b -> a
const Builder
"half an hour"  , n
0     )
      , (n
minute forall a. Num a => a -> a -> a
* n
31 , Builder -> Int -> Builder
intF  Builder
" minutes"      , n
minute)
      , (n
hour        , forall a b. a -> b -> a
const Builder
"an hour"       , n
0     )
      , (n
hour forall a. Num a => a -> a -> a
* n
2    , Builder -> Int -> Builder
intF  Builder
" hours"        , n
hour  )
      , (n
hour forall a. Num a => a -> a -> a
* n
3    , forall a b. a -> b -> a
const Builder
"a few hours"   , n
0     )
      , (n
hour forall a. Num a => a -> a -> a
* n
4    , Builder -> Int -> Builder
intF  Builder
" hours"        , n
hour  )
      , (n
day         , forall a b. a -> b -> a
const Builder
"a day"         , n
0     )
      , (n
day forall a. Num a => a -> a -> a
* n
2     , Builder -> Int -> Builder
intF  Builder
" days"         , n
day   )
      , (n
week        , forall a b. a -> b -> a
const Builder
"a week"        , n
0     )
      , (n
week forall a. Num a => a -> a -> a
* n
2    , Builder -> Int -> Builder
intF  Builder
" weeks"        , n
week  )
      , (n
month       , forall a b. a -> b -> a
const Builder
"a month"       , n
0     )
      , (n
month forall a. Num a => a -> a -> a
* n
2   , Builder -> Int -> Builder
intF  Builder
" months"       , n
month )
      , (n
year        , forall a b. a -> b -> a
const Builder
"a year"        , n
0     )
      , (n
year forall a. Num a => a -> a -> a
* n
2    , Builder -> Int -> Builder
intF  Builder
" years"        , n
year  )
      ]
      where year :: n
year   = n
month  forall a. Num a => a -> a -> a
* n
12
            month :: n
month  = n
day    forall a. Num a => a -> a -> a
* n
30
            week :: n
week   = n
day    forall a. Num a => a -> a -> a
* n
7
            day :: n
day    = n
hour   forall a. Num a => a -> a -> a
* n
24
            hour :: n
hour   = n
minute forall a. Num a => a -> a -> a
* n
60
            minute :: n
minute = n
60

-- | Display the absolute value time span in years.
--
-- >>> epochF t    -- time passed since Jan 1, 1970
-- "1518646845"
-- >>> yearsF 3 1518646845
-- "48.156"
yearsF :: RealFrac n
       => Int -- ^ Decimal places.
       -> n
       -> Builder
yearsF :: forall n. RealFrac n => Int -> n -> Builder
yearsF Int
n = forall a. Real a => Int -> a -> Builder
fixedF Int
n forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Num a => a -> a
abs forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall {a}. Fractional a => a -> a
count
  where count :: a -> a
count a
x = a
x forall a. Fractional a => a -> a -> a
/ a
365 forall a. Fractional a => a -> a -> a
/ a
24 forall a. Fractional a => a -> a -> a
/ a
60 forall a. Fractional a => a -> a -> a
/ a
60

-- | Display the absolute value time span in days.
--
-- >>> daysF 3 1518646845
-- "17576.931"
daysF :: RealFrac n
      => Int -- ^ Decimal places.
      -> n
      -> Builder
daysF :: forall n. RealFrac n => Int -> n -> Builder
daysF Int
n = forall a. Real a => Int -> a -> Builder
fixedF Int
n forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Num a => a -> a
abs forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall {a}. Fractional a => a -> a
count
  where count :: a -> a
count a
x = a
x forall a. Fractional a => a -> a -> a
/ a
24 forall a. Fractional a => a -> a -> a
/ a
60 forall a. Fractional a => a -> a -> a
/ a
60

-- | Display the absolute value time span in hours.
--
-- >>> hoursF 3 3600
-- "1.000"
hoursF :: RealFrac n
       => Int -- ^ Decimal places.
       -> n
       -> Builder
hoursF :: forall n. RealFrac n => Int -> n -> Builder
hoursF Int
n = forall a. Real a => Int -> a -> Builder
fixedF Int
n forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Num a => a -> a
abs forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall {a}. Fractional a => a -> a
count
  where count :: a -> a
count a
x = a
x forall a. Fractional a => a -> a -> a
/ a
60 forall a. Fractional a => a -> a -> a
/ a
60

-- | Display the absolute value time span in minutes.
--
-- >>> minutesF 3 150
-- "2.500"
minutesF :: RealFrac n
         => Int -- ^ Decimal places.
         -> n
         -> Builder
minutesF :: forall n. RealFrac n => Int -> n -> Builder
minutesF Int
n = forall a. Real a => Int -> a -> Builder
fixedF Int
n forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Num a => a -> a
abs forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall {a}. Fractional a => a -> a
count
  where count :: a -> a
count a
x = a
x forall a. Fractional a => a -> a -> a
/ a
60

-- | Display the absolute value time span in seconds.
--
-- >>> secondsF 3 100
-- "100.000"
secondsF :: RealFrac n
         => Int -- ^ Decimal places.
         -> n
         -> Builder
secondsF :: forall n. RealFrac n => Int -> n -> Builder
secondsF Int
n = forall a. Real a => Int -> a -> Builder
fixedF Int
n forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Num a => a -> a
abs