2007-10-07 17:14:02 +05:30
|
|
|
/*
|
2021-12-05 21:05:27 +05:30
|
|
|
* SPDX-FileCopyrightText: 1991 - 1994, Julianne Frances Haugh
|
|
|
|
* SPDX-FileCopyrightText: 1996 - 1999, Marek Michałkiewicz
|
|
|
|
* SPDX-FileCopyrightText: 2003 - 2005, Tomasz Kłoczko
|
|
|
|
* SPDX-FileCopyrightText: 2008 , Nicolas François
|
2007-10-07 17:14:02 +05:30
|
|
|
*
|
2021-12-05 21:05:27 +05:30
|
|
|
* SPDX-License-Identifier: BSD-3-Clause
|
2007-10-07 17:14:02 +05:30
|
|
|
*/
|
|
|
|
|
|
|
|
#if !defined(__GLIBC__)
|
|
|
|
#define _XOPEN_SOURCE 500
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
2010-03-18 17:23:49 +05:30
|
|
|
#include <ctype.h>
|
|
|
|
|
2007-11-11 05:16:11 +05:30
|
|
|
#ident "$Id$"
|
2007-10-07 17:17:01 +05:30
|
|
|
|
2007-10-07 17:14:02 +05:30
|
|
|
#include "defines.h"
|
2008-01-05 18:53:22 +05:30
|
|
|
#include "prototypes.h"
|
|
|
|
|
2007-10-07 17:14:02 +05:30
|
|
|
#ifndef USE_GETDATE
|
|
|
|
#define USE_GETDATE 1
|
|
|
|
#endif
|
2010-03-18 17:23:49 +05:30
|
|
|
|
2007-10-07 17:14:02 +05:30
|
|
|
#if USE_GETDATE
|
|
|
|
#include "getdate.h"
|
|
|
|
/*
|
|
|
|
* strtoday() now uses get_date() (borrowed from GNU shellutils)
|
|
|
|
* which can handle many date formats, for example:
|
|
|
|
* 1970-09-17 # ISO 8601.
|
|
|
|
* 70-9-17 # This century assumed by default.
|
|
|
|
* 70-09-17 # Leading zeros are ignored.
|
|
|
|
* 9/17/72 # Common U.S. writing.
|
|
|
|
* 24 September 1972
|
|
|
|
* 24 Sept 72 # September has a special abbreviation.
|
|
|
|
* 24 Sep 72 # Three-letter abbreviations always allowed.
|
|
|
|
* Sep 24, 1972
|
|
|
|
* 24-sep-72
|
|
|
|
* 24sep72
|
|
|
|
*/
|
2007-10-07 17:15:23 +05:30
|
|
|
long strtoday (const char *str)
|
2007-10-07 17:14:02 +05:30
|
|
|
{
|
|
|
|
time_t t;
|
2010-03-18 17:23:49 +05:30
|
|
|
bool isnum = true;
|
|
|
|
const char *s = str;
|
2007-10-07 17:14:02 +05:30
|
|
|
|
|
|
|
/*
|
|
|
|
* get_date() interprets an empty string as the current date,
|
|
|
|
* which is not what we expect, unless you're a BOFH :-).
|
|
|
|
* (useradd sets sp_expire = current date for new lusers)
|
|
|
|
*/
|
2008-06-14 01:18:11 +05:30
|
|
|
if ((NULL == str) || ('\0' == *str)) {
|
2010-03-20 15:49:50 +05:30
|
|
|
return -1;
|
2010-03-18 17:23:49 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/* If a numerical value is provided, this is already a number of
|
|
|
|
* days since EPOCH.
|
|
|
|
*/
|
|
|
|
if ('-' == *s) {
|
|
|
|
s++;
|
|
|
|
}
|
|
|
|
while (' ' == *s) {
|
|
|
|
s++;
|
|
|
|
}
|
|
|
|
while (isnum && ('\0' != *s)) {
|
|
|
|
if (!isdigit (*s)) {
|
|
|
|
isnum = false;
|
|
|
|
}
|
|
|
|
s++;
|
|
|
|
}
|
|
|
|
if (isnum) {
|
|
|
|
long retdate;
|
|
|
|
if (getlong (str, &retdate) == 0) {
|
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
return retdate;
|
2008-06-14 01:18:11 +05:30
|
|
|
}
|
2007-10-07 17:14:02 +05:30
|
|
|
|
2010-03-18 17:23:49 +05:30
|
|
|
t = get_date (str, NULL);
|
2008-06-14 01:18:11 +05:30
|
|
|
if ((time_t) - 1 == t) {
|
2010-03-18 17:23:49 +05:30
|
|
|
return -2;
|
2008-06-14 01:18:11 +05:30
|
|
|
}
|
2007-10-07 17:14:02 +05:30
|
|
|
/* convert seconds to days since 1970-01-01 */
|
2008-06-14 01:18:11 +05:30
|
|
|
return (long) (t + DAY / 2) / DAY;
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
|
|
|
|
2007-10-07 17:15:23 +05:30
|
|
|
#else /* !USE_GETDATE */
|
2007-10-07 17:14:02 +05:30
|
|
|
/*
|
|
|
|
* Old code, just in case get_date() doesn't work as expected...
|
|
|
|
*/
|
|
|
|
#include <stdio.h>
|
|
|
|
#ifdef HAVE_STRPTIME
|
|
|
|
/*
|
|
|
|
* for now we allow just one format, but we can define more later
|
|
|
|
* (we try them all until one succeeds). --marekm
|
|
|
|
*/
|
2022-08-05 21:10:31 +05:30
|
|
|
static const char *const date_formats[] = {
|
2007-10-07 17:14:02 +05:30
|
|
|
"%Y-%m-%d",
|
|
|
|
(char *) 0
|
|
|
|
};
|
|
|
|
#else
|
|
|
|
/*
|
|
|
|
* days and juldays are used to compute the number of days in the
|
2017-10-23 00:03:13 +05:30
|
|
|
* current month, and the cumulative number of days in the preceding
|
2007-10-07 17:14:02 +05:30
|
|
|
* months. they are declared so that january is 1, not 0.
|
|
|
|
*/
|
2022-08-05 21:10:31 +05:30
|
|
|
static const short days[13] = { 0,
|
2007-10-07 17:15:23 +05:30
|
|
|
31, 28, 31, 30, 31, 30, /* JAN - JUN */
|
|
|
|
31, 31, 30, 31, 30, 31
|
|
|
|
}; /* JUL - DEC */
|
|
|
|
|
2022-08-05 21:10:31 +05:30
|
|
|
static const short juldays[13] = { 0,
|
2007-10-07 17:15:23 +05:30
|
|
|
0, 31, 59, 90, 120, 151, /* JAN - JUN */
|
|
|
|
181, 212, 243, 273, 304, 334
|
|
|
|
}; /* JUL - DEC */
|
2007-10-07 17:14:02 +05:30
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
* strtoday - compute the number of days since 1970.
|
|
|
|
*
|
|
|
|
* the total number of days prior to the current date is
|
|
|
|
* computed. january 1, 1970 is used as the origin with
|
|
|
|
* it having a day number of 0.
|
|
|
|
*/
|
|
|
|
|
2007-10-07 17:15:23 +05:30
|
|
|
long strtoday (const char *str)
|
2007-10-07 17:14:02 +05:30
|
|
|
{
|
|
|
|
#ifdef HAVE_STRPTIME
|
|
|
|
struct tm tp;
|
2022-08-05 21:10:31 +05:30
|
|
|
const char *const *fmt;
|
2007-10-07 17:14:02 +05:30
|
|
|
char *cp;
|
|
|
|
time_t result;
|
|
|
|
|
2007-10-07 17:15:23 +05:30
|
|
|
memzero (&tp, sizeof tp);
|
2007-10-07 17:14:02 +05:30
|
|
|
for (fmt = date_formats; *fmt; fmt++) {
|
2007-10-07 17:15:23 +05:30
|
|
|
cp = strptime ((char *) str, *fmt, &tp);
|
2008-06-14 01:18:11 +05:30
|
|
|
if ((NULL == cp) || ('\0' != *cp)) {
|
2007-10-07 17:14:02 +05:30
|
|
|
continue;
|
2008-06-14 01:18:11 +05:30
|
|
|
}
|
2007-10-07 17:14:02 +05:30
|
|
|
|
2007-10-07 17:15:23 +05:30
|
|
|
result = mktime (&tp);
|
2008-06-14 01:18:11 +05:30
|
|
|
if ((time_t) - 1 == result) {
|
2007-10-07 17:14:02 +05:30
|
|
|
continue;
|
2008-06-14 01:18:11 +05:30
|
|
|
}
|
2007-10-07 17:14:02 +05:30
|
|
|
|
2008-06-14 01:18:11 +05:30
|
|
|
return (long) (result / DAY); /* success */
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
#else
|
2007-10-07 17:15:23 +05:30
|
|
|
char slop[2];
|
|
|
|
int month;
|
|
|
|
int day;
|
|
|
|
int year;
|
|
|
|
long total;
|
2007-10-07 17:14:02 +05:30
|
|
|
|
|
|
|
/*
|
|
|
|
* start by separating the month, day and year. the order
|
|
|
|
* is compiled in ...
|
|
|
|
*/
|
|
|
|
|
2008-06-14 01:18:11 +05:30
|
|
|
if (sscanf (str, "%d/%d/%d%c", &year, &month, &day, slop) != 3) {
|
2007-10-07 17:14:02 +05:30
|
|
|
return -1;
|
2008-06-14 01:18:11 +05:30
|
|
|
}
|
2007-10-07 17:14:02 +05:30
|
|
|
|
|
|
|
/*
|
|
|
|
* the month, day of the month, and year are checked for
|
|
|
|
* correctness and the year adjusted so it falls between
|
|
|
|
* 1970 and 2069.
|
|
|
|
*/
|
|
|
|
|
2008-06-14 01:18:11 +05:30
|
|
|
if ((month < 1) || (month > 12)) {
|
2007-10-07 17:14:02 +05:30
|
|
|
return -1;
|
2008-06-14 01:18:11 +05:30
|
|
|
}
|
2007-10-07 17:14:02 +05:30
|
|
|
|
2008-06-14 01:18:11 +05:30
|
|
|
if (day < 1) {
|
2007-10-07 17:14:02 +05:30
|
|
|
return -1;
|
2008-06-14 01:18:11 +05:30
|
|
|
}
|
2007-10-07 17:14:02 +05:30
|
|
|
|
2008-06-14 01:18:11 +05:30
|
|
|
if ( ((2 != month) || ((year % 4) != 0))
|
|
|
|
&& (day > days[month])) {
|
2007-10-07 17:14:02 +05:30
|
|
|
return -1;
|
2008-06-14 01:18:11 +05:30
|
|
|
} else if ((month == 2) && ((year % 4) == 0) && (day > 29)) {
|
2007-10-07 17:14:02 +05:30
|
|
|
return -1;
|
2008-06-14 01:18:11 +05:30
|
|
|
}
|
2007-10-07 17:14:02 +05:30
|
|
|
|
2008-06-14 01:18:11 +05:30
|
|
|
if (year < 0) {
|
2007-10-07 17:14:02 +05:30
|
|
|
return -1;
|
2008-06-14 01:18:11 +05:30
|
|
|
} else if (year <= 69) {
|
2007-10-07 17:14:02 +05:30
|
|
|
year += 2000;
|
2008-06-14 01:18:11 +05:30
|
|
|
} else if (year <= 99) {
|
2007-10-07 17:14:02 +05:30
|
|
|
year += 1900;
|
2008-06-14 01:18:11 +05:30
|
|
|
}
|
2007-10-07 17:14:02 +05:30
|
|
|
|
|
|
|
/*
|
|
|
|
* On systems with 32-bit signed time_t, time wraps around in 2038
|
|
|
|
* - for now we just limit the year to 2037 (instead of 2069).
|
|
|
|
* This limit can be removed once no one is using 32-bit systems
|
|
|
|
* anymore :-). --marekm
|
|
|
|
*/
|
2008-06-14 01:18:11 +05:30
|
|
|
if ((year < 1970) || (year > 2037)) {
|
2007-10-07 17:14:02 +05:30
|
|
|
return -1;
|
2008-06-14 01:18:11 +05:30
|
|
|
}
|
2007-10-07 17:14:02 +05:30
|
|
|
|
|
|
|
/*
|
|
|
|
* the total number of days is the total number of days in all
|
|
|
|
* the whole years, plus the number of leap days, plus the
|
|
|
|
* number of days in the whole months preceding, plus the number
|
|
|
|
* of days so far in the month.
|
|
|
|
*/
|
|
|
|
|
|
|
|
total = (long) ((year - 1970) * 365L) + (((year + 1) - 1970) / 4);
|
2007-10-07 17:16:07 +05:30
|
|
|
total += (long) juldays[month] + (month > 2 && (year % 4) == 0 ? 1 : 0);
|
2007-10-07 17:14:02 +05:30
|
|
|
total += (long) day - 1;
|
|
|
|
|
|
|
|
return total;
|
2007-10-07 17:15:23 +05:30
|
|
|
#endif /* HAVE_STRPTIME */
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
2007-10-07 17:15:23 +05:30
|
|
|
#endif /* !USE_GETDATE */
|