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 long webcit_fmt_date(char *buf, size_t siz, time_t thetime, int Format)
66 time_format = get_time_format_cached ();
67 today_timet = time(NULL);
68 localtime_r(&today_timet, &today_tm);
70 localtime_r(&thetime, &tm);
73 * DATEFMT_FULL: full display
74 * DATEFMT_BRIEF: if date == today, show only the time
75 * otherwise, for messages up to 6 months old,
76 * show the month and day, and the time
77 * older than 6 months, show only the date
78 * DATEFMT_RAWDATE: show full date, regardless of age
79 * DATEFMT_LOCALEDATE: show full date as prefered for the locale
84 if ((tm.tm_year == today_tm.tm_year)
85 &&(tm.tm_mon == today_tm.tm_mon)
86 &&(tm.tm_mday == today_tm.tm_mday)) {
87 if (time_format == WC_TIMEFORMAT_24)
88 retlen = wc_strftime(buf, siz, "%k:%M", &tm);
90 retlen = wc_strftime(buf, siz, "%l:%M%p", &tm);
92 else if (today_timet - thetime < 15552000) {
93 if (time_format == WC_TIMEFORMAT_24)
94 retlen = wc_strftime(buf, siz, "%b %d %k:%M", &tm);
96 retlen = wc_strftime(buf, siz, "%b %d %l:%M%p", &tm);
99 retlen = wc_strftime(buf, siz, "%b %d %Y", &tm);
103 if (time_format == WC_TIMEFORMAT_24)
104 retlen = wc_strftime(buf, siz, "%a %b %d %Y %T %Z", &tm);
106 retlen = wc_strftime(buf, siz, "%a %b %d %Y %r %Z", &tm);
108 case DATEFMT_RAWDATE:
109 retlen = wc_strftime(buf, siz, "%a %b %d %Y", &tm);
111 case DATEFMT_LOCALEDATE:
112 retlen = wc_strftime(buf, siz, "%x", &tm);
120 * Try to guess whether the user will prefer 12 hour or 24 hour time based on the locale.
122 long guess_calhourformat(void) {
125 memset(&tm, 0, sizeof tm);
126 wc_strftime(buf, 64, "%X", &tm);
127 if (buf[strlen(buf)-1] == 'M') {
135 * learn the users timeformat preference.
137 int get_time_format_cached (void)
140 int *time_format_cache;
141 time_format_cache = &(WC->time_format_cache);
142 if (*time_format_cache == WC_TIMEFORMAT_NONE)
144 get_pref_long("calhourformat", &calhourformat, 99);
146 /* If we don't know the user's time format preference yet,
147 * make a guess based on the locale.
149 if (calhourformat == 99) {
150 calhourformat = guess_calhourformat();
153 /* Now set the preference */
154 if (calhourformat == 24)
155 *time_format_cache = WC_TIMEFORMAT_24;
157 *time_format_cache = WC_TIMEFORMAT_AMPM;
159 return *time_format_cache;
163 * Format TIME ONLY for output
164 * buf the output buffer
165 * thetime time to format into buf
167 void fmt_time(char *buf, size_t siz, time_t thetime)
173 time_format = get_time_format_cached ();
175 tm = localtime(&thetime);
182 if (time_format == WC_TIMEFORMAT_24) {
183 snprintf(buf, siz, "%d:%02d",
184 tm->tm_hour, tm->tm_min
188 snprintf(buf, siz, "%d:%02d%s",
189 hour, tm->tm_min, ((tm->tm_hour > 12) ? "pm" : "am")
198 * Break down the timestamp used in HTTP headers
199 * Should read rfc1123 and rfc850 dates OK
200 * FIXME won't read asctime
201 * Doesn't understand timezone, but we only should be using GMT/UTC anyway
203 time_t httpdate_to_timestamp(StrBuf *buf)
209 /** Skip day of week, to number */
210 for (c = ChrPtr(buf); *c != ' '; c++)
214 memset(&tt, 0, sizeof(tt));
216 /* Get day of month */
217 tt.tm_mday = atoi(c);
218 for (; *c != ' ' && *c != '-'; c++);
223 case 'A': /* April, August */
224 tt.tm_mon = (c[1] == 'p') ? 3 : 7;
226 case 'D': /* December */
229 case 'F': /* February */
232 case 'M': /* March, May */
233 tt.tm_mon = (c[2] == 'r') ? 2 : 4;
235 case 'J': /* January, June, July */
236 tt.tm_mon = (c[2] == 'n') ? ((c[1] == 'a') ? 0 : 5) : 6;
238 case 'N': /* November */
241 case 'O': /* October */
244 case 'S': /* September */
249 break; /* NOTREACHED */
255 tt.tm_year = atoi(c);
256 for (; *c != ' '; c++);
258 if (tt.tm_year >= 1900)
262 tt.tm_hour = atoi(c);
263 for (; *c != ':'; c++);
268 for (; *c != ':'; c++);
273 for (; *c && *c != ' '; c++);
275 /* Got everything; let's go. The global 'timezone' variable contains the
276 * local timezone's offset from UTC, in seconds, so we apply that to tm_sec.
277 * This produces an illegal value for tm_sec, but mktime() will normalize
278 * it for us. This eliminates the need to temporarily switch the environment
279 * variable TZ to UTC, which is good because it fails to switch back on
284 tt.tm_sec = tt.tm_sec - tt.tm_gmtoff;
286 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);