* fix another off by one while retrieving the iterator counter params
[citadel.git] / webcit / fmt_date.c
blobff895f20ec6cdaba4b8d0a0228215506a3021c91
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 { /* TODO: this gives empty strings on debian. */
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 brief)
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);
63 if (brief) {
65 /* If date == today, show only the time */
66 if ((tm.tm_year == today_tm.tm_year)
67 &&(tm.tm_mon == today_tm.tm_mon)
68 &&(tm.tm_mday == today_tm.tm_mday)) {
69 if (time_format == WC_TIMEFORMAT_24)
70 wc_strftime(buf, 32, "%k:%M", &tm);
71 else
72 wc_strftime(buf, 32, "%l:%M%p", &tm);
74 /* Otherwise, for messages up to 6 months old, show the month and day, and the time */
75 else if (today_timet - thetime < 15552000) {
76 if (time_format == WC_TIMEFORMAT_24)
77 wc_strftime(buf, 32, "%b %d %k:%M", &tm);
78 else
79 wc_strftime(buf, 32, "%b %d %l:%M%p", &tm);
81 /* older than 6 months, show only the date */
82 else {
83 wc_strftime(buf, 32, "%b %d %Y", &tm);
86 else {
87 if (time_format == WC_TIMEFORMAT_24)
88 wc_strftime(buf, 32, "%a %b %d %Y %T %Z", &tm);
89 else
90 wc_strftime(buf, 32, "%a %b %d %Y %r %Z", &tm);
95 /*
96 * Try to guess whether the user will prefer 12 hour or 24 hour time based on the locale.
98 long guess_calhourformat(void) {
99 char buf[32];
100 struct tm tm;
101 memset(&tm, 0, sizeof tm);
102 wc_strftime(buf, 32, "%X", &tm);
103 if (buf[strlen(buf)-1] == 'M') {
104 return 12;
106 return 24;
111 * learn the users timeformat preference.
113 int get_time_format_cached (void)
115 long calhourformat;
116 int *time_format_cache;
117 time_format_cache = &(WC->time_format_cache);
118 if (*time_format_cache == WC_TIMEFORMAT_NONE)
120 get_pref_long("calhourformat", &calhourformat, 99);
122 /* If we don't know the user's time format preference yet,
123 * make a guess based on the locale.
125 if (calhourformat == 99) {
126 calhourformat = guess_calhourformat();
129 /* Now set the preference */
130 if (calhourformat == 24)
131 *time_format_cache = WC_TIMEFORMAT_24;
132 else
133 *time_format_cache = WC_TIMEFORMAT_AMPM;
135 return *time_format_cache;
139 * Format TIME ONLY for output
140 * buf the output buffer
141 * thetime time to format into buf
143 void fmt_time(char *buf, time_t thetime)
145 struct tm *tm;
146 int hour;
147 int time_format;
149 time_format = get_time_format_cached ();
150 buf[0] = 0;
151 tm = localtime(&thetime);
152 hour = tm->tm_hour;
153 if (hour == 0)
154 hour = 12;
155 else if (hour > 12)
156 hour = hour - 12;
158 if (time_format == WC_TIMEFORMAT_24) {
159 sprintf(buf, "%d:%02d",
160 tm->tm_hour, tm->tm_min
163 else {
164 sprintf(buf, "%d:%02d%s",
165 hour, tm->tm_min, ((tm->tm_hour > 12) ? "pm" : "am")
174 * Break down the timestamp used in HTTP headers
175 * Should read rfc1123 and rfc850 dates OK
176 * FIXME won't read asctime
177 * Doesn't understand timezone, but we only should be using GMT/UTC anyway
179 time_t httpdate_to_timestamp(StrBuf *buf)
181 time_t t = 0;
182 struct tm tt;
183 const char *c;
185 /** Skip day of week, to number */
186 for (c = ChrPtr(buf); *c != ' '; c++)
188 c++;
190 memset(&tt, 0, sizeof(tt));
192 /* Get day of month */
193 tt.tm_mday = atoi(c);
194 for (; *c != ' ' && *c != '-'; c++);
195 c++;
197 /* Get month */
198 switch (*c) {
199 case 'A': /* April, August */
200 tt.tm_mon = (c[1] == 'p') ? 3 : 7;
201 break;
202 case 'D': /* December */
203 tt.tm_mon = 11;
204 break;
205 case 'F': /* February */
206 tt.tm_mon = 1;
207 break;
208 case 'M': /* March, May */
209 tt.tm_mon = (c[2] == 'r') ? 2 : 4;
210 break;
211 case 'J': /* January, June, July */
212 tt.tm_mon = (c[2] == 'n') ? ((c[1] == 'a') ? 0 : 5) : 6;
213 break;
214 case 'N': /* November */
215 tt.tm_mon = 10;
216 break;
217 case 'O': /* October */
218 tt.tm_mon = 9;
219 break;
220 case 'S': /* September */
221 tt.tm_mon = 8;
222 break;
223 default:
224 return 42;
225 break; /* NOTREACHED */
227 c += 4;
229 tt.tm_year = 0;
230 /* Get year */
231 tt.tm_year = atoi(c);
232 for (; *c != ' '; c++);
233 c++;
234 if (tt.tm_year >= 1900)
235 tt.tm_year -= 1900;
237 /* Get hour */
238 tt.tm_hour = atoi(c);
239 for (; *c != ':'; c++);
240 c++;
242 /* Get minute */
243 tt.tm_min = atoi(c);
244 for (; *c != ':'; c++);
245 c++;
247 /* Get second */
248 tt.tm_sec = atoi(c);
249 for (; *c && *c != ' '; c++);
251 /* Got everything; let's go. The global 'timezone' variable contains the
252 * local timezone's offset from UTC, in seconds, so we apply that to tm_sec.
253 * This produces an illegal value for tm_sec, but mktime() will normalize
254 * it for us. This eliminates the need to temporarily switch the environment
255 * variable TZ to UTC, which is good because it fails to switch back on
256 * some systems.
258 tzset();
259 tt.tm_sec = tt.tm_sec - (int)timezone;
260 t = mktime(&tt);
261 return t;
265 void LoadTimeformatSettingsCache(StrBuf *Preference, long lvalue)
267 int *time_format_cache;
269 time_format_cache = &(WC->time_format_cache);
270 if (lvalue == 24)
271 *time_format_cache = WC_TIMEFORMAT_24;
272 else
273 *time_format_cache = WC_TIMEFORMAT_AMPM;
278 void
279 InitModule_DATETIME
280 (void)
282 RegisterPreference(HKEY("calhourformat"), _("Time format"), PRF_INT, LoadTimeformatSettingsCache);