Commit | Line | Data |
---|---|---|
d436bd52 RK |
1 | /* Derived from getdate.c in glibc 2.3.6. This is pretty much |
2 | * standard getdate() except that you supply the template in an | |
3 | * argument, rather than messing around with environment variables and | |
4 | * files. */ | |
5 | ||
6 | /* Convert a string representation of time to a time value. | |
7 | Copyright (C) 1997,1998,1999,2000,2001,2003 Free Software Foundation, Inc. | |
8 | This file is part of the GNU C Library. | |
9 | Contributed by Mark Kettenis <kettenis@phys.uva.nl>, 1997. | |
10 | ||
11 | The GNU C Library is free software; you can redistribute it and/or | |
12 | modify it under the terms of the GNU Lesser General Public | |
13 | License as published by the Free Software Foundation; either | |
14 | version 2.1 of the License, or (at your option) any later version. | |
15 | ||
16 | The GNU C Library is distributed in the hope that it will be useful, | |
17 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
18 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
19 | Lesser General Public License for more details. | |
20 | ||
21 | You should have received a copy of the GNU Lesser General Public | |
22 | License along with the GNU C Library; if not, write to the Free | |
23 | Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA | |
24 | 02111-1307 USA. */ | |
25 | ||
835a5b33 | 26 | #define _GNU_SOURCE 1 /* to expose strptime */ |
d436bd52 RK |
27 | #include <limits.h> |
28 | #include <stdio.h> | |
29 | #include <stdlib.h> | |
30 | #include <string.h> | |
31 | #include <time.h> | |
32 | ||
33 | #include "dateparse.h" | |
34 | ||
35 | #define TM_YEAR_BASE 1900 | |
36 | ||
37 | ||
38 | /* Prototypes for local functions. */ | |
39 | static int first_wday (int year, int mon, int wday); | |
40 | static int check_mday (int year, int mon, int mday); | |
41 | ||
42 | # define isleap(year) \ | |
43 | ((year) % 4 == 0 && ((year) % 100 != 0 || (year) % 400 == 0)) | |
44 | ||
45 | /* Set to one of the following values to indicate an error. | |
46 | 1 the DATEMSK environment variable is null or undefined, | |
47 | 2 the template file cannot be opened for reading, | |
48 | 3 failed to get file status information, | |
49 | 4 the template file is not a regular file, | |
50 | 5 an error is encountered while reading the template file, | |
51 | 6 memory allication failed (not enough memory available), | |
52 | 7 there is no line in the template that matches the input, | |
53 | 8 invalid input specification Example: February 31 or a time is | |
54 | specified that can not be represented in a time_t (representing | |
55 | the time in seconds since 00:00:00 UTC, January 1, 1970) */ | |
835a5b33 | 56 | /*int xgetdate_err;*/ |
d436bd52 RK |
57 | |
58 | ||
59 | /* Returns the first weekday WDAY of month MON in the year YEAR. */ | |
60 | static int | |
61 | first_wday (int year, int mon, int wday) | |
62 | { | |
63 | struct tm tm; | |
64 | ||
65 | if (wday == INT_MIN) | |
66 | return 1; | |
67 | ||
68 | memset (&tm, 0, sizeof (struct tm)); | |
69 | tm.tm_year = year; | |
70 | tm.tm_mon = mon; | |
71 | tm.tm_mday = 1; | |
72 | mktime (&tm); | |
73 | ||
74 | return (1 + (wday - tm.tm_wday + 7) % 7); | |
75 | } | |
76 | ||
77 | ||
78 | /* Returns 1 if MDAY is a valid day of the month in month MON of year | |
79 | YEAR, and 0 if it is not. */ | |
80 | static int | |
81 | check_mday (int year, int mon, int mday) | |
82 | { | |
83 | switch (mon) | |
84 | { | |
85 | case 0: | |
86 | case 2: | |
87 | case 4: | |
88 | case 6: | |
89 | case 7: | |
90 | case 9: | |
91 | case 11: | |
92 | if (mday >= 1 && mday <= 31) | |
93 | return 1; | |
94 | break; | |
95 | case 3: | |
96 | case 5: | |
97 | case 8: | |
98 | case 10: | |
99 | if (mday >= 1 && mday <= 30) | |
100 | return 1; | |
101 | break; | |
102 | case 1: | |
103 | if (mday >= 1 && mday <= (isleap (year) ? 29 : 28)) | |
104 | return 1; | |
105 | break; | |
106 | } | |
107 | ||
108 | return 0; | |
109 | } | |
110 | ||
111 | ||
112 | int | |
113 | xgetdate_r (const char *string, struct tm *tp, | |
114 | const char *const *template) | |
115 | { | |
116 | const char *line; | |
117 | size_t len; | |
118 | char *result = NULL; | |
119 | time_t timer; | |
120 | struct tm tm; | |
121 | int mday_ok = 0; | |
122 | ||
123 | line = NULL; | |
124 | len = 0; | |
125 | while((line = *template++)) | |
126 | { | |
127 | /* Do the conversion. */ | |
128 | tp->tm_year = tp->tm_mon = tp->tm_mday = tp->tm_wday = INT_MIN; | |
129 | tp->tm_hour = tp->tm_sec = tp->tm_min = INT_MIN; | |
130 | tp->tm_isdst = -1; | |
131 | tp->tm_gmtoff = 0; | |
132 | tp->tm_zone = NULL; | |
133 | result = strptime (string, line, tp); | |
134 | if (result && *result == '\0') | |
135 | break; | |
136 | } | |
137 | ||
138 | if (result == NULL || *result != '\0') | |
139 | return 7; | |
140 | ||
141 | /* Get current time. */ | |
142 | time (&timer); | |
143 | localtime_r (&timer, &tm); | |
144 | ||
145 | /* If only the weekday is given, today is assumed if the given day | |
146 | is equal to the current day and next week if it is less. */ | |
147 | if (tp->tm_wday >= 0 && tp->tm_wday <= 6 && tp->tm_year == INT_MIN | |
148 | && tp->tm_mon == INT_MIN && tp->tm_mday == INT_MIN) | |
149 | { | |
150 | tp->tm_year = tm.tm_year; | |
151 | tp->tm_mon = tm.tm_mon; | |
152 | tp->tm_mday = tm.tm_mday + (tp->tm_wday - tm.tm_wday + 7) % 7; | |
153 | mday_ok = 1; | |
154 | } | |
155 | ||
156 | /* If only the month is given, the current month is assumed if the | |
157 | given month is equal to the current month and next year if it is | |
158 | less and no year is given (the first day of month is assumed if | |
159 | no day is given. */ | |
160 | if (tp->tm_mon >= 0 && tp->tm_mon <= 11 && tp->tm_mday == INT_MIN) | |
161 | { | |
162 | if (tp->tm_year == INT_MIN) | |
163 | tp->tm_year = tm.tm_year + (((tp->tm_mon - tm.tm_mon) < 0) ? 1 : 0); | |
164 | tp->tm_mday = first_wday (tp->tm_year, tp->tm_mon, tp->tm_wday); | |
165 | mday_ok = 1; | |
166 | } | |
167 | ||
168 | /* If no hour, minute and second are given the current hour, minute | |
169 | and second are assumed. */ | |
170 | if (tp->tm_hour == INT_MIN && tp->tm_min == INT_MIN && tp->tm_sec == INT_MIN) | |
171 | { | |
172 | tp->tm_hour = tm.tm_hour; | |
173 | tp->tm_min = tm.tm_min; | |
174 | tp->tm_sec = tm.tm_sec; | |
175 | } | |
176 | ||
177 | /* If no date is given, today is assumed if the given hour is | |
178 | greater than the current hour and tomorrow is assumed if | |
179 | it is less. */ | |
180 | if (tp->tm_hour >= 0 && tp->tm_hour <= 23 | |
181 | && tp->tm_year == INT_MIN && tp->tm_mon == INT_MIN | |
182 | && tp->tm_mday == INT_MIN && tp->tm_wday == INT_MIN) | |
183 | { | |
184 | tp->tm_year = tm.tm_year; | |
185 | tp->tm_mon = tm.tm_mon; | |
186 | tp->tm_mday = tm.tm_mday + ((tp->tm_hour - tm.tm_hour) < 0 ? 1 : 0); | |
187 | mday_ok = 1; | |
188 | } | |
189 | ||
190 | /* Fill in the gaps. */ | |
191 | if (tp->tm_year == INT_MIN) | |
192 | tp->tm_year = tm.tm_year; | |
193 | if (tp->tm_hour == INT_MIN) | |
194 | tp->tm_hour = 0; | |
195 | if (tp->tm_min == INT_MIN) | |
196 | tp->tm_min = 0; | |
197 | if (tp->tm_sec == INT_MIN) | |
198 | tp->tm_sec = 0; | |
199 | ||
200 | /* Check if the day of month is within range, and if the time can be | |
201 | represented in a time_t. We make use of the fact that the mktime | |
202 | call normalizes the struct tm. */ | |
203 | if ((!mday_ok && !check_mday (TM_YEAR_BASE + tp->tm_year, tp->tm_mon, | |
204 | tp->tm_mday)) | |
205 | || mktime (tp) == (time_t) -1) | |
206 | return 8; | |
207 | ||
208 | return 0; | |
209 | } | |
210 | ||
211 | ||
835a5b33 | 212 | #if 0 |
d436bd52 RK |
213 | struct tm * |
214 | xgetdate (const char *string, const char *const *template) | |
215 | { | |
216 | /* Buffer returned by getdate. */ | |
217 | static struct tm tmbuf; | |
218 | int errval = xgetdate_r (string, &tmbuf, template); | |
219 | ||
220 | if (errval != 0) | |
221 | { | |
835a5b33 | 222 | xgetdate_err = errval; |
d436bd52 RK |
223 | return NULL; |
224 | } | |
225 | ||
226 | return &tmbuf; | |
227 | } | |
835a5b33 | 228 | #endif |