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