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