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)
26 if (wc_locales[WC->selected_language] == NULL) {
27 return strftime(s, max, format, tm);
29 else { // TODO: this gives empty strings on debian.
30 return strftime_l(s, max, format, tm, wc_locales[WC->selected_language]);
33 return strftime(s, max, format, tm);
39 * Format a date/time stamp for output
41 void webcit_fmt_date(char *buf, time_t thetime, int brief)
48 time_format = get_time_format_cached ();
49 today_timet = time(NULL);
50 localtime_r(&today_timet, &today_tm);
52 localtime_r(&thetime, &tm);
56 /* If date == today, show only the time */
57 if ((tm.tm_year == today_tm.tm_year)
58 &&(tm.tm_mon == today_tm.tm_mon)
59 &&(tm.tm_mday == today_tm.tm_mday)) {
60 if (time_format == WC_TIMEFORMAT_24)
61 wc_strftime(buf, 32, "%k:%M", &tm);
63 wc_strftime(buf, 32, "%l:%M%p", &tm);
65 /* Otherwise, for messages up to 6 months old, show the month and day, and the time */
66 else if (today_timet - thetime < 15552000) {
67 if (time_format == WC_TIMEFORMAT_24)
68 wc_strftime(buf, 32, "%b %d %k:%M", &tm);
70 wc_strftime(buf, 32, "%b %d %l:%M%p", &tm);
72 /* older than 6 months, show only the date */
74 wc_strftime(buf, 32, "%b %d %Y", &tm);
78 if (time_format == WC_TIMEFORMAT_24)
79 wc_strftime(buf, 32, "%a %b %d %Y %T %Z", &tm);
81 wc_strftime(buf, 32, "%a %b %d %Y %r %Z", &tm);
87 * learn the users timeformat preference.
89 int get_time_format_cached (void)
91 char calhourformat[16];
92 int *time_format_cache;
93 time_format_cache = &(WC->time_format_cache);
94 if (*time_format_cache == WC_TIMEFORMAT_NONE)
96 get_preference("calhourformat", calhourformat, sizeof calhourformat);
97 if (!strcasecmp(calhourformat, "24"))
98 *time_format_cache = WC_TIMEFORMAT_24;
100 *time_format_cache = WC_TIMEFORMAT_AMPM;
102 return *time_format_cache;
106 * Format TIME ONLY for output
107 * buf the output buffer
108 * thetime time to format into buf
110 void fmt_time(char *buf, time_t thetime)
116 time_format = get_time_format_cached ();
118 tm = localtime(&thetime);
125 if (time_format == WC_TIMEFORMAT_24) {
126 sprintf(buf, "%2d:%02d",
127 tm->tm_hour, tm->tm_min
131 sprintf(buf, "%d:%02d%s",
132 hour, tm->tm_min, ((tm->tm_hour > 12) ? "pm" : "am")
141 * Break down the timestamp used in HTTP headers
142 * Should read rfc1123 and rfc850 dates OK
143 * FIXME won't read asctime
144 * Doesn't understand timezone, but we only should be using GMT/UTC anyway
146 time_t httpdate_to_timestamp(char *buf)
152 /** Skip day of week, to number */
153 for (c = buf; *c != ' '; c++)
157 /* Get day of month */
158 tt.tm_mday = atoi(c);
159 for (; *c != ' ' && *c != '-'; c++);
164 case 'A': /* April, August */
165 tt.tm_mon = (c[1] == 'p') ? 3 : 7;
167 case 'D': /* December */
170 case 'F': /* February */
173 case 'M': /* March, May */
174 tt.tm_mon = (c[2] == 'r') ? 2 : 4;
176 case 'J': /* January, June, July */
177 tt.tm_mon = (c[2] == 'n') ? ((c[1] == 'a') ? 0 : 5) : 6;
179 case 'N': /* November */
182 case 'O': /* October */
185 case 'S': /* September */
190 break; /* NOTREACHED */
196 tt.tm_year = atoi(c);
197 for (; *c != ' '; c++);
199 if (tt.tm_year >= 1900)
203 tt.tm_hour = atoi(c);
204 for (; *c != ':'; c++);
209 for (; *c != ':'; c++);
214 for (; *c && *c != ' '; c++);
216 /* Got everything; let's go. The global 'timezone' variable contains the
217 * local timezone's offset from UTC, in seconds, so we apply that to tm_sec.
218 * This produces an illegal value for tm_sec, but mktime() will normalize
219 * it for us. This eliminates the need to temporarily switch the environment
220 * variable TZ to UTC, which is good because it fails to switch back on
224 tt.tm_sec = tt.tm_sec - (int)timezone;