1 /* Convert a string representation of time to a time value.
2 Copyright (C) 1997-2023 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
21 #include <stdio_ext.h>
31 #define TM_YEAR_BASE 1900
34 /* Prototypes for local functions. */
35 static int first_wday (int year
, int mon
, int wday
);
36 static int check_mday (int year
, int mon
, int mday
);
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
48 specified that can not be represented in a time_t (representing
49 the time in seconds since 00:00:00 UTC, January 1, 1970) */
53 /* Returns the first weekday WDAY of month MON in the year YEAR. */
55 first_wday (int year
, int mon
, int wday
)
62 memset (&tm
, 0, sizeof (struct tm
));
68 return (1 + (wday
- tm
.tm_wday
+ 7) % 7);
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. */
75 check_mday (int year
, int mon
, int mday
)
86 if (mday
>= 1 && mday
<= 31)
93 if (mday
>= 1 && mday
<= 30)
97 if (mday
>= 1 && mday
<= (__isleap (year
) ? 29 : 28))
107 __getdate_r (const char *string
, struct tm
*tp
)
116 struct __stat64_t64 st
;
117 bool mday_ok
= false;
119 datemsk
= getenv ("DATEMSK");
120 if (datemsk
== NULL
|| *datemsk
== '\0')
123 if (__stat64_time64 (datemsk
, &st
) < 0)
126 if (!S_ISREG (st
.st_mode
))
129 if (__access (datemsk
, R_OK
) < 0)
132 /* Open the template file. */
133 fp
= fopen (datemsk
, "rce");
137 /* No threads reading this stream. */
138 __fsetlocking (fp
, FSETLOCKING_BYCALLER
);
140 /* Skip leading whitespace. */
141 while (isspace (*string
))
144 size_t inlen
, oldlen
;
146 oldlen
= inlen
= strlen (string
);
148 /* Skip trailing whitespace. */
149 while (inlen
> 0 && isspace (string
[inlen
- 1]))
156 bool using_malloc
= false;
158 if (__libc_use_alloca (inlen
+ 1))
159 instr
= alloca (inlen
+ 1);
162 instr
= malloc (inlen
+ 1);
170 memcpy (instr
, string
, inlen
);
184 n
= __getline (&line
, &len
, fp
);
187 if (line
[n
- 1] == '\n')
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
;
196 result
= strptime (string
, line
, tp
);
197 if (result
&& *result
== '\0')
200 while (!__feof_unlocked (fp
));
204 /* Free the buffer. */
207 /* Check for errors. */
208 if (__ferror_unlocked (fp
))
214 /* Close template file. */
217 if (result
== NULL
|| *result
!= '\0')
220 /* Get current time. */
221 timer
= time64_now ();
222 __localtime64_r (&timer
, &tm
);
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
)
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;
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
239 if (tp
->tm_mon
>= 0 && tp
->tm_mon
<= 11 && tp
->tm_mday
== INT_MIN
)
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
);
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
)
251 tp
->tm_hour
= tm
.tm_hour
;
252 tp
->tm_min
= tm
.tm_min
;
253 tp
->tm_sec
= tm
.tm_sec
;
256 /* Fill in the gaps. */
257 if (tp
->tm_hour
== INT_MIN
)
259 if (tp
->tm_min
== INT_MIN
)
261 if (tp
->tm_sec
== INT_MIN
)
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
267 if (tp
->tm_hour
>= 0 && tp
->tm_hour
<= 23
268 && tp
->tm_mon
== INT_MIN
269 && tp
->tm_mday
== INT_MIN
&& tp
->tm_wday
== INT_MIN
)
271 tp
->tm_mon
= tm
.tm_mon
;
272 tp
->tm_mday
= tm
.tm_mday
+ ((tp
->tm_hour
- tm
.tm_hour
) < 0 ? 1 : 0);
277 if (tp
->tm_year
== INT_MIN
)
278 tp
->tm_year
= tm
.tm_year
;
279 if (tp
->tm_mon
== INT_MIN
)
280 tp
->tm_mon
= tm
.tm_mon
;
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. */
285 if ((!mday_ok
&& !check_mday (TM_YEAR_BASE
+ tp
->tm_year
, tp
->tm_mon
,
287 || __mktime64 (tp
) == (time_t) -1)
292 weak_alias (__getdate_r
, getdate_r
)
293 libc_hidden_def (__getdate_r
)
296 getdate (const char *string
)
298 /* Buffer returned by getdate. */
299 static struct tm tmbuf
;
300 int errval
= __getdate_r (string
, &tmbuf
);
304 getdate_err
= errval
;