首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从时间库< v1.9.1中的NominalDiffTime中获取秒数?

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

Stack Overflow用户
提问于 2018-10-27 09:33:20
回答 1查看 823关注 0票数 1

在时间版本1.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,但它输出:

代码语言:javascript
复制
 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中)为:

代码语言:javascript
复制
-- | 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构造函数没有导出,我假设这意味着我需要对库进行分叉才能修改它?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-10-27 10:20:43

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

代码语言:javascript
复制
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是类似于“转换函数将处理它为秒。”的文档,它的类型是RealFrac,它为您提供了“转换”函数properFractionceilingfloorroundtruncate,您可以使用这些函数,因为您希望它被四舍五入。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53020538

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档