似乎在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
构造函数没有导出,我认为这意味着我需要派生该库来进行修改吗?
答案 0 :(得分:1)
首先是一个工作代码示例:
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.",类型为RealFrac,可为您提供“转换”功能properFraction
,ceiling
,floor
,round
,truncate
,您可以根据需要对其进行四舍五入。