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