4 * Miscellaneous routines
10 typedef unsigned char byte;
16 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
17 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
21 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
25 * Format a date/time stamp for output
27 void fmt_date(char *buf, time_t thetime, int brief)
33 char calhourformat[16];
35 get_preference("calhourformat", calhourformat, sizeof calhourformat);
37 today_timet = time(NULL);
38 localtime_r(&today_timet, &today_tm);
40 localtime_r(&thetime, &tm);
51 if ((tm.tm_year == today_tm.tm_year)
52 &&(tm.tm_mon == today_tm.tm_mon)
53 &&(tm.tm_mday == today_tm.tm_mday)) {
54 if (!strcasecmp(calhourformat, "24")) {
55 sprintf(buf, "%2d:%02d",
60 sprintf(buf, "%2d:%02d%s",
62 ((tm.tm_hour >= 12) ? "pm" : "am")
67 sprintf(buf, "%s %d %d",
75 if (!strcasecmp(calhourformat, "24")) {
76 sprintf(buf, "%s %d %d %2d:%02d",
84 sprintf(buf, "%s %d %d %2d:%02d%s",
88 hour, tm.tm_min, ((tm.tm_hour >= 12) ? "pm" : "am")
97 * Format TIME ONLY for output
99 void fmt_time(char *buf, time_t thetime)
103 char calhourformat[16];
105 get_preference("calhourformat", calhourformat, sizeof calhourformat);
108 tm = localtime(&thetime);
115 if (!strcasecmp(calhourformat, "24")) {
116 sprintf(buf, "%2d:%02d",
117 tm->tm_hour, tm->tm_min
121 sprintf(buf, "%d:%02d%s",
122 hour, tm->tm_min, ((tm->tm_hour > 12) ? "pm" : "am")
131 * Format a date/time stamp to the format used in HTTP headers
133 void httpdate(char *buf, time_t thetime)
138 tm = localtime(&thetime);
140 sprintf(buf, "%s, %02d %s %4d %02d:%02d:%02d",
141 ascdays[tm->tm_wday],
143 ascmonths[tm->tm_mon],
144 tm->tm_year + 1900, tm->tm_hour, tm->tm_min, tm->tm_sec);
149 * Break down the timestamp used in HTTP headers
150 * Should read rfc1123 and rfc850 dates OK
151 * FIXME won't read asctime
152 * Doesn't understand timezone, but we only should be using GMT/UTC anyway
154 time_t httpdate_to_timestamp(const char *buf)
161 /* Skip day of week, to number */
162 for (c = buf; *c != ' '; c++)
166 /* Get day of month */
167 tt.tm_mday = atoi(c);
168 for (; *c != ' ' && *c != '-'; c++);
173 case 'A': /* April, August */
174 tt.tm_mon = (c[1] == 'p') ? 3 : 7;
176 case 'D': /* December */
179 case 'F': /* February */
182 case 'M': /* March, May */
183 tt.tm_mon = (c[2] == 'r') ? 2 : 4;
185 case 'J': /* January, June, July */
186 tt.tm_mon = (c[2] == 'n') ? ((c[1] == 'a') ? 0 : 5) : 6;
188 case 'N': /* November */
191 case 'O': /* October */
194 case 'S': /* September */
199 break; /* NOTREACHED */
205 tt.tm_year = atoi(c);
206 for (; *c != ' '; c++);
208 if (tt.tm_year >= 1900)
212 tt.tm_hour = atoi(c);
213 for (; *c != ':'; c++);
218 for (; *c != ':'; c++);
223 for (; *c && *c != ' '; c++);
225 /* Got everything; let's go */
226 /* First, change to UTC */
228 sprintf(tz, "TZ=%s", getenv("TZ"));