bey*_*kay 4 haskell iso8601 haskell-stack
我已经尝试了一些可以在网上找到的方法来将当前时间打印为 ISO8601 字符串,但没有一个起作用:
\n这个答案假设您已经知道如何获取当前时间,但我无法弄清楚。
\n我不断收到错误,Could not find module \xe2\x80\x98System.Locale\xe2\x80\x99因为它似乎System.Locale已被弃用/默认情况下不再安装?
我尝试安装old-local,但这给了我新的错误,我认为是由于time和 之间的不兼容造成的old-locale:
\xe2\x80\xa2 Couldn\'t match expected type \xe2\x80\x98time-1.9.3:Data.Time.Format.Locale.TimeLocale\xe2\x80\x99\n with actual type \xe2\x80\x98System.Locale.TimeLocale\xe2\x80\x99\n NB: \xe2\x80\x98time-1.9.3:Data.Time.Format.Locale.TimeLocale\xe2\x80\x99\n is defined in \xe2\x80\x98Data.Time.Format.Locale\xe2\x80\x99 in package \xe2\x80\x98time-1.9.3\xe2\x80\x99\n \xe2\x80\x98System.Locale.TimeLocale\xe2\x80\x99\n is defined in \xe2\x80\x98System.Locale\xe2\x80\x99 in package \xe2\x80\x98old-locale-1.0.0.7\xe2\x80\x99\n\xe2\x80\xa2 In the first argument of \xe2\x80\x98formatTime\xe2\x80\x99, namely \xe2\x80\x98defaultTimeLocale\xe2\x80\x99\n In the expression: formatTime defaultTimeLocale "%FT%T%QZ"\n In an equation for \xe2\x80\x98iso8601\xe2\x80\x99:\n iso8601 = formatTime defaultTimeLocale "%FT%T%QZ"\n |\n49 | iso8601 = formatTime defaultTimeLocale "%FT%T%QZ"\n | ^^^^^^^^^^^^^^^^^\n\nRun Code Online (Sandbox Code Playgroud)\n此 wiki 页面获取当前时间,但未将其格式化为 ISO8601。
\n这篇博文看起来确实不错,但只有一节关于格式,实际上与 1 相同:
\n\xe2\x80\xa2 Couldn\'t match expected type \xe2\x80\x98time-1.9.3:Data.Time.Format.Locale.TimeLocale\xe2\x80\x99\n with actual type \xe2\x80\x98System.Locale.TimeLocale\xe2\x80\x99\n NB: \xe2\x80\x98time-1.9.3:Data.Time.Format.Locale.TimeLocale\xe2\x80\x99\n is defined in \xe2\x80\x98Data.Time.Format.Locale\xe2\x80\x99 in package \xe2\x80\x98time-1.9.3\xe2\x80\x99\n \xe2\x80\x98System.Locale.TimeLocale\xe2\x80\x99\n is defined in \xe2\x80\x98System.Locale\xe2\x80\x99 in package \xe2\x80\x98old-locale-1.0.0.7\xe2\x80\x99\n\xe2\x80\xa2 In the first argument of \xe2\x80\x98formatTime\xe2\x80\x99, namely \xe2\x80\x98defaultTimeLocale\xe2\x80\x99\n In the expression: formatTime defaultTimeLocale "%FT%T%QZ"\n In an equation for \xe2\x80\x98iso8601\xe2\x80\x99:\n iso8601 = formatTime defaultTimeLocale "%FT%T%QZ"\n |\n49 | iso8601 = formatTime defaultTimeLocale "%FT%T%QZ"\n | ^^^^^^^^^^^^^^^^^\n\nRun Code Online (Sandbox Code Playgroud)\n我想要的是能够调用一个函数,让它以 ISO8601 字符串的形式打印出当前时间。我猜这实际上是一个 IO 操作,因为它不是一个严格的函数。
\n小智 5
我想知道 time package\xe2\x80\x99s Data.Time.Format.ISO8601模块中的某些内容是否有帮助?该iso8601Show函数将 UTCTime 转换为 ISO8601 格式的字符串。
import Data.Time (getCurrentTime)\nimport Data.Time.Format.ISO8601 (iso8601Show)\n\nmain :: IO ()\nmain = do\n now <- getCurrentTime\n putStrLn (iso8601Show now)\nRun Code Online (Sandbox Code Playgroud)\n在 ghci 中:
\n>>> import Data.Time (getCurrentTime)\n>>> import Data.Time.Format.ISO8601 (iso8601Show)\n>>> now <- getCurrentTime\n>>> putStrLn (iso8601Show now)\n2022-09-09T08:06:21.630747Z\nRun Code Online (Sandbox Code Playgroud)\n