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
|
|
|
*/
|
|
|
|
|
|
|
|
#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
|
|
|
|
2008-01-05 18:53:22 +05:30
|
|
|
#include "prototypes.h"
|
2007-10-07 17:14:02 +05:30
|
|
|
#include "getdate.h"
|
2022-12-22 20:56:21 +05:30
|
|
|
|
2007-10-07 17:14:02 +05:30
|
|
|
/*
|
|
|
|
* 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
|
|
|
}
|