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]);
34 return strftime(s, max, format, tm);
37 return strftime(s, max, format, tm);
43 * Format a date/time stamp for output
45 void webcit_fmt_date(char *buf, time_t thetime, int brief)
52 time_format = get_time_format_cached ();
53 today_timet = time(NULL);
54 localtime_r(&today_timet, &today_tm);
56 localtime_r(&thetime, &tm);
60 /* If date == today, show only the time */
61 if ((tm.tm_year == today_tm.tm_year)
62 &&(tm.tm_mon == today_tm.tm_mon)
63 &&(tm.tm_mday == today_tm.tm_mday)) {
64 if (time_format == WC_TIMEFORMAT_24)
65 wc_strftime(buf, 32, "%k:%M", &tm);
67 wc_strftime(buf, 32, "%l:%M%p", &tm);
69 /* Otherwise, for messages up to 6 months old, show the month and day, and the time */
70 else if (today_timet - thetime < 15552000) {
71 if (time_format == WC_TIMEFORMAT_24)
72 wc_strftime(buf, 32, "%b %d %k:%M", &tm);
74 wc_strftime(buf, 32, "%b %d %l:%M%p", &tm);
76 /* older than 6 months, show only the date */
78 wc_strftime(buf, 32, "%b %d %Y", &tm);
82 if (time_format == WC_TIMEFORMAT_24)
83 wc_strftime(buf, 32, "%a %b %d %Y %T %Z", &tm);
85 wc_strftime(buf, 32, "%a %b %d %Y %r %Z", &tm);
91 * learn the users timeformat preference.
93 int get_time_format_cached (void)
95 char calhourformat[16];
96 int *time_format_cache;
97 time_format_cache = &(WC->time_format_cache);
98 if (*time_format_cache == WC_TIMEFORMAT_NONE)
100 get_preference("calhourformat", calhourformat, sizeof calhourformat);
101 if (!strcasecmp(calhourformat, "24"))
102 *time_format_cache = WC_TIMEFORMAT_24;
104 *time_format_cache = WC_TIMEFORMAT_AMPM;
106 return *time_format_cache;
110 * Format TIME ONLY for output
111 * buf the output buffer
112 * thetime time to format into buf
114 void fmt_time(char *buf, time_t thetime)
120 time_format = get_time_format_cached ();
122 tm = localtime(&thetime);
129 if (time_format == WC_TIMEFORMAT_24) {
130 sprintf(buf, "%2d:%02d",
131 tm->tm_hour, tm->tm_min
135 sprintf(buf, "%d:%02d%s",
136 hour, tm->tm_min, ((tm->tm_hour > 12) ? "pm" : "am")
145 * Break down the timestamp used in HTTP headers
146 * Should read rfc1123 and rfc850 dates OK
147 * FIXME won't read asctime
148 * Doesn't understand timezone, but we only should be using GMT/UTC anyway
150 time_t httpdate_to_timestamp(char *buf)
156 /** Skip day of week, to number */
157 for (c = buf; *c != ' '; c++)
161 /* Get day of month */
162 tt.tm_mday = atoi(c);
163 for (; *c != ' ' && *c != '-'; c++);
168 case 'A': /* April, August */
169 tt.tm_mon = (c[1] == 'p') ? 3 : 7;
171 case 'D': /* December */
174 case 'F': /* February */
177 case 'M': /* March, May */
178 tt.tm_mon = (c[2] == 'r') ? 2 : 4;
180 case 'J': /* January, June, July */
181 tt.tm_mon = (c[2] == 'n') ? ((c[1] == 'a') ? 0 : 5) : 6;
183 case 'N': /* November */
186 case 'O': /* October */
189 case 'S': /* September */
194 break; /* NOTREACHED */
200 tt.tm_year = atoi(c);
201 for (; *c != ' '; c++);
203 if (tt.tm_year >= 1900)
207 tt.tm_hour = atoi(c);
208 for (; *c != ':'; c++);
213 for (; *c != ':'; c++);
218 for (; *c && *c != ' '; c++);
220 /* Got everything; let's go. The global 'timezone' variable contains the
221 * local timezone's offset from UTC, in seconds, so we apply that to tm_sec.
222 * This produces an illegal value for tm_sec, but mktime() will normalize
223 * it for us. This eliminates the need to temporarily switch the environment
224 * variable TZ to UTC, which is good because it fails to switch back on
228 tt.tm_sec = tt.tm_sec - (int)timezone;