]>
Commit | Line | Data |
---|---|---|
f21acc89 | 1 | /* Convert a string representation of time to a time value. |
71412a8c | 2 | Copyright (C) 1997, 1998 Free Software Foundation, Inc. |
f21acc89 UD |
3 | This file is part of the GNU C Library. |
4 | Contributed by Mark Kettenis <kettenis@phys.uva.nl>, 1997. | |
5 | ||
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. | |
10 | ||
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. | |
15 | ||
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. */ | |
20 | ||
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> | |
27 | ||
28 | ||
29 | /* Prototypes for local functions. */ | |
30 | static int first_wday (int year, int mon, int wday); | |
31 | static int check_mday (int year, int mon, int mday); | |
32 | ||
33 | ||
34 | /* Set to one of the following values to indicate an error. | |
35 | 1 the DATEMSK environment variable is null or undefined, | |
36 | 2 the template file cannot be opened for reading, | |
37 | 3 failed to get file status information, | |
38 | 4 the template file is not a regular file, | |
39 | 5 an error is encountered while reading the template file, | |
40 | 6 memory allication failed (not enough memory available), | |
41 | 7 there is no line in the template that matches the input, | |
42 | 8 invalid input specification Example: February 31 or a time is | |
43 | specified that can not be represented in a time_t (representing | |
44 | the time in seconds since 00:00:00 UTC, January 1, 1970) */ | |
45 | int getdate_err = 0; | |
46 | ||
47 | ||
48 | /* Returns the first weekday WDAY of month MON in the year YEAR. */ | |
49 | static int | |
50 | first_wday (int year, int mon, int wday) | |
51 | { | |
52 | struct tm tm; | |
53 | ||
54 | if (wday == INT_MIN) | |
55 | return 1; | |
56 | ||
57 | memset (&tm, 0, sizeof (struct tm)); | |
58 | tm.tm_year = year; | |
59 | tm.tm_mon = mon; | |
60 | tm.tm_mday = 1; | |
61 | mktime (&tm); | |
62 | ||
63 | return (1 + (wday - tm.tm_wday + 7) % 7); | |
64 | } | |
65 | ||
66 | ||
67 | /* Returns 1 if MDAY is a valid day of the month in month MON of year | |
68 | YEAR, and 0 if it is not. */ | |
69 | static int | |
70 | check_mday (int year, int mon, int mday) | |
71 | { | |
72 | switch (mon) | |
73 | { | |
51c68755 UD |
74 | case 0: |
75 | case 2: | |
f21acc89 UD |
76 | case 4: |
77 | case 6: | |
51c68755 | 78 | case 7: |
f21acc89 UD |
79 | case 9: |
80 | case 11: | |
51c68755 UD |
81 | if (mday >= 1 && mday <= 31) |
82 | return 1; | |
83 | break; | |
84 | case 3: | |
85 | case 5: | |
86 | case 8: | |
87 | case 10: | |
f21acc89 UD |
88 | if (mday >= 1 && mday <= 30) |
89 | return 1; | |
90 | break; | |
51c68755 | 91 | case 1: |
f21acc89 UD |
92 | if (mday >= 1 && mday < (__isleap (year) ? 29 : 28)) |
93 | return 1; | |
94 | break; | |
95 | } | |
96 | ||
97 | return 0; | |
98 | } | |
99 | ||
100 | ||
101 | int | |
102 | __getdate_r (const char *string, struct tm *tp) | |
103 | { | |
104 | FILE *fp; | |
105 | char *line; | |
106 | size_t len; | |
107 | char *datemsk; | |
108 | char *result = NULL; | |
109 | time_t timer; | |
110 | struct tm tm; | |
111 | struct stat st; | |
112 | int mday_ok = 0; | |
113 | ||
114 | datemsk = getenv ("DATEMSK"); | |
115 | if (datemsk == NULL || *datemsk == '\0') | |
116 | return 1; | |
117 | ||
118 | if (stat (datemsk, &st) < 0) | |
119 | return 3; | |
120 | ||
121 | if (!S_ISREG (st.st_mode)) | |
122 | return 4; | |
123 | ||
124 | /* Open the template file. */ | |
125 | fp = fopen (datemsk, "r"); | |
126 | if (fp == NULL) | |
127 | return 2; | |
128 | ||
129 | line = NULL; | |
130 | len = 0; | |
131 | do | |
132 | { | |
133 | ssize_t n; | |
134 | ||
135 | n = __getline (&line, &len, fp); | |
136 | if (n < 0) | |
137 | break; | |
138 | if (line[n - 1] == '\n') | |
139 | line[n - 1] = '\0'; | |
140 | ||
141 | /* Do the conversion. */ | |
142 | tp->tm_year = tp->tm_mon = tp->tm_mday = tp->tm_wday = INT_MIN; | |
143 | tp->tm_hour = tp->tm_sec = tp->tm_min = INT_MIN; | |
144 | result = strptime (string, line, tp); | |
145 | if (result && *result == '\0') | |
146 | break; | |
147 | } | |
71412a8c | 148 | while (!feof_unlocked (fp)); |
f21acc89 UD |
149 | |
150 | /* Free the buffer. */ | |
151 | free (line); | |
152 | ||
153 | /* Check for errors. */ | |
71412a8c | 154 | if (ferror_unlocked (fp)) |
f21acc89 UD |
155 | { |
156 | fclose (fp); | |
157 | return 5; | |
158 | } | |
159 | ||
160 | /* Close template file. */ | |
161 | fclose (fp); | |
162 | ||
907a1bac | 163 | if (result == NULL || *result != '\0') |
f21acc89 UD |
164 | return 7; |
165 | ||
166 | /* Get current time. */ | |
167 | time (&timer); | |
168 | __localtime_r (&timer, &tm); | |
169 | ||
170 | /* If only the weekday is given, today is assumed if the given day | |
171 | is equal to the current day and next week if it is less. */ | |
172 | if (tp->tm_wday >= 0 && tp->tm_wday <= 6 && tp->tm_year == INT_MIN | |
173 | && tp->tm_mon == INT_MIN && tp->tm_mday == INT_MIN) | |
174 | { | |
175 | tp->tm_year = tm.tm_year; | |
176 | tp->tm_mon = tm.tm_mon; | |
177 | tp->tm_mday = tm.tm_mday + (tp->tm_wday - tm.tm_wday + 7) % 7; | |
178 | mday_ok = 1; | |
179 | } | |
180 | ||
181 | /* If only the month is given, the current month is assumed if the | |
182 | given month is equal to the current month and next year if it is | |
183 | less and no year is given (the first day of month is assumed if | |
184 | no day is given. */ | |
185 | if (tp->tm_mon >= 0 && tp->tm_mon <= 11 && tp->tm_mday == INT_MIN) | |
186 | { | |
187 | if (tp->tm_year == INT_MIN) | |
188 | tp->tm_year = tm.tm_year + (((tp->tm_mon - tm.tm_mon) < 0) ? 1 : 0); | |
189 | tp->tm_mday = first_wday (tp->tm_year, tp->tm_mon, tp->tm_wday); | |
190 | mday_ok = 1; | |
191 | } | |
192 | ||
193 | /* If no hour, minute and second are given the current hour, minute | |
194 | and second are assumed. */ | |
195 | if (tp->tm_hour == INT_MIN && tp->tm_min == INT_MIN && tp->tm_sec == INT_MIN) | |
196 | { | |
197 | tp->tm_hour = tm.tm_hour; | |
198 | tp->tm_min = tm.tm_min; | |
199 | tp->tm_sec = tm.tm_sec; | |
200 | } | |
201 | ||
202 | /* If no date is given, today is assumed if the given hour is | |
203 | greater than the current hour and tomorrow is assumed if | |
204 | it is less. */ | |
205 | if (tp->tm_hour >= 0 && tp->tm_hour <= 23 | |
206 | && tp->tm_year == INT_MIN && tp->tm_mon == INT_MIN | |
207 | && tp->tm_mday == INT_MIN && tp->tm_wday == INT_MIN) | |
208 | { | |
209 | tp->tm_year = tm.tm_year; | |
210 | tp->tm_mon = tm.tm_mon; | |
211 | tp->tm_mday = tm.tm_mday + ((tp->tm_hour - tm.tm_hour) < 0 ? 1 : 0); | |
212 | mday_ok = 1; | |
213 | } | |
214 | ||
215 | /* Fill in the gaps. */ | |
216 | if (tp->tm_year == INT_MIN) | |
217 | tp->tm_year = tm.tm_year; | |
218 | if (tp->tm_hour == INT_MIN) | |
219 | tp->tm_hour = 0; | |
220 | if (tp->tm_min == INT_MIN) | |
221 | tp->tm_min = 0; | |
222 | if (tp->tm_sec == INT_MIN) | |
223 | tp->tm_sec = 0; | |
224 | ||
225 | /* Check if the day of month is within range, and if the time can be | |
226 | represented in a time_t. We make use of the fact that the mktime | |
227 | call normalizes the struct tm. */ | |
228 | if ((!mday_ok && !check_mday (tp->tm_year, tp->tm_mon, tp->tm_mday)) | |
229 | || mktime (tp) == (time_t) -1) | |
230 | return 8; | |
231 | ||
232 | return 0; | |
233 | } | |
234 | #ifdef weak_alias | |
235 | weak_alias (__getdate_r, getdate_r) | |
236 | #endif | |
237 | ||
238 | ||
239 | struct tm * | |
240 | getdate (const char *string) | |
241 | { | |
242 | /* Buffer returned by getdate. */ | |
243 | static struct tm tmbuf; | |
244 | int errval = __getdate_r (string, &tmbuf); | |
245 | ||
907a1bac UD |
246 | if (errval != 0) |
247 | { | |
248 | getdate_err = errval; | |
249 | return NULL; | |
250 | } | |
251 | ||
252 | return &tmbuf; | |
f21acc89 | 253 | } |