9 extern locale_t wc_locales[];
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
26 size_t wc_strftime(char *s, size_t max, const char *format, const struct tm *tm)
31 if (wc_locales[WC->selected_language] == NULL) {
32 return strftime(s, max, format, tm);
35 return strftime_l(s, max, format, tm, wc_locales[WC->selected_language]);
38 return strftime(s, max, format, tm);
41 return strftime(s, max, format, tm);
48 * Format a date/time stamp for output
50 void webcit_fmt_date(char *buf, time_t thetime, int 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);
64 * DATEFMT_FULL: full display
65 * DATEFMT_BRIEF: if date == today, show only the time
66 * otherwise, for messages up to 6 months old,
67 * show the month and day, and the time
68 * older than 6 months, show only the date
69 * DATEFMT_RAWDATE: show full date, regardless of age
74 if ((tm.tm_year == today_tm.tm_year)
75 &&(tm.tm_mon == today_tm.tm_mon)
76 &&(tm.tm_mday == today_tm.tm_mday)) {
77 if (time_format == WC_TIMEFORMAT_24)
78 wc_strftime(buf, 32, "%k:%M", &tm);
80 wc_strftime(buf, 32, "%l:%M%p", &tm);
82 else if (today_timet - thetime < 15552000) {
83 if (time_format == WC_TIMEFORMAT_24)
84 wc_strftime(buf, 32, "%b %d %k:%M", &tm);
86 wc_strftime(buf, 32, "%b %d %l:%M%p", &tm);
89 wc_strftime(buf, 32, "%b %d %Y", &tm);
93 if (time_format == WC_TIMEFORMAT_24)
94 wc_strftime(buf, 32, "%a %b %d %Y %T %Z", &tm);
96 wc_strftime(buf, 32, "%a %b %d %Y %r %Z", &tm);
99 wc_strftime(buf, 32, "%a %b %d %Y", &tm);
106 * Try to guess whether the user will prefer 12 hour or 24 hour time based on the locale.
108 long guess_calhourformat(void) {
111 memset(&tm, 0, sizeof tm);
112 wc_strftime(buf, 32, "%X", &tm);
113 if (buf[strlen(buf)-1] == 'M') {
121 * learn the users timeformat preference.
123 int get_time_format_cached (void)
126 int *time_format_cache;
127 time_format_cache = &(WC->time_format_cache);
128 if (*time_format_cache == WC_TIMEFORMAT_NONE)
130 get_pref_long("calhourformat", &calhourformat, 99);
132 /* If we don't know the user's time format preference yet,
133 * make a guess based on the locale.
135 if (calhourformat == 99) {
136 calhourformat = guess_calhourformat();
139 /* Now set the preference */
140 if (calhourformat == 24)
141 *time_format_cache = WC_TIMEFORMAT_24;
143 *time_format_cache = WC_TIMEFORMAT_AMPM;
145 return *time_format_cache;
149 * Format TIME ONLY for output
150 * buf the output buffer
151 * thetime time to format into buf
153 void fmt_time(char *buf, time_t thetime)
159 time_format = get_time_format_cached ();
161 tm = localtime(&thetime);
168 if (time_format == WC_TIMEFORMAT_24) {
169 sprintf(buf, "%d:%02d",
170 tm->tm_hour, tm->tm_min
174 sprintf(buf, "%d:%02d%s",
175 hour, tm->tm_min, ((tm->tm_hour > 12) ? "pm" : "am")
184 * Break down the timestamp used in HTTP headers
185 * Should read rfc1123 and rfc850 dates OK
186 * FIXME won't read asctime
187 * Doesn't understand timezone, but we only should be using GMT/UTC anyway
189 time_t httpdate_to_timestamp(StrBuf *buf)
195 /** Skip day of week, to number */
196 for (c = ChrPtr(buf); *c != ' '; c++)
200 memset(&tt, 0, sizeof(tt));
202 /* Get day of month */
203 tt.tm_mday = atoi(c);
204 for (; *c != ' ' && *c != '-'; c++);
209 case 'A': /* April, August */
210 tt.tm_mon = (c[1] == 'p') ? 3 : 7;
212 case 'D': /* December */
215 case 'F': /* February */
218 case 'M': /* March, May */
219 tt.tm_mon = (c[2] == 'r') ? 2 : 4;
221 case 'J': /* January, June, July */
222 tt.tm_mon = (c[2] == 'n') ? ((c[1] == 'a') ? 0 : 5) : 6;
224 case 'N': /* November */
227 case 'O': /* October */
230 case 'S': /* September */
235 break; /* NOTREACHED */
241 tt.tm_year = atoi(c);
242 for (; *c != ' '; c++);
244 if (tt.tm_year >= 1900)
248 tt.tm_hour = atoi(c);
249 for (; *c != ':'; c++);
254 for (; *c != ':'; c++);
259 for (; *c && *c != ' '; c++);
261 /* Got everything; let's go. The global 'timezone' variable contains the
262 * local timezone's offset from UTC, in seconds, so we apply that to tm_sec.
263 * This produces an illegal value for tm_sec, but mktime() will normalize
264 * it for us. This eliminates the need to temporarily switch the environment
265 * variable TZ to UTC, which is good because it fails to switch back on
269 tt.tm_sec = tt.tm_sec - (int)timezone;
275 void LoadTimeformatSettingsCache(StrBuf *Preference, long lvalue)
277 int *time_format_cache;
279 time_format_cache = &(WC->time_format_cache);
281 *time_format_cache = WC_TIMEFORMAT_24;
283 *time_format_cache = WC_TIMEFORMAT_AMPM;
292 RegisterPreference("calhourformat", _("Time format"), PRF_INT, LoadTimeformatSettingsCache);