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)
27 if (wc_locales[WC->selected_language] == NULL) {
28 return strftime(s, max, format, tm);
30 else { // TODO: this gives empty strings on debian.
31 return strftime_l(s, max, format, tm, wc_locales[WC->selected_language]);
35 return strftime(s, max, format, tm);
41 * Format a date/time stamp for output
43 void webcit_fmt_date(char *buf, time_t thetime, int brief)
50 time_format = get_time_format_cached ();
51 today_timet = time(NULL);
52 localtime_r(&today_timet, &today_tm);
54 localtime_r(&thetime, &tm);
58 /* If date == today, show only the time */
59 if ((tm.tm_year == today_tm.tm_year)
60 &&(tm.tm_mon == today_tm.tm_mon)
61 &&(tm.tm_mday == today_tm.tm_mday)) {
62 if (time_format == WC_TIMEFORMAT_24)
63 wc_strftime(buf, 32, "%k:%M", &tm);
65 wc_strftime(buf, 32, "%l:%M%p", &tm);
67 /* Otherwise, for messages up to 6 months old, show the month and day, and the time */
68 else if (today_timet - thetime < 15552000) {
69 if (time_format == WC_TIMEFORMAT_24)
70 wc_strftime(buf, 32, "%b %d %k:%M", &tm);
72 wc_strftime(buf, 32, "%b %d %l:%M%p", &tm);
74 /* older than 6 months, show only the date */
76 wc_strftime(buf, 32, "%b %d %Y", &tm);
80 if (time_format == WC_TIMEFORMAT_24)
81 wc_strftime(buf, 32, "%a %b %d %Y %T %Z", &tm);
83 wc_strftime(buf, 32, "%a %b %d %Y %r %Z", &tm);
89 * learn the users timeformat preference.
91 int get_time_format_cached (void)
93 char calhourformat[16];
94 int *time_format_cache;
95 time_format_cache = &(WC->time_format_cache);
96 if (*time_format_cache == WC_TIMEFORMAT_NONE)
98 get_preference("calhourformat", calhourformat, sizeof calhourformat);
99 if (!strcasecmp(calhourformat, "24"))
100 *time_format_cache = WC_TIMEFORMAT_24;
102 *time_format_cache = WC_TIMEFORMAT_AMPM;
104 return *time_format_cache;
108 * Format TIME ONLY for output
109 * buf the output buffer
110 * thetime time to format into buf
112 void fmt_time(char *buf, time_t thetime)
118 time_format = get_time_format_cached ();
120 tm = localtime(&thetime);
127 if (time_format == WC_TIMEFORMAT_24) {
128 sprintf(buf, "%2d:%02d",
129 tm->tm_hour, tm->tm_min
133 sprintf(buf, "%d:%02d%s",
134 hour, tm->tm_min, ((tm->tm_hour > 12) ? "pm" : "am")
143 * Break down the timestamp used in HTTP headers
144 * Should read rfc1123 and rfc850 dates OK
145 * FIXME won't read asctime
146 * Doesn't understand timezone, but we only should be using GMT/UTC anyway
148 time_t httpdate_to_timestamp(char *buf)
154 /** Skip day of week, to number */
155 for (c = buf; *c != ' '; c++)
159 /* Get day of month */
160 tt.tm_mday = atoi(c);
161 for (; *c != ' ' && *c != '-'; c++);
166 case 'A': /* April, August */
167 tt.tm_mon = (c[1] == 'p') ? 3 : 7;
169 case 'D': /* December */
172 case 'F': /* February */
175 case 'M': /* March, May */
176 tt.tm_mon = (c[2] == 'r') ? 2 : 4;
178 case 'J': /* January, June, July */
179 tt.tm_mon = (c[2] == 'n') ? ((c[1] == 'a') ? 0 : 5) : 6;
181 case 'N': /* November */
184 case 'O': /* October */
187 case 'S': /* September */
192 break; /* NOTREACHED */
198 tt.tm_year = atoi(c);
199 for (; *c != ' '; c++);
201 if (tt.tm_year >= 1900)
205 tt.tm_hour = atoi(c);
206 for (; *c != ':'; c++);
211 for (; *c != ':'; c++);
216 for (; *c && *c != ' '; c++);
218 /* Got everything; let's go. The global 'timezone' variable contains the
219 * local timezone's offset from UTC, in seconds, so we apply that to tm_sec.
220 * This produces an illegal value for tm_sec, but mktime() will normalize
221 * it for us. This eliminates the need to temporarily switch the environment
222 * variable TZ to UTC, which is good because it fails to switch back on
226 tt.tm_sec = tt.tm_sec - (int)timezone;