哈斯克尔:如何在日期和时间上打印秒数? [英] Haskell: How to pretty print number of seconds as a date and time?

查看:148
本文介绍了哈斯克尔:如何在日期和时间上打印秒数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个整数值,即自Epoch以来的秒数。我可以将它输出为一个大整数,但我想将它显示为人类可读的日期和时间。

I have an integral value that is the number of seconds since the Epoch. I can output it as a big integer, but I want to show it as a human-readable date and time.

例如:

For example:

secToTimestamp :: Int32 -> [Char]

返回类似于:

2016-01-01 14:11:11


推荐答案

为了简化 时间 基于解决方案(因为时间是处理任何时间相关的事实模块):

In the interests of having a simple time based solution (since time is the defacto module for manipulating anything time related):

import Data.Time.Clock.POSIX
import Data.Time.Format

secToTimestamp :: Int32 -> String
secToTimestamp = formatTime defaultTimeLocale "%F %X" . posixSecondsToUTCTime . fromIntegral

这篇关于哈斯克尔:如何在日期和时间上打印秒数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆