4 * Copyright (c) 1996-2010 by the citadel.org team
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "webserver.h"
25 extern locale_t *wc_locales;
28 typedef unsigned char byte;
30 #define FALSE 0 /**< no. */
31 #define TRUE 1 /**< yes. */
34 * Wrapper around strftime() or strftime_l()
35 * depending upon how our build is configured.
37 * s String target buffer
38 * max Maximum size of string target buffer
39 * format strftime() format
42 size_t wc_strftime(char *s, size_t max, const char *format, const struct tm *tm)
47 if (wc_locales[WC->selected_language] == NULL) {
48 return strftime(s, max, format, tm);
51 return strftime_l(s, max, format, tm, wc_locales[WC->selected_language]);
54 return strftime(s, max, format, tm);
57 return strftime(s, max, format, tm);
64 * Format a date/time stamp for output
66 void webcit_fmt_date(char *buf, size_t siz, time_t thetime, int Format)
73 time_format = get_time_format_cached ();
74 today_timet = time(NULL);
75 localtime_r(&today_timet, &today_tm);
77 localtime_r(&thetime, &tm);
80 * DATEFMT_FULL: full display
81 * DATEFMT_BRIEF: if date == today, show only the time
82 * otherwise, for messages up to 6 months old,
83 * show the month and day, and the time
84 * older than 6 months, show only the date
85 * DATEFMT_RAWDATE: show full date, regardless of age
86 * DATEFMT_LOCALEDATE: show full date as prefered for the locale
91 if ((tm.tm_year == today_tm.tm_year)
92 &&(tm.tm_mon == today_tm.tm_mon)
93 &&(tm.tm_mday == today_tm.tm_mday)) {
94 if (time_format == WC_TIMEFORMAT_24)
95 wc_strftime(buf, siz, "%k:%M", &tm);
97 wc_strftime(buf, siz, "%l:%M%p", &tm);
99 else if (today_timet - thetime < 15552000) {
100 if (time_format == WC_TIMEFORMAT_24)
101 wc_strftime(buf, siz, "%b %d %k:%M", &tm);
103 wc_strftime(buf, siz, "%b %d %l:%M%p", &tm);
106 wc_strftime(buf, siz, "%b %d %Y", &tm);
110 if (time_format == WC_TIMEFORMAT_24)
111 wc_strftime(buf, siz, "%a %b %d %Y %T %Z", &tm);
113 wc_strftime(buf, siz, "%a %b %d %Y %r %Z", &tm);
115 case DATEFMT_RAWDATE:
116 wc_strftime(buf, siz, "%a %b %d %Y", &tm);
118 case DATEFMT_LOCALEDATE:
119 wc_strftime(buf, siz, "%x", &tm);
126 * Try to guess whether the user will prefer 12 hour or 24 hour time based on the locale.
128 long guess_calhourformat(void) {
131 memset(&tm, 0, sizeof tm);
132 wc_strftime(buf, 64, "%X", &tm);
133 if (buf[strlen(buf)-1] == 'M') {
141 * learn the users timeformat preference.
143 int get_time_format_cached (void)
146 int *time_format_cache;
147 time_format_cache = &(WC->time_format_cache);
148 if (*time_format_cache == WC_TIMEFORMAT_NONE)
150 get_pref_long("calhourformat", &calhourformat, 99);
152 /* If we don't know the user's time format preference yet,
153 * make a guess based on the locale.
155 if (calhourformat == 99) {
156 calhourformat = guess_calhourformat();
159 /* Now set the preference */
160 if (calhourformat == 24)
161 *time_format_cache = WC_TIMEFORMAT_24;
163 *time_format_cache = WC_TIMEFORMAT_AMPM;
165 return *time_format_cache;
169 * Format TIME ONLY for output
170 * buf the output buffer
171 * thetime time to format into buf
173 void fmt_time(char *buf, size_t siz, time_t thetime)
179 time_format = get_time_format_cached ();
181 tm = localtime(&thetime);
188 if (time_format == WC_TIMEFORMAT_24) {
189 snprintf(buf, siz, "%d:%02d",
190 tm->tm_hour, tm->tm_min
194 snprintf(buf, siz, "%d:%02d%s",
195 hour, tm->tm_min, ((tm->tm_hour > 12) ? "pm" : "am")
204 * Break down the timestamp used in HTTP headers
205 * Should read rfc1123 and rfc850 dates OK
206 * FIXME won't read asctime
207 * Doesn't understand timezone, but we only should be using GMT/UTC anyway
209 time_t httpdate_to_timestamp(StrBuf *buf)
215 /** Skip day of week, to number */
216 for (c = ChrPtr(buf); *c != ' '; c++)
220 memset(&tt, 0, sizeof(tt));
222 /* Get day of month */
223 tt.tm_mday = atoi(c);
224 for (; *c != ' ' && *c != '-'; c++);
229 case 'A': /* April, August */
230 tt.tm_mon = (c[1] == 'p') ? 3 : 7;
232 case 'D': /* December */
235 case 'F': /* February */
238 case 'M': /* March, May */
239 tt.tm_mon = (c[2] == 'r') ? 2 : 4;
241 case 'J': /* January, June, July */
242 tt.tm_mon = (c[2] == 'n') ? ((c[1] == 'a') ? 0 : 5) : 6;
244 case 'N': /* November */
247 case 'O': /* October */
250 case 'S': /* September */
255 break; /* NOTREACHED */
261 tt.tm_year = atoi(c);
262 for (; *c != ' '; c++);
264 if (tt.tm_year >= 1900)
268 tt.tm_hour = atoi(c);
269 for (; *c != ':'; c++);
274 for (; *c != ':'; c++);
279 for (; *c && *c != ' '; c++);
281 /* Got everything; let's go. The global 'timezone' variable contains the
282 * local timezone's offset from UTC, in seconds, so we apply that to tm_sec.
283 * This produces an illegal value for tm_sec, but mktime() will normalize
284 * it for us. This eliminates the need to temporarily switch the environment
285 * variable TZ to UTC, which is good because it fails to switch back on
289 tt.tm_sec = tt.tm_sec - (int)timezone;
295 void LoadTimeformatSettingsCache(StrBuf *Preference, long lvalue)
297 int *time_format_cache;
299 time_format_cache = &(WC->time_format_cache);
301 *time_format_cache = WC_TIMEFORMAT_24;
303 *time_format_cache = WC_TIMEFORMAT_AMPM;
312 RegisterPreference("calhourformat", _("Time format"), PRF_INT, LoadTimeformatSettingsCache);