0
我有一個整數值,它是自Epoch以來的秒數。我可以將它輸出爲一個大整數,但我想將它顯示爲人類可讀的日期和時間。Haskell:如何打印日期和時間的秒數?
例如:
secToTimestamp :: Int32 -> [Char]
它返回類似:
2016-01-01 14:11:11
我有一個整數值,它是自Epoch以來的秒數。我可以將它輸出爲一個大整數,但我想將它顯示爲人類可讀的日期和時間。Haskell:如何打印日期和時間的秒數?
例如:
secToTimestamp :: Int32 -> [Char]
它返回類似:
2016-01-01 14:11:11
可能使用unix-time
模塊
{-# LANGUAGE OverloadedStrings #-}
import Prelude
import qualified Data.ByteString.Char8 as B
import Data.UnixTime
import Data.Int
import Data.Functor
secToTimestampGMT :: Int32 -> [Char]
secToTimestampGMT t = B.unpack $ formatUnixTimeGMT "%Y-%m-%d %H-%M-%S" $ UnixTime (fromIntegral t) 0
secToTimestamp :: Int32 -> IO [Char]
secToTimestamp t = B.unpack <$> (formatUnixTime "%Y-%m-%d %H-%M-%S" $ UnixTime (fromIntegral t) 0)
在具有簡單time
基於溶液的利益(因爲time
是操作符的事實模塊g時間相關):
import Data.Time.Clock.POSIX
import Data.Time.Format
secToTimestamp :: Int32 -> String
secToTimestamp = formatTime defaultTimeLocale "%F %X" . posixSecondsToUTCTime . fromIntegral
[''time'](https://hackage.haskell.org/package/time) – Alec