5 * \defgroup FormatDates Miscellaneous routines formating dates
10 #include "webserver.h"
12 typedef unsigned char byte; /**< a byte. */
16 #define FALSE 0 /**< no. */
17 #define TRUE 1 /**< yes. */
20 * \brief Format a date/time stamp for output
21 * \param buf the output buffer
22 * \param thetime time to convert to string
23 * \param brief do we want compact view?????
25 void fmt_date(char *buf, time_t thetime, int brief)
31 char calhourformat[16];
32 static char *ascmonths[12] = { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL } ;
34 if (ascmonths[0] == NULL) {
35 ascmonths[0] = _("Jan");
36 ascmonths[1] = _("Feb");
37 ascmonths[2] = _("Mar");
38 ascmonths[3] = _("Apr");
39 ascmonths[4] = _("May");
40 ascmonths[5] = _("Jun");
41 ascmonths[6] = _("Jul");
42 ascmonths[7] = _("Aug");
43 ascmonths[8] = _("Sep");
44 ascmonths[9] = _("Oct");
45 ascmonths[10] = _("Nov");
46 ascmonths[11] = _("Dec");
49 get_preference("calhourformat", calhourformat, sizeof calhourformat);
51 today_timet = time(NULL);
52 localtime_r(&today_timet, &today_tm);
54 localtime_r(&thetime, &tm);
65 /** If date == today, show only the time */
66 if ((tm.tm_year == today_tm.tm_year)
67 &&(tm.tm_mon == today_tm.tm_mon)
68 &&(tm.tm_mday == today_tm.tm_mday)) {
69 if (!strcasecmp(calhourformat, "24")) {
70 sprintf(buf, "%2d:%02d",
75 sprintf(buf, "%2d:%02d%s",
77 ((tm.tm_hour >= 12) ? "pm" : "am")
82 /** Otherwise, for messages up to 6 months old, show the
83 * month and day, and the time */
84 else if (today_timet - thetime < 15552000) {
85 if (!strcasecmp(calhourformat, "24")) {
86 sprintf(buf, "%s %d %2d:%02d",
93 sprintf(buf, "%s %d %2d:%02d%s",
97 ((tm.tm_hour >= 12) ? "pm" : "am")
102 /** older than 6 months, show only the date */
104 sprintf(buf, "%s %d %d",
105 ascmonths[tm.tm_mon],
112 if (!strcasecmp(calhourformat, "24")) {
113 sprintf(buf, "%s %d %d %2d:%02d",
114 ascmonths[tm.tm_mon],
117 tm.tm_hour, tm.tm_min
121 sprintf(buf, "%s %d %d %2d:%02d%s",
122 ascmonths[tm.tm_mon],
125 hour, tm.tm_min, ((tm.tm_hour >= 12) ? "pm" : "am")
134 * \brief Format TIME ONLY for output
135 * \param buf the output buffer
136 * \param thetime time to format into buf
138 void fmt_time(char *buf, time_t thetime)
142 char calhourformat[16];
144 get_preference("calhourformat", calhourformat, sizeof calhourformat);
147 tm = localtime(&thetime);
154 if (!strcasecmp(calhourformat, "24")) {
155 sprintf(buf, "%2d:%02d",
156 tm->tm_hour, tm->tm_min
160 sprintf(buf, "%d:%02d%s",
161 hour, tm->tm_min, ((tm->tm_hour > 12) ? "pm" : "am")
170 * \brief Break down the timestamp used in HTTP headers
171 * Should read rfc1123 and rfc850 dates OK
172 * \todo FIXME won't read asctime
173 * Doesn't understand timezone, but we only should be using GMT/UTC anyway
174 * \param buf time to parse
175 * \return the time found in buf
177 time_t httpdate_to_timestamp(const char *buf)
184 /** Skip day of week, to number */
185 for (c = buf; *c != ' '; c++)
189 /* Get day of month */
190 tt.tm_mday = atoi(c);
191 for (; *c != ' ' && *c != '-'; c++);
196 case 'A': /** April, August */
197 tt.tm_mon = (c[1] == 'p') ? 3 : 7;
199 case 'D': /** December */
202 case 'F': /** February */
205 case 'M': /** March, May */
206 tt.tm_mon = (c[2] == 'r') ? 2 : 4;
208 case 'J': /** January, June, July */
209 tt.tm_mon = (c[2] == 'n') ? ((c[1] == 'a') ? 0 : 5) : 6;
211 case 'N': /** November */
214 case 'O': /** October */
217 case 'S': /** September */
222 break; /** NOTREACHED */
228 tt.tm_year = atoi(c);
229 for (; *c != ' '; c++);
231 if (tt.tm_year >= 1900)
235 tt.tm_hour = atoi(c);
236 for (; *c != ':'; c++);
241 for (; *c != ':'; c++);
246 for (; *c && *c != ' '; c++);
248 /** Got everything; let's go */
249 /** First, change to UTC */
251 sprintf(tz, "TZ=%s", getenv("TZ"));
265 * /brief Initialize the strings used to display months and weekdays.
267 void initialize_months_and_days(void) {
268 wdays[0] = _("Sunday");
269 wdays[1] = _("Monday");
270 wdays[2] = _("Tuesday");
271 wdays[3] = _("Wednesday");
272 wdays[4] = _("Thursday");
273 wdays[5] = _("Friday");
274 wdays[6] = _("Saturday");
276 months[0] = _("January");
277 months[1] = _("February");
278 months[2] = _("March");
279 months[3] = _("April");
280 months[4] = _("May");
281 months[5] = _("June");
282 months[6] = _("July");
283 months[7] = _("August");
284 months[8] = _("September");
285 months[9] = _("October");
286 months[10] = _("November");
287 months[11] = _("December");