haskell,如何将POSIXTime类型转换为字符串

时间:2013-02-05 10:27:37

标签: haskell types

Prelude> import Data.Time.Clock.POSIX (getPOSIXTime)
Prelude Data.Time.Clock.POSIX> ptime <- getPOSIXTime
Prelude Data.Time.Clock.POSIX> ptime
1360059876.245525s
Prelude Data.Time.Clock.POSIX> :t ptime
ptime :: Data.Time.Clock.POSIX.POSIXTime
Prelude Data.Time.Clock.POSIX> 

我想将ptime转换为字符串类型,怎么做?

1 个答案:

答案 0 :(得分:1)

使用posixSecondsToUTCTime也在Data.Time.Clock.POSIX中将POSIXTime转换为UTCTime;然后从Data.Time.Format formatTimeUTCTime格式化为String

e.g。

formatTime defaultTimeLocale "%FT%T" (posixSecondsToUTCTime ptime)
-- ^ ISO 8601
formatTime defaultTimeLocale "%s%Qs" (posixSecondsToUTCTime ptime)
-- ^ fractional seconds since the epoch (but see caveat noted in documentation)

defaultTimeLocale来自System.Locale。)