tags:

views:

51

answers:

3

RFC1123 defines a number of things, among them, the format of Dates to be used in internet protocols. HTTP (RFC2616) specifies that date formats must be generated in conformance with RFC1123.

It looks like this:

Date: Wed, 28 Apr 2010 02:31:05 GMT

How can I generate an RFC1123 time string from C code, running on Windows? I don't have the use of C# and DateTime.ToString().

I know I could write the code myself, to emit timezones and day abbreviations, but I'm hoping this already exists in the Windows API.

Thanks.

+1  A: 

This is untested, but should be reasonably close:

time_t t = time(NULL);
struct tm *my_tm = gmtime(&t);
strftime(buffer, buf_size, "%a, %d %b %Y %H:%M:%S GMT", my_tm);
puts(buffer);
Jerry Coffin
+1  A: 

Probably, InternetTimeFromSystemTime from Wininet API.

RFC format used. Currently, the only valid format is INTERNET_RFC1123_FORMAT.

S.Mark
Thank you. This is exactly the thing. But, do you know - should I have any concerns about using wininet.dll from a server application? Will it ever be the case that wininet.dll is not present? For example, if no IE is installed on a server, will wininet.dll be missing?
Cheeso
Cheeso, yeah, you need wininet.dll, but its supposed to be part of Windows. If you already implement your own, may be you don't need to use that.
S.Mark
A: 

This is what I used:

static const char *DAY_NAMES[] =
  { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
static const char *MONTH_NAMES[] =
  { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
    "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };

char *Rfc1123_DateTimeNow()
{
    const int RFC1123_TIME_LEN = 29;
    time_t t;
    struct tm tm;
    char * buf = malloc(RFC1123_TIME_LEN+1);

    time(&t);
    gmtime_s(&tm, &t);

    strftime(buf, RFC1123_TIME_LEN+1, "---, %d --- %Y %H:%M:%S GMT", &tm);
    memcpy(buf, DAY_NAMES[tm.tm_wday], 3);
    memcpy(buf+8, MONTH_NAMES[tm.tm_mon], 3);

    return buf;
}
Cheeso