Haskell将ByteString转换为UTC时间
问题描述:
我一直试图在Haskell中创建一个ByteString,它是一个日期时间,并将其转换为UTC时间,并考虑原始编码的时区。我对Haskell很新,所以我可能会犯一个非常基本的错误。Haskell将ByteString转换为UTC时间
convertStringToUtc s =
do
estTimeZone <- hoursToTimeZone -5
time <- read $ B.unpack(s)
localTimeToUTC estTimeZone time
我得到的错误是:
Couldn't match expected type `Int -> b'
against inferred type `UTCTime'
In the expression: localTimeToUTC estTimeZone time
In the expression:
do { estTimeZone <- hoursToTimeZone - 5;
time <- read $ B.unpack (s);
localTimeToUTC estTimeZone time }
答
首先,-5需要在括号,否则它解析为试图从hoursToTimeZone
功能,这也解释了错误类型减去5。
此外,这里的所有功能都是纯粹的,所以它们不需要在一元do{...}
。如果要明确指定步骤,只需使用let
表达式。
convertStringToUtc s =
let estTimeZone = hoursToTimeZone (-5)
time = read $ B.unpack s
in localTimeToUTC estTimeZone time