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