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)
53 char calhourformat[16];
55 get_preference("calhourformat", calhourformat, sizeof calhourformat);
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 Format TIME ONLY for output
95 * \param buf the output buffer
96 * \param thetime time to format into buf
98 void fmt_time(char *buf, time_t thetime)
102 char calhourformat[16];
104 get_preference("calhourformat", calhourformat, sizeof calhourformat);
107 tm = localtime(&thetime);
114 if (!strcasecmp(calhourformat, "24")) {
115 sprintf(buf, "%2d:%02d",
116 tm->tm_hour, tm->tm_min
120 sprintf(buf, "%d:%02d%s",
121 hour, tm->tm_min, ((tm->tm_hour > 12) ? "pm" : "am")
130 * \brief Break down the timestamp used in HTTP headers
131 * Should read rfc1123 and rfc850 dates OK
132 * \todo FIXME won't read asctime
133 * Doesn't understand timezone, but we only should be using GMT/UTC anyway
134 * \param buf time to parse
135 * \return the time found in buf
137 time_t httpdate_to_timestamp(char *buf)
144 /** Skip day of week, to number */
145 for (c = buf; *c != ' '; c++)
149 /* Get day of month */
150 tt.tm_mday = atoi(c);
151 for (; *c != ' ' && *c != '-'; c++);
156 case 'A': /** April, August */
157 tt.tm_mon = (c[1] == 'p') ? 3 : 7;
159 case 'D': /** December */
162 case 'F': /** February */
165 case 'M': /** March, May */
166 tt.tm_mon = (c[2] == 'r') ? 2 : 4;
168 case 'J': /** January, June, July */
169 tt.tm_mon = (c[2] == 'n') ? ((c[1] == 'a') ? 0 : 5) : 6;
171 case 'N': /** November */
174 case 'O': /** October */
177 case 'S': /** September */
182 break; /** NOTREACHED */
188 tt.tm_year = atoi(c);
189 for (; *c != ' '; c++);
191 if (tt.tm_year >= 1900)
195 tt.tm_hour = atoi(c);
196 for (; *c != ':'; c++);
201 for (; *c != ':'; c++);
206 for (; *c && *c != ' '; c++);
208 /** Got everything; let's go */
209 /** First, change to UTC */
211 sprintf(tz, "TZ=%s", getenv("TZ"));