3 * David L. Nugent. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY DAVID L. NUGENT AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL DAVID L. NUGENT OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 static const char rcsid[] =
43 numerics(char const * str)
45 int rc = isdigit((unsigned char)*str);
48 while (isdigit((unsigned char)*str) || *str == 'x')
54 aindex(char const * arr[], char const ** str, int len)
60 l = strlen(strncpy(mystr, *str, len));
61 for (i = 0; i < l; i++)
62 mystr[i] = (char) tolower((unsigned char)mystr[i]);
63 for (i = 0; arr[i] && strcmp(mystr, arr[i]) != 0; i++);
66 else { /* Skip past it */
67 while (**str && isalpha((unsigned char)**str))
69 /* And any following whitespace */
70 while (**str && (**str == ',' || isspace((unsigned char)**str)))
77 weekday(char const ** str)
79 static char const *days[] =
80 {"sun", "mon", "tue", "wed", "thu", "fri", "sat", NULL};
82 return aindex(days, str, 3);
86 parse_datesub(char const * str, struct tm *t)
92 const char *valid_formats[] = {
117 "%H:%M\t%S %d-%b-%y",
118 "%H:%M\t%S %d-%b-%Y",
119 "%H:%M\t%S %d-%m-%y",
120 "%H:%M\t%S %d-%m-%Y",
125 "%d-%b-%y\t%H:%M:%S",
126 "%d-%b-%Y\t%H:%M:%S",
127 "%d-%m-%y\t%H:%M:%S",
128 "%d-%m-%Y\t%H:%M:%S",
132 l = newlocale(LC_ALL_MASK, "C", NULL);
134 memset(&tm, 0, sizeof(tm));
135 for (i=0; valid_formats[i] != NULL; i++) {
136 ret = strptime_l(str, valid_formats[i], &tm, l);
137 if (ret && *ret == '\0') {
138 t->tm_mday = tm.tm_mday;
139 t->tm_mon = tm.tm_mon;
140 t->tm_year = tm.tm_year;
141 t->tm_hour = tm.tm_hour;
142 t->tm_min = tm.tm_min;
143 t->tm_sec = tm.tm_sec;
151 errx(EXIT_FAILURE, "Invalid date");
156 * Parse time must be flexible, it handles the following formats:
157 * nnnnnnnnnnn UNIX timestamp (all numeric), 0 = now
158 * 0xnnnnnnnn UNIX timestamp in hexadecimal
159 * 0nnnnnnnnn UNIX timestamp in octal
161 * +nnnn[smhdwoy] Given time + nnnn hours, mins, days, weeks, months or years
162 * -nnnn[smhdwoy] Given time - nnnn hours, mins, days, weeks, months or years
163 * dd[ ./-]mmm[ ./-]yy Date }
164 * hh:mm:ss Time } May be combined
168 parse_date(time_t dt, char const * str)
178 while (*str && isspace((unsigned char)*str))
182 dt = strtol(str, &p, 0);
183 } else if (*str == '+' || *str == '-') {
184 val = strtol(str, &p, 0);
187 case 'H': /* hours */
192 case 'M': /* minutes */
196 case 'S': /* seconds */
201 dt += (val * 86400L);
204 case 'W': /* weeks */
205 dt += (val * 604800L);
208 case 'O': /* months */
210 T->tm_mon += (int) val;
214 case 'Y': /* years */
216 T->tm_year += (int) val;
221 if (T->tm_mday != i) {
224 dt -= (time_t) 86400L;
226 default: /* unknown */
227 break; /* leave untouched */
233 * Skip past any weekday prefix
236 strlcpy(tmp, str, sizeof(tmp));
241 * See if we can break off any timezone
243 while ((q = strrchr(tmp, ' ')) != NULL) {
244 if (strchr("(+-", q[1]) != NULL)
249 while (q[j] && isupper((unsigned char)q[j]))
258 parse_datesub(tmp, T);