5 * \defgroup FormatDates Miscellaneous routines formating dates
10 #include "webserver.h"
12 typedef unsigned char byte; /**< a byte. */
14 #define FALSE 0 /**< no. */
15 #define TRUE 1 /**< yes. */
18 * \brief Wrapper around strftime() or strftime_l()
19 * depending upon how our build is configured.
21 * \param s String target buffer
22 * \param max Maximum size of string target buffer
23 * \param format strftime() format
24 * \param tm Input date/time
26 size_t wc_strftime(char *s, size_t max, const char *format, const struct tm *tm)
29 if (wc_locales[WC->selected_language] == NULL) {
30 return strftime(s, max, format, tm);
33 return strftime_l(s, max, format, tm, wc_locales[WC->selected_language]);
36 return strftime(s, max, format, tm);
42 * \brief Format a date/time stamp for output
43 * \param buf the output buffer
44 * \param thetime time to convert to string
45 * \param brief do we want compact view?????
47 void fmt_date(char *buf, time_t thetime, int brief)
55 time_format = get_time_format_cached ();
56 /// TODO: what about the time format?
57 today_timet = time(NULL);
58 localtime_r(&today_timet, &today_tm);
60 localtime_r(&thetime, &tm);
71 /** If date == today, show only the time */
72 if ((tm.tm_year == today_tm.tm_year)
73 &&(tm.tm_mon == today_tm.tm_mon)
74 &&(tm.tm_mday == today_tm.tm_mday)) {
75 wc_strftime(buf, 32, "%l:%M%p", &tm);
77 /** Otherwise, for messages up to 6 months old, show the
78 * month and day, and the time */
79 else if (today_timet - thetime < 15552000) {
80 wc_strftime(buf, 32, "%b %d %l:%M%p", &tm);
82 /** older than 6 months, show only the date */
84 wc_strftime(buf, 32, "%b %d %Y", &tm);
88 wc_strftime(buf, 32, "%c", &tm);
94 * \brief learn the users timeformat preference.
96 int get_time_format_cached (void)
98 char calhourformat[16];
99 int *time_format_cache;
100 time_format_cache = &(WC->time_format_cache);
101 if (*time_format_cache == WC_TIMEFORMAT_NONE)
103 get_preference("calhourformat", calhourformat, sizeof calhourformat);
104 if (!strcasecmp(calhourformat, "24"))
105 *time_format_cache = WC_TIMEFORMAT_24;
107 *time_format_cache = WC_TIMEFORMAT_AMPM;
109 return *time_format_cache;
113 * \brief Format TIME ONLY for output
114 * \param buf the output buffer
115 * \param thetime time to format into buf
117 void fmt_time(char *buf, time_t thetime)
123 time_format = get_time_format_cached ();
125 tm = localtime(&thetime);
132 if (time_format == WC_TIMEFORMAT_24) {
133 sprintf(buf, "%2d:%02d",
134 tm->tm_hour, tm->tm_min
138 sprintf(buf, "%d:%02d%s",
139 hour, tm->tm_min, ((tm->tm_hour > 12) ? "pm" : "am")
148 * \brief Break down the timestamp used in HTTP headers
149 * Should read rfc1123 and rfc850 dates OK
150 * \todo FIXME won't read asctime
151 * Doesn't understand timezone, but we only should be using GMT/UTC anyway
152 * \param buf time to parse
153 * \return the time found in buf
155 time_t httpdate_to_timestamp(char *buf)
162 /** Skip day of week, to number */
163 for (c = buf; *c != ' '; c++)
167 /* Get day of month */
168 tt.tm_mday = atoi(c);
169 for (; *c != ' ' && *c != '-'; c++);
174 case 'A': /** April, August */
175 tt.tm_mon = (c[1] == 'p') ? 3 : 7;
177 case 'D': /** December */
180 case 'F': /** February */
183 case 'M': /** March, May */
184 tt.tm_mon = (c[2] == 'r') ? 2 : 4;
186 case 'J': /** January, June, July */
187 tt.tm_mon = (c[2] == 'n') ? ((c[1] == 'a') ? 0 : 5) : 6;
189 case 'N': /** November */
192 case 'O': /** October */
195 case 'S': /** September */
200 break; /** NOTREACHED */
206 tt.tm_year = atoi(c);
207 for (; *c != ' '; c++);
209 if (tt.tm_year >= 1900)
213 tt.tm_hour = atoi(c);
214 for (; *c != ':'; c++);
219 for (; *c != ':'; c++);
224 for (; *c && *c != ' '; c++);
226 /** Got everything; let's go */
227 /** First, change to UTC */
229 sprintf(tz, "TZ=%s", getenv("TZ"));