5 * \defgroup FormatDates Miscellaneous routines formating dates
11 typedef unsigned char byte; /**< a byte. */
13 #define FALSE 0 /**< no. */
14 #define TRUE 1 /**< yes. */
16 /** \todo translate */
19 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
20 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
25 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
29 * \brief Format a date/time stamp for output
30 * \param buf the output buffer
31 * \param thetime time to convert to string
32 * \param brief do we want compact view?????
34 void fmt_date(char *buf, time_t thetime, int brief)
40 char calhourformat[16];
42 get_preference("calhourformat", calhourformat, sizeof calhourformat);
44 today_timet = time(NULL);
45 localtime_r(&today_timet, &today_tm);
47 localtime_r(&thetime, &tm);
58 /** If date == today, show only the time */
59 if ((tm.tm_year == today_tm.tm_year)
60 &&(tm.tm_mon == today_tm.tm_mon)
61 &&(tm.tm_mday == today_tm.tm_mday)) {
62 if (!strcasecmp(calhourformat, "24")) {
63 sprintf(buf, "%2d:%02d",
68 sprintf(buf, "%2d:%02d%s",
70 ((tm.tm_hour >= 12) ? "pm" : "am")
75 /** Otherwise, for messages up to 6 months old, show the
76 * month and day, and the time */
77 else if (today_timet - thetime < 15552000) {
78 if (!strcasecmp(calhourformat, "24")) {
79 sprintf(buf, "%s %d %2d:%02d",
86 sprintf(buf, "%s %d %2d:%02d%s",
90 ((tm.tm_hour >= 12) ? "pm" : "am")
95 /** older than 6 months, show only the date */
97 sprintf(buf, "%s %d %d",
105 if (!strcasecmp(calhourformat, "24")) {
106 sprintf(buf, "%s %d %d %2d:%02d",
107 ascmonths[tm.tm_mon],
110 tm.tm_hour, tm.tm_min
114 sprintf(buf, "%s %d %d %2d:%02d%s",
115 ascmonths[tm.tm_mon],
118 hour, tm.tm_min, ((tm.tm_hour >= 12) ? "pm" : "am")
127 * \brief Format TIME ONLY for output
128 * \param buf the output buffer
129 * \param thetime time to format into buf
131 void fmt_time(char *buf, time_t thetime)
135 char calhourformat[16];
137 get_preference("calhourformat", calhourformat, sizeof calhourformat);
140 tm = localtime(&thetime);
147 if (!strcasecmp(calhourformat, "24")) {
148 sprintf(buf, "%2d:%02d",
149 tm->tm_hour, tm->tm_min
153 sprintf(buf, "%d:%02d%s",
154 hour, tm->tm_min, ((tm->tm_hour > 12) ? "pm" : "am")
163 * \brief Format a date/time stamp to the format used in HTTP headers
164 * \param buf give back result here.
165 * \param thetime time to translate
167 void httpdate(char *buf, time_t thetime)
172 tm = localtime(&thetime);
174 sprintf(buf, "%s, %02d %s %4d %02d:%02d:%02d",
175 ascdays[tm->tm_wday],
177 ascmonths[tm->tm_mon],
178 tm->tm_year + 1900, tm->tm_hour, tm->tm_min, tm->tm_sec);
183 * \brief Break down the timestamp used in HTTP headers
184 * Should read rfc1123 and rfc850 dates OK
185 * \todo FIXME won't read asctime
186 * Doesn't understand timezone, but we only should be using GMT/UTC anyway
187 * \param buf time to parse
188 * \return the time found in buf
190 time_t httpdate_to_timestamp(const char *buf)
197 /** Skip day of week, to number */
198 for (c = buf; *c != ' '; c++)
202 /* Get day of month */
203 tt.tm_mday = atoi(c);
204 for (; *c != ' ' && *c != '-'; c++);
209 case 'A': /** April, August */
210 tt.tm_mon = (c[1] == 'p') ? 3 : 7;
212 case 'D': /** December */
215 case 'F': /** February */
218 case 'M': /** March, May */
219 tt.tm_mon = (c[2] == 'r') ? 2 : 4;
221 case 'J': /** January, June, July */
222 tt.tm_mon = (c[2] == 'n') ? ((c[1] == 'a') ? 0 : 5) : 6;
224 case 'N': /** November */
227 case 'O': /** October */
230 case 'S': /** September */
235 break; /** NOTREACHED */
241 tt.tm_year = atoi(c);
242 for (; *c != ' '; c++);
244 if (tt.tm_year >= 1900)
248 tt.tm_hour = atoi(c);
249 for (; *c != ':'; c++);
254 for (; *c != ':'; c++);
259 for (; *c && *c != ' '; c++);
261 /** Got everything; let's go */
262 /** First, change to UTC */
264 sprintf(tz, "TZ=%s", getenv("TZ"));