* sysdeps/mach/hurd/i386/init-first.c (init1) [! SHARED]: Add decls
[glibc.git] / time / getdate.c
blob851efacfd20a50192ac88e28767a12ff671fee0d
1 /* Convert a string representation of time to a time value.
2 Copyright (C) 1997,1998,1999,2000,2001,2003 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, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA. */
21 #include <limits.h>
22 #include <stdio.h>
23 #include <stdio_ext.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <time.h>
27 #include <unistd.h>
28 #include <sys/stat.h>
30 #define TM_YEAR_BASE 1900
33 /* Prototypes for local functions. */
34 static int first_wday (int year, int mon, int wday);
35 static int check_mday (int year, int mon, int mday);
38 /* Set to one of the following values to indicate an error.
39 1 the DATEMSK environment variable is null or undefined,
40 2 the template file cannot be opened for reading,
41 3 failed to get file status information,
42 4 the template file is not a regular file,
43 5 an error is encountered while reading the template file,
44 6 memory allication failed (not enough memory available),
45 7 there is no line in the template that matches the input,
46 8 invalid input specification Example: February 31 or a time is
47 specified that can not be represented in a time_t (representing
48 the time in seconds since 00:00:00 UTC, January 1, 1970) */
49 int getdate_err;
52 /* Returns the first weekday WDAY of month MON in the year YEAR. */
53 static int
54 first_wday (int year, int mon, int wday)
56 struct tm tm;
58 if (wday == INT_MIN)
59 return 1;
61 memset (&tm, 0, sizeof (struct tm));
62 tm.tm_year = year;
63 tm.tm_mon = mon;
64 tm.tm_mday = 1;
65 mktime (&tm);
67 return (1 + (wday - tm.tm_wday + 7) % 7);
71 /* Returns 1 if MDAY is a valid day of the month in month MON of year
72 YEAR, and 0 if it is not. */
73 static int
74 check_mday (int year, int mon, int mday)
76 switch (mon)
78 case 0:
79 case 2:
80 case 4:
81 case 6:
82 case 7:
83 case 9:
84 case 11:
85 if (mday >= 1 && mday <= 31)
86 return 1;
87 break;
88 case 3:
89 case 5:
90 case 8:
91 case 10:
92 if (mday >= 1 && mday <= 30)
93 return 1;
94 break;
95 case 1:
96 if (mday >= 1 && mday <= (__isleap (year) ? 29 : 28))
97 return 1;
98 break;
101 return 0;
106 __getdate_r (const char *string, struct tm *tp)
108 FILE *fp;
109 char *line;
110 size_t len;
111 char *datemsk;
112 char *result = NULL;
113 time_t timer;
114 struct tm tm;
115 struct stat64 st;
116 int mday_ok = 0;
118 datemsk = getenv ("DATEMSK");
119 if (datemsk == NULL || *datemsk == '\0')
120 return 1;
122 if (stat64 (datemsk, &st) < 0)
123 return 3;
125 if (!S_ISREG (st.st_mode))
126 return 4;
128 if (__access (datemsk, R_OK) < 0)
129 return 2;
131 /* Open the template file. */
132 fp = fopen (datemsk, "rc");
133 if (fp == NULL)
134 return 2;
136 /* No threads reading this stream. */
137 __fsetlocking (fp, FSETLOCKING_BYCALLER);
139 line = NULL;
140 len = 0;
143 ssize_t n;
145 n = __getline (&line, &len, fp);
146 if (n < 0)
147 break;
148 if (line[n - 1] == '\n')
149 line[n - 1] = '\0';
151 /* Do the conversion. */
152 tp->tm_year = tp->tm_mon = tp->tm_mday = tp->tm_wday = INT_MIN;
153 tp->tm_hour = tp->tm_sec = tp->tm_min = INT_MIN;
154 tp->tm_isdst = -1;
155 tp->tm_gmtoff = 0;
156 tp->tm_zone = NULL;
157 result = strptime (string, line, tp);
158 if (result && *result == '\0')
159 break;
161 while (!feof_unlocked (fp));
163 /* Free the buffer. */
164 free (line);
166 /* Check for errors. */
167 if (ferror_unlocked (fp))
169 fclose (fp);
170 return 5;
173 /* Close template file. */
174 fclose (fp);
176 if (result == NULL || *result != '\0')
177 return 7;
179 /* Get current time. */
180 time (&timer);
181 __localtime_r (&timer, &tm);
183 /* If only the weekday is given, today is assumed if the given day
184 is equal to the current day and next week if it is less. */
185 if (tp->tm_wday >= 0 && tp->tm_wday <= 6 && tp->tm_year == INT_MIN
186 && tp->tm_mon == INT_MIN && tp->tm_mday == INT_MIN)
188 tp->tm_year = tm.tm_year;
189 tp->tm_mon = tm.tm_mon;
190 tp->tm_mday = tm.tm_mday + (tp->tm_wday - tm.tm_wday + 7) % 7;
191 mday_ok = 1;
194 /* If only the month is given, the current month is assumed if the
195 given month is equal to the current month and next year if it is
196 less and no year is given (the first day of month is assumed if
197 no day is given. */
198 if (tp->tm_mon >= 0 && tp->tm_mon <= 11 && tp->tm_mday == INT_MIN)
200 if (tp->tm_year == INT_MIN)
201 tp->tm_year = tm.tm_year + (((tp->tm_mon - tm.tm_mon) < 0) ? 1 : 0);
202 tp->tm_mday = first_wday (tp->tm_year, tp->tm_mon, tp->tm_wday);
203 mday_ok = 1;
206 /* If no hour, minute and second are given the current hour, minute
207 and second are assumed. */
208 if (tp->tm_hour == INT_MIN && tp->tm_min == INT_MIN && tp->tm_sec == INT_MIN)
210 tp->tm_hour = tm.tm_hour;
211 tp->tm_min = tm.tm_min;
212 tp->tm_sec = tm.tm_sec;
215 /* If no date is given, today is assumed if the given hour is
216 greater than the current hour and tomorrow is assumed if
217 it is less. */
218 if (tp->tm_hour >= 0 && tp->tm_hour <= 23
219 && tp->tm_year == INT_MIN && tp->tm_mon == INT_MIN
220 && tp->tm_mday == INT_MIN && tp->tm_wday == INT_MIN)
222 tp->tm_year = tm.tm_year;
223 tp->tm_mon = tm.tm_mon;
224 tp->tm_mday = tm.tm_mday + ((tp->tm_hour - tm.tm_hour) < 0 ? 1 : 0);
225 mday_ok = 1;
228 /* Fill in the gaps. */
229 if (tp->tm_year == INT_MIN)
230 tp->tm_year = tm.tm_year;
231 if (tp->tm_hour == INT_MIN)
232 tp->tm_hour = 0;
233 if (tp->tm_min == INT_MIN)
234 tp->tm_min = 0;
235 if (tp->tm_sec == INT_MIN)
236 tp->tm_sec = 0;
238 /* Check if the day of month is within range, and if the time can be
239 represented in a time_t. We make use of the fact that the mktime
240 call normalizes the struct tm. */
241 if ((!mday_ok && !check_mday (TM_YEAR_BASE + tp->tm_year, tp->tm_mon,
242 tp->tm_mday))
243 || mktime (tp) == (time_t) -1)
244 return 8;
246 return 0;
248 #ifdef weak_alias
249 weak_alias (__getdate_r, getdate_r)
250 #endif
253 struct tm *
254 getdate (const char *string)
256 /* Buffer returned by getdate. */
257 static struct tm tmbuf;
258 int errval = __getdate_r (string, &tmbuf);
260 if (errval != 0)
262 getdate_err = errval;
263 return NULL;
266 return &tmbuf;