xref: /freebsd/bin/date/vary.c (revision a8445737e740901f5f2c8d24c12ef7fc8b00134e)
1 /*-
2  * Copyright (c) 1997 Brian Somers <brian@Awfulhak.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #ifndef lint
28 static const char rcsid[] =
29 	"$Id$";
30 #endif /* not lint */
31 
32 #include <time.h>
33 #include <string.h>
34 #include <stdlib.h>
35 #include "vary.h"
36 
37 struct trans {
38   int val;
39   char *str;
40 };
41 
42 static struct trans trans_mon[] = {
43   { 1, "january" }, { 2, "february" }, { 3, "march" }, { 4, "april" },
44   { 6, "june" }, { 7, "july" }, { 8, "august" }, { 9, "september" },
45   { 10, "october" }, { 11, "november" }, { 12, "december" },
46   { -1, NULL }
47 };
48 
49 static struct trans trans_wday[] = {
50   { 0, "sunday" }, { 1, "monday" }, { 2, "tuesday" }, { 3, "wednesday" },
51   { 4, "thursday" }, { 5, "friday" }, { 6, "saturday" },
52   { -1, NULL }
53 };
54 
55 static char digits[] = "0123456789";
56 
57 static int
58 trans(const struct trans t[], const char *arg)
59 {
60   int f;
61 
62   for (f = 0; t[f].val != -1; f++)
63     if (!strncasecmp(t[f].str, arg, 3) ||
64         !strncasecmp(t[f].str, arg, strlen(t[f].str)))
65       return t[f].val;
66 
67   return -1;
68 }
69 
70 struct vary *
71 vary_append(struct vary *v, char *arg)
72 {
73   struct vary *result, **nextp;
74 
75   if (v) {
76     result = v;
77     while (v->next)
78       v = v->next;
79     nextp = &v->next;
80   } else
81     nextp = &result;
82 
83   *nextp = (struct vary *)malloc(sizeof(struct vary));
84   (*nextp)->arg = arg;
85   (*nextp)->next = NULL;
86   return result;
87 }
88 
89 static int mdays[12] = { 31, 0, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
90 
91 static int
92 daysinmonth(const struct tm *t)
93 {
94   int year;
95 
96   year = t->tm_year + 1900;
97 
98   if (t->tm_mon == 1)
99     if (!(year % 400))
100       return 29;
101     else if (!(year % 100))
102       return 28;
103     else if (!(year % 4))
104       return 29;
105     else
106       return 28;
107   else if (t->tm_mon >= 0 && t->tm_mon < 12)
108     return mdays[t->tm_mon];
109 
110   return 0;
111 }
112 
113 
114 static int
115 adjyear(struct tm *t, char type, int val)
116 {
117   switch (type) {
118     case '+':
119       t->tm_year += val;
120       break;
121     case '-':
122       t->tm_year -= val;
123       break;
124     default:
125       t->tm_year = val;
126       if (t->tm_year < 69)
127       	t->tm_year += 100;		/* as per date.c */
128       else if (t->tm_year > 1900)
129         t->tm_year -= 1900;             /* struct tm holds years since 1900 */
130       break;
131   }
132   return mktime(t) != -1;
133 }
134 
135 static int
136 adjmon(struct tm *t, char type, int val, int istext)
137 {
138   if (val < 0)
139     return 0;
140 
141   switch (type) {
142     case '+':
143       if (istext)
144         if (val <= t->tm_mon)
145           val += 11 - t->tm_mon;	/* early next year */
146         else
147           val -= t->tm_mon + 1;		/* later this year */
148       if (!adjyear(t, '+', (t->tm_mon + val) / 12))
149         return 0;
150       val %= 12;
151       t->tm_mon += val;
152       if (t->tm_mon > 11)
153         t->tm_mon -= 12;
154       break;
155 
156     case '-':
157       if (istext)
158         if (val-1 > t->tm_mon)
159           val = 13 - val + t->tm_mon;	/* later last year */
160         else
161           val = t->tm_mon - val + 1;	/* early this year */
162       if (!adjyear(t, '-', val / 12))
163         return 0;
164       val %= 12;
165       if (val > t->tm_mon) {
166         if (!adjyear(t, '-', 1))
167           return 0;
168         val -= 12;
169       }
170       t->tm_mon -= val;
171       break;
172 
173     default:
174       if (val > 12 || val < 1)
175         return 0;
176       t->tm_mon = --val;
177   }
178 
179   return mktime(t) != -1;
180 }
181 
182 static int
183 adjday(struct tm *t, char type, int val)
184 {
185   int mdays;
186   switch (type) {
187     case '+':
188       while (val) {
189         mdays = daysinmonth(t);
190         if (val > mdays - t->tm_mday) {
191           val -= mdays - t->tm_mday + 1;
192           t->tm_mday = 1;
193           if (!adjmon(t, '+', 1, 0))
194             return 0;
195         } else {
196           t->tm_mday += val;
197           val = 0;
198         }
199       }
200       break;
201     case '-':
202       while (val)
203         if (val >= t->tm_mday) {
204           val -= t->tm_mday;
205           t->tm_mday = 1;
206           if (!adjmon(t, '-', 1, 0))
207             return 0;
208           t->tm_mday = daysinmonth(t);
209         } else {
210           t->tm_mday -= val;
211           val = 0;
212         }
213       break;
214     default:
215       if (val > 0 && val <= daysinmonth(t))
216         t->tm_mday = val;
217       else
218         return 0;
219       break;
220   }
221 
222   return mktime(t) != -1;
223 }
224 
225 static int
226 adjwday(struct tm *t, char type, int val, int istext)
227 {
228   if (val < 0)
229     return 0;
230 
231   switch (type) {
232     case '+':
233       if (istext)
234         if (val < t->tm_wday)
235           val = 7 - t->tm_wday + val;  /* early next week */
236         else
237           val -= t->tm_wday;           /* later this week */
238       else
239         val *= 7;                      /* "-W +5" == "5 weeks in the future" */
240       return adjday(t, '+', val);
241     case '-':
242       if (istext)
243         if (val > t->tm_wday)
244           val = 7 - val + t->tm_wday;  /* later last week */
245         else
246           val = t->tm_wday - val;      /* early this week */
247       else
248         val *= 7;                      /* "-W -5" == "5 weeks ago" */
249       return adjday(t, '-', val);
250     default:
251       if (val < t->tm_wday)
252         return adjday(t, '-', t->tm_wday - val);
253       else if (val > 6)
254         return 0;
255       else if (val > t->tm_wday)
256         return adjday(t, '+', val - t->tm_wday);
257   }
258   return 1;
259 }
260 
261 static int
262 adjhour(struct tm *t, char type, int val)
263 {
264   if (val < 0)
265     return 0;
266 
267   switch (type) {
268     case '+':
269       if (!adjday(t, '+', (t->tm_hour + val) / 24))
270         return 0;
271       val %= 24;
272       t->tm_hour += val;
273       if (t->tm_hour > 23)
274         t->tm_hour -= 24;
275       break;
276 
277     case '-':
278       if (!adjday(t, '-', val / 24))
279         return 0;
280       val %= 24;
281       if (val > t->tm_hour) {
282         if (!adjday(t, '-', 1))
283           return 0;
284         val -= 24;
285       }
286       t->tm_hour -= val;
287       break;
288 
289     default:
290       if (val > 23)
291         return 0;
292       t->tm_hour = val;
293   }
294 
295   return mktime(t) != -1;
296 }
297 
298 static int
299 adjmin(struct tm *t, char type, int val)
300 {
301   if (val < 0)
302     return 0;
303 
304   switch (type) {
305     case '+':
306       if (!adjhour(t, '+', (t->tm_min + val) / 60))
307         return 0;
308       val %= 60;
309       t->tm_min += val;
310       if (t->tm_min > 59)
311         t->tm_min -= 60;
312       break;
313 
314     case '-':
315       if (!adjhour(t, '-', val / 60))
316         return 0;
317       val %= 60;
318       if (val > t->tm_min) {
319         if (!adjhour(t, '-', 1))
320           return 0;
321         val -= 60;
322       }
323       t->tm_min -= val;
324       break;
325 
326     default:
327       if (val > 59)
328         return 0;
329       t->tm_min = val;
330   }
331 
332   return mktime(t) != -1;
333 }
334 
335 const struct vary *
336 vary_apply(const struct vary *v, struct tm *t)
337 {
338   char type;
339   char which;
340   char *arg;
341   int len;
342   int val;
343 
344   for (; v; v = v->next) {
345     type = *v->arg;
346     arg = v->arg;
347     if (type == '+' || type == '-')
348       arg++;
349     else
350       type = '\0';
351     len = strlen(arg);
352     if (len < 2)
353       return v;
354 
355     if (strspn(arg, digits) != len-1) {
356       val = trans(trans_wday, arg);
357       if (val != -1) {
358           if (!adjwday(t, type, val, 1))
359             return v;
360       } else {
361         val = trans(trans_mon, arg);
362         if (val != -1) {
363           if (!adjmon(t, type, val, 1))
364             return v;
365         } else
366           return v;
367       }
368     } else {
369       val = atoi(arg);
370       which = arg[len-1];
371 
372       switch (which) {
373         case 'M':
374           if (!adjmin(t, type, val))
375             return v;
376           break;
377         case 'H':
378           if (!adjhour(t, type, val))
379             return v;
380           break;
381         case 'd':
382           if (!adjday(t, type, val))
383             return v;
384           break;
385         case 'w':
386           if (!adjwday(t, type, val, 0))
387             return v;
388           break;
389         case 'm':
390           if (!adjmon(t, type, val, 0))
391             return v;
392           break;
393         case 'y':
394           if (!adjyear(t, type, val))
395             return v;
396           break;
397         default:
398           return v;
399       }
400     }
401   }
402   return 0;
403 }
404 
405 void
406 vary_destroy(struct vary *v)
407 {
408   struct vary *n;
409 
410   while (v) {
411     n = v->next;
412     free(v);
413     v = n;
414   }
415 }
416