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