* All OS-level includes are now included from webcit.h instead of from
[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 "webcit.h"
10
11 static char *httpdate_months[] = {
12         "Jan", "Feb", "Mar", "Apr", "May", "Jun",
13         "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
14 };
15
16 static char *httpdate_weekdays[] = {
17         "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
18 };
19
20
21 /*
22  * Supplied with a unix timestamp, generate a textual time/date stamp
23  */
24 void http_datestring(char *buf, size_t n, time_t xtime) {
25         struct tm t;
26
27         long offset;
28         char offsign;
29
30         localtime_r(&xtime, &t);
31
32         /* Convert "seconds west of GMT" to "hours/minutes offset" */
33 #ifdef HAVE_STRUCT_TM_TM_GMTOFF
34         offset = t.tm_gmtoff;
35 #else
36         offset = timezone;
37 #endif
38         if (offset > 0) {
39                 offsign = '+';
40         }
41         else {
42                 offset = 0L - offset;
43                 offsign = '-';
44         }
45         offset = ( (offset / 3600) * 100 ) + ( offset % 60 );
46
47         snprintf(buf, n, "%s, %02d %s %04d %02d:%02d:%02d %c%04ld",
48                 httpdate_weekdays[t.tm_wday],
49                 t.tm_mday,
50                 httpdate_months[t.tm_mon],
51                 t.tm_year + 1900,
52                 t.tm_hour,
53                 t.tm_min,
54                 t.tm_sec,
55                 offsign, offset
56         );
57 }
58