2 * SPDX-License-Identifier: BSD-2-Clause
5 * David L. Nugent. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY DAVID L. NUGENT AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL DAVID L. NUGENT OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 numerics(char const * str)
42 return (str[strspn(str, "0123456789x")] == '\0');
46 aindex(char const * arr[], char const ** str, int len)
52 l = strlen(strncpy(mystr, *str, len));
53 for (i = 0; i < l; i++)
54 mystr[i] = (char) tolower((unsigned char)mystr[i]);
55 for (i = 0; arr[i] && strcmp(mystr, arr[i]) != 0; i++);
58 else { /* Skip past it */
59 while (**str && isalpha((unsigned char)**str))
61 /* And any following whitespace */
62 while (**str && (**str == ',' || isspace((unsigned char)**str)))
69 weekday(char const ** str)
71 static char const *days[] =
72 {"sun", "mon", "tue", "wed", "thu", "fri", "sat", NULL};
74 return aindex(days, str, 3);
78 parse_datesub(char const * str, struct tm *t)
84 const char *valid_formats[] = {
109 "%H:%M\t%S %d-%b-%y",
110 "%H:%M\t%S %d-%b-%Y",
111 "%H:%M\t%S %d-%m-%y",
112 "%H:%M\t%S %d-%m-%Y",
117 "%d-%b-%y\t%H:%M:%S",
118 "%d-%b-%Y\t%H:%M:%S",
119 "%d-%m-%y\t%H:%M:%S",
120 "%d-%m-%Y\t%H:%M:%S",
124 l = newlocale(LC_ALL_MASK, "C", NULL);
126 for (i=0; valid_formats[i] != NULL; i++) {
127 memset(&tm, 0, sizeof(tm));
128 ret = strptime_l(str, valid_formats[i], &tm, l);
129 if (ret && *ret == '\0') {
130 t->tm_mday = tm.tm_mday;
131 t->tm_mon = tm.tm_mon;
132 t->tm_year = tm.tm_year;
133 t->tm_hour = tm.tm_hour;
134 t->tm_min = tm.tm_min;
135 t->tm_sec = tm.tm_sec;
143 errx(EXIT_FAILURE, "Invalid date");
148 * Parse time must be flexible, it handles the following formats:
149 * nnnnnnnnnnn UNIX timestamp (all numeric), 0 = now
150 * 0xnnnnnnnn UNIX timestamp in hexadecimal
151 * 0nnnnnnnnn UNIX timestamp in octal
153 * +nnnn[smhdwoy] Given time + nnnn hours, mins, days, weeks, months or years
154 * -nnnn[smhdwoy] Given time - nnnn hours, mins, days, weeks, months or years
155 * dd[ ./-]mmm[ ./-]yy Date }
156 * hh:mm:ss Time } May be combined
160 parse_date(time_t dt, char const * str)
170 while (*str && isspace((unsigned char)*str))
174 dt = strtol(str, &p, 0);
175 } else if (*str == '+' || *str == '-') {
176 val = strtol(str, &p, 0);
179 case 'H': /* hours */
184 case 'M': /* minutes */
188 case 'S': /* seconds */
193 dt += (val * 86400L);
196 case 'W': /* weeks */
197 dt += (val * 604800L);
200 case 'O': /* months */
202 T->tm_mon += (int) val;
206 case 'Y': /* years */
208 T->tm_year += (int) val;
213 if (T->tm_mday != i) {
216 dt -= (time_t) 86400L;
218 default: /* unknown */
219 break; /* leave untouched */
225 * Skip past any weekday prefix
228 strlcpy(tmp, str, sizeof(tmp));
233 * See if we can break off any timezone
235 while ((q = strrchr(tmp, ' ')) != NULL) {
236 if (strchr("(+-", q[1]) != NULL)
241 while (q[j] && isupper((unsigned char)q[j]))
250 parse_datesub(tmp, T);