chiark / gitweb /
Doxygen file headers for most files
[disorder] / lib / xgetdate.c
1 /** @file lib/xgetdate.c
2  * @brief Date parsing
3  *
4  * Derived from getdate.c in glibc 2.3.6.  This is pretty much
5  * standard getdate() except that you supply the template in an
6  * argument, rather than messing around with environment variables and
7  * files.
8  */
9
10 /* Convert a string representation of time to a time value.
11    Copyright (C) 1997,1998,1999,2000,2001,2003 Free Software Foundation, Inc.
12    This file is part of the GNU C Library.
13    Contributed by Mark Kettenis <kettenis@phys.uva.nl>, 1997.
14
15    The GNU C Library is free software; you can redistribute it and/or
16    modify it under the terms of the GNU Lesser General Public
17    License as published by the Free Software Foundation; either
18    version 2.1 of the License, or (at your option) any later version.
19
20    The GNU C Library is distributed in the hope that it will be useful,
21    but WITHOUT ANY WARRANTY; without even the implied warranty of
22    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
23    Lesser General Public License for more details.
24
25    You should have received a copy of the GNU Lesser General Public
26    License along with the GNU C Library; if not, write to the Free
27    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
28    02111-1307 USA.  */
29
30 #define _GNU_SOURCE 1           /* to expose strptime */
31 #include <limits.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <time.h>
36
37 #include "dateparse.h"
38
39 #define TM_YEAR_BASE 1900
40
41
42 /* Prototypes for local functions.  */
43 static int first_wday (int year, int mon, int wday);
44 static int check_mday (int year, int mon, int mday);
45
46 # define isleap(year)   \
47   ((year) % 4 == 0 && ((year) % 100 != 0 || (year) % 400 == 0))
48
49 /* Set to one of the following values to indicate an error.
50      1  the DATEMSK environment variable is null or undefined,
51      2  the template file cannot be opened for reading,
52      3  failed to get file status information,
53      4  the template file is not a regular file,
54      5  an error is encountered while reading the template file,
55      6  memory allication failed (not enough memory available),
56      7  there is no line in the template that matches the input,
57      8  invalid input specification Example: February 31 or a time is
58         specified that can not be represented in a time_t (representing
59         the time in seconds since 00:00:00 UTC, January 1, 1970) */
60 /*int xgetdate_err;*/
61
62
63 /* Returns the first weekday WDAY of month MON in the year YEAR.  */
64 static int
65 first_wday (int year, int mon, int wday)
66 {
67   struct tm tm;
68
69   if (wday == INT_MIN)
70     return 1;
71
72   memset (&tm, 0, sizeof (struct tm));
73   tm.tm_year = year;
74   tm.tm_mon = mon;
75   tm.tm_mday = 1;
76   mktime (&tm);
77
78   return (1 + (wday - tm.tm_wday + 7) % 7);
79 }
80
81
82 /* Returns 1 if MDAY is a valid day of the month in month MON of year
83    YEAR, and 0 if it is not.  */
84 static int
85 check_mday (int year, int mon, int mday)
86 {
87   switch (mon)
88     {
89     case 0:
90     case 2:
91     case 4:
92     case 6:
93     case 7:
94     case 9:
95     case 11:
96       if (mday >= 1 && mday <= 31)
97         return 1;
98       break;
99     case 3:
100     case 5:
101     case 8:
102     case 10:
103       if (mday >= 1 && mday <= 30)
104         return 1;
105       break;
106     case 1:
107       if (mday >= 1 && mday <= (isleap (year) ? 29 : 28))
108         return 1;
109       break;
110     }
111
112   return 0;
113 }
114
115
116 int
117 xgetdate_r (const char *string, struct tm *tp,
118             const char *const *template)
119 {
120   const char *line;
121   size_t len;
122   char *result = NULL;
123   time_t timer;
124   struct tm tm;
125   int mday_ok = 0;
126
127   line = NULL;
128   len = 0;
129   while((line = *template++))
130     {
131       /* Do the conversion.  */
132       tp->tm_year = tp->tm_mon = tp->tm_mday = tp->tm_wday = INT_MIN;
133       tp->tm_hour = tp->tm_sec = tp->tm_min = INT_MIN;
134       tp->tm_isdst = -1;
135       tp->tm_gmtoff = 0;
136       tp->tm_zone = NULL;
137       result = strptime (string, line, tp);
138       if (result && *result == '\0')
139         break;
140     }
141
142   if (result == NULL || *result != '\0')
143     return 7;
144
145   /* Get current time.  */
146   time (&timer);
147   localtime_r (&timer, &tm);
148
149   /* If only the weekday is given, today is assumed if the given day
150      is equal to the current day and next week if it is less.  */
151   if (tp->tm_wday >= 0 && tp->tm_wday <= 6 && tp->tm_year == INT_MIN
152       && tp->tm_mon == INT_MIN && tp->tm_mday == INT_MIN)
153     {
154       tp->tm_year = tm.tm_year;
155       tp->tm_mon = tm.tm_mon;
156       tp->tm_mday = tm.tm_mday + (tp->tm_wday - tm.tm_wday + 7) % 7;
157       mday_ok = 1;
158     }
159
160   /* If only the month is given, the current month is assumed if the
161      given month is equal to the current month and next year if it is
162      less and no year is given (the first day of month is assumed if
163      no day is given.  */
164   if (tp->tm_mon >= 0 && tp->tm_mon <= 11 && tp->tm_mday == INT_MIN)
165     {
166       if (tp->tm_year == INT_MIN)
167         tp->tm_year = tm.tm_year + (((tp->tm_mon - tm.tm_mon) < 0) ? 1 : 0);
168       tp->tm_mday = first_wday (tp->tm_year, tp->tm_mon, tp->tm_wday);
169       mday_ok = 1;
170     }
171
172   /* If no hour, minute and second are given the current hour, minute
173      and second are assumed.  */
174   if (tp->tm_hour == INT_MIN && tp->tm_min == INT_MIN && tp->tm_sec == INT_MIN)
175     {
176       tp->tm_hour = tm.tm_hour;
177       tp->tm_min = tm.tm_min;
178       tp->tm_sec = tm.tm_sec;
179     }
180
181   /* If no date is given, today is assumed if the given hour is
182      greater than the current hour and tomorrow is assumed if
183      it is less.  */
184   if (tp->tm_hour >= 0 && tp->tm_hour <= 23
185       && tp->tm_year == INT_MIN && tp->tm_mon == INT_MIN
186       && tp->tm_mday == INT_MIN && tp->tm_wday == INT_MIN)
187     {
188       tp->tm_year = tm.tm_year;
189       tp->tm_mon = tm.tm_mon;
190       tp->tm_mday = tm.tm_mday + ((tp->tm_hour - tm.tm_hour) < 0 ? 1 : 0);
191       mday_ok = 1;
192     }
193
194   /* Fill in the gaps.  */
195   if (tp->tm_year == INT_MIN)
196     tp->tm_year = tm.tm_year;
197   if (tp->tm_hour == INT_MIN)
198     tp->tm_hour = 0;
199   if (tp->tm_min == INT_MIN)
200     tp->tm_min = 0;
201   if (tp->tm_sec == INT_MIN)
202     tp->tm_sec = 0;
203
204   /* Check if the day of month is within range, and if the time can be
205      represented in a time_t.  We make use of the fact that the mktime
206      call normalizes the struct tm.  */
207   if ((!mday_ok && !check_mday (TM_YEAR_BASE + tp->tm_year, tp->tm_mon,
208                                 tp->tm_mday))
209       || mktime (tp) == (time_t) -1)
210     return 8;
211
212   return 0;
213 }
214
215
216 #if 0
217 struct tm *
218   xgetdate (const char *string, const char *const *template)
219 {
220   /* Buffer returned by getdate.  */
221   static struct tm tmbuf;
222   int errval = xgetdate_r (string, &tmbuf, template);
223
224   if (errval != 0)
225     {
226       xgetdate_err = errval;
227       return NULL;
228     }
229
230   return &tmbuf;
231 }
232 #endif