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 return strftime_l(s, max, format, tm, wc_locales[WC->selected_language]);
31 return strftime(s, max, format, tm);
37 * \brief Format a date/time stamp for output
38 * \param buf the output buffer
39 * \param thetime time to convert to string
40 * \param brief do we want compact view?????
42 void fmt_date(char *buf, time_t thetime, int brief)
48 char calhourformat[16];
50 get_preference("calhourformat", calhourformat, sizeof calhourformat);
52 today_timet = time(NULL);
53 localtime_r(&today_timet, &today_tm);
55 localtime_r(&thetime, &tm);
66 /** If date == today, show only the time */
67 if ((tm.tm_year == today_tm.tm_year)
68 &&(tm.tm_mon == today_tm.tm_mon)
69 &&(tm.tm_mday == today_tm.tm_mday)) {
70 wc_strftime(buf, 32, "%l:%M%p", &tm);
72 /** Otherwise, for messages up to 6 months old, show the
73 * month and day, and the time */
74 else if (today_timet - thetime < 15552000) {
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 wc_strftime(buf, 32, "%c", &tm);
89 * \brief Format TIME ONLY for output
90 * \param buf the output buffer
91 * \param thetime time to format into buf
93 void fmt_time(char *buf, time_t thetime)
97 char calhourformat[16];
99 get_preference("calhourformat", calhourformat, sizeof calhourformat);
102 tm = localtime(&thetime);
109 if (!strcasecmp(calhourformat, "24")) {
110 sprintf(buf, "%2d:%02d",
111 tm->tm_hour, tm->tm_min
115 sprintf(buf, "%d:%02d%s",
116 hour, tm->tm_min, ((tm->tm_hour > 12) ? "pm" : "am")
125 * \brief Break down the timestamp used in HTTP headers
126 * Should read rfc1123 and rfc850 dates OK
127 * \todo FIXME won't read asctime
128 * Doesn't understand timezone, but we only should be using GMT/UTC anyway
129 * \param buf time to parse
130 * \return the time found in buf
132 time_t httpdate_to_timestamp(char *buf)
139 /** Skip day of week, to number */
140 for (c = buf; *c != ' '; c++)
144 /* Get day of month */
145 tt.tm_mday = atoi(c);
146 for (; *c != ' ' && *c != '-'; c++);
151 case 'A': /** April, August */
152 tt.tm_mon = (c[1] == 'p') ? 3 : 7;
154 case 'D': /** December */
157 case 'F': /** February */
160 case 'M': /** March, May */
161 tt.tm_mon = (c[2] == 'r') ? 2 : 4;
163 case 'J': /** January, June, July */
164 tt.tm_mon = (c[2] == 'n') ? ((c[1] == 'a') ? 0 : 5) : 6;
166 case 'N': /** November */
169 case 'O': /** October */
172 case 'S': /** September */
177 break; /** NOTREACHED */
183 tt.tm_year = atoi(c);
184 for (; *c != ' '; c++);
186 if (tt.tm_year >= 1900)
190 tt.tm_hour = atoi(c);
191 for (; *c != ':'; c++);
196 for (; *c != ':'; c++);
201 for (; *c && *c != ' '; c++);
203 /** Got everything; let's go */
204 /** First, change to UTC */
206 sprintf(tz, "TZ=%s", getenv("TZ"));