1 /** @file lib/xgetdate.c
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
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.
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.
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.
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
30 #define _GNU_SOURCE 1 /* to expose strptime */
37 #include "dateparse.h"
40 #define TM_YEAR_BASE 1900
43 /* Prototypes for local functions. */
44 static int first_wday (int year, int mon, int wday);
45 static int check_mday (int year, int mon, int mday);
47 # define isleap(year) \
48 ((year) % 4 == 0 && ((year) % 100 != 0 || (year) % 400 == 0))
50 /* Set to one of the following values to indicate an error.
51 1 the DATEMSK environment variable is null or undefined,
52 2 the template file cannot be opened for reading,
53 3 failed to get file status information,
54 4 the template file is not a regular file,
55 5 an error is encountered while reading the template file,
56 6 memory allication failed (not enough memory available),
57 7 there is no line in the template that matches the input,
58 8 invalid input specification Example: February 31 or a time is
59 specified that can not be represented in a time_t (representing
60 the time in seconds since 00:00:00 UTC, January 1, 1970) */
64 /* Returns the first weekday WDAY of month MON in the year YEAR. */
66 first_wday (int year, int mon, int wday)
73 memset (&tm, 0, sizeof (struct tm));
79 return (1 + (wday - tm.tm_wday + 7) % 7);
83 /* Returns 1 if MDAY is a valid day of the month in month MON of year
84 YEAR, and 0 if it is not. */
86 check_mday (int year, int mon, int mday)
97 if (mday >= 1 && mday <= 31)
104 if (mday >= 1 && mday <= 30)
108 if (mday >= 1 && mday <= (isleap (year) ? 29 : 28))
118 xgetdate_r (const char *string, struct tm *tp,
119 const char *const *template)
130 while((line = *template++))
132 /* Do the conversion. */
133 tp->tm_year = tp->tm_mon = tp->tm_mday = tp->tm_wday = INT_MIN;
134 tp->tm_hour = tp->tm_sec = tp->tm_min = INT_MIN;
138 result = my_strptime (string, line, tp);
139 if (result && *result == '\0')
143 if (result == NULL || *result != '\0')
146 /* Get current time. */
148 localtime_r (&timer, &tm);
150 /* If only the weekday is given, today is assumed if the given day
151 is equal to the current day and next week if it is less. */
152 if (tp->tm_wday >= 0 && tp->tm_wday <= 6 && tp->tm_year == INT_MIN
153 && tp->tm_mon == INT_MIN && tp->tm_mday == INT_MIN)
155 tp->tm_year = tm.tm_year;
156 tp->tm_mon = tm.tm_mon;
157 tp->tm_mday = tm.tm_mday + (tp->tm_wday - tm.tm_wday + 7) % 7;
161 /* If only the month is given, the current month is assumed if the
162 given month is equal to the current month and next year if it is
163 less and no year is given (the first day of month is assumed if
165 if (tp->tm_mon >= 0 && tp->tm_mon <= 11 && tp->tm_mday == INT_MIN)
167 if (tp->tm_year == INT_MIN)
168 tp->tm_year = tm.tm_year + (((tp->tm_mon - tm.tm_mon) < 0) ? 1 : 0);
169 tp->tm_mday = first_wday (tp->tm_year, tp->tm_mon, tp->tm_wday);
173 /* If no hour, minute and second are given the current hour, minute
174 and second are assumed. */
175 if (tp->tm_hour == INT_MIN && tp->tm_min == INT_MIN && tp->tm_sec == INT_MIN)
177 tp->tm_hour = tm.tm_hour;
178 tp->tm_min = tm.tm_min;
179 tp->tm_sec = tm.tm_sec;
182 /* If no date is given, today is assumed if the given hour is
183 greater than the current hour and tomorrow is assumed if
185 if (tp->tm_hour >= 0 && tp->tm_hour <= 23
186 && tp->tm_year == INT_MIN && tp->tm_mon == INT_MIN
187 && tp->tm_mday == INT_MIN && tp->tm_wday == INT_MIN)
189 tp->tm_year = tm.tm_year;
190 tp->tm_mon = tm.tm_mon;
191 tp->tm_mday = tm.tm_mday + ((tp->tm_hour - tm.tm_hour) < 0 ? 1 : 0);
195 /* Fill in the gaps. */
196 if (tp->tm_year == INT_MIN)
197 tp->tm_year = tm.tm_year;
198 if (tp->tm_hour == INT_MIN)
200 if (tp->tm_min == INT_MIN)
202 if (tp->tm_sec == INT_MIN)
205 /* Check if the day of month is within range, and if the time can be
206 represented in a time_t. We make use of the fact that the mktime
207 call normalizes the struct tm. */
208 if ((!mday_ok && !check_mday (TM_YEAR_BASE + tp->tm_year, tp->tm_mon,
210 || mktime (tp) == (time_t) -1)
219 xgetdate (const char *string, const char *const *template)
221 /* Buffer returned by getdate. */
222 static struct tm tmbuf;
223 int errval = xgetdate_r (string, &tmbuf, template);
227 xgetdate_err = errval;