1996-12-09 14:05:35 +00:00
|
|
|
/*-
|
1996-12-10 23:59:04 +00:00
|
|
|
* Copyright (C) 1996
|
|
|
|
* David L. Nugent. All rights reserved.
|
1996-12-09 14:05:35 +00:00
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
1996-12-10 23:59:04 +00:00
|
|
|
* notice, this list of conditions and the following disclaimer.
|
1996-12-09 14:05:35 +00:00
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
1996-12-10 23:59:04 +00:00
|
|
|
* THIS SOFTWARE IS PROVIDED BY DAVID L. NUGENT AND CONTRIBUTORS ``AS IS'' AND
|
1996-12-09 14:05:35 +00:00
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
1996-12-10 23:59:04 +00:00
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL DAVID L. NUGENT OR CONTRIBUTORS BE LIABLE
|
1996-12-09 14:05:35 +00:00
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
1997-10-10 06:23:42 +00:00
|
|
|
#ifndef lint
|
|
|
|
static const char rcsid[] =
|
1999-08-28 01:35:59 +00:00
|
|
|
"$FreeBSD$";
|
1997-10-10 06:23:42 +00:00
|
|
|
#endif /* not lint */
|
|
|
|
|
2015-08-02 13:22:46 +00:00
|
|
|
#include <ctype.h>
|
|
|
|
#include <err.h>
|
1996-12-09 14:05:35 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2015-07-05 09:48:03 +00:00
|
|
|
#include <xlocale.h>
|
1996-12-09 14:05:35 +00:00
|
|
|
|
|
|
|
#include "psdate.h"
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
numerics(char const * str)
|
|
|
|
{
|
|
|
|
|
2017-01-22 20:03:18 +00:00
|
|
|
return (str[strspn(str, "0123456789x")] == '\0');
|
1996-12-09 14:05:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
aindex(char const * arr[], char const ** str, int len)
|
|
|
|
{
|
|
|
|
int l, i;
|
|
|
|
char mystr[32];
|
|
|
|
|
|
|
|
mystr[len] = '\0';
|
|
|
|
l = strlen(strncpy(mystr, *str, len));
|
|
|
|
for (i = 0; i < l; i++)
|
2000-06-22 16:48:41 +00:00
|
|
|
mystr[i] = (char) tolower((unsigned char)mystr[i]);
|
1996-12-09 14:05:35 +00:00
|
|
|
for (i = 0; arr[i] && strcmp(mystr, arr[i]) != 0; i++);
|
|
|
|
if (arr[i] == NULL)
|
|
|
|
i = -1;
|
|
|
|
else { /* Skip past it */
|
2000-06-22 16:48:41 +00:00
|
|
|
while (**str && isalpha((unsigned char)**str))
|
1996-12-09 14:05:35 +00:00
|
|
|
++(*str);
|
|
|
|
/* And any following whitespace */
|
2000-06-22 16:48:41 +00:00
|
|
|
while (**str && (**str == ',' || isspace((unsigned char)**str)))
|
1996-12-09 14:05:35 +00:00
|
|
|
++(*str);
|
|
|
|
} /* Return index */
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
weekday(char const ** str)
|
|
|
|
{
|
|
|
|
static char const *days[] =
|
|
|
|
{"sun", "mon", "tue", "wed", "thu", "fri", "sat", NULL};
|
|
|
|
|
|
|
|
return aindex(days, str, 3);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2015-07-05 10:11:35 +00:00
|
|
|
parse_datesub(char const * str, struct tm *t)
|
1996-12-09 14:05:35 +00:00
|
|
|
{
|
2015-07-05 09:48:03 +00:00
|
|
|
struct tm tm;
|
|
|
|
locale_t l;
|
|
|
|
int i;
|
|
|
|
char *ret;
|
|
|
|
const char *valid_formats[] = {
|
|
|
|
"%d-%b-%y",
|
|
|
|
"%d-%b-%Y",
|
|
|
|
"%d-%m-%y",
|
|
|
|
"%d-%m-%Y",
|
2015-07-05 10:11:35 +00:00
|
|
|
"%H:%M %d-%b-%y",
|
|
|
|
"%H:%M %d-%b-%Y",
|
|
|
|
"%H:%M %d-%m-%y",
|
|
|
|
"%H:%M %d-%m-%Y",
|
|
|
|
"%H:%M:%S %d-%b-%y",
|
|
|
|
"%H:%M:%S %d-%b-%Y",
|
|
|
|
"%H:%M:%S %d-%m-%y",
|
|
|
|
"%H:%M:%S %d-%m-%Y",
|
|
|
|
"%d-%b-%y %H:%M",
|
|
|
|
"%d-%b-%Y %H:%M",
|
|
|
|
"%d-%m-%y %H:%M",
|
|
|
|
"%d-%m-%Y %H:%M",
|
|
|
|
"%d-%b-%y %H:%M:%S",
|
|
|
|
"%d-%b-%Y %H:%M:%S",
|
|
|
|
"%d-%m-%y %H:%M:%S",
|
|
|
|
"%d-%m-%Y %H:%M:%S",
|
|
|
|
"%H:%M\t%d-%b-%y",
|
|
|
|
"%H:%M\t%d-%b-%Y",
|
|
|
|
"%H:%M\t%d-%m-%y",
|
|
|
|
"%H:%M\t%d-%m-%Y",
|
|
|
|
"%H:%M\t%S %d-%b-%y",
|
|
|
|
"%H:%M\t%S %d-%b-%Y",
|
|
|
|
"%H:%M\t%S %d-%m-%y",
|
|
|
|
"%H:%M\t%S %d-%m-%Y",
|
|
|
|
"%d-%b-%y\t%H:%M",
|
|
|
|
"%d-%b-%Y\t%H:%M",
|
|
|
|
"%d-%m-%y\t%H:%M",
|
|
|
|
"%d-%m-%Y\t%H:%M",
|
|
|
|
"%d-%b-%y\t%H:%M:%S",
|
|
|
|
"%d-%b-%Y\t%H:%M:%S",
|
|
|
|
"%d-%m-%y\t%H:%M:%S",
|
|
|
|
"%d-%m-%Y\t%H:%M:%S",
|
2015-07-05 09:48:03 +00:00
|
|
|
NULL,
|
|
|
|
};
|
|
|
|
|
|
|
|
l = newlocale(LC_ALL_MASK, "C", NULL);
|
|
|
|
|
|
|
|
memset(&tm, 0, sizeof(tm));
|
|
|
|
for (i=0; valid_formats[i] != NULL; i++) {
|
|
|
|
ret = strptime_l(str, valid_formats[i], &tm, l);
|
|
|
|
if (ret && *ret == '\0') {
|
2015-07-05 10:11:35 +00:00
|
|
|
t->tm_mday = tm.tm_mday;
|
|
|
|
t->tm_mon = tm.tm_mon;
|
|
|
|
t->tm_year = tm.tm_year;
|
|
|
|
t->tm_hour = tm.tm_hour;
|
|
|
|
t->tm_min = tm.tm_min;
|
|
|
|
t->tm_sec = tm.tm_sec;
|
2015-07-05 09:48:03 +00:00
|
|
|
freelocale(l);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
1996-12-09 14:05:35 +00:00
|
|
|
|
2015-07-05 09:48:03 +00:00
|
|
|
freelocale(l);
|
1996-12-09 14:05:35 +00:00
|
|
|
|
2015-07-05 09:48:03 +00:00
|
|
|
errx(EXIT_FAILURE, "Invalid date");
|
1996-12-09 14:05:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*-
|
|
|
|
* Parse time must be flexible, it handles the following formats:
|
|
|
|
* nnnnnnnnnnn UNIX timestamp (all numeric), 0 = now
|
|
|
|
* 0xnnnnnnnn UNIX timestamp in hexadecimal
|
|
|
|
* 0nnnnnnnnn UNIX timestamp in octal
|
|
|
|
* 0 Given time
|
|
|
|
* +nnnn[smhdwoy] Given time + nnnn hours, mins, days, weeks, months or years
|
|
|
|
* -nnnn[smhdwoy] Given time - nnnn hours, mins, days, weeks, months or years
|
|
|
|
* dd[ ./-]mmm[ ./-]yy Date }
|
|
|
|
* hh:mm:ss Time } May be combined
|
|
|
|
*/
|
|
|
|
|
|
|
|
time_t
|
|
|
|
parse_date(time_t dt, char const * str)
|
|
|
|
{
|
|
|
|
char *p;
|
|
|
|
int i;
|
|
|
|
long val;
|
|
|
|
struct tm *T;
|
|
|
|
|
|
|
|
if (dt == 0)
|
|
|
|
dt = time(NULL);
|
|
|
|
|
2000-06-22 16:48:41 +00:00
|
|
|
while (*str && isspace((unsigned char)*str))
|
1996-12-09 14:05:35 +00:00
|
|
|
++str;
|
|
|
|
|
|
|
|
if (numerics(str)) {
|
1999-03-15 08:16:01 +00:00
|
|
|
dt = strtol(str, &p, 0);
|
1996-12-09 14:05:35 +00:00
|
|
|
} else if (*str == '+' || *str == '-') {
|
|
|
|
val = strtol(str, &p, 0);
|
|
|
|
switch (*p) {
|
|
|
|
case 'h':
|
|
|
|
case 'H': /* hours */
|
|
|
|
dt += (val * 3600L);
|
|
|
|
break;
|
|
|
|
case '\0':
|
|
|
|
case 'm':
|
|
|
|
case 'M': /* minutes */
|
|
|
|
dt += (val * 60L);
|
|
|
|
break;
|
|
|
|
case 's':
|
|
|
|
case 'S': /* seconds */
|
|
|
|
dt += val;
|
|
|
|
break;
|
|
|
|
case 'd':
|
|
|
|
case 'D': /* days */
|
|
|
|
dt += (val * 86400L);
|
|
|
|
break;
|
|
|
|
case 'w':
|
|
|
|
case 'W': /* weeks */
|
|
|
|
dt += (val * 604800L);
|
|
|
|
break;
|
|
|
|
case 'o':
|
|
|
|
case 'O': /* months */
|
|
|
|
T = localtime(&dt);
|
|
|
|
T->tm_mon += (int) val;
|
|
|
|
i = T->tm_mday;
|
|
|
|
goto fixday;
|
|
|
|
case 'y':
|
|
|
|
case 'Y': /* years */
|
|
|
|
T = localtime(&dt);
|
|
|
|
T->tm_year += (int) val;
|
|
|
|
i = T->tm_mday;
|
|
|
|
fixday:
|
|
|
|
dt = mktime(T);
|
|
|
|
T = localtime(&dt);
|
|
|
|
if (T->tm_mday != i) {
|
|
|
|
T->tm_mday = 1;
|
|
|
|
dt = mktime(T);
|
|
|
|
dt -= (time_t) 86400L;
|
|
|
|
}
|
|
|
|
default: /* unknown */
|
|
|
|
break; /* leave untouched */
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
char *q, tmp[64];
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Skip past any weekday prefix
|
|
|
|
*/
|
|
|
|
weekday(&str);
|
2004-06-17 14:07:16 +00:00
|
|
|
strlcpy(tmp, str, sizeof(tmp));
|
|
|
|
str = tmp;
|
1996-12-09 14:05:35 +00:00
|
|
|
T = localtime(&dt);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* See if we can break off any timezone
|
|
|
|
*/
|
|
|
|
while ((q = strrchr(tmp, ' ')) != NULL) {
|
|
|
|
if (strchr("(+-", q[1]) != NULL)
|
|
|
|
*q = '\0';
|
|
|
|
else {
|
|
|
|
int j = 1;
|
|
|
|
|
2000-06-22 16:48:41 +00:00
|
|
|
while (q[j] && isupper((unsigned char)q[j]))
|
1996-12-09 14:05:35 +00:00
|
|
|
++j;
|
|
|
|
if (q[j] == '\0')
|
|
|
|
*q = '\0';
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-05 10:11:35 +00:00
|
|
|
parse_datesub(tmp, T);
|
1996-12-09 14:05:35 +00:00
|
|
|
dt = mktime(T);
|
|
|
|
}
|
|
|
|
return dt;
|
|
|
|
}
|