Created
May 25, 2010 19:39
-
-
Save jacobstanley/413587 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
------------------------------------------------------------------------------ | |
-- 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