在 Haskell 中获取当前小时/分钟/秒
问题:
使用 haskell,你想获取当前时区的当前小时/分钟/秒作为整数值。
解决方案
你需要 time 包来完成此任务。使用 cabal install time 安装它。
另请参见此之前的博客文章了解如何获取当前年/月/日。
time_example.hs
import Data.Time.Clock
import Data.Time.LocalTime
main = do
now <- getCurrentTime
timezone <- getCurrentTimeZone
let (TimeOfDay hour minute second) = localTimeOfDay $ utcToLocalTime timezone now
putStrLn $ "Hour: " ++ show hour
putStrLn $ "Minute: " ++ show minute
-- 注意:Second 是 @Pico@ 类型:它包含小数部分。
-- 使用 @fromIntegral@ 将其转换为普通整数。
putStrLn $ "Second: " ++ show secondCheck out similar posts by category:
Haskell
If this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow