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