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 strftime_l(buf, 32, "%c", &tm, wc_locales[WC->selected_language]);
119 * \brief Format TIME ONLY for output
120 * \param buf the output buffer
121 * \param thetime time to format into buf
123 void fmt_time(char *buf, time_t thetime)
127 char calhourformat[16];
129 get_preference("calhourformat", calhourformat, sizeof calhourformat);
132 tm = localtime(&thetime);
139 if (!strcasecmp(calhourformat, "24")) {
140 sprintf(buf, "%2d:%02d",
141 tm->tm_hour, tm->tm_min
145 sprintf(buf, "%d:%02d%s",
146 hour, tm->tm_min, ((tm->tm_hour > 12) ? "pm" : "am")
155 * \brief Break down the timestamp used in HTTP headers
156 * Should read rfc1123 and rfc850 dates OK
157 * \todo FIXME won't read asctime
158 * Doesn't understand timezone, but we only should be using GMT/UTC anyway
159 * \param buf time to parse
160 * \return the time found in buf
162 time_t httpdate_to_timestamp(const char *buf)
169 /** Skip day of week, to number */
170 for (c = buf; *c != ' '; c++)
174 /* Get day of month */
175 tt.tm_mday = atoi(c);
176 for (; *c != ' ' && *c != '-'; c++);
181 case 'A': /** April, August */
182 tt.tm_mon = (c[1] == 'p') ? 3 : 7;
184 case 'D': /** December */
187 case 'F': /** February */
190 case 'M': /** March, May */
191 tt.tm_mon = (c[2] == 'r') ? 2 : 4;
193 case 'J': /** January, June, July */
194 tt.tm_mon = (c[2] == 'n') ? ((c[1] == 'a') ? 0 : 5) : 6;
196 case 'N': /** November */
199 case 'O': /** October */
202 case 'S': /** September */
207 break; /** NOTREACHED */
213 tt.tm_year = atoi(c);
214 for (; *c != ' '; c++);
216 if (tt.tm_year >= 1900)
220 tt.tm_hour = atoi(c);
221 for (; *c != ':'; c++);
226 for (; *c != ':'; c++);
231 for (; *c && *c != ' '; c++);
233 /** Got everything; let's go */
234 /** First, change to UTC */
236 sprintf(tz, "TZ=%s", getenv("TZ"));
250 * /brief Initialize the strings used to display months and weekdays.
252 void initialize_months_and_days(void) {
253 wdays[0] = _("Sunday");
254 wdays[1] = _("Monday");
255 wdays[2] = _("Tuesday");
256 wdays[3] = _("Wednesday");
257 wdays[4] = _("Thursday");
258 wdays[5] = _("Friday");
259 wdays[6] = _("Saturday");
261 months[0] = _("January");
262 months[1] = _("February");
263 months[2] = _("March");
264 months[3] = _("April");
265 months[4] = _("May");
266 months[5] = _("June");
267 months[6] = _("July");
268 months[7] = _("August");
269 months[8] = _("September");
270 months[9] = _("October");
271 months[10] = _("November");
272 months[11] = _("December");