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