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