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);
88 * \brief Convenience function to return a month name
90 * \param m Numeric month
92 char *monthname(int m)
94 static char months[12][32];
95 static int initialized = 0;
102 for (i=0; i<12; ++i) {
103 tt = 1137997451 + (i * 2592000);
104 localtime_r(&tt, &tm);
105 wc_strftime(months[i], 32, "%B", &tm);
106 lprintf(9, "%s\n", months[i]);
118 * \brief Format TIME ONLY for output
119 * \param buf the output buffer
120 * \param thetime time to format into buf
122 void fmt_time(char *buf, time_t thetime)
126 char calhourformat[16];
128 get_preference("calhourformat", calhourformat, sizeof calhourformat);
131 tm = localtime(&thetime);
138 if (!strcasecmp(calhourformat, "24")) {
139 sprintf(buf, "%2d:%02d",
140 tm->tm_hour, tm->tm_min
144 sprintf(buf, "%d:%02d%s",
145 hour, tm->tm_min, ((tm->tm_hour > 12) ? "pm" : "am")
154 * \brief Break down the timestamp used in HTTP headers
155 * Should read rfc1123 and rfc850 dates OK
156 * \todo FIXME won't read asctime
157 * Doesn't understand timezone, but we only should be using GMT/UTC anyway
158 * \param buf time to parse
159 * \return the time found in buf
161 time_t httpdate_to_timestamp(char *buf)
168 /** Skip day of week, to number */
169 for (c = buf; *c != ' '; c++)
173 /* Get day of month */
174 tt.tm_mday = atoi(c);
175 for (; *c != ' ' && *c != '-'; c++);
180 case 'A': /** April, August */
181 tt.tm_mon = (c[1] == 'p') ? 3 : 7;
183 case 'D': /** December */
186 case 'F': /** February */
189 case 'M': /** March, May */
190 tt.tm_mon = (c[2] == 'r') ? 2 : 4;
192 case 'J': /** January, June, July */
193 tt.tm_mon = (c[2] == 'n') ? ((c[1] == 'a') ? 0 : 5) : 6;
195 case 'N': /** November */
198 case 'O': /** October */
201 case 'S': /** September */
206 break; /** NOTREACHED */
212 tt.tm_year = atoi(c);
213 for (; *c != ' '; c++);
215 if (tt.tm_year >= 1900)
219 tt.tm_hour = atoi(c);
220 for (; *c != ':'; c++);
225 for (; *c != ':'; c++);
230 for (; *c && *c != ' '; c++);
232 /** Got everything; let's go */
233 /** First, change to UTC */
235 sprintf(tz, "TZ=%s", getenv("TZ"));