]> granicus.if.org Git - shadow/blob - libmisc/strtoday.c
Updated copyright dates.
[shadow] / libmisc / strtoday.c
1 /*
2  * Copyright (c) 1991 - 1994, Julianne Frances Haugh
3  * Copyright (c) 1996 - 1999, Marek Michałkiewicz
4  * Copyright (c) 2003 - 2005, Tomasz Kłoczko
5  * Copyright (c) 2008       , Nicolas François
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. The name of the copyright holders or contributors may not be used to
17  *    endorse or promote products derived from this software without
18  *    specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
23  * PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT
24  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32
33 #if !defined(__GLIBC__)
34 #define _XOPEN_SOURCE 500
35 #endif
36
37 #include <config.h>
38
39 #ident "$Id$"
40
41 #include "defines.h"
42 #include "prototypes.h"
43
44 #ifndef USE_GETDATE
45 #define USE_GETDATE 1
46 #endif
47 #if USE_GETDATE
48 #include "getdate.h"
49 /*
50  * strtoday() now uses get_date() (borrowed from GNU shellutils)
51  * which can handle many date formats, for example:
52  *      1970-09-17      # ISO 8601.
53  *      70-9-17         # This century assumed by default.
54  *      70-09-17        # Leading zeros are ignored.
55  *      9/17/72         # Common U.S. writing.
56  *      24 September 1972
57  *      24 Sept 72      # September has a special abbreviation.
58  *      24 Sep 72       # Three-letter abbreviations always allowed.
59  *      Sep 24, 1972
60  *      24-sep-72
61  *      24sep72
62  */
63 long strtoday (const char *str)
64 {
65         time_t t;
66
67         /*
68          * get_date() interprets an empty string as the current date,
69          * which is not what we expect, unless you're a BOFH :-).
70          * (useradd sets sp_expire = current date for new lusers)
71          */
72         if (!str || *str == '\0')
73                 return -1;
74
75         t = get_date (str, (time_t *) 0);
76         if (t == (time_t) - 1)
77                 return -1;
78         /* convert seconds to days since 1970-01-01 */
79         return (t + DAY / 2) / DAY;
80 }
81
82 #else                           /* !USE_GETDATE */
83 /*
84  * Old code, just in case get_date() doesn't work as expected...
85  */
86 #include <stdio.h>
87 #ifdef HAVE_STRPTIME
88 /*
89  * for now we allow just one format, but we can define more later
90  * (we try them all until one succeeds).  --marekm
91  */
92 static char *date_formats[] = {
93         "%Y-%m-%d",
94         (char *) 0
95 };
96 #else
97 /*
98  * days and juldays are used to compute the number of days in the
99  * current month, and the cummulative number of days in the preceding
100  * months.  they are declared so that january is 1, not 0.
101  */
102 static short days[13] = { 0,
103         31, 28, 31, 30, 31, 30, /* JAN - JUN */
104         31, 31, 30, 31, 30, 31
105 };                              /* JUL - DEC */
106
107 static short juldays[13] = { 0,
108         0, 31, 59, 90, 120, 151,        /* JAN - JUN */
109         181, 212, 243, 273, 304, 334
110 };                              /* JUL - DEC */
111 #endif
112
113 /*
114  * strtoday - compute the number of days since 1970.
115  *
116  * the total number of days prior to the current date is
117  * computed.  january 1, 1970 is used as the origin with
118  * it having a day number of 0.
119  */
120
121 long strtoday (const char *str)
122 {
123 #ifdef HAVE_STRPTIME
124         struct tm tp;
125         char *const *fmt;
126         char *cp;
127         time_t result;
128
129         memzero (&tp, sizeof tp);
130         for (fmt = date_formats; *fmt; fmt++) {
131                 cp = strptime ((char *) str, *fmt, &tp);
132                 if (!cp || *cp != '\0')
133                         continue;
134
135                 result = mktime (&tp);
136                 if (result == (time_t) - 1)
137                         continue;
138
139                 return result / DAY;    /* success */
140         }
141         return -1;
142 #else
143         char slop[2];
144         int month;
145         int day;
146         int year;
147         long total;
148
149         /*
150          * start by separating the month, day and year.  the order
151          * is compiled in ...
152          */
153
154         if (sscanf (str, "%d/%d/%d%c", &year, &month, &day, slop) != 3)
155                 return -1;
156
157         /*
158          * the month, day of the month, and year are checked for
159          * correctness and the year adjusted so it falls between
160          * 1970 and 2069.
161          */
162
163         if (month < 1 || month > 12)
164                 return -1;
165
166         if (day < 1)
167                 return -1;
168
169         if ((month != 2 || (year % 4) != 0) && day > days[month])
170                 return -1;
171         else if ((month == 2 && (year % 4) == 0) && day > 29)
172                 return -1;
173
174         if (year < 0)
175                 return -1;
176         else if (year <= 69)
177                 year += 2000;
178         else if (year <= 99)
179                 year += 1900;
180
181         /*
182          * On systems with 32-bit signed time_t, time wraps around in 2038
183          * - for now we just limit the year to 2037 (instead of 2069).
184          * This limit can be removed once no one is using 32-bit systems
185          * anymore :-).  --marekm
186          */
187         if (year < 1970 || year > 2037)
188                 return -1;
189
190         /*
191          * the total number of days is the total number of days in all
192          * the whole years, plus the number of leap days, plus the
193          * number of days in the whole months preceding, plus the number
194          * of days so far in the month.
195          */
196
197         total = (long) ((year - 1970) * 365L) + (((year + 1) - 1970) / 4);
198         total += (long) juldays[month] + (month > 2 && (year % 4) == 0 ? 1 : 0);
199         total += (long) day - 1;
200
201         return total;
202 #endif                          /* HAVE_STRPTIME */
203 }
204 #endif                          /* !USE_GETDATE */