1 /* $NetBSD: getdate.c,v 1.4 2018/01/04 20:57:29 kamil Exp $ */
3 * Copyright (c) 2009 The NetBSD Foundation, Inc.
6 * This code is derived from software contributed to The NetBSD Foundation
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
31 #include "namespace.h"
43 #include "un-namespace.h"
45 #define TMSENTINEL (-1)
48 * getdate_err is set to one of the following values on error.
50 * 1 The DATEMSK environment variable is null or undefined.
51 * 2 The template file cannot be opened for reading.
52 * 3 Failed to get file status information.
53 * 4 Template file is not a regular file.
54 * 5 Encountered an error while reading the template file.
55 * 6 Cannot allocate memory.
56 * 7 Input string does not match any line in the template.
57 * 8 Input string is invalid (for example, February 31) or could not
58 * be represented in a time_t.
64 getdate(const char *str)
66 char *datemsk, *line, *rp;
69 static struct tm rtm, tmnow;
70 struct tm *tmp, *rtmp = &rtm;
74 if (((datemsk = getenv("DATEMSK")) == NULL) || *datemsk == '\0') {
79 if (stat(datemsk, &sb) < 0) {
84 if ((sb.st_mode & S_IFMT) != S_IFREG) {
89 if ((fp = fopen(datemsk, "re")) == NULL) {
94 /* loop through datemsk file */
97 while ((line = fparseln(fp, NULL, &lineno, NULL, 0)) != NULL) {
98 /* initialize tmp with sentinels */
99 rtm.tm_sec = rtm.tm_min = rtm.tm_hour = TMSENTINEL;
100 rtm.tm_mday = rtm.tm_mon = rtm.tm_year = TMSENTINEL;
101 rtm.tm_wday = rtm.tm_yday = rtm.tm_isdst = TMSENTINEL;
104 rp = strptime(str, line, rtmp);
110 if (errno != 0 || ferror(fp)) {
118 if (feof(fp) || (rp != NULL && *rp != '\0')) {
125 tmp = localtime(&now);
129 * This implementation does not accept setting the broken-down time
130 * to anything other than the localtime(). It is not possible to
131 * change the scanned timezone with %Z.
133 * Note IRIX and Solaris accept only the current zone for %Z.
134 * XXX Is there any implementation that matches the standard?
135 * XXX (Or am I reading the standard wrong?)
137 * Note: Neither XPG 6 (POSIX 2004) nor XPG 7 (POSIX 2008)
138 * requires strptime(3) support for %Z.
142 * Given only a weekday find the first matching weekday starting
143 * with the current weekday and moving into the future.
145 if (rtm.tm_wday != TMSENTINEL && rtm.tm_year == TMSENTINEL &&
146 rtm.tm_mon == TMSENTINEL && rtm.tm_mday == TMSENTINEL) {
147 rtm.tm_year = tmnow.tm_year;
148 rtm.tm_mon = tmnow.tm_mon;
149 rtm.tm_mday = tmnow.tm_mday +
150 (rtm.tm_wday - tmnow.tm_wday + 7) % 7;
154 * Given only a month (and no year) find the first matching month
155 * starting with the current month and moving into the future.
157 if (rtm.tm_mon != TMSENTINEL) {
158 if (rtm.tm_year == TMSENTINEL) {
159 rtm.tm_year = tmnow.tm_year +
160 ((rtm.tm_mon < tmnow.tm_mon)? 1 : 0);
162 if (rtm.tm_mday == TMSENTINEL) {
163 /* assume the first of the month */
166 * XXX This isn't documented! Just observed behavior.
168 * Given the weekday find the first matching weekday
169 * starting with the weekday of the first day of the
170 * the month and moving into the future.
172 if (rtm.tm_wday != TMSENTINEL) {
175 memset(&tm, 0, sizeof(struct tm));
176 tm.tm_year = rtm.tm_year;
177 tm.tm_mon = rtm.tm_mon;
181 (rtm.tm_wday - tm.tm_wday + 7) % 7;
187 * Given no time of day assume the current time of day.
189 if (rtm.tm_hour == TMSENTINEL &&
190 rtm.tm_min == TMSENTINEL && rtm.tm_sec == TMSENTINEL) {
191 rtm.tm_hour = tmnow.tm_hour;
192 rtm.tm_min = tmnow.tm_min;
193 rtm.tm_sec = tmnow.tm_sec;
196 * Given an hour and no date, find the first matching hour starting
197 * with the current hour and moving into the future
199 if (rtm.tm_hour != TMSENTINEL &&
200 rtm.tm_year == TMSENTINEL && rtm.tm_mon == TMSENTINEL &&
201 rtm.tm_mday == TMSENTINEL) {
202 rtm.tm_year = tmnow.tm_year;
203 rtm.tm_mon = tmnow.tm_mon;
204 rtm.tm_mday = tmnow.tm_mday;
205 if (rtm.tm_hour < tmnow.tm_hour)
210 * Set to 'sane' values; mktime(3) does funny things otherwise.
211 * No hours, no minutes, no seconds, no service.
213 if (rtm.tm_hour == TMSENTINEL)
215 if (rtm.tm_min == TMSENTINEL)
217 if (rtm.tm_sec == TMSENTINEL)
221 * Given only a year the values of month, day of month, day of year,
222 * week day and is daylight (summer) time are unspecified.
223 * (Specified on the Solaris man page not POSIX.)
225 if (rtm.tm_year != TMSENTINEL &&
226 rtm.tm_mon == TMSENTINEL && rtm.tm_mday == TMSENTINEL) {
230 * XXX More undocumented functionality but observed.
232 * Given the weekday find the first matching weekday
233 * starting with the weekday of the first day of the
234 * month and moving into the future.
236 if (rtm.tm_wday != TMSENTINEL) {
239 memset(&tm, 0, sizeof(struct tm));
240 tm.tm_year = rtm.tm_year;
241 tm.tm_mon = rtm.tm_mon;
244 rtm.tm_mday += (rtm.tm_wday - tm.tm_wday + 7) % 7;
249 * Given only the century but no year within, the current year
250 * is assumed. (Specified on the Solaris man page not POSIX.)
252 * Warning ugly end case
254 * This is more work since strptime(3) doesn't "do the right thing".
256 if (rtm.tm_year != TMSENTINEL && (rtm.tm_year - 1900) >= 0) {
258 rtm.tm_year += (tmnow.tm_year % 100);
262 * mktime() will normalize all values and also check that the
263 * value will fit into a time_t.
265 * This is only for POSIX correctness. A date >= 1900 is
266 * really ok, but using a time_t limits things.
268 if (mktime(rtmp) < 0) {