6 * Manipulate character strings representing dates.
10 * #include <parsedate.h>
13 * struct parseddate *pd;
15 * pd = parsedate (date);
17 * compute_unixtime (pd);
19 * break_down_unixtime (pd);
21 * date = mail_date_string (pd);
23 * date = uucp_date_string (pd);
27 * The returned value from "parsedate", "mail_date_string", or
28 * "uucp_date_string" points to static data whose contents are
29 * overwritten by the next call to the same routine.
31 * "compute_unixtime" is implicitly called by "parsedate".
35 * struct parseddate *parsedate (date) char *date;
36 * Parse a character string representing a date and time into
37 * individual values in a "struct parseddate" data structure.
39 * compute_unixtime (pd) struct parseddate *pd;
40 * Given a mostly filled-in "struct parseddate", compute the day
41 * of the week and the internal UNIX representation of the date.
43 * break_down_unixtime (pd) struct parseddate *pd;
44 * Compute the date and time corresponding to the "unixtime" and
45 * "zone" values in a "struct parseddate".
47 * char *mail_date_string (pd) struct parseddate *pd;
48 * Generate a character string representing a date and time in
49 * the RFC822 (ARPANET mail standard) format.
51 * char *uucp_date_string (pd) struct parseddate *pd;
52 * Generate a character string representing a date and time in
53 * the UUCP mail format.
61 #include "parsedate.h"
63 static char *RCSID = "$Id: parsedate.c,v 1.1 2002-07-25 08:01:27 arjen Exp $";
67 compute_unixtime (register struct parseddate *pd);
69 /* Number of seconds in various time intervals. */
70 #define SEC_PER_MIN 60
71 #define SEC_PER_HOUR (60*SEC_PER_MIN)
72 #define SEC_PER_DAY (24*SEC_PER_HOUR)
73 #define SEC_PER_YEAR (365*SEC_PER_DAY)
75 /* Number of days in each month. */
76 static int monthsize[] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
78 /* Three-letter abbreviations of month and day names. */
79 static char monthname[] = "JanFebMarAprMayJunJulAugSepOctNovDec";
80 static char dayname[] = "SunMonTueWedThuFriSat";
82 /* struct parseddate *parsedate (date) char *date;
83 * Analyze a character string representing a date and time. The
84 * returned value points to a data structure with the desired
85 * information. (NOTE: The returned value points to static data
86 * whose contents are overwritten by each call.)
88 struct parseddate * parsedate (char *date)
90 register int year_save;
91 extern struct parseddate yyans;
94 /* Initialize the returned-value structure. */
105 yyans.c_weekday = -1;
108 /* Parse the argument string. */
110 if (yyparse () != 0 && yyans.error == NULL)
111 yyans.error = yyinbuf;
113 /* Validate the day of the month, compute/validate the day of the
114 * week, and compute the internal UNIX form of the time. See if
115 * "compute_unixtime" found fault with the year or the day of the
116 * month. (Note that we have to remember the original "year" value
117 * because it might legitimately have been -1 to begin with.)
119 year_save = yyans.year; compute_unixtime (&yyans);
120 if (yyans.error == NULL
121 && (yyans.year != year_save
122 || (yyans.month > 0 && yyans.day < 0)
123 || (yyans.month < 0 && yyans.day > 0)))
124 yyans.error = yyinbuf;
129 /* compute_unixtime (pd) struct parseddate *pd;
130 * Given a mostly filled-in "struct parseddate", compute the day of
131 * the week and the internal UNIX representation of the date.
133 * A year before 1600 will be rejected and replaced with -1. A
134 * date from 1600 on which falls outside the range representable in
135 * internal UNIX form will still have the correct day of the week
138 * The day of the week is always computed on the assumption that the
139 * Gregorian calendar is in use. Days of the week for dates in the
140 * far future may turn out to be incorrect if any changes are made
141 * to the calendar between now and then.
143 compute_unixtime (register struct parseddate *pd)
145 register int weekday, n, l, a;
147 /* Validate the year. */
148 if (pd->year >= 0 && pd->year < 1600) pd->year = -1;
150 /* Validate the day of the month. Also calculate the number of days
151 * in February (even if this is not February, we will need the num-
152 * ber of days in February later on when computing the UNIX time).
155 { if (pd->year < 0) monthsize[2] = 29;
156 else if (pd->year % 4 != 0) monthsize[2] = 28;
157 else if (pd->year % 100 != 0) monthsize[2] = 29;
158 else if (pd->year % 400 != 0) monthsize[2] = 28;
159 else monthsize[2] = 29;
160 if (pd->day <= 0 || pd->day > monthsize[pd->month])
164 /* Compute the day of the week. The next several lines constitute a
165 * perpetual-calendar formula. Note, of course, that the "claimed"
166 * day of the week (pd->c_weekday) is ignored here.
168 if (pd->year > 0 && pd->month > 0 && pd->day > 0)
169 { if (pd->month >= 3) n = pd->year / 100,
171 else n = (pd->year-1) / 100,
172 l = (pd->year-1) % 100;
173 a = (26 * ((pd->month+9)%12 + 1) - 2) / 10;
174 weekday = (a+(l>>2)+(n>>2)+l-(n+n)+pd->day);
175 while (weekday < 0) weekday += 7;
176 pd->weekday = weekday % 7;
179 /* Figure out the internal UNIX form of the date. */
180 if (pd->year >= 1969 && pd->year <= 2038
181 && pd->month > 0 && pd->day > 0
182 && pd->hour >= 0 && pd->minute >= 0
183 && pd->zone != -1 && pd->zone > -1440 && pd->zone < 1440)
185 SEC_PER_YEAR * (pd->year - 1970)
186 + SEC_PER_DAY * ((pd->year - 1969) / 4)
187 /* month is taken care of later */
188 + SEC_PER_DAY * (pd->day - 1)
189 + SEC_PER_HOUR * pd->hour
190 + SEC_PER_MIN * pd->minute
191 /* seconds are taken care of later */
192 - SEC_PER_MIN * pd->zone;
194 pd->unixtime += pd->second;
195 for (n = pd->month - 1; n > 0; n--)
196 pd->unixtime += SEC_PER_DAY * monthsize[n];
197 if (pd->unixtime < 0) pd->unixtime = -1;
199 else pd->unixtime = -1;
202 /* break_down_unixtime (pd) struct parseddate *pd;
203 * Given the "unixtime" and "zone" fields of a "struct parseddate",
204 * compute the values of the "year", "month", "day", "hour", "min-
205 * ute", "second", and "weekday" fields. The "dst" and "error"
206 * fields of the structure are not used or modified.
208 break_down_unixtime (pd)
209 register struct parseddate *pd;
210 { register unsigned long timevalue;
213 /* Validate the "unixtime" and "zone" fields. */
215 || pd->zone == -1 || pd->zone <= -1440 || pd->zone >= 1440)
216 { /* Sorry, can't do it. */
217 pd->year = -1; pd->month = -1; pd->day = -1;
218 pd->hour = -1; pd->minute = -1; pd->second = -1;
223 /* Even though "pd->unixtime" must be non-negative, and thus cannot
224 * indicate a time earlier than 1970, a negative "pd->zone" could
225 * cause the local date to be Wednesday, 31 December 1969. Such a
226 * date requires special handling.
228 * A local date earlier than 31 December 1969 is impossible because
229 * "pd->zone" must represent a time-zone shift of less than a day.
231 if (pd->zone < 0 && pd->unixtime + SEC_PER_MIN * pd->zone < 0)
232 { pd->year = 1969; pd->month = 12; pd->day = 31;
233 pd->weekday = 3; /* Wednesday */
234 timevalue = pd->unixtime + SEC_PER_MIN * pd->zone + SEC_PER_DAY;
235 /* Note: 0 <= timevalue < SEC_PER_DAY */
236 pd->hour = timevalue / SEC_PER_HOUR;
237 pd->minute = (timevalue % SEC_PER_HOUR) / SEC_PER_MIN;
238 pd->second = timevalue % SEC_PER_MIN;
242 /* Handle the general case (local time is 1970 or later). */
243 timevalue = pd->unixtime + SEC_PER_MIN * pd->zone;
245 /* day of the week (1 January 1970 was a Thursday) . . . */
246 pd->weekday = (timevalue/SEC_PER_DAY + 4 /* Thursday */) % 7;
248 /* year (note that the only possible century year here is 2000,
249 * a leap year -- hence no special tests for century years are
252 for (m = 1970; ; m++)
253 { n = (m%4==0) ? SEC_PER_YEAR+SEC_PER_DAY : SEC_PER_YEAR;
254 if (n > timevalue) break;
258 monthsize[2] = (m%4==0) ? 29 : 28;
262 { n = SEC_PER_DAY * monthsize[m];
263 if (n > timevalue) break;
268 /* day, hour, minute, and second . . . */
269 pd->day = (timevalue / SEC_PER_DAY) + 1;
270 pd->hour = (timevalue % SEC_PER_DAY) / SEC_PER_HOUR;
271 pd->minute = (timevalue % SEC_PER_HOUR) / SEC_PER_MIN;
272 pd->second = timevalue % SEC_PER_MIN;
275 /* char *mail_date_string (pd) struct parseddate *pd;
276 * Generate a character string representing a date and time in the
277 * RFC822 (ARPANET mail standard) format. A value of NULL is re-
278 * turned if "pd" does not contain all necessary data fields.
279 * (NOTE: The returned value points to static data whose contents
280 * are overwritten by each call.)
283 mail_date_string (pd)
284 register struct parseddate *pd;
286 static char answer[50];
288 /* Check the day of the month and compute the day of the week. */
289 compute_unixtime (pd);
291 /* Make sure all required fields are present. */
292 if (pd->year < 0 || pd->month < 0 || pd->day < 0
293 || pd->hour < 0 || pd->minute < 0
294 || pd->zone == -1 || pd->zone <= -1440 || pd->zone >= 1440)
295 return NULL; /* impossible to generate string */
297 /* Generate the answer string. */
299 "%.3s, %d %.3s %d %02d:%02d",
300 dayname + 3*pd->weekday,
301 pd->day, monthname + 3*(pd->month-1),
302 (pd->year >= 1960 && pd->year <= 1999)
303 ? pd->year - 1900 : pd->year,
304 pd->hour, pd->minute);
305 c = answer + strlen (answer);
306 if (pd->second >= 0) sprintf (c, ":%02d", pd->second), c += 3;
309 { /* NOTE: Only zone abbreviations in RFC822 are used here. */
310 case 0: strcpy (c, pd->dst ? "+0000" : "GMT"); break;
311 case -240: strcpy (c, pd->dst ? "EDT" : "-0400"); break;
312 case -300: strcpy (c, pd->dst ? "CDT" : "EST"); break;
313 case -360: strcpy (c, pd->dst ? "MDT" : "CST"); break;
314 case -420: strcpy (c, pd->dst ? "PDT" : "MST"); break;
315 case -480: strcpy (c, pd->dst ? "-0800" : "PST"); break;
318 sprintf (c, "+%02d%02d", pd->zone/60, pd->zone%60);
319 else sprintf (c, "-%02d%02d", -pd->zone/60, -pd->zone%60);
325 /* char *uucp_date_string (pd) struct parseddate *pd;
326 * Generate a character string representing a date and time in the
327 * UUCP mail format. A value of NULL is returned if "pd" does not
328 * contain all necessary data fields. (NOTE: The returned value
329 * points to static data whose contents are overwritten by each
333 uucp_date_string (pd)
334 register struct parseddate *pd;
336 static char answer[50];
338 /* Check the day of the month and compute the day of the week. */
339 compute_unixtime (pd);
341 /* Make sure all required fields are present. */
342 if (pd->year < 0 || pd->month < 0 || pd->day < 0
343 || pd->hour < 0 || pd->minute < 0
344 || pd->zone == -1 || pd->zone <= -1440 || pd->zone >= 1440)
345 return NULL; /* impossible to generate string */
347 /* Generate the answer string. */
349 "%.3s %.3s %d %02d:%02d",
350 dayname + 3*pd->weekday,
351 monthname + 3*(pd->month-1), pd->day,
352 pd->hour, pd->minute);
353 c = answer + strlen (answer);
354 if (pd->second >= 0) sprintf (c, ":%02d", pd->second), c += 3;
356 { /* NOTE: Only zone abbreviations in RFC822 are used here. */
357 case 0: strcpy (c, pd->dst ? "+0000" : "-GMT"); break;
358 case -240: strcpy (c, pd->dst ? "-EDT" : "-0400"); break;
359 case -300: strcpy (c, pd->dst ? "-CDT" : "-EST"); break;
360 case -360: strcpy (c, pd->dst ? "-MDT" : "-CST"); break;
361 case -420: strcpy (c, pd->dst ? "-PDT" : "-MST"); break;
362 case -480: strcpy (c, pd->dst ? "-0800" : "-PST"); break;
365 sprintf (c, "+%02d%02d", pd->zone/60, pd->zone%60);
366 else sprintf (c, "-%02d%02d", -pd->zone/60, -pd->zone%60);
368 c = answer + strlen (answer);
369 sprintf (c, " %d", pd->year);