Skip to content

Instantly share code, notes, and snippets.

@jacobstanley
Created May 25, 2010 19:39
Show Gist options
  • Save jacobstanley/413587 to your computer and use it in GitHub Desktop.
Save jacobstanley/413587 to your computer and use it in GitHub Desktop.
------------------------------------------------------------------------------
-- HTTP dates
-- | Converts a 'CTime' into an HTTP timestamp.
formatHttpTime :: CTime -> IO ByteString
-- | Converts an HTTP timestamp into a 'CTime'.
parseHttpTime :: ByteString -> IO CTime
#ifdef WIN32
formatHttpTime = return . format . posixSecondsToUTCTime . toPOSIXTime
where
format :: UTCTime -> ByteString
format = fromStr . formatTime defaultTimeLocale "%a, %d %b %Y %X GMT"
toPOSIXTime :: CTime -> POSIXTime
toPOSIXTime = toEnum . fromEnum
parseHttpTime = return . toCTime . parse . toStr
where
parse :: String -> Maybe UTCTime
parse = parseTime defaultTimeLocale "%a, %d %b %Y %H:%M:%S GMT"
toCTime :: Maybe UTCTime -> CTime
toCTime (Just t) = toEnum $ fromEnum $ utcTimeToPOSIXSeconds t
toCTime Nothing = toEnum $ 0
#else
formatHttpTime t = allocaBytes 40 $ \ptr -> do
c_format_http_time t ptr
S.packCString ptr
parseHttpTime s = S.useAsCString s $ \ptr ->
c_parse_http_time ptr
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment