]> git.ipfire.org Git - thirdparty/glibc.git/blame - time/getdate.c
Prefer https to http for gnu.org and fsf.org URLs
[thirdparty/glibc.git] / time / getdate.c
CommitLineData
f21acc89 1/* Convert a string representation of time to a time value.
04277e02 2 Copyright (C) 1997-2019 Free Software Foundation, Inc.
f21acc89
UD
3 This file is part of the GNU C Library.
4 Contributed by Mark Kettenis <kettenis@phys.uva.nl>, 1997.
5
6 The GNU C Library is free software; you can redistribute it and/or
41bdb6e2
AJ
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
f21acc89
UD
10
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
41bdb6e2 14 Lesser General Public License for more details.
f21acc89 15
41bdb6e2 16 You should have received a copy of the GNU Lesser General Public
59ba27a6 17 License along with the GNU C Library; if not, see
5a82c748 18 <https://www.gnu.org/licenses/>. */
f21acc89
UD
19
20#include <limits.h>
21#include <stdio.h>
2706ee38 22#include <stdio_ext.h>
f21acc89 23#include <stdlib.h>
ebd7aede 24#include <stdbool.h>
f21acc89
UD
25#include <string.h>
26#include <time.h>
8edf6e0d 27#include <unistd.h>
f21acc89 28#include <sys/stat.h>
abe7f530
SP
29#include <ctype.h>
30#include <alloca.h>
f21acc89 31
a40ef91f
UD
32#define TM_YEAR_BASE 1900
33
f21acc89
UD
34
35/* Prototypes for local functions. */
36static int first_wday (int year, int mon, int wday);
37static int check_mday (int year, int mon, int mday);
38
39
40/* Set to one of the following values to indicate an error.
41 1 the DATEMSK environment variable is null or undefined,
42 2 the template file cannot be opened for reading,
43 3 failed to get file status information,
44 4 the template file is not a regular file,
45 5 an error is encountered while reading the template file,
46 6 memory allication failed (not enough memory available),
47 7 there is no line in the template that matches the input,
48 8 invalid input specification Example: February 31 or a time is
312be3f9 49 specified that can not be represented in a time_t (representing
f21acc89 50 the time in seconds since 00:00:00 UTC, January 1, 1970) */
c4563d2d 51int getdate_err;
f21acc89
UD
52
53
54/* Returns the first weekday WDAY of month MON in the year YEAR. */
55static int
56first_wday (int year, int mon, int wday)
57{
58 struct tm tm;
59
60 if (wday == INT_MIN)
61 return 1;
62
63 memset (&tm, 0, sizeof (struct tm));
64 tm.tm_year = year;
65 tm.tm_mon = mon;
66 tm.tm_mday = 1;
67 mktime (&tm);
68
69 return (1 + (wday - tm.tm_wday + 7) % 7);
70}
71
72
73/* Returns 1 if MDAY is a valid day of the month in month MON of year
74 YEAR, and 0 if it is not. */
75static int
76check_mday (int year, int mon, int mday)
77{
78 switch (mon)
79 {
51c68755
UD
80 case 0:
81 case 2:
f21acc89
UD
82 case 4:
83 case 6:
51c68755 84 case 7:
f21acc89
UD
85 case 9:
86 case 11:
51c68755
UD
87 if (mday >= 1 && mday <= 31)
88 return 1;
89 break;
90 case 3:
91 case 5:
92 case 8:
93 case 10:
f21acc89
UD
94 if (mday >= 1 && mday <= 30)
95 return 1;
96 break;
51c68755 97 case 1:
06b54322 98 if (mday >= 1 && mday <= (__isleap (year) ? 29 : 28))
f21acc89
UD
99 return 1;
100 break;
101 }
102
103 return 0;
104}
105
106
107int
108__getdate_r (const char *string, struct tm *tp)
109{
110 FILE *fp;
111 char *line;
112 size_t len;
113 char *datemsk;
114 char *result = NULL;
115 time_t timer;
116 struct tm tm;
8edf6e0d 117 struct stat64 st;
f21acc89
UD
118 int mday_ok = 0;
119
120 datemsk = getenv ("DATEMSK");
121 if (datemsk == NULL || *datemsk == '\0')
122 return 1;
123
8edf6e0d 124 if (stat64 (datemsk, &st) < 0)
f21acc89
UD
125 return 3;
126
127 if (!S_ISREG (st.st_mode))
128 return 4;
129
8edf6e0d
UD
130 if (__access (datemsk, R_OK) < 0)
131 return 2;
132
f21acc89 133 /* Open the template file. */
312be3f9 134 fp = fopen (datemsk, "rce");
f21acc89
UD
135 if (fp == NULL)
136 return 2;
137
2706ee38
UD
138 /* No threads reading this stream. */
139 __fsetlocking (fp, FSETLOCKING_BYCALLER);
140
abe7f530
SP
141 /* Skip leading whitespace. */
142 while (isspace (*string))
143 string++;
144
145 size_t inlen, oldlen;
146
147 oldlen = inlen = strlen (string);
148
149 /* Skip trailing whitespace. */
150 while (inlen > 0 && isspace (string[inlen - 1]))
151 inlen--;
152
153 char *instr = NULL;
154
155 if (inlen < oldlen)
156 {
157 bool using_malloc = false;
158
159 if (__libc_use_alloca (inlen + 1))
160 instr = alloca (inlen + 1);
161 else
162 {
163 instr = malloc (inlen + 1);
164 if (instr == NULL)
165 {
166 fclose (fp);
167 return 6;
168 }
169 using_malloc = true;
170 }
171 memcpy (instr, string, inlen);
172 instr[inlen] = '\0';
173 string = instr;
174
175 if (!using_malloc)
176 instr = NULL;
177 }
178
f21acc89
UD
179 line = NULL;
180 len = 0;
181 do
182 {
183 ssize_t n;
184
185 n = __getline (&line, &len, fp);
186 if (n < 0)
187 break;
188 if (line[n - 1] == '\n')
189 line[n - 1] = '\0';
190
191 /* Do the conversion. */
192 tp->tm_year = tp->tm_mon = tp->tm_mday = tp->tm_wday = INT_MIN;
193 tp->tm_hour = tp->tm_sec = tp->tm_min = INT_MIN;
e84ba51a
UD
194 tp->tm_isdst = -1;
195 tp->tm_gmtoff = 0;
196 tp->tm_zone = NULL;
f21acc89
UD
197 result = strptime (string, line, tp);
198 if (result && *result == '\0')
199 break;
200 }
7fc03cf3 201 while (!__feof_unlocked (fp));
f21acc89 202
abe7f530
SP
203 free (instr);
204
f21acc89
UD
205 /* Free the buffer. */
206 free (line);
207
208 /* Check for errors. */
ec481ad8 209 if (__ferror_unlocked (fp))
f21acc89
UD
210 {
211 fclose (fp);
212 return 5;
213 }
214
215 /* Close template file. */
216 fclose (fp);
217
907a1bac 218 if (result == NULL || *result != '\0')
f21acc89
UD
219 return 7;
220
221 /* Get current time. */
1baae4aa 222 timer = time (NULL);
f21acc89
UD
223 __localtime_r (&timer, &tm);
224
225 /* If only the weekday is given, today is assumed if the given day
226 is equal to the current day and next week if it is less. */
227 if (tp->tm_wday >= 0 && tp->tm_wday <= 6 && tp->tm_year == INT_MIN
228 && tp->tm_mon == INT_MIN && tp->tm_mday == INT_MIN)
229 {
230 tp->tm_year = tm.tm_year;
231 tp->tm_mon = tm.tm_mon;
232 tp->tm_mday = tm.tm_mday + (tp->tm_wday - tm.tm_wday + 7) % 7;
233 mday_ok = 1;
234 }
235
236 /* If only the month is given, the current month is assumed if the
237 given month is equal to the current month and next year if it is
238 less and no year is given (the first day of month is assumed if
239 no day is given. */
240 if (tp->tm_mon >= 0 && tp->tm_mon <= 11 && tp->tm_mday == INT_MIN)
241 {
242 if (tp->tm_year == INT_MIN)
243 tp->tm_year = tm.tm_year + (((tp->tm_mon - tm.tm_mon) < 0) ? 1 : 0);
244 tp->tm_mday = first_wday (tp->tm_year, tp->tm_mon, tp->tm_wday);
245 mday_ok = 1;
246 }
247
248 /* If no hour, minute and second are given the current hour, minute
249 and second are assumed. */
250 if (tp->tm_hour == INT_MIN && tp->tm_min == INT_MIN && tp->tm_sec == INT_MIN)
251 {
252 tp->tm_hour = tm.tm_hour;
253 tp->tm_min = tm.tm_min;
254 tp->tm_sec = tm.tm_sec;
255 }
256
b4354cf4
UD
257 /* Fill in the gaps. */
258 if (tp->tm_hour == INT_MIN)
259 tp->tm_hour = 0;
260 if (tp->tm_min == INT_MIN)
261 tp->tm_min = 0;
262 if (tp->tm_sec == INT_MIN)
263 tp->tm_sec = 0;
264
f21acc89
UD
265 /* If no date is given, today is assumed if the given hour is
266 greater than the current hour and tomorrow is assumed if
267 it is less. */
268 if (tp->tm_hour >= 0 && tp->tm_hour <= 23
b4354cf4 269 && tp->tm_mon == INT_MIN
f21acc89
UD
270 && tp->tm_mday == INT_MIN && tp->tm_wday == INT_MIN)
271 {
f21acc89
UD
272 tp->tm_mon = tm.tm_mon;
273 tp->tm_mday = tm.tm_mday + ((tp->tm_hour - tm.tm_hour) < 0 ? 1 : 0);
274 mday_ok = 1;
275 }
276
b4354cf4 277 /* More fillers. */
f21acc89
UD
278 if (tp->tm_year == INT_MIN)
279 tp->tm_year = tm.tm_year;
b4354cf4
UD
280 if (tp->tm_mon == INT_MIN)
281 tp->tm_mon = tm.tm_mon;
f21acc89
UD
282
283 /* Check if the day of month is within range, and if the time can be
284 represented in a time_t. We make use of the fact that the mktime
285 call normalizes the struct tm. */
a40ef91f
UD
286 if ((!mday_ok && !check_mday (TM_YEAR_BASE + tp->tm_year, tp->tm_mon,
287 tp->tm_mday))
f21acc89
UD
288 || mktime (tp) == (time_t) -1)
289 return 8;
290
291 return 0;
292}
293#ifdef weak_alias
294weak_alias (__getdate_r, getdate_r)
295#endif
296
297
298struct tm *
299getdate (const char *string)
300{
301 /* Buffer returned by getdate. */
302 static struct tm tmbuf;
303 int errval = __getdate_r (string, &tmbuf);
304
907a1bac
UD
305 if (errval != 0)
306 {
307 getdate_err = errval;
308 return NULL;
309 }
310
311 return &tmbuf;
f21acc89 312}