* sysdeps/m68k/dl-machine.h (RTLD_START): Readd _dl_start_user
[glibc/pb-stable.git] / time / getdate.c
blob626fb49d7a22c498ae4c3bdbcfcbba1a9cd58e0a
1 /* Convert a string representation of time to a time value.
2 Copyright (C) 1997, 1998, 1999, 2000 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 Library General Public License as
8 published by the Free Software Foundation; either version 2 of the
9 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 Library General Public License for more details.
16 You should have received a copy of the GNU Library General Public
17 License along with the GNU C Library; see the file COPYING.LIB. If not,
18 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
21 #include <limits.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <time.h>
26 #include <sys/stat.h>
28 #define TM_YEAR_BASE 1900
31 /* Prototypes for local functions. */
32 static int first_wday (int year, int mon, int wday);
33 static int check_mday (int year, int mon, int mday);
36 /* Set to one of the following values to indicate an error.
37 1 the DATEMSK environment variable is null or undefined,
38 2 the template file cannot be opened for reading,
39 3 failed to get file status information,
40 4 the template file is not a regular file,
41 5 an error is encountered while reading the template file,
42 6 memory allication failed (not enough memory available),
43 7 there is no line in the template that matches the input,
44 8 invalid input specification Example: February 31 or a time is
45 specified that can not be represented in a time_t (representing
46 the time in seconds since 00:00:00 UTC, January 1, 1970) */
47 int getdate_err;
50 /* Returns the first weekday WDAY of month MON in the year YEAR. */
51 static int
52 first_wday (int year, int mon, int wday)
54 struct tm tm;
56 if (wday == INT_MIN)
57 return 1;
59 memset (&tm, 0, sizeof (struct tm));
60 tm.tm_year = year;
61 tm.tm_mon = mon;
62 tm.tm_mday = 1;
63 mktime (&tm);
65 return (1 + (wday - tm.tm_wday + 7) % 7);
69 /* Returns 1 if MDAY is a valid day of the month in month MON of year
70 YEAR, and 0 if it is not. */
71 static int
72 check_mday (int year, int mon, int mday)
74 switch (mon)
76 case 0:
77 case 2:
78 case 4:
79 case 6:
80 case 7:
81 case 9:
82 case 11:
83 if (mday >= 1 && mday <= 31)
84 return 1;
85 break;
86 case 3:
87 case 5:
88 case 8:
89 case 10:
90 if (mday >= 1 && mday <= 30)
91 return 1;
92 break;
93 case 1:
94 if (mday >= 1 && mday <= (__isleap (year) ? 29 : 28))
95 return 1;
96 break;
99 return 0;
104 __getdate_r (const char *string, struct tm *tp)
106 FILE *fp;
107 char *line;
108 size_t len;
109 char *datemsk;
110 char *result = NULL;
111 time_t timer;
112 struct tm tm;
113 struct stat st;
114 int mday_ok = 0;
116 datemsk = getenv ("DATEMSK");
117 if (datemsk == NULL || *datemsk == '\0')
118 return 1;
120 if (stat (datemsk, &st) < 0)
121 return 3;
123 if (!S_ISREG (st.st_mode))
124 return 4;
126 /* Open the template file. */
127 fp = fopen (datemsk, "r");
128 if (fp == NULL)
129 return 2;
131 line = NULL;
132 len = 0;
135 ssize_t n;
137 n = __getline (&line, &len, fp);
138 if (n < 0)
139 break;
140 if (line[n - 1] == '\n')
141 line[n - 1] = '\0';
143 /* Do the conversion. */
144 tp->tm_year = tp->tm_mon = tp->tm_mday = tp->tm_wday = INT_MIN;
145 tp->tm_hour = tp->tm_sec = tp->tm_min = INT_MIN;
146 result = strptime (string, line, tp);
147 if (result && *result == '\0')
148 break;
150 while (!feof_unlocked (fp));
152 /* Free the buffer. */
153 free (line);
155 /* Check for errors. */
156 if (ferror_unlocked (fp))
158 fclose (fp);
159 return 5;
162 /* Close template file. */
163 fclose (fp);
165 if (result == NULL || *result != '\0')
166 return 7;
168 /* Get current time. */
169 time (&timer);
170 __localtime_r (&timer, &tm);
172 /* If only the weekday is given, today is assumed if the given day
173 is equal to the current day and next week if it is less. */
174 if (tp->tm_wday >= 0 && tp->tm_wday <= 6 && tp->tm_year == INT_MIN
175 && tp->tm_mon == INT_MIN && tp->tm_mday == INT_MIN)
177 tp->tm_year = tm.tm_year;
178 tp->tm_mon = tm.tm_mon;
179 tp->tm_mday = tm.tm_mday + (tp->tm_wday - tm.tm_wday + 7) % 7;
180 mday_ok = 1;
183 /* If only the month is given, the current month is assumed if the
184 given month is equal to the current month and next year if it is
185 less and no year is given (the first day of month is assumed if
186 no day is given. */
187 if (tp->tm_mon >= 0 && tp->tm_mon <= 11 && tp->tm_mday == INT_MIN)
189 if (tp->tm_year == INT_MIN)
190 tp->tm_year = tm.tm_year + (((tp->tm_mon - tm.tm_mon) < 0) ? 1 : 0);
191 tp->tm_mday = first_wday (tp->tm_year, tp->tm_mon, tp->tm_wday);
192 mday_ok = 1;
195 /* If no hour, minute and second are given the current hour, minute
196 and second are assumed. */
197 if (tp->tm_hour == INT_MIN && tp->tm_min == INT_MIN && tp->tm_sec == INT_MIN)
199 tp->tm_hour = tm.tm_hour;
200 tp->tm_min = tm.tm_min;
201 tp->tm_sec = tm.tm_sec;
204 /* If no date is given, today is assumed if the given hour is
205 greater than the current hour and tomorrow is assumed if
206 it is less. */
207 if (tp->tm_hour >= 0 && tp->tm_hour <= 23
208 && tp->tm_year == INT_MIN && tp->tm_mon == INT_MIN
209 && tp->tm_mday == INT_MIN && tp->tm_wday == INT_MIN)
211 tp->tm_year = tm.tm_year;
212 tp->tm_mon = tm.tm_mon;
213 tp->tm_mday = tm.tm_mday + ((tp->tm_hour - tm.tm_hour) < 0 ? 1 : 0);
214 mday_ok = 1;
217 /* Fill in the gaps. */
218 if (tp->tm_year == INT_MIN)
219 tp->tm_year = tm.tm_year;
220 if (tp->tm_hour == INT_MIN)
221 tp->tm_hour = 0;
222 if (tp->tm_min == INT_MIN)
223 tp->tm_min = 0;
224 if (tp->tm_sec == INT_MIN)
225 tp->tm_sec = 0;
227 /* Check if the day of month is within range, and if the time can be
228 represented in a time_t. We make use of the fact that the mktime
229 call normalizes the struct tm. */
230 if ((!mday_ok && !check_mday (TM_YEAR_BASE + tp->tm_year, tp->tm_mon,
231 tp->tm_mday))
232 || mktime (tp) == (time_t) -1)
233 return 8;
235 return 0;
237 #ifdef weak_alias
238 weak_alias (__getdate_r, getdate_r)
239 #endif
242 struct tm *
243 getdate (const char *string)
245 /* Buffer returned by getdate. */
246 static struct tm tmbuf;
247 int errval = __getdate_r (string, &tmbuf);
249 if (errval != 0)
251 getdate_err = errval;
252 return NULL;
255 return &tmbuf;