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