[英]How do I create a UTCTime from Int values, using the thyme library in Haskell?
[英]How can I get a Binary instance of the thyme package's UTCTime data type?
我希望能夠將CommandRecord
序列化為二進制文件,以便將其保存到文件中。 但是,我的天真做法是:由於以下錯誤*, instance Binary CommandRecord
無法正常工作。
我可以使用哪種方法來實現上述目標? 現在,我的想法是通過放棄使用thyme
庫和UTCTime
而不是使用time
庫和UTCTime
(已經定義了Binary實例)的方法來解決此問題。
我有以下數據類型:
data CommandRecord = CommandRecord {
command :: Text
, timedate :: UTCTime
, path :: Text
} deriving Generic
我有以下進口:
import Data.Thyme.Clock
import Data.Binary
import Data.Binary.Orphans
我正在使用以下軟件包:
錯誤*:
/home/chris/Projects/Haskell/MoscoviumOrange/src/Main.hs:28:10-
29: error:
• No instance for (Binary UTCTime)
arising from a use of ‘binary-0.8.5.1:Data.Binary.Class
.$dmput’
There are instances for similar types:
instance Binary
time-1.8.0.2:Data.Time.Clock.Internal.UTCTim
e.UTCTime
-- Defined in ‘Data.Binary.Orphans’
• In the expression:
binary-0.8.5.1:Data.Binary.Class.$dmput @CommandRecord
In an equation for ‘put’:
put = binary-0.8.5.1:Data.Binary.Class.$dmput
@CommandRecord
In the instance declaration for ‘Binary CommandRecord’
|
28 | instance Binary CommandRecord
| ^^^^^^^^^^^^^^^^^^^^
/home/chris/Projects/Haskell/MoscoviumOrange/src/Main.hs:28:10-
29: error:
• No instance for (Binary UTCTime)
arising from a use of ‘binary-0.8.5.1:Data.Binary.Class
.$dmget’
There are instances for similar types:
instance Binary
time-1.8.0.2:Data.Time.Clock.Internal.UTCTim
e.UTCTime
-- Defined in ‘Data.Binary.Orphans’
• In the expression:
binary-0.8.5.1:Data.Binary.Class.$dmget @CommandRecord
In an equation for ‘get’:
get = binary-0.8.5.1:Data.Binary.Class.$dmget
@CommandRecord
In the instance declaration for ‘Binary CommandRecord’
|
28 | instance Binary CommandRecord
| ^^^^^^^^^^^^^^^^^^^^
啊,是的,但瑣碎但缺少實例問題。 我會做什么:
instance Binary Thyme
作為孤立實例添加到您的模塊。 確認它可以工作。 #if !MIN_VERSION_thyme(0,3,6)
開關中。 原因是,如果出現的新版本的thyme
也已經包含該實例,則您不希望重復的實例破壞您的構建。 .cabal
文件中具有依賴項綁定的thyme<=0.3.5
,因此將不嘗試沒有實例的新版本。 (這似乎使實例上的切換失效。要點是,您可以在事后編輯關於Hackage的依賴關系范圍,並且當出現帶有實例的thyme
版本時應該這樣做。) thyme
維護者提出拉取請求。 這應該是沒有爭議的,因為thyme
無論如何都間接依賴於Binary
。 但是,僅當您要編寫的是可執行文件庫或小型專家庫時才建議這樣做。 如果這是許多其他項目可能使用的庫,則存在與其他人的孤立實例沖突的風險。 在這種情況下,您應該改用一種解決方法,直到thyme
添加實例為止:
data CommandRecord = CommandRecord {
command :: Text
, timedate :: UTCTime'
, path :: Text
} deriving Generic
newtype UTCTime' = UTCTime' {stdUTCThyme :: UTCTime}
instance Binary UTCTime' where
put = gput . from . stdUTCThyme
get = UTCTime' . to <$> gget
跟隨@leftaroundabout的1.點,我結束了以下模塊:
{-# OPTIONS -Wno-orphans #-}
module ThymeBinaryInstances where
import Data.Binary
import Data.Thyme.Internal.Micro
import Data.Thyme.Clock
instance Binary UTCTime
instance Binary NominalDiffTime
instance Binary Micro
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.