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);
44 * Format a date/time stamp for output
46 void webcit_fmt_date(char *buf, time_t thetime, int brief)
53 time_format = get_time_format_cached ();
54 today_timet = time(NULL);
55 localtime_r(&today_timet, &today_tm);
57 localtime_r(&thetime, &tm);
61 /* If date == today, show only the time */
62 if ((tm.tm_year == today_tm.tm_year)
63 &&(tm.tm_mon == today_tm.tm_mon)
64 &&(tm.tm_mday == today_tm.tm_mday)) {
65 if (time_format == WC_TIMEFORMAT_24)
66 wc_strftime(buf, 32, "%k:%M", &tm);
68 wc_strftime(buf, 32, "%l:%M%p", &tm);
70 /* Otherwise, for messages up to 6 months old, show the month and day, and the time */
71 else if (today_timet - thetime < 15552000) {
72 if (time_format == WC_TIMEFORMAT_24)
73 wc_strftime(buf, 32, "%b %d %k:%M", &tm);
75 wc_strftime(buf, 32, "%b %d %l:%M%p", &tm);
77 /* older than 6 months, show only the date */
79 wc_strftime(buf, 32, "%b %d %Y", &tm);
83 if (time_format == WC_TIMEFORMAT_24)
84 wc_strftime(buf, 32, "%a %b %d %Y %T %Z", &tm);
86 wc_strftime(buf, 32, "%a %b %d %Y %r %Z", &tm);
92 * learn the users timeformat preference.
94 int get_time_format_cached (void)
97 int *time_format_cache;
98 time_format_cache = &(WC->time_format_cache);
99 if (*time_format_cache == WC_TIMEFORMAT_NONE)
101 get_pref_long("calhourformat", &calhourformat, 24);
102 if (calhourformat == 24)
103 *time_format_cache = WC_TIMEFORMAT_24;
105 *time_format_cache = WC_TIMEFORMAT_AMPM;
107 return *time_format_cache;
111 * Format TIME ONLY for output
112 * buf the output buffer
113 * thetime time to format into buf
115 void fmt_time(char *buf, time_t thetime)
121 time_format = get_time_format_cached ();
123 tm = localtime(&thetime);
130 if (time_format == WC_TIMEFORMAT_24) {
131 sprintf(buf, "%2d:%02d",
132 tm->tm_hour, tm->tm_min
136 sprintf(buf, "%d:%02d%s",
137 hour, tm->tm_min, ((tm->tm_hour > 12) ? "pm" : "am")
146 * Break down the timestamp used in HTTP headers
147 * Should read rfc1123 and rfc850 dates OK
148 * FIXME won't read asctime
149 * Doesn't understand timezone, but we only should be using GMT/UTC anyway
151 time_t httpdate_to_timestamp(StrBuf *buf)
157 /** Skip day of week, to number */
158 for (c = ChrPtr(buf); *c != ' '; c++)
162 memset(&tt, 0, sizeof(tt));
164 /* Get day of month */
165 tt.tm_mday = atoi(c);
166 for (; *c != ' ' && *c != '-'; c++);
171 case 'A': /* April, August */
172 tt.tm_mon = (c[1] == 'p') ? 3 : 7;
174 case 'D': /* December */
177 case 'F': /* February */
180 case 'M': /* March, May */
181 tt.tm_mon = (c[2] == 'r') ? 2 : 4;
183 case 'J': /* January, June, July */
184 tt.tm_mon = (c[2] == 'n') ? ((c[1] == 'a') ? 0 : 5) : 6;
186 case 'N': /* November */
189 case 'O': /* October */
192 case 'S': /* September */
197 break; /* NOTREACHED */
203 tt.tm_year = atoi(c);
204 for (; *c != ' '; c++);
206 if (tt.tm_year >= 1900)
210 tt.tm_hour = atoi(c);
211 for (; *c != ':'; c++);
216 for (; *c != ':'; c++);
221 for (; *c && *c != ' '; c++);
223 /* Got everything; let's go. The global 'timezone' variable contains the
224 * local timezone's offset from UTC, in seconds, so we apply that to tm_sec.
225 * This produces an illegal value for tm_sec, but mktime() will normalize
226 * it for us. This eliminates the need to temporarily switch the environment
227 * variable TZ to UTC, which is good because it fails to switch back on
231 tt.tm_sec = tt.tm_sec - (int)timezone;