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