我有年、月、日、小时和分钟的值(它们都是 Int 类型) - 我怎样才能将它们转换为UTCTime
?
就像如何使用 Haskell 中的百里香库从 Int 值创建 UTCTime 一样?但改用time
图书馆。
我有年、月、日、小时和分钟的值(它们都是 Int 类型) - 我怎样才能将它们转换为UTCTime
?
就像如何使用 Haskell 中的百里香库从 Int 值创建 UTCTime 一样?但改用time
图书馆。
import Data.Time.Calendar
import Data.Time
example :: UTCTime
example = UTCTime (fromGregorian 2018 10 27) (secondsToDiffTime 0)
放在这里供参考。检查两个方法 parseTimeM 和 parseTimeOnError。两者都可以从这里得到。parseTimeM 是安全版本,而 parseTimeOnError 是不安全的(抛出异常)。
我之前使用过 parseTimeOnError 。您需要将给定的信息转换为特定格式的字符串,并使用格式和字符串作为输入调用函数。
import Data.Time
import Data.Time.Format
import Data.Time.Clock
import Text.Printf
getDateString :: Int -> Int -> Int -> Int -> Int -> String
getDateString year month day hour minute = (printf "%02d" month) ++ "/" ++ (printf "%02d" day) ++ "/" ++ show year ++ " " ++ show hour ++ ":" ++ show minute
getTheUTCDate :: String -> UTCTime
getTheUTCDate strDate = parseTimeOrError True defaultTimeLocale "%D %R" strDate
main = do
let p = getDateString 12 7 6 23 45
print ("Hello ", " ", getTheUTCDate p)