duration

A tiny compile-time time utility library inspired by zeit/ms

https://github.com/ryota-ka/duration#readme

Version on this page:0.1.0.0
LTS Haskell 22.39:0.2.0.0
Stackage Nightly 2024-10-31:0.2.0.0
Latest on Hackage:0.2.0.0

See all snapshots duration appears in

BSD-3-Clause licensed by Ryota Kameoka
Maintained by [email protected]
This version can be pinned in stack with:duration-0.1.0.0@sha256:370cd2adbdcc307ca2a4696b301d34ff35bdf17c12e025feefd9b20bc30a9d8e,1869

Module documentation for 0.1.0.0

duration

A tiny compile-time time utility library, inspired by zeit/ms.

Examples

{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE TypeApplications #-}

module Main where

import Data.Time.Clock (DiffTime, NominalDiffTime)
import Data.Time.Clock.Duration (t, s, ms, µs, ns, ps)

main :: IO ()
main = do
    print @DiffTime [t| 1 day |] -- 86400s
    print @DiffTime [t| 2h |]    -- 7200s
    print @DiffTime [t| -1m |]   -- -60s

    print @NominalDiffTime [s| 1 |]  -- 1s
    print @NominalDiffTime [ms| 1 |] -- 0.001s
    print @NominalDiffTime [µs| 1 |] -- 0.000001s
    print @NominalDiffTime [ns| 1 |] -- 0.000000001s
    print @NominalDiffTime [ps| 1 |] -- 0.000000000001s

    print @Int      [s| 2 days |]  -- 172800
    print @Integer  [ms| 5s |]     -- 5000
    print @Double   [µs| 2min |]   -- 1.2e8
    print @Float    [ns| -1 sec |] -- -1.0e9
    print @Rational [ps| 10ms |]   -- 10000000000 % 1