How do I get the amount of seconds from NominalDiffTime in the time library < v1.9.1?

1.1k Views Asked by At

It seems there is a function for this in time v1.9.1 (http://hackage.haskell.org/package/time-1.9.2/docs/Data-Time-Clock.html#v:nominalDiffTimeToSeconds) but my stack configuration is not compatible with this version.

I've tried stack solver with adding - time-1.9.2 as a stack extra-dep but it outputs:

 stack solver
Using configuration file: stack.yaml
Using cabal packages:
- ./

Using resolver: lts-12.12
Using compiler: ghc-8.4.3
Asking cabal to calculate a build plan...
Trying with packages from lts-12.12 and 4 external packages as hard constraints...
cabal: Could not resolve dependencies:
next goal: time (dependency of app-0.1.0.0)
rejecting: time-1.8.0.2/installed-1.8... (constraint from main config
/tmp/cabal-solver30194/cabal.config requires ==1.9.2)
trying: time-1.9.2
next goal: conduit (dependency of yaml-0.8.32)
rejecting: conduit-1.3.1 (constraint from main config
/tmp/cabal-solver30194/cabal.config requires ==1.3.0.3)
trying: conduit-1.3.0.3
next goal: unix (dependency of conduit-1.3.0.3)
rejecting: unix-2.7.2.2/installed-2.7... (conflict: time==1.9.2, unix =>
time==1.8.0.2/installed-1.8...)
rejecting: unix-2.7.2.2 (conflict: time==1.9.2, unix => time>=1.2 && <1.9)
rejecting: unix-2.7.2.1, unix-2.7.2.0, unix-2.7.1.0, unix-2.7.0.1,
unix-2.7.0.0, unix-2.6.0.1, unix-2.6.0.0, unix-2.5.1.1, unix-2.5.1.0,
unix-2.5.0.0, unix-2.4.2.0, unix-2.4.1.0, unix-2.4.0.2, unix-2.4.0.1,
unix-2.4.0.0, unix-2.3.2.0, unix-2.3.1.0, unix-2.3.0.0, unix-2.2.0.0, unix-2.0
(constraint from main config /tmp/cabal-solver30194/cabal.config requires
==2.7.2.2)
Dependency tree exhaustively searched.
Could not parse cabal-install errors:

>>>> Cabal errors begin
<<<< Cabal errors end

CallStack (from HasCallStack):
  error, called at src/Stack/Solver.hs:130:25 in stack-1.7.1-JqFYW3fz7If7um4NzPRwPj:Stack.Solver

I see it is defined (in v1.8.0.2 http://hackage.haskell.org/package/time-1.8.0.2/docs/src/Data.Time.Clock.Internal.NominalDiffTime.html#NominalDiffTime) as:

-- | This is a length of time, as measured by UTC.
-- Conversion functions will treat it as seconds.
-- It has a precision of 10^-12 s.
-- It ignores leap-seconds, so it's not necessarily a fixed amount of clock time.
-- For instance, 23:00 UTC + 2 hours of NominalDiffTime = 01:00 UTC (+ 1 day),
-- regardless of whether a leap-second intervened.
newtype NominalDiffTime = MkNominalDiffTime Pico deriving (Eq,Ord
#if LANGUAGE_DeriveDataTypeable
#if LANGUAGE_Rank2Types
#if HAS_DataPico
    ,Data, Typeable
#endif
#endif
#endif
    )

But the MkNominalDiffTime constructor is not exported, which I would assume means I would need to fork the library to modify this?

1

There are 1 best solutions below

0
On BEST ANSWER

First a working code example:

module Main where

import Control.Concurrent
import Data.Time.Clock

seeIntegersAreSeconds :: Integer -> String
seeIntegersAreSeconds = show

main = do
  t1 <- getCurrentTime
  threadDelay 2000000
  t2 <- getCurrentTime
  let myNominalDiffTime = diffUTCTime t2 t1
  let (myNominalDiffTimeInSeconds, _) = properFraction myNominalDiffTime
  putStrLn $ "diff: " ++ seeIntegersAreSeconds myNominalDiffTimeInSeconds

NominalDiffTime is documentend like "Conversion functions will treat it as seconds." and is of typeclass RealFrac which hands you the "conversion" functions properFraction, ceiling, floor, round, truncate, which you could use, as you like it to be rounded.