1 /* Convert a string representation of time to a time value.
2 Copyright (C) 1997-2017 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Mark Kettenis <kettenis@phys.uva.nl>, 1997.
6 The GNU C Library is free software; you can redistribute it and/or
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.
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
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <http://www.gnu.org/licenses/>. */
22 #include <stdio_ext.h>
32 #define TM_YEAR_BASE 1900
35 /* Prototypes for local functions. */
36 static int first_wday (int year
, int mon
, int wday
);
37 static int check_mday (int year
, int mon
, int mday
);
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
49 specified that can not be represented in a time_t (representing
50 the time in seconds since 00:00:00 UTC, January 1, 1970) */
54 /* Returns the first weekday WDAY of month MON in the year YEAR. */
56 first_wday (int year
, int mon
, int wday
)
63 memset (&tm
, 0, sizeof (struct tm
));
69 return (1 + (wday
- tm
.tm_wday
+ 7) % 7);
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. */
76 check_mday (int year
, int mon
, int mday
)
87 if (mday
>= 1 && mday
<= 31)
94 if (mday
>= 1 && mday
<= 30)
98 if (mday
>= 1 && mday
<= (__isleap (year
) ? 29 : 28))
108 __getdate_r (const char *string
, struct tm
*tp
)
120 datemsk
= getenv ("DATEMSK");
121 if (datemsk
== NULL
|| *datemsk
== '\0')
124 if (stat64 (datemsk
, &st
) < 0)
127 if (!S_ISREG (st
.st_mode
))
130 if (__access (datemsk
, R_OK
) < 0)
133 /* Open the template file. */
134 fp
= fopen (datemsk
, "rce");
138 /* No threads reading this stream. */
139 __fsetlocking (fp
, FSETLOCKING_BYCALLER
);
141 /* Skip leading whitespace. */
142 while (isspace (*string
))
145 size_t inlen
, oldlen
;
147 oldlen
= inlen
= strlen (string
);
149 /* Skip trailing whitespace. */
150 while (inlen
> 0 && isspace (string
[inlen
- 1]))
157 bool using_malloc
= false;
159 if (__libc_use_alloca (inlen
+ 1))
160 instr
= alloca (inlen
+ 1);
163 instr
= malloc (inlen
+ 1);
171 memcpy (instr
, string
, inlen
);
185 n
= __getline (&line
, &len
, fp
);
188 if (line
[n
- 1] == '\n')
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
;
197 result
= strptime (string
, line
, tp
);
198 if (result
&& *result
== '\0')
201 while (!feof_unlocked (fp
));
205 /* Free the buffer. */
208 /* Check for errors. */
209 if (ferror_unlocked (fp
))
215 /* Close template file. */
218 if (result
== NULL
|| *result
!= '\0')
221 /* Get current time. */
223 __localtime_r (&timer
, &tm
);
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
)
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;
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
240 if (tp
->tm_mon
>= 0 && tp
->tm_mon
<= 11 && tp
->tm_mday
== INT_MIN
)
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
);
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
)
252 tp
->tm_hour
= tm
.tm_hour
;
253 tp
->tm_min
= tm
.tm_min
;
254 tp
->tm_sec
= tm
.tm_sec
;
257 /* Fill in the gaps. */
258 if (tp
->tm_hour
== INT_MIN
)
260 if (tp
->tm_min
== INT_MIN
)
262 if (tp
->tm_sec
== INT_MIN
)
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
268 if (tp
->tm_hour
>= 0 && tp
->tm_hour
<= 23
269 && tp
->tm_mon
== INT_MIN
270 && tp
->tm_mday
== INT_MIN
&& tp
->tm_wday
== INT_MIN
)
272 tp
->tm_mon
= tm
.tm_mon
;
273 tp
->tm_mday
= tm
.tm_mday
+ ((tp
->tm_hour
- tm
.tm_hour
) < 0 ? 1 : 0);
278 if (tp
->tm_year
== INT_MIN
)
279 tp
->tm_year
= tm
.tm_year
;
280 if (tp
->tm_mon
== INT_MIN
)
281 tp
->tm_mon
= tm
.tm_mon
;
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. */
286 if ((!mday_ok
&& !check_mday (TM_YEAR_BASE
+ tp
->tm_year
, tp
->tm_mon
,
288 || mktime (tp
) == (time_t) -1)
294 weak_alias (__getdate_r
, getdate_r
)
299 getdate (const char *string
)
301 /* Buffer returned by getdate. */
302 static struct tm tmbuf
;
303 int errval
= __getdate_r (string
, &tmbuf
);
307 getdate_err
= errval
;