Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I generate an RFC1123 Date string, from C code (Win32)

Tags:

windows

winapi

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.

like image 310
Cheeso Avatar asked Apr 28 '10 05:04

Cheeso


1 Answers

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;
}
like image 121
Cheeso Avatar answered Dec 08 '22 15:12

Cheeso