summaryrefslogtreecommitdiffstats
path: root/libmisc/strtoday.c
blob: d32187389f95d9e3b1ad2451a0f33284903fa84c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/*
 * 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
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

#if !defined(__GLIBC__)
#define _XOPEN_SOURCE 500
#endif

#include <config.h>

#include <ctype.h>

#ident "$Id$"

#include "defines.h"
#include "prototypes.h"

#ifndef USE_GETDATE
#define USE_GETDATE 1
#endif

#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
 */
long strtoday (const char *str)
{
	time_t t;
	bool isnum = true;
	const char *s = str;

	/*
	 * 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)
	 */
	if ((NULL == str) || ('\0' == *str)) {
		return -1;
	}

	/* 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;
	}

	t = get_date (str, NULL);
	if ((time_t) - 1 == t) {
		return -2;
	}
	/* convert seconds to days since 1970-01-01 */
	return (long) (t + DAY / 2) / DAY;
}

#else				/* !USE_GETDATE */
/*
 * 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
 */
static const char *const date_formats[] = {
	"%Y-%m-%d",
	(char *) 0
};
#else
/*
 * days and juldays are used to compute the number of days in the
 * current month, and the cumulative number of days in the preceding
 * months.  they are declared so that january is 1, not 0.
 */
static const short days[13] = { 0,
	31, 28, 31, 30, 31, 30,	/* JAN - JUN */
	31, 31, 30, 31, 30, 31
};				/* JUL - DEC */

static const short juldays[13] = { 0,
	0, 31, 59, 90, 120, 151,	/* JAN - JUN */
	181, 212, 243, 273, 304, 334
};				/* JUL - DEC */
#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.
 */

long strtoday (const char *str)
{
#ifdef HAVE_STRPTIME
	struct tm tp;
	const char *const *fmt;
	char *cp;
	time_t result;

	memzero (&tp, sizeof tp);
	for (fmt = date_formats; *fmt; fmt++) {
		cp = strptime ((char *) str, *fmt, &tp);
		if ((NULL == cp) || ('\0' != *cp)) {
			continue;
		}

		result = mktime (&tp);
		if ((time_t) - 1 == result) {
			continue;
		}

		return (long) (result / DAY);	/* success */
	}
	return -1;
#else
	char slop[2];
	int month;
	int day;
	int year;
	long total;

	/*
	 * start by separating the month, day and year.  the order
	 * is compiled in ...
	 */

	if (sscanf (str, "%d/%d/%d%c", &year, &month, &day, slop) != 3) {
		return -1;
	}

	/*
	 * the month, day of the month, and year are checked for
	 * correctness and the year adjusted so it falls between
	 * 1970 and 2069.
	 */

	if ((month < 1) || (month > 12)) {
		return -1;
	}

	if (day < 1) {
		return -1;
	}

	if (   ((2 != month) || ((year % 4) != 0))
	    && (day > days[month])) {
		return -1;
	} else if ((month == 2) && ((year % 4) == 0) && (day > 29)) {
		return -1;
	}

	if (year < 0) {
		return -1;
	} else if (year <= 69) {
		year += 2000;
	} else if (year <= 99) {
		year += 1900;
	}

	/*
	 * 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
	 */
	if ((year < 1970) || (year > 2037)) {
		return -1;
	}

	/*
	 * 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);
	total += (long) juldays[month] + (month > 2 && (year % 4) == 0 ? 1 : 0);
	total += (long) day - 1;

	return total;
#endif				/* HAVE_STRPTIME */
}
#endif				/* !USE_GETDATE */