getdate.c revision 1.2 1 1.2 mbalmer /* $NetBSD: getdate.c,v 1.2 2009/11/22 18:07:39 mbalmer Exp $ */
2 1.1 ginsbach /*
3 1.1 ginsbach * Copyright (c) 2009 The NetBSD Foundation, Inc.
4 1.1 ginsbach * All rights reserved.
5 1.1 ginsbach *
6 1.1 ginsbach * This code is derived from software contributed to The NetBSD Foundation
7 1.1 ginsbach * by Brian Ginsbach.
8 1.1 ginsbach *
9 1.1 ginsbach * Redistribution and use in source and binary forms, with or without
10 1.1 ginsbach * modification, are permitted provided that the following conditions
11 1.1 ginsbach * are met:
12 1.1 ginsbach * 1. Redistributions of source code must retain the above copyright
13 1.1 ginsbach * notice, this list of conditions and the following disclaimer.
14 1.1 ginsbach * 2. Redistributions in binary form must reproduce the above copyright
15 1.1 ginsbach * notice, this list of conditions and the following disclaimer in the
16 1.1 ginsbach * documentation and/or other materials provided with the distribution.
17 1.1 ginsbach *
18 1.1 ginsbach * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19 1.1 ginsbach * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20 1.1 ginsbach * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 1.1 ginsbach * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22 1.1 ginsbach * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 1.1 ginsbach * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 1.1 ginsbach * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 1.1 ginsbach * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 1.1 ginsbach * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 1.1 ginsbach * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 1.1 ginsbach * POSSIBILITY OF SUCH DAMAGE.
29 1.1 ginsbach */
30 1.1 ginsbach
31 1.1 ginsbach #include <sys/stat.h>
32 1.1 ginsbach
33 1.1 ginsbach #include <errno.h>
34 1.1 ginsbach #include <stdio.h>
35 1.1 ginsbach #include <stdlib.h>
36 1.1 ginsbach #include <string.h>
37 1.1 ginsbach #include <time.h>
38 1.1 ginsbach #include <unistd.h>
39 1.1 ginsbach #include <util.h>
40 1.1 ginsbach
41 1.1 ginsbach #define TMSENTINEL (-1)
42 1.1 ginsbach
43 1.1 ginsbach /*
44 1.1 ginsbach * getdate_err is set to one of the following values on error.
45 1.1 ginsbach *
46 1.1 ginsbach * 1 The DATEMSK environment variable is null or undefined.
47 1.1 ginsbach * 2 The template file cannot be opened for reading.
48 1.1 ginsbach * 3 Failed to get file status information.
49 1.1 ginsbach * 4 Template file is not a regular file.
50 1.1 ginsbach * 5 Encountered an error while reading the template file.
51 1.1 ginsbach * 6 Cannot allocate memory.
52 1.1 ginsbach * 7 Input string does not match any line in the template.
53 1.1 ginsbach * 8 Input string is invalid (for example, February 31) or could not
54 1.1 ginsbach * be represented in a time_t.
55 1.1 ginsbach */
56 1.1 ginsbach
57 1.1 ginsbach int getdate_err;
58 1.1 ginsbach
59 1.1 ginsbach struct tm *
60 1.1 ginsbach getdate(const char *str)
61 1.1 ginsbach {
62 1.1 ginsbach char *datemsk, *line, *rp;
63 1.1 ginsbach FILE *fp;
64 1.1 ginsbach struct stat sb;
65 1.1 ginsbach static struct tm rtm, tmnow;
66 1.1 ginsbach struct tm *tmp, *rtmp = &rtm;
67 1.1 ginsbach size_t lineno = 0;
68 1.1 ginsbach time_t now;
69 1.1 ginsbach
70 1.1 ginsbach if (((datemsk = getenv("DATEMSK")) == NULL) || *datemsk == '\0') {
71 1.1 ginsbach getdate_err = 1;
72 1.1 ginsbach return (NULL);
73 1.1 ginsbach }
74 1.1 ginsbach
75 1.1 ginsbach if (stat(datemsk, &sb) < 0) {
76 1.1 ginsbach getdate_err = 3;
77 1.1 ginsbach return (NULL);
78 1.1 ginsbach }
79 1.1 ginsbach
80 1.1 ginsbach if ((sb.st_mode & S_IFMT) != S_IFREG) {
81 1.1 ginsbach getdate_err = 4;
82 1.1 ginsbach return (NULL);
83 1.1 ginsbach }
84 1.1 ginsbach
85 1.1 ginsbach if ((fp = fopen(datemsk, "r")) == NULL) {
86 1.1 ginsbach getdate_err = 2;
87 1.1 ginsbach return (NULL);
88 1.1 ginsbach }
89 1.1 ginsbach
90 1.1 ginsbach /* loop through datemsk file */
91 1.1 ginsbach errno = 0;
92 1.1 ginsbach rp = NULL;
93 1.1 ginsbach while ((line = fparseln(fp, NULL, &lineno, NULL, 0)) != NULL) {
94 1.1 ginsbach /* initialize tmp with sentinels */
95 1.1 ginsbach rtm.tm_sec = rtm.tm_min = rtm.tm_hour = TMSENTINEL;
96 1.1 ginsbach rtm.tm_mday = rtm.tm_mon = rtm.tm_year = TMSENTINEL;
97 1.1 ginsbach rtm.tm_wday = rtm.tm_yday = rtm.tm_isdst = TMSENTINEL;
98 1.1 ginsbach rtm.tm_gmtoff = 0;
99 1.1 ginsbach rtm.tm_zone = NULL;
100 1.1 ginsbach rp = strptime(str, line, rtmp);
101 1.1 ginsbach free(line);
102 1.1 ginsbach if (rp != NULL)
103 1.1 ginsbach break;
104 1.1 ginsbach errno = 0;
105 1.1 ginsbach }
106 1.1 ginsbach if (errno != 0 || ferror(fp)) {
107 1.1 ginsbach if (errno == ENOMEM)
108 1.1 ginsbach getdate_err = 6;
109 1.1 ginsbach else
110 1.1 ginsbach getdate_err = 5;
111 1.1 ginsbach fclose(fp);
112 1.1 ginsbach return (NULL);
113 1.1 ginsbach }
114 1.1 ginsbach if (feof(fp) || (rp != NULL && *rp != '\0')) {
115 1.1 ginsbach getdate_err = 7;
116 1.1 ginsbach return (NULL);
117 1.1 ginsbach }
118 1.1 ginsbach fclose(fp);
119 1.1 ginsbach
120 1.1 ginsbach time(&now);
121 1.1 ginsbach tmp = localtime(&now);
122 1.1 ginsbach tmnow = *tmp;
123 1.1 ginsbach
124 1.1 ginsbach /*
125 1.1 ginsbach * This implementation does not accept setting the broken-down time
126 1.1 ginsbach * to anything other than the localtime(). It is not possible to
127 1.1 ginsbach * change the scanned timezone with %Z.
128 1.1 ginsbach *
129 1.1 ginsbach * Note IRIX and Solaris accept only the current zone for %Z.
130 1.1 ginsbach * XXX Is there any implementation that matches the standard?
131 1.1 ginsbach * XXX (Or am I reading the standard wrong?)
132 1.1 ginsbach *
133 1.1 ginsbach * Note: Neither XPG 6 (POSIX 2004) nor XPG 7 (POSIX 2008)
134 1.1 ginsbach * requires strptime(3) support for %Z.
135 1.1 ginsbach */
136 1.1 ginsbach
137 1.1 ginsbach /*
138 1.1 ginsbach * Given only a weekday find the first matching weekday starting
139 1.1 ginsbach * with the current weekday and moving into the future.
140 1.1 ginsbach */
141 1.1 ginsbach if (rtm.tm_wday != TMSENTINEL && rtm.tm_year == TMSENTINEL &&
142 1.1 ginsbach rtm.tm_mon == TMSENTINEL && rtm.tm_mday == TMSENTINEL) {
143 1.1 ginsbach rtm.tm_year = tmnow.tm_year;
144 1.1 ginsbach rtm.tm_mon = tmnow.tm_mon;
145 1.1 ginsbach rtm.tm_mday = tmnow.tm_mday +
146 1.1 ginsbach (rtm.tm_wday - tmnow.tm_wday + 7) % 7;
147 1.1 ginsbach }
148 1.1 ginsbach
149 1.1 ginsbach /*
150 1.1 ginsbach * Given only a month (and no year) find the first matching month
151 1.1 ginsbach * starting with the current month and moving into the future.
152 1.1 ginsbach */
153 1.1 ginsbach if (rtm.tm_mon != TMSENTINEL) {
154 1.1 ginsbach if (rtm.tm_year == TMSENTINEL) {
155 1.1 ginsbach rtm.tm_year = tmnow.tm_year +
156 1.1 ginsbach ((rtm.tm_mon < tmnow.tm_mon)? 1 : 0);
157 1.1 ginsbach }
158 1.1 ginsbach if (rtm.tm_mday == TMSENTINEL) {
159 1.1 ginsbach /* assume the first of the month */
160 1.1 ginsbach rtm.tm_mday = 1;
161 1.1 ginsbach /*
162 1.1 ginsbach * XXX This isn't documented! Just observed behavior.
163 1.1 ginsbach *
164 1.1 ginsbach * Given the weekday find the first matching weekday
165 1.1 ginsbach * starting with the weekday of the first day of the
166 1.1 ginsbach * the month and moving into the future.
167 1.1 ginsbach */
168 1.1 ginsbach if (rtm.tm_wday != TMSENTINEL) {
169 1.1 ginsbach struct tm tm;
170 1.1 ginsbach
171 1.1 ginsbach memset(&tm, 0, sizeof(struct tm));
172 1.1 ginsbach tm.tm_year = rtm.tm_year;
173 1.1 ginsbach tm.tm_mon = rtm.tm_mon;
174 1.1 ginsbach tm.tm_mday = 1;
175 1.1 ginsbach mktime(&tm);
176 1.1 ginsbach rtm.tm_mday +=
177 1.1 ginsbach (rtm.tm_wday - tm.tm_wday + 7) % 7;
178 1.1 ginsbach }
179 1.1 ginsbach }
180 1.1 ginsbach }
181 1.1 ginsbach
182 1.1 ginsbach /*
183 1.1 ginsbach * Given no time of day assume the current time of day.
184 1.1 ginsbach */
185 1.1 ginsbach if (rtm.tm_hour == TMSENTINEL &&
186 1.1 ginsbach rtm.tm_min == TMSENTINEL && rtm.tm_sec == TMSENTINEL) {
187 1.1 ginsbach rtm.tm_hour = tmnow.tm_hour;
188 1.1 ginsbach rtm.tm_min = tmnow.tm_min;
189 1.1 ginsbach rtm.tm_sec = tmnow.tm_sec;
190 1.1 ginsbach }
191 1.1 ginsbach /*
192 1.1 ginsbach * Given an hour and no date, find the first matching hour starting
193 1.1 ginsbach * with the current hour and moving into the future
194 1.1 ginsbach */
195 1.1 ginsbach if (rtm.tm_hour != TMSENTINEL &&
196 1.1 ginsbach rtm.tm_year == TMSENTINEL && rtm.tm_mon == TMSENTINEL &&
197 1.1 ginsbach rtm.tm_mday == TMSENTINEL) {
198 1.1 ginsbach rtm.tm_year = tmnow.tm_year;
199 1.1 ginsbach rtm.tm_mon = tmnow.tm_mon;
200 1.1 ginsbach rtm.tm_mday = tmnow.tm_mday;
201 1.1 ginsbach if (rtm.tm_hour < tmnow.tm_hour)
202 1.1 ginsbach rtm.tm_hour += 24;
203 1.1 ginsbach }
204 1.1 ginsbach
205 1.1 ginsbach /*
206 1.1 ginsbach * Set to 'sane' values; mktime(3) does funny things otherwise.
207 1.1 ginsbach * No hours, no minutes, no seconds, no service.
208 1.1 ginsbach */
209 1.1 ginsbach if (rtm.tm_hour == TMSENTINEL)
210 1.1 ginsbach rtm.tm_hour = 0;
211 1.1 ginsbach if (rtm.tm_min == TMSENTINEL)
212 1.1 ginsbach rtm.tm_min = 0;
213 1.1 ginsbach if (rtm.tm_sec == TMSENTINEL)
214 1.1 ginsbach rtm.tm_sec = 0;
215 1.1 ginsbach
216 1.1 ginsbach /*
217 1.1 ginsbach * Given only a year the values of month, day of month, day of year,
218 1.1 ginsbach * week day and is daylight (summer) time are unspecified.
219 1.1 ginsbach * (Specified on the Solaris man page not POSIX.)
220 1.1 ginsbach */
221 1.1 ginsbach if (rtm.tm_year != TMSENTINEL &&
222 1.1 ginsbach rtm.tm_mon == TMSENTINEL && rtm.tm_mday == TMSENTINEL) {
223 1.1 ginsbach rtm.tm_mon = 0;
224 1.1 ginsbach rtm.tm_mday = 1;
225 1.1 ginsbach /*
226 1.1 ginsbach * XXX More undocumented functionality but observed.
227 1.1 ginsbach *
228 1.1 ginsbach * Given the weekday find the first matching weekday
229 1.2 mbalmer * starting with the weekday of the first day of the
230 1.1 ginsbach * month and moving into the future.
231 1.1 ginsbach */
232 1.1 ginsbach if (rtm.tm_wday != TMSENTINEL) {
233 1.1 ginsbach struct tm tm;
234 1.1 ginsbach
235 1.1 ginsbach memset(&tm, 0, sizeof(struct tm));
236 1.1 ginsbach tm.tm_year = rtm.tm_year;
237 1.1 ginsbach tm.tm_mon = rtm.tm_mon;
238 1.1 ginsbach tm.tm_mday = 1;
239 1.1 ginsbach mktime(&tm);
240 1.1 ginsbach rtm.tm_mday += (rtm.tm_wday - tm.tm_wday + 7) % 7;
241 1.1 ginsbach }
242 1.1 ginsbach }
243 1.1 ginsbach
244 1.1 ginsbach /*
245 1.1 ginsbach * Given only the century but no year within, the current year
246 1.1 ginsbach * is assumed. (Specified on the Solaris man page not POSIX.)
247 1.1 ginsbach *
248 1.1 ginsbach * Warning ugly end case
249 1.1 ginsbach *
250 1.1 ginsbach * This is more work since strptime(3) doesn't "do the right thing".
251 1.1 ginsbach */
252 1.1 ginsbach if (rtm.tm_year != TMSENTINEL && (rtm.tm_year - 1900) >= 0) {
253 1.1 ginsbach rtm.tm_year -= 1900;
254 1.1 ginsbach rtm.tm_year += (tmnow.tm_year % 100);
255 1.1 ginsbach }
256 1.1 ginsbach
257 1.1 ginsbach /*
258 1.1 ginsbach * mktime() will normalize all values and also check that the
259 1.1 ginsbach * value will fit into a time_t.
260 1.1 ginsbach *
261 1.1 ginsbach * This is only for POSIX correctness. A date >= 1900 is
262 1.1 ginsbach * really ok, but using a time_t limits things.
263 1.1 ginsbach */
264 1.1 ginsbach if (mktime(rtmp) < 0) {
265 1.1 ginsbach getdate_err = 8;
266 1.1 ginsbach return (NULL);
267 1.1 ginsbach }
268 1.1 ginsbach
269 1.1 ginsbach return (rtmp);
270 1.1 ginsbach }
271