代码之家  ›  专栏  ›  技术社区  ›  Steve Severance

haskell将bytestring转换为UTC时间

  •  1
  • Steve Severance  · 技术社区  · 16 年前

    我一直试图在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 }
    
    1 回复  |  直到 15 年前
        1
  •  5
  •   hzap    16 年前

    首先,-5必须在括号中,否则将被解析为试图从 hoursToTimeZone 函数,解释类型错误。

    而且,这里的所有函数都是纯函数,所以它们不必是一元函数。 do{...} . 就用一个 let 表达式(如果要显式命名步骤)。

    convertStringToUtc s = 
        let estTimeZone = hoursToTimeZone (-5)
            time = read $ B.unpack s
        in  localTimeToUTC estTimeZone time