8 typedef unsigned char byte;
10 #define FALSE 0 /**< no. */
11 #define TRUE 1 /**< yes. */
14 * Wrapper around strftime() or strftime_l()
15 * depending upon how our build is configured.
17 * s String target buffer
18 * max Maximum size of string target buffer
19 * format strftime() format
22 size_t wc_strftime(char *s, size_t max, const char *format, const struct tm *tm)
25 if (wc_locales[WC->selected_language] == NULL) {
26 return strftime(s, max, format, tm);
28 else { // TODO: this gives empty strings on debian.
29 return strftime_l(s, max, format, tm, wc_locales[WC->selected_language]);
32 return strftime(s, max, format, tm);
38 * Format a date/time stamp for output
40 void webcit_fmt_date(char *buf, time_t thetime, int brief)
47 time_format = get_time_format_cached ();
48 today_timet = time(NULL);
49 localtime_r(&today_timet, &today_tm);
51 localtime_r(&thetime, &tm);
55 /* If date == today, show only the time */
56 if ((tm.tm_year == today_tm.tm_year)
57 &&(tm.tm_mon == today_tm.tm_mon)
58 &&(tm.tm_mday == today_tm.tm_mday)) {
59 if (time_format == WC_TIMEFORMAT_24)
60 wc_strftime(buf, 32, "%k:%M", &tm);
62 wc_strftime(buf, 32, "%l:%M%p", &tm);
64 /* Otherwise, for messages up to 6 months old, show the month and day, and the time */
65 else if (today_timet - thetime < 15552000) {
66 if (time_format == WC_TIMEFORMAT_24)
67 wc_strftime(buf, 32, "%b %d %k:%M", &tm);
69 wc_strftime(buf, 32, "%b %d %l:%M%p", &tm);
71 /* older than 6 months, show only the date */
73 wc_strftime(buf, 32, "%b %d %Y", &tm);
77 if (time_format == WC_TIMEFORMAT_24)
78 wc_strftime(buf, 32, "%a %b %d %Y %T %Z", &tm);
80 wc_strftime(buf, 32, "%a %b %d %Y %r %Z", &tm);
86 * learn the users timeformat preference.
88 int get_time_format_cached (void)
90 char calhourformat[16];
91 int *time_format_cache;
92 time_format_cache = &(WC->time_format_cache);
93 if (*time_format_cache == WC_TIMEFORMAT_NONE)
95 get_preference("calhourformat", calhourformat, sizeof calhourformat);
96 if (!strcasecmp(calhourformat, "24"))
97 *time_format_cache = WC_TIMEFORMAT_24;
99 *time_format_cache = WC_TIMEFORMAT_AMPM;
101 return *time_format_cache;
105 * Format TIME ONLY for output
106 * buf the output buffer
107 * thetime time to format into buf
109 void fmt_time(char *buf, time_t thetime)
115 time_format = get_time_format_cached ();
117 tm = localtime(&thetime);
124 if (time_format == WC_TIMEFORMAT_24) {
125 sprintf(buf, "%2d:%02d",
126 tm->tm_hour, tm->tm_min
130 sprintf(buf, "%d:%02d%s",
131 hour, tm->tm_min, ((tm->tm_hour > 12) ? "pm" : "am")
140 * Break down the timestamp used in HTTP headers
141 * Should read rfc1123 and rfc850 dates OK
142 * FIXME won't read asctime
143 * Doesn't understand timezone, but we only should be using GMT/UTC anyway
145 time_t httpdate_to_timestamp(char *buf)
151 /** Skip day of week, to number */
152 for (c = buf; *c != ' '; c++)
156 /* Get day of month */
157 tt.tm_mday = atoi(c);
158 for (; *c != ' ' && *c != '-'; c++);
163 case 'A': /* April, August */
164 tt.tm_mon = (c[1] == 'p') ? 3 : 7;
166 case 'D': /* December */
169 case 'F': /* February */
172 case 'M': /* March, May */
173 tt.tm_mon = (c[2] == 'r') ? 2 : 4;
175 case 'J': /* January, June, July */
176 tt.tm_mon = (c[2] == 'n') ? ((c[1] == 'a') ? 0 : 5) : 6;
178 case 'N': /* November */
181 case 'O': /* October */
184 case 'S': /* September */
189 break; /* NOTREACHED */
195 tt.tm_year = atoi(c);
196 for (; *c != ' '; c++);
198 if (tt.tm_year >= 1900)
202 tt.tm_hour = atoi(c);
203 for (; *c != ':'; c++);
208 for (; *c != ':'; c++);
213 for (; *c && *c != ' '; c++);
215 /* Got everything; let's go. The global 'timezone' variable contains the
216 * local timezone's offset from UTC, in seconds, so we apply that to tm_sec.
217 * This produces an illegal value for tm_sec, but mktime() will normalize
218 * it for us. This eliminates the need to temporarily switch the environment
219 * variable TZ to UTC, which is good because it fails to switch back on
223 tt.tm_sec = tt.tm_sec - (int)timezone;