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);
34 else { /* TODO: this gives empty strings on debian. */
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 brief)
57 time_format = get_time_format_cached ();
58 today_timet = time(NULL);
59 localtime_r(&today_timet, &today_tm);
61 localtime_r(&thetime, &tm);
65 /* If date == today, show only the time */
66 if ((tm.tm_year == today_tm.tm_year)
67 &&(tm.tm_mon == today_tm.tm_mon)
68 &&(tm.tm_mday == today_tm.tm_mday)) {
69 if (time_format == WC_TIMEFORMAT_24)
70 wc_strftime(buf, 32, "%k:%M", &tm);
72 wc_strftime(buf, 32, "%l:%M%p", &tm);
74 /* Otherwise, for messages up to 6 months old, show the month and day, and the time */
75 else if (today_timet - thetime < 15552000) {
76 if (time_format == WC_TIMEFORMAT_24)
77 wc_strftime(buf, 32, "%b %d %k:%M", &tm);
79 wc_strftime(buf, 32, "%b %d %l:%M%p", &tm);
81 /* older than 6 months, show only the date */
83 wc_strftime(buf, 32, "%b %d %Y", &tm);
87 if (time_format == WC_TIMEFORMAT_24)
88 wc_strftime(buf, 32, "%a %b %d %Y %T %Z", &tm);
90 wc_strftime(buf, 32, "%a %b %d %Y %r %Z", &tm);
96 * learn the users timeformat preference.
98 int get_time_format_cached (void)
101 int *time_format_cache;
102 time_format_cache = &(WC->time_format_cache);
103 if (*time_format_cache == WC_TIMEFORMAT_NONE)
105 get_pref_long("calhourformat", &calhourformat, 24);
106 if (calhourformat == 24)
107 *time_format_cache = WC_TIMEFORMAT_24;
109 *time_format_cache = WC_TIMEFORMAT_AMPM;
111 return *time_format_cache;
115 * Format TIME ONLY for output
116 * buf the output buffer
117 * thetime time to format into buf
119 void fmt_time(char *buf, time_t thetime)
125 time_format = get_time_format_cached ();
127 tm = localtime(&thetime);
134 if (time_format == WC_TIMEFORMAT_24) {
135 sprintf(buf, "%2d:%02d",
136 tm->tm_hour, tm->tm_min
140 sprintf(buf, "%d:%02d%s",
141 hour, tm->tm_min, ((tm->tm_hour > 12) ? "pm" : "am")
150 * Break down the timestamp used in HTTP headers
151 * Should read rfc1123 and rfc850 dates OK
152 * FIXME won't read asctime
153 * Doesn't understand timezone, but we only should be using GMT/UTC anyway
155 time_t httpdate_to_timestamp(StrBuf *buf)
161 /** Skip day of week, to number */
162 for (c = ChrPtr(buf); *c != ' '; c++)
166 memset(&tt, 0, sizeof(tt));
168 /* Get day of month */
169 tt.tm_mday = atoi(c);
170 for (; *c != ' ' && *c != '-'; c++);
175 case 'A': /* April, August */
176 tt.tm_mon = (c[1] == 'p') ? 3 : 7;
178 case 'D': /* December */
181 case 'F': /* February */
184 case 'M': /* March, May */
185 tt.tm_mon = (c[2] == 'r') ? 2 : 4;
187 case 'J': /* January, June, July */
188 tt.tm_mon = (c[2] == 'n') ? ((c[1] == 'a') ? 0 : 5) : 6;
190 case 'N': /* November */
193 case 'O': /* October */
196 case 'S': /* September */
201 break; /* NOTREACHED */
207 tt.tm_year = atoi(c);
208 for (; *c != ' '; c++);
210 if (tt.tm_year >= 1900)
214 tt.tm_hour = atoi(c);
215 for (; *c != ':'; c++);
220 for (; *c != ':'; c++);
225 for (; *c && *c != ' '; c++);
227 /* Got everything; let's go. The global 'timezone' variable contains the
228 * local timezone's offset from UTC, in seconds, so we apply that to tm_sec.
229 * This produces an illegal value for tm_sec, but mktime() will normalize
230 * it for us. This eliminates the need to temporarily switch the environment
231 * variable TZ to UTC, which is good because it fails to switch back on
235 tt.tm_sec = tt.tm_sec - (int)timezone;
241 void LoadTimeformatSettingsCache(StrBuf *Preference, long lvalue)
243 int *time_format_cache;
245 time_format_cache = &(WC->time_format_cache);
247 *time_format_cache = WC_TIMEFORMAT_24;
249 *time_format_cache = WC_TIMEFORMAT_AMPM;
258 RegisterPreference(HKEY("calhourformat"), _("Time format"), PRF_INT, LoadTimeformatSettingsCache);