如何从时间库 < v1.9.1 中的 NominalDiffTime 获取秒数?

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

似乎在 time v1.9.1 (http://hackage.haskell.org/package/time-1.9.2/docs/Data-Time-Clock.html#v:nominalDiffTimeToSeconds) 中有一个功能,但我的堆栈配置与这个版本不兼容。

我已经尝试 stack solver 添加 - time-1.9.2 作为堆栈 extra-dep 但它输出:

 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

我看到它被定义(在 v1.8.0.2 http://hackage.haskell.org/package/time-1.8.0.2/docs/src/Data.Time.Clock.Internal.NominalDiffTime.html#NominalDiffTime 中)为:

-- | 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
    )

但是 MkNominalDiffTime 构造函数没有导出,我想这意味着我需要分叉库来修改它?

首先是一个工作代码示例:

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"Conversion functions will treat it as seconds." and is of typeclass RealFrac 一样是 documentend,它为您提供 "conversion" 函数 properFractionceilingfloorround, truncate,你可以使用它,因为你喜欢它是四舍五入的。