indent -kr -i8 -brf -bbb -fnc -l132 -nce on all of webcit-classic
[citadel.git] / webcit / fmt_date.c
1
2 /*
3  * Copyright (c) 1996-2012 by the citadel.org team
4  *
5  * This program is open source software.  You can redistribute it and/or
6  * modify it under the terms of the GNU General Public License, version 3.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  */
13
14 #include "webcit.h"
15 #include "webserver.h"
16
17 #ifdef HAVE_USELOCALE
18 extern locale_t *wc_locales;
19 #endif
20
21 typedef unsigned char byte;
22
23 #define FALSE 0 /**< no. */
24
25 #define TRUE 1 /**< yes. */
26
27 /*
28  * Wrapper around strftime() or strftime_l()
29  * depending upon how our build is configured.
30  *
31  * s            String target buffer
32  * max          Maximum size of string target buffer
33  * format       strftime() format
34  * tm           Input date/time
35  */
36 size_t wc_strftime(char *s, size_t max, const char *format, const struct tm *tm) {
37
38 #ifdef ENABLE_NLS
39 #ifdef HAVE_USELOCALE
40         if (wc_locales[WC->selected_language] == NULL) {
41                 return strftime(s, max, format, tm);
42         }
43         else {
44                 return strftime_l(s, max, format, tm, wc_locales[WC->selected_language]);
45         }
46 #else
47         return strftime(s, max, format, tm);
48 #endif
49 #else
50         return strftime(s, max, format, tm);
51 #endif
52 }
53
54
55
56 /*
57  * Format a date/time stamp for output 
58  */
59 long webcit_fmt_date(char *buf, size_t siz, time_t thetime, int Format) {
60         long retlen = 0;
61         struct tm tm;
62         struct tm today_tm;
63         time_t today_timet;
64         int time_format;
65
66         time_format = get_time_format_cached();
67         today_timet = time(NULL);
68         localtime_r(&today_timet, &today_tm);
69
70         localtime_r(&thetime, &tm);
71
72         /*
73          * DATEFMT_FULL:      full display 
74          * DATEFMT_BRIEF:     if date == today, show only the time
75          *                    otherwise, for messages up to 6 months old, 
76          *                 show the month and day, and the time
77          *                    older than 6 months, show only the date
78          * DATEFMT_RAWDATE:   show full date, regardless of age 
79          * DATEFMT_LOCALEDATE:   show full date as prefered for the locale
80          */
81
82         switch (Format) {
83         case DATEFMT_BRIEF:
84                 if ((tm.tm_year == today_tm.tm_year)
85                     && (tm.tm_mon == today_tm.tm_mon)
86                     && (tm.tm_mday == today_tm.tm_mday)) {
87                         if (time_format == WC_TIMEFORMAT_24)
88                                 retlen = wc_strftime(buf, siz, "%k:%M", &tm);
89                         else
90                                 retlen = wc_strftime(buf, siz, "%l:%M%p", &tm);
91                 }
92                 else if (today_timet - thetime < 15552000) {
93                         if (time_format == WC_TIMEFORMAT_24)
94                                 retlen = wc_strftime(buf, siz, "%b %d %k:%M", &tm);
95                         else
96                                 retlen = wc_strftime(buf, siz, "%b %d %l:%M%p", &tm);
97                 }
98                 else {
99                         retlen = wc_strftime(buf, siz, "%b %d %Y", &tm);
100                 }
101                 break;
102         case DATEFMT_FULL:
103                 if (time_format == WC_TIMEFORMAT_24)
104                         retlen = wc_strftime(buf, siz, "%a %b %d %Y %T %Z", &tm);
105                 else
106                         retlen = wc_strftime(buf, siz, "%a %b %d %Y %r %Z", &tm);
107                 break;
108         case DATEFMT_RAWDATE:
109                 retlen = wc_strftime(buf, siz, "%a %b %d %Y", &tm);
110                 break;
111         case DATEFMT_LOCALEDATE:
112                 retlen = wc_strftime(buf, siz, "%x", &tm);
113                 break;
114         }
115         return retlen;
116 }
117
118
119 /* 
120  * Try to guess whether the user will prefer 12 hour or 24 hour time based on the locale.
121  */
122 long guess_calhourformat(void) {
123         char buf[64];
124         struct tm tm;
125         memset(&tm, 0, sizeof tm);
126         wc_strftime(buf, 64, "%X", &tm);
127         if (buf[strlen(buf) - 1] == 'M') {
128                 return 12;
129         }
130         return 24;
131 }
132
133
134 /*
135  * learn the users timeformat preference.
136  */
137 int get_time_format_cached(void) {
138         long calhourformat;
139         int *time_format_cache;
140         time_format_cache = &(WC->time_format_cache);
141         if (*time_format_cache == WC_TIMEFORMAT_NONE) {
142                 get_pref_long("calhourformat", &calhourformat, 99);
143
144                 /* If we don't know the user's time format preference yet,
145                  * make a guess based on the locale.
146                  */
147                 if (calhourformat == 99) {
148                         calhourformat = guess_calhourformat();
149                 }
150
151                 /* Now set the preference */
152                 if (calhourformat == 24)
153                         *time_format_cache = WC_TIMEFORMAT_24;
154                 else
155                         *time_format_cache = WC_TIMEFORMAT_AMPM;
156         }
157         return *time_format_cache;
158 }
159
160 /*
161  * Format TIME ONLY for output 
162  * buf          the output buffer
163  * thetime      time to format into buf
164  */
165 void fmt_time(char *buf, size_t siz, time_t thetime) {
166         struct tm *tm;
167         int hour;
168         int time_format;
169
170         time_format = get_time_format_cached();
171         buf[0] = 0;
172         tm = localtime(&thetime);
173         hour = tm->tm_hour;
174         if (hour == 0)
175                 hour = 12;
176         else if (hour > 12)
177                 hour = hour - 12;
178
179         if (time_format == WC_TIMEFORMAT_24) {
180                 snprintf(buf, siz, "%d:%02d", tm->tm_hour, tm->tm_min);
181         }
182         else {
183                 snprintf(buf, siz, "%d:%02d%s", hour, tm->tm_min, ((tm->tm_hour > 12) ? "pm" : "am")
184                     );
185         }
186 }
187
188
189
190
191 /*
192  * Break down the timestamp used in HTTP headers
193  * Should read rfc1123 and rfc850 dates OK
194  * FIXME won't read asctime
195  * Doesn't understand timezone, but we only should be using GMT/UTC anyway
196  */
197 time_t httpdate_to_timestamp(StrBuf * buf) {
198         time_t t = 0;
199         struct tm tt;
200         const char *c;
201
202         /** Skip day of week, to number */
203         for (c = ChrPtr(buf); *c != ' '; c++);
204         c++;
205
206         memset(&tt, 0, sizeof(tt));
207
208         /* Get day of month */
209         tt.tm_mday = atoi(c);
210         for (; *c != ' ' && *c != '-'; c++);
211         c++;
212
213         /* Get month */
214         switch (*c) {
215         case 'A':               /* April, August */
216                 tt.tm_mon = (c[1] == 'p') ? 3 : 7;
217                 break;
218         case 'D':               /* December */
219                 tt.tm_mon = 11;
220                 break;
221         case 'F':               /* February */
222                 tt.tm_mon = 1;
223                 break;
224         case 'M':               /* March, May */
225                 tt.tm_mon = (c[2] == 'r') ? 2 : 4;
226                 break;
227         case 'J':               /* January, June, July */
228                 tt.tm_mon = (c[2] == 'n') ? ((c[1] == 'a') ? 0 : 5) : 6;
229                 break;
230         case 'N':               /* November */
231                 tt.tm_mon = 10;
232                 break;
233         case 'O':               /* October */
234                 tt.tm_mon = 9;
235                 break;
236         case 'S':               /* September */
237                 tt.tm_mon = 8;
238                 break;
239         default:
240                 return 42;
241                 break;          /* NOTREACHED */
242         }
243         c += 4;
244
245         tt.tm_year = 0;
246         /* Get year */
247         tt.tm_year = atoi(c);
248         for (; *c != ' '; c++);
249         c++;
250         if (tt.tm_year >= 1900)
251                 tt.tm_year -= 1900;
252
253         /* Get hour */
254         tt.tm_hour = atoi(c);
255         for (; *c != ':'; c++);
256         c++;
257
258         /* Get minute */
259         tt.tm_min = atoi(c);
260         for (; *c != ':'; c++);
261         c++;
262
263         /* Get second */
264         tt.tm_sec = atoi(c);
265         for (; *c && *c != ' '; c++);
266
267         /* Got everything; let's go.  The global 'timezone' variable contains the
268          * local timezone's offset from UTC, in seconds, so we apply that to tm_sec.
269          * This produces an illegal value for tm_sec, but mktime() will normalize
270          * it for us.  This eliminates the need to temporarily switch the environment
271          * variable TZ to UTC, which is good because it fails to switch back on
272          * some systems.
273          */
274         tzset();
275 #ifdef __FreeBSD__
276         tt.tm_sec = tt.tm_sec - tt.tm_gmtoff;
277 #else
278         tt.tm_sec = tt.tm_sec - (int) timezone;
279 #endif
280         t = mktime(&tt);
281         return t;
282 }
283
284
285 void LoadTimeformatSettingsCache(StrBuf * Preference, long lvalue) {
286         int *time_format_cache;
287
288         time_format_cache = &(WC->time_format_cache);
289         if (lvalue == 24)
290                 *time_format_cache = WC_TIMEFORMAT_24;
291         else
292                 *time_format_cache = WC_TIMEFORMAT_AMPM;
293 }
294
295
296
297 void InitModule_DATETIME(void) {
298         RegisterPreference("calhourformat", _("Time format"), PRF_INT, LoadTimeformatSettingsCache);
299
300
301 }