Update copyright notices with scripts/update-copyrights.
[glibc.git] / time / getdate.c
blob637dd18fcfac7f10f1df60992c88a5aa485ecd0c
1 /* Convert a string representation of time to a time value.
2 Copyright (C) 1997-2013 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/>. */
20 #include <limits.h>
21 #include <stdio.h>
22 #include <stdio_ext.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <time.h>
26 #include <unistd.h>
27 #include <sys/stat.h>
29 #define TM_YEAR_BASE 1900
32 /* Prototypes for local functions. */
33 static int first_wday (int year, int mon, int wday);
34 static int check_mday (int year, int mon, int mday);
37 /* Set to one of the following values to indicate an error.
38 1 the DATEMSK environment variable is null or undefined,
39 2 the template file cannot be opened for reading,
40 3 failed to get file status information,
41 4 the template file is not a regular file,
42 5 an error is encountered while reading the template file,
43 6 memory allication failed (not enough memory available),
44 7 there is no line in the template that matches the input,
45 8 invalid input specification Example: February 31 or a time is
46 specified that can not be represented in a time_t (representing
47 the time in seconds since 00:00:00 UTC, January 1, 1970) */
48 int getdate_err;
51 /* Returns the first weekday WDAY of month MON in the year YEAR. */
52 static int
53 first_wday (int year, int mon, int wday)
55 struct tm tm;
57 if (wday == INT_MIN)
58 return 1;
60 memset (&tm, 0, sizeof (struct tm));
61 tm.tm_year = year;
62 tm.tm_mon = mon;
63 tm.tm_mday = 1;
64 mktime (&tm);
66 return (1 + (wday - tm.tm_wday + 7) % 7);
70 /* Returns 1 if MDAY is a valid day of the month in month MON of year
71 YEAR, and 0 if it is not. */
72 static int
73 check_mday (int year, int mon, int mday)
75 switch (mon)
77 case 0:
78 case 2:
79 case 4:
80 case 6:
81 case 7:
82 case 9:
83 case 11:
84 if (mday >= 1 && mday <= 31)
85 return 1;
86 break;
87 case 3:
88 case 5:
89 case 8:
90 case 10:
91 if (mday >= 1 && mday <= 30)
92 return 1;
93 break;
94 case 1:
95 if (mday >= 1 && mday <= (__isleap (year) ? 29 : 28))
96 return 1;
97 break;
100 return 0;
105 __getdate_r (const char *string, struct tm *tp)
107 FILE *fp;
108 char *line;
109 size_t len;
110 char *datemsk;
111 char *result = NULL;
112 time_t timer;
113 struct tm tm;
114 struct stat64 st;
115 int mday_ok = 0;
117 datemsk = getenv ("DATEMSK");
118 if (datemsk == NULL || *datemsk == '\0')
119 return 1;
121 if (stat64 (datemsk, &st) < 0)
122 return 3;
124 if (!S_ISREG (st.st_mode))
125 return 4;
127 if (__access (datemsk, R_OK) < 0)
128 return 2;
130 /* Open the template file. */
131 fp = fopen (datemsk, "rce");
132 if (fp == NULL)
133 return 2;
135 /* No threads reading this stream. */
136 __fsetlocking (fp, FSETLOCKING_BYCALLER);
138 line = NULL;
139 len = 0;
142 ssize_t n;
144 n = __getline (&line, &len, fp);
145 if (n < 0)
146 break;
147 if (line[n - 1] == '\n')
148 line[n - 1] = '\0';
150 /* Do the conversion. */
151 tp->tm_year = tp->tm_mon = tp->tm_mday = tp->tm_wday = INT_MIN;
152 tp->tm_hour = tp->tm_sec = tp->tm_min = INT_MIN;
153 tp->tm_isdst = -1;
154 tp->tm_gmtoff = 0;
155 tp->tm_zone = NULL;
156 result = strptime (string, line, tp);
157 if (result && *result == '\0')
158 break;
160 while (!feof_unlocked (fp));
162 /* Free the buffer. */
163 free (line);
165 /* Check for errors. */
166 if (ferror_unlocked (fp))
168 fclose (fp);
169 return 5;
172 /* Close template file. */
173 fclose (fp);
175 if (result == NULL || *result != '\0')
176 return 7;
178 /* Get current time. */
179 time (&timer);
180 __localtime_r (&timer, &tm);
182 /* If only the weekday is given, today is assumed if the given day
183 is equal to the current day and next week if it is less. */
184 if (tp->tm_wday >= 0 && tp->tm_wday <= 6 && tp->tm_year == INT_MIN
185 && tp->tm_mon == INT_MIN && tp->tm_mday == INT_MIN)
187 tp->tm_year = tm.tm_year;
188 tp->tm_mon = tm.tm_mon;
189 tp->tm_mday = tm.tm_mday + (tp->tm_wday - tm.tm_wday + 7) % 7;
190 mday_ok = 1;
193 /* If only the month is given, the current month is assumed if the
194 given month is equal to the current month and next year if it is
195 less and no year is given (the first day of month is assumed if
196 no day is given. */
197 if (tp->tm_mon >= 0 && tp->tm_mon <= 11 && tp->tm_mday == INT_MIN)
199 if (tp->tm_year == INT_MIN)
200 tp->tm_year = tm.tm_year + (((tp->tm_mon - tm.tm_mon) < 0) ? 1 : 0);
201 tp->tm_mday = first_wday (tp->tm_year, tp->tm_mon, tp->tm_wday);
202 mday_ok = 1;
205 /* If no hour, minute and second are given the current hour, minute
206 and second are assumed. */
207 if (tp->tm_hour == INT_MIN && tp->tm_min == INT_MIN && tp->tm_sec == INT_MIN)
209 tp->tm_hour = tm.tm_hour;
210 tp->tm_min = tm.tm_min;
211 tp->tm_sec = tm.tm_sec;
214 /* Fill in the gaps. */
215 if (tp->tm_hour == INT_MIN)
216 tp->tm_hour = 0;
217 if (tp->tm_min == INT_MIN)
218 tp->tm_min = 0;
219 if (tp->tm_sec == INT_MIN)
220 tp->tm_sec = 0;
222 /* If no date is given, today is assumed if the given hour is
223 greater than the current hour and tomorrow is assumed if
224 it is less. */
225 if (tp->tm_hour >= 0 && tp->tm_hour <= 23
226 && tp->tm_mon == INT_MIN
227 && tp->tm_mday == INT_MIN && tp->tm_wday == INT_MIN)
229 tp->tm_mon = tm.tm_mon;
230 tp->tm_mday = tm.tm_mday + ((tp->tm_hour - tm.tm_hour) < 0 ? 1 : 0);
231 mday_ok = 1;
234 /* More fillers. */
235 if (tp->tm_year == INT_MIN)
236 tp->tm_year = tm.tm_year;
237 if (tp->tm_mon == INT_MIN)
238 tp->tm_mon = tm.tm_mon;
240 /* Check if the day of month is within range, and if the time can be
241 represented in a time_t. We make use of the fact that the mktime
242 call normalizes the struct tm. */
243 if ((!mday_ok && !check_mday (TM_YEAR_BASE + tp->tm_year, tp->tm_mon,
244 tp->tm_mday))
245 || mktime (tp) == (time_t) -1)
246 return 8;
248 return 0;
250 #ifdef weak_alias
251 weak_alias (__getdate_r, getdate_r)
252 #endif
255 struct tm *
256 getdate (const char *string)
258 /* Buffer returned by getdate. */
259 static struct tm tmbuf;
260 int errval = __getdate_r (string, &tmbuf);
262 if (errval != 0)
264 getdate_err = errval;
265 return NULL;
268 return &tmbuf;