indent -kr -i8 -brf -bbb -fnc -l132 -nce on all of webcit-classic
[citadel.git] / webcit / http_datestring.c
1 #include "webcit.h"
2
3 #ifdef __FreeBSD__
4
5 /** I like to believe there is a better way to do this. */
6 #define HAVE_STRUCT_TM_TM_GMTOFF
7 #endif
8
9 /** HTTP Months - do not translate - these are not for human consumption */
10 static char *httpdate_months[] = {
11         "Jan", "Feb", "Mar", "Apr", "May", "Jun",
12         "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
13 };
14
15 /** HTTP Weekdays - do not translate - these are not for human consumption */
16 static char *httpdate_weekdays[] = {
17         "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
18 };
19
20
21 /**
22  * \brief Supplied with a unix timestamp, generate a textual time/date stamp
23  * \param buf the return buffer
24  * \param n the size of the buffer
25  * \param xtime the time to format as string
26  */
27 void http_datestring(char *buf, size_t n, time_t xtime) {
28         struct tm t;
29
30         long offset;
31         char offsign;
32
33         localtime_r(&xtime, &t);
34
35         /** Convert "seconds west of GMT" to "hours/minutes offset" */
36 #ifdef HAVE_STRUCT_TM_TM_GMTOFF
37         offset = t.tm_gmtoff;
38 #else
39         offset = timezone;
40 #endif
41         if (offset > 0) {
42                 offsign = '+';
43         }
44         else {
45                 offset = 0L - offset;
46                 offsign = '-';
47         }
48         offset = ((offset / 3600) * 100) + (offset % 60);
49
50         snprintf(buf, n, "%s, %02d %s %04d %02d:%02d:%02d %c%04ld",
51                  httpdate_weekdays[t.tm_wday],
52                  t.tm_mday, httpdate_months[t.tm_mon], t.tm_year + 1900, t.tm_hour, t.tm_min, t.tm_sec, offsign, offset);
53 }
54
55
56 void tmplput_nowstr(StrBuf * Target, WCTemplputParams * TP) {
57         char buf[64];
58         long bufused;
59         time_t now;
60
61         now = time(NULL);
62 #ifdef HAVE_SOLARIS_LOCALTIME_R
63         asctime_r(localtime(&now), buf, sizeof(buf));
64 #else
65         asctime_r(localtime(&now), buf);
66 #endif
67         bufused = strlen(buf);
68         if ((bufused > 0) && (buf[bufused - 1] == '\n')) {
69                 buf[bufused - 1] = '\0';
70                 bufused--;
71         }
72         StrEscAppend(Target, NULL, buf, 0, 0);
73 }
74 void tmplput_nowno(StrBuf * Target, WCTemplputParams * TP) {
75         time_t now;
76         now = time(NULL);
77         StrBufAppendPrintf(Target, "%ld", now);
78 }
79
80 void InitModule_DATE(void) {
81         RegisterNamespace("DATE:NOW:STR", 0, 0, tmplput_nowstr, NULL, CTX_NONE);
82         RegisterNamespace("DATE:NOW:NO", 0, 0, tmplput_nowno, NULL, CTX_NONE);
83 }