non-negative-time-diff
type safe diffUTCTime
Both arguments of @diffUTCTime@ function from @time@ package have the
same type. It is easy to mix them.
> f = do
> started <- getCurrentTime
> threadDelay 10_000_000
> ended <- getCurrentTime
> pure $ started `diffUTCTime` ended
This package provides a stricter @diffUTCTime@ that significantly
reduces possibility of mixing its arguments by an accident.
> import Data.Time.Clock.NonNegativeTimeDiff
> f = do
> started <- getCurrentTime
> threadDelay 10_000_000
> ended <- getTimeAfter started
> pure $ ended `diffUTCTime` started
== STM use case
#stm-use-case#
The STM package is shipped without a function to get current time. Let’s
consider a situtation like this:
> data Ctx
> = Ctx { m :: Map Int UTCTime
> , s :: TVar NominalDiffTime
> , q :: TQueue Int
> }
>
> f (c :: Ctx) = do
> now <- getCurrentTime
> atomically $ do
> i <- readTQueue q
> lookup i c.m >>= \case
> Nothing -> pure ()
> Just t -> modifyTVar' c.s (+ diffUTCTime now t)
@now@ might be less than @t@ because the queue might be empty by the
time @f@ is invoked. The package API can correct the above snippet as
follows:
> data Ctx
> = Ctx { m :: Map Int UtcBox
> , s :: TVar NominalDiffTime
> , q :: TQueue Int
> }
>
> f (c :: Ctx) = do
> atomically $ do
> i <- readTQueue q
> lookup i c.m >>= \case
> Nothing -> pure ()
> Just t ->
> doAfter tb \t -> do
> now <- getTimeAfter t
> modifyTVar' c.s (+ diffUTCTime now t)
== File access time
#file-access-time#
Another popular usecase where original @diffUTCTime@ might be misused.
> isFileOlderThan :: FilePath -> NominalDiffTime -> IO Bool
> isFileOlderThan fp maxAge = do
> now <- getCurrentTime
> mt <- getModificationTime fp
> when (mt `diffUTCTime` now > maxAge) $ do
> removeFile fp
File age is always negative in the above example - this eventually would
cause a space leak on disk.
Corrected version:
> isFileOlderThan :: FilePath -> NominalDiffTime -> IO Bool
> isFileOlderThan fp maxAge =
> getModificationTime fp >>= (`doAfter` \mt -> do
> now <- getTimeAfter mt
> when (now `diffUTCTime` mt > maxAge) $ do
> removeFile fp)
== Requirements
#requirements#
Unboxing @UtcBox@ values requires a GHC
<https://hackage.haskell.org/package/ghc-typelits-natnormalise natnormalise plugin>:
> {-# GHC_OPTIONS -fplugin GHC.TypeLits.Normalise #-}
=== Static linking
#static-linking#
In case of static linking define macro @STATIC@ to disable natnormalise
GHC plugin that is not available in such setup. UtcBox version for the
static build is less strict, because it does not have existential
variable.
> cabal build --ghc-option=-optP=-DSTATIC
| version | uploaded | uploader | changes | |
|---|---|---|---|---|
| 0.0.2 | 2026-03-14 12:57 UTC | DaniilIaitskov | +35/−6 lines, 3 filesPVP ok | |
| 0.0.1 | 2026-03-09 13:54 UTC | DaniilIaitskov | +273/−0 lines, 4 filesdep +aesondep +basedep +deepseq |
Dependencies of non-negative-time-diff-0.0.2
aeson <3 2.3.2.0 → 2.3.3.0
| 2026-11-23 21:23 UTC | 2.3.3.0 | +124/−38 lines, 10 filesPVP ok |
| 2026-09-12 18:04 UTC | 2.3.2.0 | +97/−60 lines, 13 filesPVP ok |
| 2026-07-05 10:37 UTC | 2.3.1.0 | +101/−42 lines, 17 filesdep ~text-iso8601PVP ok |
| 2026-05-21 08:35 UTC | 2.3.0.0 | +54/−41 lines, 17 filesdep ~nothunksdep ~text-iso8601new-uploaderPVP ok |
| 2026-08-29 13:20 UTC | 2.2.5.1 | +36/−24 lines, 4 filesnew-uploaderPVP ok |
showing 5 of 106 versions
base >=4.7 && <5 4.22.0.0 → 4.22.1.0
| 2026-12-01 12:38 UTC | 4.22.1.0 | +388/−6 lines, 9 filesdep ~ghc-internalPVP: major bump suggested |
| 2025-12-19 11:52 UTC | 4.22.0.0 | +886/−215 lines, 28 filesdep ~ghc-internalPVP ok |
| 2026-03-27 12:26 UTC | 4.21.2.0 | +10/−3 lines, 4 filesdep ~ghc-internalPVP ok |
| 2025-12-27 10:31 UTC | 4.21.1.0 | +17/−5 lines, 4 filesdep ~ghc-internalPVP ok |
| 2024-12-16 09:24 UTC | 4.21.0.0 | +1170/−396 lines, 39 filesdep ~ghc-internalPVP ok |
showing 5 of 61 versions
deepseq <2 1.5.2.0 → 1.6.0.0
| 2024-09-08 16:18 UTC | 1.6.0.0 | +5/−14 lines, 3 filesdep ~basePVP ok |
| 2025-06-14 22:25 UTC | 1.5.2.0 | +13/−4 lines, 3 filesPVP: major bump suggested |
| 2024-09-09 12:48 UTC | 1.5.1.0 | +69/−14 lines, 3 filesdep −arraydep ~basePVP: major bump suggested |
| 2023-07-27 19:32 UTC | 1.5.0.0 | +619/−532 lines, 5 filesdep ~basePVP ok |
| 2022-12-18 00:14 UTC | 1.4.8.1 | +34/−9 lines, 3 filesdep ~basePVP: major bump suggested |
showing 5 of 26 versions
directory <2 1.3.10.1 → 1.3.11.0
| 2026-05-27 05:59 UTC | 1.3.11.0 | +4560/−4504 lines, 25 filesdep ~file-iodep ~filepathdep ~os-stringPVP ok |
| 2026-01-05 11:08 UTC | 1.3.10.1 | +4989/−4830 lines, 43 filesPVP: major bump suggested |
| 2025-12-24 02:55 UTC | 1.3.10.0 | +90/−39 lines, 7 filesdep ~Win32dep ~basePVP: major bump suggested |
| 2024-10-11 10:43 UTC | 1.3.9.0 | +235/−158 lines, 14 filesdep +file-iodep ~basedep ~timePVP: major bump suggested |
| 2024-05-20 06:52 UTC | 1.3.8.5 | +15/−13 lines, 7 filesdep ~basePVP ok |
showing 5 of 55 versions
ghc-typelits-natnormalise <1 0.9.6 → 0.9.7
| 2026-09-19 12:15 UTC | 0.9.7 | +187/−69 lines, 10 filesdep +singletonsdep ~ghcdep ~ghc-tcplugin-apiPVP: major bump suggested |
| 2026-05-13 12:55 UTC | 0.9.6 | +11/−7 lines, 3 filesdep ~ghc-tcplugin-apidep ~transformersPVP: major bump suggested |
| 2026-03-19 17:12 UTC | 0.9.5 | +30/−22 lines, 3 filesPVP ok |
| 2026-03-19 16:46 UTC | 0.9.4 | +792/−695 lines, 9 filesdep +interpolatedep +processdep +temporarydep −template-haskelldep ~ghc-tcplugin-apinew-uploaderPVP ok |
| 2025-12-02 22:23 UTC | 0.9.3 | +46/−10 lines, 5 filesPVP ok |
showing 5 of 51 versions
mtl <3 2.3.1 → 2.3.2
| 2025-12-08 04:58 UTC | 2.3.2 | +112/−21 lines, 12 filesdep ~basenew-uploaderPVP: major bump suggested |
| 2022-10-31 21:28 UTC | 2.3.1 | +188/−33 lines, 8 filesdep ~basePVP: major bump suggested |
| 2022-05-07 20:34 UTC | 2.3 | +1284/−520 lines, 29 filesdep ~basedep ~transformersnew-uploaderPVP ok |
| 2018-02-24 22:15 UTC | 2.2.2 | +152/−36 lines, 14 filesdep ~basePVP: major bump suggested |
| 2014-06-02 02:22 UTC | 2.2.1 | +179/−4 lines, 8 filesdep ~basedep ~transformersPVP: major bump suggested |
showing 5 of 20 versions
safecopy <1 0.10.4.2 → 0.10.4.3
| 2025-08-27 19:05 UTC | 0.10.4.3 | +99/−99 lines, 6 filesdep −QuickCheckdep −quickcheck-instancesdep ~template-haskelldep ~timenew-uploaderPVP: major bump suggested |
| 2021-04-14 01:34 UTC | 0.10.4.2 | +4/−4 lines, 1 filesdep ~QuickCheckdep ~arraydep ~bytestringPVP ok |
| 2021-02-21 12:14 UTC | 0.10.4.1 | +2/−2 lines, 2 filesPVP ok |
| 2021-02-21 02:35 UTC | 0.10.4 | +31/−21 lines, 4 filesPVP: major bump suggested |
| 2021-01-11 18:24 UTC | 0.10.3.1 | +21/−12 lines, 5 filesdep ~template-haskellPVP ok |
showing 5 of 44 versions
time <2 1.16 → 1.16.0.1
| 2026-07-20 21:55 UTC | 1.16.0.1 | +1203/−148 lines, 25 filesPVP ok |
| 2026-05-03 08:43 UTC | 1.16 | +353/−64 lines, 29 filesdep +template-haskell-liftdep ~template-haskellPVP ok |
| 2025-08-04 16:22 UTC | 1.15 | +1407/−743 lines, 56 filesdep ~basedep ~deepseqdep ~template-haskellPVP ok |
| 2024-03-10 22:18 UTC | 1.14 | +6594/−5536 lines, 73 filesdep +template-haskelldep ~basePVP ok |
| 2021-10-18 05:36 UTC | 1.13 | +1279/−1589 lines, 59 filesdep ~basePVP ok |
showing 5 of 48 versions
Reverse dependencies
1 packages depend on non-negative-time-diff