开发者

Haskell Convert ByteString To UTC Time

I have been开发者_如何学C trying to make a function in Haskell to take a ByteString which is a datetime and convert it to UTC time taking into account the time zone from the original encoding. I am very new to Haskell so I may be making a really basic mistake.

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

The error I get is:

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 }


First, the -5 needs to be in parentheses, otherwise it's parsed as trying to subtract 5 from the hoursToTimeZone function, which explains the type error.

Also, all functions here are pure, so they don't need to be in a monadic do{...}. Just use a let expression if you want to explicitly name the steps.

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜