Formatting dates with strftime

strftime is the Standard C library date formatting routine. An RFC 1123 date can be formatted with it:

#include <time.h>
#include <locale.h>
#include <stdlib.h>
	
char expire_time[32]; /* this date format is of deterministic length */
time_t expire;
int	strflen;

setlocale(LC_TIME, "POSIX");
/* make sure we get a system-independent date */
if( _putenv_s( "TZ", "GMT" ) != 0 )
   printf( "Unable to set TZ\n" );
else
   tzset();
/* actually format it */

strflen = strftime(expire_time, sizeof(expire_time), "%a, %d %b %Y %H:%M:%S +0000", gmtime(&expire));

This example is a Netscape cookie (that spec has been superseded by HTTP State Management Mechanism - Proposed Standard RFC 2109 which references RFC 2616 for Expires, which points to RFC 1123) expiry time.


Provided with no warranty by Henry Troup

notes: a lot of people don't think this code is quite right... they may be correct. It does compile and run; some of these routines are deprecated. Last modified at time_t 1163178746 (GMT). For email, see RFC 2822 and note the differences in time-zones, but RFC 1123 says "should use numeric zones". YMMV

This page is placed in the Public Domain