Created
May 23, 2010 18:54
-
-
Save jacobstanley/411163 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
-- | Converts a 'ClockTime' into an HTTP timestamp. | |
formatHttpTime :: UTCTime -> ByteString | |
formatHttpTime = fromStr . formatTime defaultTimeLocale "%a, %d %b %Y %X GMT" | |
-- | Converts an HTTP timestamp into a 'UTCTime'. | |
parseHttpTime :: ByteString -> Maybe UTCTime | |
parseHttpTime s' = | |
parseTime defaultTimeLocale "%a, %d %b %Y %H:%M:%S GMT" s | |
where | |
s = toStr s' |
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
-- | Converts a 'CTime' into an HTTP timestamp. | |
formatHttpTime :: CTime -> IO ByteString | |
formatHttpTime t = allocaBytes 40 $ \ptr -> do | |
c_format_http_time t ptr | |
S.packCString ptr | |
------------------------------------------------------------------------------ | |
-- | Converts an HTTP timestamp into a 'CTime'. | |
parseHttpTime :: ByteString -> IO CTime | |
parseHttpTime s = S.useAsCString s $ \ptr -> | |
c_parse_http_time ptr |
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
#define _XOPEN_SOURCE | |
#include <time.h> | |
#include <locale.h> | |
void set_c_locale() { | |
setlocale(LC_TIME, "C"); | |
} | |
time_t c_parse_http_time(char* s) { | |
struct tm dest; | |
strptime(s, "%a, %d %b %Y %H:%M:%S GMT", &dest); | |
return mktime(&dest); | |
} | |
void c_format_http_time(time_t src, char* dest) { | |
struct tm t; | |
gmtime_r(&src, &t); | |
strftime(dest, 40, "%a, %d %b %Y %H:%M:%S GMT", &t); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment