5 * \defgroup FormatDates Miscellaneous routines formating dates
11 typedef unsigned char byte; /**< a byte. */
15 #define FALSE 0 /**< no. */
16 #define TRUE 1 /**< yes. */
19 * \brief Format a date/time stamp for output
20 * \param buf the output buffer
21 * \param thetime time to convert to string
22 * \param brief do we want compact view?????
24 void fmt_date(char *buf, time_t thetime, int brief)
30 char calhourformat[16];
31 static char *ascmonths[12] = { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL } ;
33 if (ascmonths[0] == NULL) {
34 ascmonths[0] = _("Jan");
35 ascmonths[1] = _("Feb");
36 ascmonths[2] = _("Mar");
37 ascmonths[3] = _("Apr");
38 ascmonths[4] = _("May");
39 ascmonths[5] = _("Jun");
40 ascmonths[6] = _("Jul");
41 ascmonths[7] = _("Aug");
42 ascmonths[8] = _("Sep");
43 ascmonths[9] = _("Oct");
44 ascmonths[10] = _("Nov");
45 ascmonths[11] = _("Dec");
48 get_preference("calhourformat", calhourformat, sizeof calhourformat);
50 today_timet = time(NULL);
51 localtime_r(&today_timet, &today_tm);
53 localtime_r(&thetime, &tm);
64 /** If date == today, show only the time */
65 if ((tm.tm_year == today_tm.tm_year)
66 &&(tm.tm_mon == today_tm.tm_mon)
67 &&(tm.tm_mday == today_tm.tm_mday)) {
68 if (!strcasecmp(calhourformat, "24")) {
69 sprintf(buf, "%2d:%02d",
74 sprintf(buf, "%2d:%02d%s",
76 ((tm.tm_hour >= 12) ? "pm" : "am")
81 /** Otherwise, for messages up to 6 months old, show the
82 * month and day, and the time */
83 else if (today_timet - thetime < 15552000) {
84 if (!strcasecmp(calhourformat, "24")) {
85 sprintf(buf, "%s %d %2d:%02d",
92 sprintf(buf, "%s %d %2d:%02d%s",
96 ((tm.tm_hour >= 12) ? "pm" : "am")
101 /** older than 6 months, show only the date */
103 sprintf(buf, "%s %d %d",
104 ascmonths[tm.tm_mon],
111 if (!strcasecmp(calhourformat, "24")) {
112 sprintf(buf, "%s %d %d %2d:%02d",
113 ascmonths[tm.tm_mon],
116 tm.tm_hour, tm.tm_min
120 sprintf(buf, "%s %d %d %2d:%02d%s",
121 ascmonths[tm.tm_mon],
124 hour, tm.tm_min, ((tm.tm_hour >= 12) ? "pm" : "am")
133 * \brief Format TIME ONLY for output
134 * \param buf the output buffer
135 * \param thetime time to format into buf
137 void fmt_time(char *buf, time_t thetime)
141 char calhourformat[16];
143 get_preference("calhourformat", calhourformat, sizeof calhourformat);
146 tm = localtime(&thetime);
153 if (!strcasecmp(calhourformat, "24")) {
154 sprintf(buf, "%2d:%02d",
155 tm->tm_hour, tm->tm_min
159 sprintf(buf, "%d:%02d%s",
160 hour, tm->tm_min, ((tm->tm_hour > 12) ? "pm" : "am")
169 * \brief Break down the timestamp used in HTTP headers
170 * Should read rfc1123 and rfc850 dates OK
171 * \todo FIXME won't read asctime
172 * Doesn't understand timezone, but we only should be using GMT/UTC anyway
173 * \param buf time to parse
174 * \return the time found in buf
176 time_t httpdate_to_timestamp(const char *buf)
183 /** Skip day of week, to number */
184 for (c = buf; *c != ' '; c++)
188 /* Get day of month */
189 tt.tm_mday = atoi(c);
190 for (; *c != ' ' && *c != '-'; c++);
195 case 'A': /** April, August */
196 tt.tm_mon = (c[1] == 'p') ? 3 : 7;
198 case 'D': /** December */
201 case 'F': /** February */
204 case 'M': /** March, May */
205 tt.tm_mon = (c[2] == 'r') ? 2 : 4;
207 case 'J': /** January, June, July */
208 tt.tm_mon = (c[2] == 'n') ? ((c[1] == 'a') ? 0 : 5) : 6;
210 case 'N': /** November */
213 case 'O': /** October */
216 case 'S': /** September */
221 break; /** NOTREACHED */
227 tt.tm_year = atoi(c);
228 for (; *c != ' '; c++);
230 if (tt.tm_year >= 1900)
234 tt.tm_hour = atoi(c);
235 for (; *c != ':'; c++);
240 for (; *c != ':'; c++);
245 for (; *c && *c != ' '; c++);
247 /** Got everything; let's go */
248 /** First, change to UTC */
250 sprintf(tz, "TZ=%s", getenv("TZ"));
264 * /brief Initialize the strings used to display months and weekdays.
266 void initialize_months_and_days(void) {
267 wdays[0] = _("Sunday");
268 wdays[1] = _("Monday");
269 wdays[2] = _("Tuesday");
270 wdays[3] = _("Wednesday");
271 wdays[4] = _("Thursday");
272 wdays[5] = _("Friday");
273 wdays[6] = _("Saturday");
275 months[0] = _("January");
276 months[1] = _("February");
277 months[2] = _("March");
278 months[3] = _("April");
279 months[4] = _("May");
280 months[5] = _("June");
281 months[6] = _("July");
282 months[7] = _("August");
283 months[8] = _("September");
284 months[9] = _("October");
285 months[10] = _("November");
286 months[11] = _("December");