Bugfix: conversion of an empty string to a date or hour object
[AXE.git] / src / parsedate.c
1
2 /*
3  * 
4  * Purpose:
5  *
6  *     Manipulate character strings representing dates.
7  *
8  * Usage:
9  *
10  *     #include <parsedate.h>
11  *
12  *     char date;
13  *     struct parseddate *pd;
14  *
15  *     pd = parsedate (date);
16  *
17  *     compute_unixtime (pd);
18  *
19  *     break_down_unixtime (pd);
20  *
21  *     date = mail_date_string (pd);
22  *
23  *     date = uucp_date_string (pd);
24  *
25  * Notes:
26  *
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.
30  *
31  *     "compute_unixtime" is implicitly called by "parsedate".
32  *
33  * Global contents:
34  *
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.
38  *    
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.
42  *    
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".
46  *    
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.
50  *    
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.
54  *
55  * Local contents:
56  *
57  *     None.
58  */
59
60 #include <stdio.h>
61 #include "parsedate.h"
62
63 static char *RCSID = "$Id: parsedate.c,v 1.2 2002-09-28 06:58:45 arjen Exp $";
64
65 extern int yyparse();
66
67 compute_unixtime (register struct parseddate *pd);
68
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)
74
75 /* Number of days in each month. */
76 static int monthsize[] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
77
78 /* Three-letter abbreviations of month and day names. */
79 static char monthname[] = "JanFebMarAprMayJunJulAugSepOctNovDec";
80 static char dayname[]   = "SunMonTueWedThuFriSat";
81
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.)
87  */
88 struct parseddate * parsedate (char *date)
89 {   register char *c;
90     register int year_save;
91     extern struct parseddate yyans;
92     extern char *yyinbuf;
93
94     /* Initialize the returned-value structure. */
95     yyans.unixtime  = -1;
96     yyans.year      = -1;
97     yyans.month     = -1;
98     yyans.day       = -1;
99     yyans.hour      = -1;
100     yyans.minute    = -1;
101     yyans.second    = -1;
102     yyans.zone      = -1;
103     yyans.dst       = -1;
104     yyans.weekday   = -1;
105     yyans.c_weekday = -1;
106     yyans.error     =  NULL;
107
108     /* Parse the argument string. */
109     yyinbuf = date;
110
111     if (yyparse () != 0 && yyans.error == NULL)
112       yyans.error = yyinbuf;
113
114     /* Validate the day of the month, compute/validate the day of the
115      * week, and compute the internal UNIX form of the time.  See if
116      * "compute_unixtime" found fault with the year or the day of the
117      * month.  (Note that we have to remember the original "year" value
118      * because it might legitimately have been -1 to begin with.)
119      */
120     year_save = yyans.year; compute_unixtime (&yyans);
121     if (yyans.error == NULL
122         && (yyans.year != year_save
123             || (yyans.month > 0 && yyans.day < 0)
124             || (yyans.month < 0 && yyans.day > 0)))
125         yyans.error = yyinbuf;
126
127     return &yyans;
128 }
129
130 /* compute_unixtime (pd) struct parseddate *pd;
131  *     Given a mostly filled-in "struct parseddate", compute the day of
132  *     the week and the internal UNIX representation of the date.
133  *
134  *     A year before 1600 will be rejected and replaced with -1.  A
135  *     date from 1600 on which falls outside the range representable in
136  *     internal UNIX form will still have the correct day of the week
137  *     computed.
138  *
139  *     The day of the week is always computed on the assumption that the
140  *     Gregorian calendar is in use.  Days of the week for dates in the
141  *     far future may turn out to be incorrect if any changes are made
142  *     to the calendar between now and then.
143  */
144 compute_unixtime (register struct parseddate *pd)
145 {
146     register int weekday, n, l, a;
147
148     /* Validate the year. */
149     if (pd->year >= 0 && pd->year < 1600) pd->year = -1;
150
151     /* Validate the day of the month.  Also calculate the number of days
152      * in February (even if this is not February, we will need the num-
153      * ber of days in February later on when computing the UNIX time).
154      */
155     if (pd->month > 0)
156     {   if      (pd->year < 0)        monthsize[2] = 29;
157         else if (pd->year %   4 != 0) monthsize[2] = 28;
158         else if (pd->year % 100 != 0) monthsize[2] = 29;
159         else if (pd->year % 400 != 0) monthsize[2] = 28;
160         else                          monthsize[2] = 29;
161         if (pd->day <= 0 || pd->day > monthsize[pd->month])
162             pd->day = -1;
163     }
164
165     /* Compute the day of the week.  The next several lines constitute a
166      * perpetual-calendar formula.  Note, of course, that the "claimed"
167      * day of the week (pd->c_weekday) is ignored here.
168      */
169     if (pd->year > 0 && pd->month > 0 && pd->day > 0)
170     {   if (pd->month >= 3) n = pd->year / 100,
171                             l = pd->year % 100;
172         else                n = (pd->year-1) / 100,
173                             l = (pd->year-1) % 100;
174         a = (26 * ((pd->month+9)%12 + 1) - 2) / 10;
175         weekday = (a+(l>>2)+(n>>2)+l-(n+n)+pd->day);
176         while (weekday < 0) weekday += 7;
177         pd->weekday = weekday % 7;
178     }
179
180     /* Figure out the internal UNIX form of the date. */
181     if (pd->year >= 1969 && pd->year <= 2038
182         && pd->month > 0 && pd->day > 0
183         && pd->hour >= 0 && pd->minute >= 0
184         && pd->zone != -1 && pd->zone > -1440 && pd->zone < 1440)
185     {   pd->unixtime =
186               SEC_PER_YEAR * (pd->year - 1970)
187             + SEC_PER_DAY  * ((pd->year - 1969) / 4)
188             /* month is taken care of later */
189             + SEC_PER_DAY  * (pd->day - 1)
190             + SEC_PER_HOUR * pd->hour
191             + SEC_PER_MIN  * pd->minute
192             /* seconds are taken care of later */
193             - SEC_PER_MIN  * pd->zone;
194         if (pd->second >= 0)
195             pd->unixtime += pd->second;
196         for (n = pd->month - 1; n > 0; n--)
197             pd->unixtime += SEC_PER_DAY * monthsize[n];
198         if (pd->unixtime < 0) pd->unixtime = -1;
199     }
200     else pd->unixtime = -1;
201 }
202
203 /* break_down_unixtime (pd) struct parseddate *pd;
204  *     Given the "unixtime" and "zone" fields of a "struct parseddate",
205  *     compute the values of the "year", "month", "day", "hour", "min-
206  *     ute", "second", and "weekday" fields.  The "dst" and "error"
207  *     fields of the structure are not used or modified.
208  */
209 break_down_unixtime (pd)
210     register struct parseddate *pd;
211 {   register unsigned long timevalue;
212     register int m, n;
213
214     /* Validate the "unixtime" and "zone" fields. */
215     if (pd->unixtime < 0
216         || pd->zone == -1 || pd->zone <= -1440 || pd->zone >= 1440)
217     {   /* Sorry, can't do it. */
218         pd->year = -1; pd->month = -1; pd->day = -1;
219         pd->hour = -1; pd->minute = -1; pd->second = -1;
220         pd->weekday = -1;
221         return;
222     }
223
224     /* Even though "pd->unixtime" must be non-negative, and thus cannot
225      * indicate a time earlier than 1970, a negative "pd->zone" could
226      * cause the local date to be Wednesday, 31 December 1969.  Such a
227      * date requires special handling.
228      *
229      * A local date earlier than 31 December 1969 is impossible because
230      * "pd->zone" must represent a time-zone shift of less than a day.
231      */
232     if (pd->zone < 0 && pd->unixtime + SEC_PER_MIN * pd->zone < 0)
233     {   pd->year = 1969; pd->month = 12; pd->day = 31;
234         pd->weekday = 3;    /* Wednesday */
235         timevalue = pd->unixtime + SEC_PER_MIN * pd->zone + SEC_PER_DAY;
236         /* Note:  0 <= timevalue < SEC_PER_DAY */
237         pd->hour = timevalue / SEC_PER_HOUR;
238         pd->minute = (timevalue % SEC_PER_HOUR) / SEC_PER_MIN;
239         pd->second = timevalue % SEC_PER_MIN;
240         return;
241     }
242
243     /* Handle the general case (local time is 1970 or later). */
244     timevalue = pd->unixtime + SEC_PER_MIN * pd->zone;
245
246     /* day of the week (1 January 1970 was a Thursday) . . . */
247     pd->weekday = (timevalue/SEC_PER_DAY + 4 /* Thursday */) % 7;
248
249     /* year (note that the only possible century year here is 2000,
250      * a leap year -- hence no special tests for century years are
251      * needed) . . .
252      */
253     for (m = 1970; ; m++)
254     {   n = (m%4==0) ? SEC_PER_YEAR+SEC_PER_DAY : SEC_PER_YEAR;
255         if (n > timevalue) break;
256         timevalue -= n;
257     }
258     pd->year = m;
259     monthsize[2] = (m%4==0) ? 29 : 28;
260
261     /* month . . . */
262     for (m = 1; ; m++)
263     {   n = SEC_PER_DAY * monthsize[m];
264         if (n > timevalue) break;
265         timevalue -= n;
266     }
267     pd->month = m;
268
269     /* day, hour, minute, and second . . . */
270     pd->day    = (timevalue / SEC_PER_DAY) + 1;
271     pd->hour   = (timevalue % SEC_PER_DAY) / SEC_PER_HOUR;
272     pd->minute = (timevalue % SEC_PER_HOUR) / SEC_PER_MIN;
273     pd->second = timevalue % SEC_PER_MIN;
274 }
275
276 /* char *mail_date_string (pd) struct parseddate *pd;
277  *     Generate a character string representing a date and time in the
278  *     RFC822 (ARPANET mail standard) format.  A value of NULL is re-
279  *     turned if "pd" does not contain all necessary data fields.
280  *     (NOTE:  The returned value points to static data whose contents
281  *     are overwritten by each call.)
282  */
283 char *
284 mail_date_string (pd)
285     register struct parseddate *pd;
286 {   register char *c;
287     static char answer[50];
288
289     /* Check the day of the month and compute the day of the week. */
290     compute_unixtime (pd);
291
292     /* Make sure all required fields are present. */
293     if (pd->year < 0 || pd->month < 0 || pd->day < 0
294         || pd->hour < 0 || pd->minute < 0
295         || pd->zone == -1 || pd->zone <= -1440 || pd->zone >= 1440)
296         return NULL;            /* impossible to generate string */
297
298     /* Generate the answer string. */
299     sprintf (answer,
300              "%.3s, %d %.3s %d %02d:%02d",
301              dayname + 3*pd->weekday,
302              pd->day, monthname + 3*(pd->month-1),
303              (pd->year >= 1960 && pd->year <= 1999)
304                  ? pd->year - 1900 : pd->year,
305              pd->hour, pd->minute);
306     c = answer + strlen (answer);
307     if (pd->second >= 0) sprintf (c, ":%02d", pd->second), c += 3;
308     *c++ = ' ';
309     switch (pd->zone)
310     {   /* NOTE:  Only zone abbreviations in RFC822 are used here. */
311         case    0: strcpy (c, pd->dst ? "+0000" : "GMT");   break;
312         case -240: strcpy (c, pd->dst ? "EDT"   : "-0400"); break;
313         case -300: strcpy (c, pd->dst ? "CDT"   : "EST");   break;
314         case -360: strcpy (c, pd->dst ? "MDT"   : "CST");   break;
315         case -420: strcpy (c, pd->dst ? "PDT"   : "MST");   break;
316         case -480: strcpy (c, pd->dst ? "-0800" : "PST");   break;
317         default:
318             if (pd->zone >= 0)
319                  sprintf (c, "+%02d%02d",  pd->zone/60,  pd->zone%60);
320             else sprintf (c, "-%02d%02d", -pd->zone/60, -pd->zone%60);
321     }
322
323     return answer;
324 }
325
326 /* char *uucp_date_string (pd) struct parseddate *pd;
327  *     Generate a character string representing a date and time in the
328  *     UUCP mail format.  A value of NULL is returned if "pd" does not
329  *     contain all necessary data fields.  (NOTE:  The returned value
330  *     points to static data whose contents are overwritten by each
331  *     call.)
332  */
333 char *
334 uucp_date_string (pd)
335     register struct parseddate *pd;
336 {   register char *c;
337     static char answer[50];
338
339     /* Check the day of the month and compute the day of the week. */
340     compute_unixtime (pd);
341
342     /* Make sure all required fields are present. */
343     if (pd->year < 0 || pd->month < 0 || pd->day < 0
344         || pd->hour < 0 || pd->minute < 0
345         || pd->zone == -1 || pd->zone <= -1440 || pd->zone >= 1440)
346         return NULL;            /* impossible to generate string */
347
348     /* Generate the answer string. */
349     sprintf (answer,
350              "%.3s %.3s %d %02d:%02d",
351              dayname + 3*pd->weekday,
352              monthname + 3*(pd->month-1), pd->day,
353              pd->hour, pd->minute);
354     c = answer + strlen (answer);
355     if (pd->second >= 0) sprintf (c, ":%02d", pd->second), c += 3;
356     switch (pd->zone)
357     {   /* NOTE:  Only zone abbreviations in RFC822 are used here. */
358         case    0: strcpy (c, pd->dst ? "+0000" : "-GMT");   break;
359         case -240: strcpy (c, pd->dst ? "-EDT"  : "-0400"); break;
360         case -300: strcpy (c, pd->dst ? "-CDT"  : "-EST");   break;
361         case -360: strcpy (c, pd->dst ? "-MDT"  : "-CST");   break;
362         case -420: strcpy (c, pd->dst ? "-PDT"  : "-MST");   break;
363         case -480: strcpy (c, pd->dst ? "-0800" : "-PST");   break;
364         default:
365             if (pd->zone >= 0)
366                  sprintf (c, "+%02d%02d",  pd->zone/60,  pd->zone%60);
367             else sprintf (c, "-%02d%02d", -pd->zone/60, -pd->zone%60);
368     }
369     c = answer + strlen (answer);
370     sprintf (c, " %d", pd->year);
371
372     return answer;
373 }