Haskell将ByteString转换为UTC时间

Ste*_*nce 1 time parsing haskell bytestring

我一直在尝试在Haskell中创建一个函数来获取一个日期时间的ByteString,并将其转换为UTC时间,同时考虑原始编码的时区.我对Haskell很新,所以我可能犯了一个非常基本的错误.

convertStringToUtc s =
  do
    estTimeZone <- hoursToTimeZone -5
    time <- read $ B.unpack(s)
    localTimeToUTC estTimeZone time
Run Code Online (Sandbox Code Playgroud)

我得到的错误是:

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 }
Run Code Online (Sandbox Code Playgroud)

hza*_*zap 5

首先,-5需要在括号中,否则它被解析为试图从hoursToTimeZone函数中减去5 ,这解释了类型错误.

此外,这里的所有功能都是纯粹的,因此它们不需要是monadic do{...}.let如果要明确命名步骤,只需使用表达式.

convertStringToUtc s = 
    let estTimeZone = hoursToTimeZone (-5)
        time = read $ B.unpack s
    in  localTimeToUTC estTimeZone time
Run Code Online (Sandbox Code Playgroud)