* Added some HTTP Date: header output
[citadel.git] / webcit / http_datestring.c
1 /*
2  * $Id$
3  *
4  * Function to generate HTTP-compliant textual time/date stamp
5  * (This module was lifted directly from the Citadel server source)
6  *
7  */
8
9 #include <stdlib.h>
10 #include <ctype.h>
11 #include <stdio.h>
12 #include <string.h>
13 # include <time.h>
14
15 static char *httpdate_months[] = {
16         "Jan", "Feb", "Mar", "Apr", "May", "Jun",
17         "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
18 };
19
20 static char *httpdate_weekdays[] = {
21         "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
22 };
23
24
25 /*
26  * Supplied with a unix timestamp, generate a textual time/date stamp
27  */
28 void http_datestring(char *buf, size_t n, time_t xtime) {
29         struct tm t;
30
31         long offset;
32         char offsign;
33
34         localtime_r(&xtime, &t);
35
36         /* Convert "seconds west of GMT" to "hours/minutes offset" */
37 #ifdef HAVE_STRUCT_TM_TM_GMTOFF
38         offset = t.tm_gmtoff;
39 #else
40         offset = timezone;
41 #endif
42         if (offset > 0) {
43                 offsign = '+';
44         }
45         else {
46                 offset = 0L - offset;
47                 offsign = '-';
48         }
49         offset = ( (offset / 3600) * 100 ) + ( offset % 60 );
50
51         snprintf(buf, n, "%s, %02d %s %04d %02d:%02d:%02d %c%04ld",
52                 httpdate_weekdays[t.tm_wday],
53                 t.tm_mday,
54                 httpdate_months[t.tm_mon],
55                 t.tm_year + 1900,
56                 t.tm_hour,
57                 t.tm_min,
58                 t.tm_sec,
59                 offsign, offset
60         );
61 }
62