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