xref: /freebsd/bin/date/vary.c (revision 6990ffd8a95caaba6858ad44ff1b3157d1efba8f)
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   "$FreeBSD$";
30 #endif /* not lint */
31 
32 #include <err.h>
33 #include <time.h>
34 #include <string.h>
35 #include <stdlib.h>
36 #include "vary.h"
37 
38 struct trans {
39   int val;
40   char *str;
41 };
42 
43 static struct trans trans_mon[] = {
44   { 1, "january" }, { 2, "february" }, { 3, "march" }, { 4, "april" },
45   { 5, "may"}, { 6, "june" }, { 7, "july" }, { 8, "august" },
46   { 9, "september" }, { 10, "october" }, { 11, "november" }, { 12, "december" },
47   { -1, NULL }
48 };
49 
50 static struct trans trans_wday[] = {
51   { 0, "sunday" }, { 1, "monday" }, { 2, "tuesday" }, { 3, "wednesday" },
52   { 4, "thursday" }, { 5, "friday" }, { 6, "saturday" },
53   { -1, NULL }
54 };
55 
56 static char digits[] = "0123456789";
57 static int adjhour(struct tm *, char, int, int);
58 
59 static int
60 domktime(struct tm *t, char type)
61 {
62   time_t ret;
63 
64   while ((ret = mktime(t)) == -1 && t->tm_year > 68 && t->tm_year < 138)
65     /* While mktime() fails, adjust by an hour */
66     adjhour(t, type == '-' ? type : '+', 1, 0);
67 
68   return ret;
69 }
70 
71 static int
72 trans(const struct trans t[], const char *arg)
73 {
74   int f;
75 
76   for (f = 0; t[f].val != -1; f++)
77     if (!strncasecmp(t[f].str, arg, 3) ||
78         !strncasecmp(t[f].str, arg, strlen(t[f].str)))
79       return t[f].val;
80 
81   return -1;
82 }
83 
84 struct vary *
85 vary_append(struct vary *v, char *arg)
86 {
87   struct vary *result, **nextp;
88 
89   if (v) {
90     result = v;
91     while (v->next)
92       v = v->next;
93     nextp = &v->next;
94   } else
95     nextp = &result;
96 
97   if ((*nextp = (struct vary *)malloc(sizeof(struct vary))) == NULL)
98     err(1, "malloc");
99   (*nextp)->arg = arg;
100   (*nextp)->next = NULL;
101   return result;
102 }
103 
104 static int mdays[12] = { 31, 0, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
105 
106 static int
107 daysinmonth(const struct tm *t)
108 {
109   int year;
110 
111   year = t->tm_year + 1900;
112 
113   if (t->tm_mon == 1)
114     if (!(year % 400))
115       return 29;
116     else if (!(year % 100))
117       return 28;
118     else if (!(year % 4))
119       return 29;
120     else
121       return 28;
122   else if (t->tm_mon >= 0 && t->tm_mon < 12)
123     return mdays[t->tm_mon];
124 
125   return 0;
126 }
127 
128 
129 static int
130 adjyear(struct tm *t, char type, int val, int mk)
131 {
132   switch (type) {
133     case '+':
134       t->tm_year += val;
135       break;
136     case '-':
137       t->tm_year -= val;
138       break;
139     default:
140       t->tm_year = val;
141       if (t->tm_year < 69)
142       	t->tm_year += 100;		/* as per date.c */
143       else if (t->tm_year > 1900)
144         t->tm_year -= 1900;             /* struct tm holds years since 1900 */
145       break;
146   }
147   return !mk || domktime(t, type) != -1;
148 }
149 
150 static int
151 adjmon(struct tm *t, char type, int val, int istext, int mk)
152 {
153   if (val < 0)
154     return 0;
155 
156   switch (type) {
157     case '+':
158       if (istext) {
159         if (val <= t->tm_mon)
160           val += 11 - t->tm_mon;	/* early next year */
161         else
162           val -= t->tm_mon + 1;		/* later this year */
163       }
164       if (val) {
165         if (!adjyear(t, '+', (t->tm_mon + val) / 12, 0))
166           return 0;
167         val %= 12;
168         t->tm_mon += val;
169         if (t->tm_mon > 11)
170           t->tm_mon -= 12;
171       }
172       break;
173 
174     case '-':
175       if (istext) {
176         if (val-1 > t->tm_mon)
177           val = 13 - val + t->tm_mon;	/* later last year */
178         else
179           val = t->tm_mon - val + 1;	/* early this year */
180       }
181       if (val) {
182         if (!adjyear(t, '-', val / 12, 0))
183           return 0;
184         val %= 12;
185         if (val > t->tm_mon) {
186           if (!adjyear(t, '-', 1, 0))
187             return 0;
188           val -= 12;
189         }
190         t->tm_mon -= val;
191       }
192       break;
193 
194     default:
195       if (val > 12 || val < 1)
196         return 0;
197       t->tm_mon = --val;
198   }
199 
200   return !mk || domktime(t, type) != -1;
201 }
202 
203 static int
204 adjday(struct tm *t, char type, int val, int mk)
205 {
206   int mdays;
207 
208   switch (type) {
209     case '+':
210       while (val) {
211         mdays = daysinmonth(t);
212         if (val > mdays - t->tm_mday) {
213           val -= mdays - t->tm_mday + 1;
214           t->tm_mday = 1;
215           if (!adjmon(t, '+', 1, 0, 0))
216             return 0;
217         } else {
218           t->tm_mday += val;
219           val = 0;
220         }
221       }
222       break;
223     case '-':
224       while (val)
225         if (val >= t->tm_mday) {
226           val -= t->tm_mday;
227           t->tm_mday = 1;
228           if (!adjmon(t, '-', 1, 0, 0))
229             return 0;
230           t->tm_mday = daysinmonth(t);
231         } else {
232           t->tm_mday -= val;
233           val = 0;
234         }
235       break;
236     default:
237       if (val > 0 && val <= daysinmonth(t))
238         t->tm_mday = val;
239       else
240         return 0;
241       break;
242   }
243 
244   return !mk || domktime(t, type) != -1;
245 }
246 
247 static int
248 adjwday(struct tm *t, char type, int val, int istext, int mk)
249 {
250   if (val < 0)
251     return 0;
252 
253   switch (type) {
254     case '+':
255       if (istext)
256         if (val < t->tm_wday)
257           val = 7 - t->tm_wday + val;  /* early next week */
258         else
259           val -= t->tm_wday;           /* later this week */
260       else
261         val *= 7;                      /* "-v+5w" == "5 weeks in the future" */
262       return !val || adjday(t, '+', val, mk);
263     case '-':
264       if (istext) {
265         if (val > t->tm_wday)
266           val = 7 - val + t->tm_wday;  /* later last week */
267         else
268           val = t->tm_wday - val;      /* early this week */
269       } else
270         val *= 7;                      /* "-v-5w" == "5 weeks ago" */
271       return !val || adjday(t, '-', val, mk);
272     default:
273       if (val < t->tm_wday)
274         return adjday(t, '-', t->tm_wday - val, mk);
275       else if (val > 6)
276         return 0;
277       else if (val > t->tm_wday)
278         return adjday(t, '+', val - t->tm_wday, mk);
279   }
280   return 1;
281 }
282 
283 static int
284 adjhour(struct tm *t, char type, int val, int mk)
285 {
286   if (val < 0)
287     return 0;
288 
289   switch (type) {
290     case '+':
291       if (val) {
292         int days;
293 
294         days = (t->tm_hour + val) / 24;
295         val %= 24;
296         t->tm_hour += val;
297         t->tm_hour %= 24;
298         if (!adjday(t, '+', days, 0))
299           return 0;
300       }
301       break;
302 
303     case '-':
304       if (val) {
305         int days;
306 
307         days = val / 24;
308         val %= 24;
309         if (val > t->tm_hour) {
310           days++;
311           val -= 24;
312         }
313         t->tm_hour -= val;
314         if (!adjday(t, '-', days, 0))
315           return 0;
316       }
317       break;
318 
319     default:
320       if (val > 23)
321         return 0;
322       t->tm_hour = val;
323   }
324 
325   return !mk || domktime(t, type) != -1;
326 }
327 
328 static int
329 adjmin(struct tm *t, char type, int val, int mk)
330 {
331   if (val < 0)
332     return 0;
333 
334   switch (type) {
335     case '+':
336       if (val) {
337         if (!adjhour(t, '+', (t->tm_min + val) / 60, 0))
338           return 0;
339         val %= 60;
340         t->tm_min += val;
341         if (t->tm_min > 59)
342           t->tm_min -= 60;
343       }
344       break;
345 
346     case '-':
347       if (val) {
348         if (!adjhour(t, '-', val / 60, 0))
349           return 0;
350         val %= 60;
351         if (val > t->tm_min) {
352           if (!adjhour(t, '-', 1, 0))
353             return 0;
354           val -= 60;
355         }
356         t->tm_min -= val;
357       }
358       break;
359 
360     default:
361       if (val > 59)
362         return 0;
363       t->tm_min = val;
364   }
365 
366   return !mk || domktime(t, type) != -1;
367 }
368 
369 static int
370 adjsec(struct tm *t, char type, int val, int mk)
371 {
372   if (val < 0)
373     return 0;
374 
375   switch (type) {
376     case '+':
377       if (val) {
378         if (!adjmin(t, '+', (t->tm_sec + val) / 60, 0))
379           return 0;
380         val %= 60;
381         t->tm_sec += val;
382         if (t->tm_sec > 59)
383           t->tm_sec -= 60;
384       }
385       break;
386 
387     case '-':
388       if (val) {
389         if (!adjmin(t, '-', val / 60, 0))
390           return 0;
391         val %= 60;
392         if (val > t->tm_sec) {
393           if (!adjmin(t, '-', 1, 0))
394             return 0;
395           val -= 60;
396         }
397         t->tm_sec -= val;
398       }
399       break;
400 
401     default:
402       if (val > 59)
403         return 0;
404       t->tm_sec = val;
405   }
406 
407   return !mk || domktime(t, type) != -1;
408 }
409 
410 const struct vary *
411 vary_apply(const struct vary *v, struct tm *t)
412 {
413   char type;
414   char which;
415   char *arg;
416   int len;
417   int val;
418 
419   for (; v; v = v->next) {
420     type = *v->arg;
421     arg = v->arg;
422     if (type == '+' || type == '-')
423       arg++;
424     else
425       type = '\0';
426     len = strlen(arg);
427     if (len < 2)
428       return v;
429 
430     if (type == '\0')
431       t->tm_isdst = -1;
432 
433     if (strspn(arg, digits) != len-1) {
434       val = trans(trans_wday, arg);
435       if (val != -1) {
436           if (!adjwday(t, type, val, 1, 1))
437             return v;
438       } else {
439         val = trans(trans_mon, arg);
440         if (val != -1) {
441           if (!adjmon(t, type, val, 1, 1))
442             return v;
443         } else
444           return v;
445       }
446     } else {
447       val = atoi(arg);
448       which = arg[len-1];
449 
450       switch (which) {
451         case 'S':
452           if (!adjsec(t, type, val, 1))
453             return v;
454           break;
455         case 'M':
456           if (!adjmin(t, type, val, 1))
457             return v;
458           break;
459         case 'H':
460           if (!adjhour(t, type, val, 1))
461             return v;
462           break;
463         case 'd':
464           t->tm_isdst = -1;
465           if (!adjday(t, type, val, 1))
466             return v;
467           break;
468         case 'w':
469           t->tm_isdst = -1;
470           if (!adjwday(t, type, val, 0, 1))
471             return v;
472           break;
473         case 'm':
474           t->tm_isdst = -1;
475           if (!adjmon(t, type, val, 0, 1))
476             return v;
477           break;
478         case 'y':
479           t->tm_isdst = -1;
480           if (!adjyear(t, type, val, 1))
481             return v;
482           break;
483         default:
484           return v;
485       }
486     }
487   }
488   return 0;
489 }
490 
491 void
492 vary_destroy(struct vary *v)
493 {
494   struct vary *n;
495 
496   while (v) {
497     n = v->next;
498     free(v);
499     v = n;
500   }
501 }
502