Skip to content

Instantly share code, notes, and snippets.

@jacobstanley
Created May 23, 2010 18:54
Show Gist options
  • Save jacobstanley/411163 to your computer and use it in GitHub Desktop.
Save jacobstanley/411163 to your computer and use it in GitHub Desktop.
-- | 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'
-- | 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
#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