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