* Fixed a multiselect bug in the mailbox view. Ctrl-click was selecting a message...
[citadel.git] / webcit / fmt_date.c
blobecc7b78bbb9fbcf7470bfb349c9ffe6ac408c1df
1 /*
2 * $Id$
3 */
5 #include "webcit.h"
6 #include "webserver.h"
8 #ifdef HAVE_USELOCALE
9 extern locale_t wc_locales[];
10 #endif
12 typedef unsigned char byte;
14 #define FALSE 0 /**< no. */
15 #define TRUE 1 /**< yes. */
18 * Wrapper around strftime() or strftime_l()
19 * depending upon how our build is configured.
21 * s String target buffer
22 * max Maximum size of string target buffer
23 * format strftime() format
24 * tm Input date/time
26 size_t wc_strftime(char *s, size_t max, const char *format, const struct tm *tm)
29 #ifdef ENABLE_NLS
30 #ifdef HAVE_USELOCALE
31 if (wc_locales[WC->selected_language] == NULL) {
32 return strftime(s, max, format, tm);
34 else {
35 return strftime_l(s, max, format, tm, wc_locales[WC->selected_language]);
37 #else
38 return strftime(s, max, format, tm);
39 #endif
40 #else
41 return strftime(s, max, format, tm);
42 #endif
48 * Format a date/time stamp for output
50 void webcit_fmt_date(char *buf, time_t thetime, int Format)
52 struct tm tm;
53 struct tm today_tm;
54 time_t today_timet;
55 int time_format;
57 time_format = get_time_format_cached ();
58 today_timet = time(NULL);
59 localtime_r(&today_timet, &today_tm);
61 localtime_r(&thetime, &tm);
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
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, 32, "%k:%M", &tm);
80 else
81 wc_strftime(buf, 32, "%l:%M%p", &tm);
83 else if (today_timet - thetime < 15552000) {
84 if (time_format == WC_TIMEFORMAT_24)
85 wc_strftime(buf, 32, "%b %d %k:%M", &tm);
86 else
87 wc_strftime(buf, 32, "%b %d %l:%M%p", &tm);
89 else {
90 wc_strftime(buf, 32, "%b %d %Y", &tm);
92 break;
93 case DATEFMT_FULL:
94 if (time_format == WC_TIMEFORMAT_24)
95 wc_strftime(buf, 32, "%a %b %d %Y %T %Z", &tm);
96 else
97 wc_strftime(buf, 32, "%a %b %d %Y %r %Z", &tm);
98 break;
99 case DATEFMT_RAWDATE:
100 wc_strftime(buf, 32, "%a %b %d %Y", &tm);
101 break;
102 case DATEFMT_LOCALEDATE:
103 wc_strftime(buf, 32, "%x", &tm);
104 break;
110 * Try to guess whether the user will prefer 12 hour or 24 hour time based on the locale.
112 long guess_calhourformat(void) {
113 char buf[32];
114 struct tm tm;
115 memset(&tm, 0, sizeof tm);
116 wc_strftime(buf, 32, "%X", &tm);
117 if (buf[strlen(buf)-1] == 'M') {
118 return 12;
120 return 24;
125 * learn the users timeformat preference.
127 int get_time_format_cached (void)
129 long calhourformat;
130 int *time_format_cache;
131 time_format_cache = &(WC->time_format_cache);
132 if (*time_format_cache == WC_TIMEFORMAT_NONE)
134 get_pref_long("calhourformat", &calhourformat, 99);
136 /* If we don't know the user's time format preference yet,
137 * make a guess based on the locale.
139 if (calhourformat == 99) {
140 calhourformat = guess_calhourformat();
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;
149 return *time_format_cache;
153 * Format TIME ONLY for output
154 * buf the output buffer
155 * thetime time to format into buf
157 void fmt_time(char *buf, time_t thetime)
159 struct tm *tm;
160 int hour;
161 int time_format;
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;
172 if (time_format == WC_TIMEFORMAT_24) {
173 sprintf(buf, "%d:%02d",
174 tm->tm_hour, tm->tm_min
177 else {
178 sprintf(buf, "%d:%02d%s",
179 hour, tm->tm_min, ((tm->tm_hour > 12) ? "pm" : "am")
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
193 time_t httpdate_to_timestamp(StrBuf *buf)
195 time_t t = 0;
196 struct tm tt;
197 const char *c;
199 /** Skip day of week, to number */
200 for (c = ChrPtr(buf); *c != ' '; c++)
202 c++;
204 memset(&tt, 0, sizeof(tt));
206 /* Get day of month */
207 tt.tm_mday = atoi(c);
208 for (; *c != ' ' && *c != '-'; c++);
209 c++;
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 */
241 c += 4;
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;
251 /* Get hour */
252 tt.tm_hour = atoi(c);
253 for (; *c != ':'; c++);
254 c++;
256 /* Get minute */
257 tt.tm_min = atoi(c);
258 for (; *c != ':'; c++);
259 c++;
261 /* Get second */
262 tt.tm_sec = atoi(c);
263 for (; *c && *c != ' '; c++);
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.
272 tzset();
273 tt.tm_sec = tt.tm_sec - (int)timezone;
274 t = mktime(&tt);
275 return t;
279 void LoadTimeformatSettingsCache(StrBuf *Preference, long lvalue)
281 int *time_format_cache;
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;
292 void
293 InitModule_DATETIME
294 (void)
296 RegisterPreference("calhourformat", _("Time format"), PRF_INT, LoadTimeformatSettingsCache);