2 * Copyright (c) 1996-2012 by the citadel.org team
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, version 3.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
14 #include "webserver.h"
17 extern locale_t *wc_locales;
20 typedef unsigned char byte;
22 #define FALSE 0 /**< no. */
23 #define TRUE 1 /**< yes. */
26 * Wrapper around strftime() or strftime_l()
27 * depending upon how our build is configured.
29 * s String target buffer
30 * max Maximum size of string target buffer
31 * format strftime() format
34 size_t wc_strftime(char *s, size_t max, const char *format, const struct tm *tm)
39 if (wc_locales[WC->selected_language] == NULL) {
40 return strftime(s, max, format, tm);
43 return strftime_l(s, max, format, tm, wc_locales[WC->selected_language]);
46 return strftime(s, max, format, tm);
49 return strftime(s, max, format, tm);
56 * Format a date/time stamp for output
58 void webcit_fmt_date(char *buf, size_t siz, time_t thetime, int Format)
65 time_format = get_time_format_cached ();
66 today_timet = time(NULL);
67 localtime_r(&today_timet, &today_tm);
69 localtime_r(&thetime, &tm);
72 * DATEFMT_FULL: full display
73 * DATEFMT_BRIEF: if date == today, show only the time
74 * otherwise, for messages up to 6 months old,
75 * show the month and day, and the time
76 * older than 6 months, show only the date
77 * DATEFMT_RAWDATE: show full date, regardless of age
78 * DATEFMT_LOCALEDATE: show full date as prefered for the locale
83 if ((tm.tm_year == today_tm.tm_year)
84 &&(tm.tm_mon == today_tm.tm_mon)
85 &&(tm.tm_mday == today_tm.tm_mday)) {
86 if (time_format == WC_TIMEFORMAT_24)
87 wc_strftime(buf, siz, "%k:%M", &tm);
89 wc_strftime(buf, siz, "%l:%M%p", &tm);
91 else if (today_timet - thetime < 15552000) {
92 if (time_format == WC_TIMEFORMAT_24)
93 wc_strftime(buf, siz, "%b %d %k:%M", &tm);
95 wc_strftime(buf, siz, "%b %d %l:%M%p", &tm);
98 wc_strftime(buf, siz, "%b %d %Y", &tm);
102 if (time_format == WC_TIMEFORMAT_24)
103 wc_strftime(buf, siz, "%a %b %d %Y %T %Z", &tm);
105 wc_strftime(buf, siz, "%a %b %d %Y %r %Z", &tm);
107 case DATEFMT_RAWDATE:
108 wc_strftime(buf, siz, "%a %b %d %Y", &tm);
110 case DATEFMT_LOCALEDATE:
111 wc_strftime(buf, siz, "%x", &tm);
118 * Try to guess whether the user will prefer 12 hour or 24 hour time based on the locale.
120 long guess_calhourformat(void) {
123 memset(&tm, 0, sizeof tm);
124 wc_strftime(buf, 64, "%X", &tm);
125 if (buf[strlen(buf)-1] == 'M') {
133 * learn the users timeformat preference.
135 int get_time_format_cached (void)
138 int *time_format_cache;
139 time_format_cache = &(WC->time_format_cache);
140 if (*time_format_cache == WC_TIMEFORMAT_NONE)
142 get_pref_long("calhourformat", &calhourformat, 99);
144 /* If we don't know the user's time format preference yet,
145 * make a guess based on the locale.
147 if (calhourformat == 99) {
148 calhourformat = guess_calhourformat();
151 /* Now set the preference */
152 if (calhourformat == 24)
153 *time_format_cache = WC_TIMEFORMAT_24;
155 *time_format_cache = WC_TIMEFORMAT_AMPM;
157 return *time_format_cache;
161 * Format TIME ONLY for output
162 * buf the output buffer
163 * thetime time to format into buf
165 void fmt_time(char *buf, size_t siz, time_t thetime)
171 time_format = get_time_format_cached ();
173 tm = localtime(&thetime);
180 if (time_format == WC_TIMEFORMAT_24) {
181 snprintf(buf, siz, "%d:%02d",
182 tm->tm_hour, tm->tm_min
186 snprintf(buf, siz, "%d:%02d%s",
187 hour, tm->tm_min, ((tm->tm_hour > 12) ? "pm" : "am")
196 * Break down the timestamp used in HTTP headers
197 * Should read rfc1123 and rfc850 dates OK
198 * FIXME won't read asctime
199 * Doesn't understand timezone, but we only should be using GMT/UTC anyway
201 time_t httpdate_to_timestamp(StrBuf *buf)
207 /** Skip day of week, to number */
208 for (c = ChrPtr(buf); *c != ' '; c++)
212 memset(&tt, 0, sizeof(tt));
214 /* Get day of month */
215 tt.tm_mday = atoi(c);
216 for (; *c != ' ' && *c != '-'; c++);
221 case 'A': /* April, August */
222 tt.tm_mon = (c[1] == 'p') ? 3 : 7;
224 case 'D': /* December */
227 case 'F': /* February */
230 case 'M': /* March, May */
231 tt.tm_mon = (c[2] == 'r') ? 2 : 4;
233 case 'J': /* January, June, July */
234 tt.tm_mon = (c[2] == 'n') ? ((c[1] == 'a') ? 0 : 5) : 6;
236 case 'N': /* November */
239 case 'O': /* October */
242 case 'S': /* September */
247 break; /* NOTREACHED */
253 tt.tm_year = atoi(c);
254 for (; *c != ' '; c++);
256 if (tt.tm_year >= 1900)
260 tt.tm_hour = atoi(c);
261 for (; *c != ':'; c++);
266 for (; *c != ':'; c++);
271 for (; *c && *c != ' '; c++);
273 /* Got everything; let's go. The global 'timezone' variable contains the
274 * local timezone's offset from UTC, in seconds, so we apply that to tm_sec.
275 * This produces an illegal value for tm_sec, but mktime() will normalize
276 * it for us. This eliminates the need to temporarily switch the environment
277 * variable TZ to UTC, which is good because it fails to switch back on
281 tt.tm_sec = tt.tm_sec - (int)timezone;
287 void LoadTimeformatSettingsCache(StrBuf *Preference, long lvalue)
289 int *time_format_cache;
291 time_format_cache = &(WC->time_format_cache);
293 *time_format_cache = WC_TIMEFORMAT_24;
295 *time_format_cache = WC_TIMEFORMAT_AMPM;
304 RegisterPreference("calhourformat", _("Time format"), PRF_INT, LoadTimeformatSettingsCache);