2 * Copyright (c) 1996-2010 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 as
6 * published by the Free Software Foundation; either version 3 of the
7 * License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 #include "webserver.h"
23 extern locale_t *wc_locales;
26 typedef unsigned char byte;
28 #define FALSE 0 /**< no. */
29 #define TRUE 1 /**< yes. */
32 * Wrapper around strftime() or strftime_l()
33 * depending upon how our build is configured.
35 * s String target buffer
36 * max Maximum size of string target buffer
37 * format strftime() format
40 size_t wc_strftime(char *s, size_t max, const char *format, const struct tm *tm)
45 if (wc_locales[WC->selected_language] == NULL) {
46 return strftime(s, max, format, tm);
49 return strftime_l(s, max, format, tm, wc_locales[WC->selected_language]);
52 return strftime(s, max, format, tm);
55 return strftime(s, max, format, tm);
62 * Format a date/time stamp for output
64 void webcit_fmt_date(char *buf, size_t siz, time_t thetime, int Format)
71 time_format = get_time_format_cached ();
72 today_timet = time(NULL);
73 localtime_r(&today_timet, &today_tm);
75 localtime_r(&thetime, &tm);
78 * DATEFMT_FULL: full display
79 * DATEFMT_BRIEF: if date == today, show only the time
80 * otherwise, for messages up to 6 months old,
81 * show the month and day, and the time
82 * older than 6 months, show only the date
83 * DATEFMT_RAWDATE: show full date, regardless of age
84 * DATEFMT_LOCALEDATE: show full date as prefered for the locale
89 if ((tm.tm_year == today_tm.tm_year)
90 &&(tm.tm_mon == today_tm.tm_mon)
91 &&(tm.tm_mday == today_tm.tm_mday)) {
92 if (time_format == WC_TIMEFORMAT_24)
93 wc_strftime(buf, siz, "%k:%M", &tm);
95 wc_strftime(buf, siz, "%l:%M%p", &tm);
97 else if (today_timet - thetime < 15552000) {
98 if (time_format == WC_TIMEFORMAT_24)
99 wc_strftime(buf, siz, "%b %d %k:%M", &tm);
101 wc_strftime(buf, siz, "%b %d %l:%M%p", &tm);
104 wc_strftime(buf, siz, "%b %d %Y", &tm);
108 if (time_format == WC_TIMEFORMAT_24)
109 wc_strftime(buf, siz, "%a %b %d %Y %T %Z", &tm);
111 wc_strftime(buf, siz, "%a %b %d %Y %r %Z", &tm);
113 case DATEFMT_RAWDATE:
114 wc_strftime(buf, siz, "%a %b %d %Y", &tm);
116 case DATEFMT_LOCALEDATE:
117 wc_strftime(buf, siz, "%x", &tm);
124 * Try to guess whether the user will prefer 12 hour or 24 hour time based on the locale.
126 long guess_calhourformat(void) {
129 memset(&tm, 0, sizeof tm);
130 wc_strftime(buf, 64, "%X", &tm);
131 if (buf[strlen(buf)-1] == 'M') {
139 * learn the users timeformat preference.
141 int get_time_format_cached (void)
144 int *time_format_cache;
145 time_format_cache = &(WC->time_format_cache);
146 if (*time_format_cache == WC_TIMEFORMAT_NONE)
148 get_pref_long("calhourformat", &calhourformat, 99);
150 /* If we don't know the user's time format preference yet,
151 * make a guess based on the locale.
153 if (calhourformat == 99) {
154 calhourformat = guess_calhourformat();
157 /* Now set the preference */
158 if (calhourformat == 24)
159 *time_format_cache = WC_TIMEFORMAT_24;
161 *time_format_cache = WC_TIMEFORMAT_AMPM;
163 return *time_format_cache;
167 * Format TIME ONLY for output
168 * buf the output buffer
169 * thetime time to format into buf
171 void fmt_time(char *buf, size_t siz, time_t thetime)
177 time_format = get_time_format_cached ();
179 tm = localtime(&thetime);
186 if (time_format == WC_TIMEFORMAT_24) {
187 snprintf(buf, siz, "%d:%02d",
188 tm->tm_hour, tm->tm_min
192 snprintf(buf, siz, "%d:%02d%s",
193 hour, tm->tm_min, ((tm->tm_hour > 12) ? "pm" : "am")
202 * Break down the timestamp used in HTTP headers
203 * Should read rfc1123 and rfc850 dates OK
204 * FIXME won't read asctime
205 * Doesn't understand timezone, but we only should be using GMT/UTC anyway
207 time_t httpdate_to_timestamp(StrBuf *buf)
213 /** Skip day of week, to number */
214 for (c = ChrPtr(buf); *c != ' '; c++)
218 memset(&tt, 0, sizeof(tt));
220 /* Get day of month */
221 tt.tm_mday = atoi(c);
222 for (; *c != ' ' && *c != '-'; c++);
227 case 'A': /* April, August */
228 tt.tm_mon = (c[1] == 'p') ? 3 : 7;
230 case 'D': /* December */
233 case 'F': /* February */
236 case 'M': /* March, May */
237 tt.tm_mon = (c[2] == 'r') ? 2 : 4;
239 case 'J': /* January, June, July */
240 tt.tm_mon = (c[2] == 'n') ? ((c[1] == 'a') ? 0 : 5) : 6;
242 case 'N': /* November */
245 case 'O': /* October */
248 case 'S': /* September */
253 break; /* NOTREACHED */
259 tt.tm_year = atoi(c);
260 for (; *c != ' '; c++);
262 if (tt.tm_year >= 1900)
266 tt.tm_hour = atoi(c);
267 for (; *c != ':'; c++);
272 for (; *c != ':'; c++);
277 for (; *c && *c != ' '; c++);
279 /* Got everything; let's go. The global 'timezone' variable contains the
280 * local timezone's offset from UTC, in seconds, so we apply that to tm_sec.
281 * This produces an illegal value for tm_sec, but mktime() will normalize
282 * it for us. This eliminates the need to temporarily switch the environment
283 * variable TZ to UTC, which is good because it fails to switch back on
287 tt.tm_sec = tt.tm_sec - (int)timezone;
293 void LoadTimeformatSettingsCache(StrBuf *Preference, long lvalue)
295 int *time_format_cache;
297 time_format_cache = &(WC->time_format_cache);
299 *time_format_cache = WC_TIMEFORMAT_24;
301 *time_format_cache = WC_TIMEFORMAT_AMPM;
310 RegisterPreference("calhourformat", _("Time format"), PRF_INT, LoadTimeformatSettingsCache);