]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/basic/calendarspec.c
calendarspec: fix duplicate detection (#5310)
[thirdparty/systemd.git] / src / basic / calendarspec.c
CommitLineData
36697dc0
LP
1/***
2 This file is part of systemd.
3
4 Copyright 2012 Lennart Poettering
5
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
10
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
18***/
19
11c3a366 20#include <alloca.h>
9dfa81a0 21#include <ctype.h>
11c3a366 22#include <errno.h>
c0aebb4b 23#include <limits.h>
11c3a366
TA
24#include <stddef.h>
25#include <stdio.h>
36697dc0
LP
26#include <stdlib.h>
27#include <string.h>
11c3a366 28#include <time.h>
36697dc0 29
b5efdb8a 30#include "alloc-util.h"
36697dc0 31#include "calendarspec.h"
0d39fa9c 32#include "fileio.h"
11c3a366 33#include "macro.h"
93cc7779 34#include "parse-util.h"
cf0fbc49 35#include "string-util.h"
36697dc0 36
a2eb5ea7
DC
37#define BITS_WEEKDAYS 127
38#define MIN_YEAR 1970
39#define MAX_YEAR 2199
489464d0 40
36697dc0
LP
41static void free_chain(CalendarComponent *c) {
42 CalendarComponent *n;
43
44 while (c) {
45 n = c->next;
46 free(c);
47 c = n;
48 }
49}
50
51void calendar_spec_free(CalendarSpec *c) {
0b76b4d8
LP
52
53 if (!c)
54 return;
36697dc0
LP
55
56 free_chain(c->year);
57 free_chain(c->month);
58 free_chain(c->day);
59 free_chain(c->hour);
60 free_chain(c->minute);
436dd70f 61 free_chain(c->microsecond);
36697dc0
LP
62
63 free(c);
64}
65
66static int component_compare(const void *_a, const void *_b) {
67 CalendarComponent * const *a = _a, * const *b = _b;
68
e78fce48 69 if ((*a)->start < (*b)->start)
36697dc0 70 return -1;
e78fce48 71 if ((*a)->start > (*b)->start)
36697dc0
LP
72 return 1;
73
e78fce48 74 if ((*a)->stop < (*b)->stop)
a2eb5ea7 75 return -1;
e78fce48 76 if ((*a)->stop > (*b)->stop)
a2eb5ea7
DC
77 return 1;
78
36697dc0
LP
79 if ((*a)->repeat < (*b)->repeat)
80 return -1;
81 if ((*a)->repeat > (*b)->repeat)
82 return 1;
83
84 return 0;
85}
86
a2eb5ea7 87static void normalize_chain(CalendarComponent **c) {
36697dc0
LP
88 unsigned n = 0, k;
89 CalendarComponent **b, *i, **j, *next;
90
91 assert(c);
92
a2eb5ea7 93 for (i = *c; i; i = i->next) {
36697dc0
LP
94 n++;
95
a2eb5ea7 96 /*
e78fce48 97 * While we're counting the chain, also normalize `stop`
a2eb5ea7
DC
98 * so the length of the range is a multiple of `repeat`
99 */
c0aebb4b 100 if (i->stop > i->start && i->repeat > 0)
e78fce48 101 i->stop -= (i->stop - i->start) % i->repeat;
a2eb5ea7
DC
102
103 }
104
36697dc0
LP
105 if (n <= 1)
106 return;
107
108 j = b = alloca(sizeof(CalendarComponent*) * n);
109 for (i = *c; i; i = i->next)
110 *(j++) = i;
111
112 qsort(b, n, sizeof(CalendarComponent*), component_compare);
113
114 b[n-1]->next = NULL;
115 next = b[n-1];
116
117 /* Drop non-unique entries */
118 for (k = n-1; k > 0; k--) {
963e3d83 119 if (component_compare(&b[k-1], &next) == 0) {
36697dc0
LP
120 free(b[k-1]);
121 continue;
122 }
123
124 b[k-1]->next = next;
125 next = b[k-1];
126 }
127
128 *c = next;
129}
130
131static void fix_year(CalendarComponent *c) {
132 /* Turns 12 → 2012, 89 → 1989 */
133
9ed794a3 134 while (c) {
e78fce48
DC
135 if (c->start >= 0 && c->start < 70)
136 c->start += 2000;
36697dc0 137
e78fce48
DC
138 if (c->stop >= 0 && c->stop < 70)
139 c->stop += 2000;
a2eb5ea7 140
e78fce48
DC
141 if (c->start >= 70 && c->start < 100)
142 c->start += 1900;
36697dc0 143
e78fce48
DC
144 if (c->stop >= 70 && c->stop < 100)
145 c->stop += 1900;
a2eb5ea7 146
482f3b54 147 c = c->next;
36697dc0
LP
148 }
149}
150
151int calendar_spec_normalize(CalendarSpec *c) {
152 assert(c);
153
489464d0 154 if (c->weekdays_bits <= 0 || c->weekdays_bits >= BITS_WEEKDAYS)
36697dc0
LP
155 c->weekdays_bits = -1;
156
8ea80351
DC
157 if (c->end_of_month && !c->day)
158 c->end_of_month = false;
159
36697dc0
LP
160 fix_year(c->year);
161
a2eb5ea7
DC
162 normalize_chain(&c->year);
163 normalize_chain(&c->month);
164 normalize_chain(&c->day);
165 normalize_chain(&c->hour);
166 normalize_chain(&c->minute);
167 normalize_chain(&c->microsecond);
36697dc0
LP
168
169 return 0;
170}
171
c58b1b3a 172_pure_ static bool chain_valid(CalendarComponent *c, int from, int to, bool end_of_month) {
36697dc0
LP
173 if (!c)
174 return true;
175
a2eb5ea7
DC
176 /* Forbid dates more than 28 days from the end of the month */
177 if (end_of_month)
178 to -= 3;
179
e78fce48 180 if (c->start < from || c->start > to)
36697dc0
LP
181 return false;
182
8ea80351
DC
183 /*
184 * c->repeat must be short enough so at least one repetition may
185 * occur before the end of the interval. For dates scheduled
e78fce48 186 * relative to the end of the month, c->start and c->stop
a2eb5ea7 187 * correspond to the Nth last day of the month.
8ea80351 188 */
e78fce48
DC
189 if (c->stop >= 0) {
190 if (c->stop < from || c ->stop > to)
a2eb5ea7 191 return false;
8ea80351 192
e78fce48 193 if (c->start + c->repeat > c->stop)
a2eb5ea7
DC
194 return false;
195 } else {
e78fce48 196 if (end_of_month && c->start - c->repeat < from)
a2eb5ea7
DC
197 return false;
198
e78fce48 199 if (!end_of_month && c->start + c->repeat > to)
a2eb5ea7
DC
200 return false;
201 }
36697dc0
LP
202
203 if (c->next)
c58b1b3a 204 return chain_valid(c->next, from, to, end_of_month);
36697dc0
LP
205
206 return true;
207}
208
44a6b1b6 209_pure_ bool calendar_spec_valid(CalendarSpec *c) {
36697dc0
LP
210 assert(c);
211
489464d0 212 if (c->weekdays_bits > BITS_WEEKDAYS)
36697dc0
LP
213 return false;
214
8ea80351 215 if (!chain_valid(c->year, MIN_YEAR, MAX_YEAR, false))
36697dc0
LP
216 return false;
217
8ea80351 218 if (!chain_valid(c->month, 1, 12, false))
36697dc0
LP
219 return false;
220
8ea80351 221 if (!chain_valid(c->day, 1, 31, c->end_of_month))
36697dc0
LP
222 return false;
223
8ea80351 224 if (!chain_valid(c->hour, 0, 23, false))
36697dc0
LP
225 return false;
226
8ea80351 227 if (!chain_valid(c->minute, 0, 59, false))
36697dc0
LP
228 return false;
229
8ea80351 230 if (!chain_valid(c->microsecond, 0, 60*USEC_PER_SEC-1, false))
36697dc0
LP
231 return false;
232
233 return true;
234}
235
236static void format_weekdays(FILE *f, const CalendarSpec *c) {
237 static const char *const days[] = {
238 "Mon",
239 "Tue",
240 "Wed",
241 "Thu",
242 "Fri",
243 "Sat",
244 "Sun"
245 };
246
247 int l, x;
e638d050 248 bool need_comma = false;
36697dc0
LP
249
250 assert(f);
251 assert(c);
489464d0 252 assert(c->weekdays_bits > 0 && c->weekdays_bits <= BITS_WEEKDAYS);
36697dc0
LP
253
254 for (x = 0, l = -1; x < (int) ELEMENTSOF(days); x++) {
255
256 if (c->weekdays_bits & (1 << x)) {
257
258 if (l < 0) {
e638d050 259 if (need_comma)
36697dc0
LP
260 fputc(',', f);
261 else
e638d050 262 need_comma = true;
36697dc0
LP
263
264 fputs(days[x], f);
265 l = x;
266 }
267
268 } else if (l >= 0) {
269
270 if (x > l + 1) {
e638d050 271 fputs(x > l + 2 ? ".." : ",", f);
36697dc0
LP
272 fputs(days[x-1], f);
273 }
274
275 l = -1;
276 }
277 }
278
279 if (l >= 0 && x > l + 1) {
e638d050 280 fputs(x > l + 2 ? ".." : ",", f);
36697dc0
LP
281 fputs(days[x-1], f);
282 }
283}
284
436dd70f 285static void format_chain(FILE *f, int space, const CalendarComponent *c, bool usec) {
7c250321 286 int d = usec ? (int) USEC_PER_SEC : 1;
9904dc00 287
36697dc0
LP
288 assert(f);
289
290 if (!c) {
291 fputc('*', f);
292 return;
293 }
294
482f3b54
DC
295 if (usec && c->start == 0 && c->repeat == USEC_PER_SEC && !c->next) {
296 fputc('*', f);
297 return;
298 }
299
e78fce48 300 assert(c->start >= 0);
7c250321 301
e78fce48
DC
302 fprintf(f, "%0*i", space, c->start / d);
303 if (c->start % d > 0)
304 fprintf(f, ".%06i", c->start % d);
7c250321 305
e78fce48
DC
306 if (c->stop > 0)
307 fprintf(f, "..%0*i", space, c->stop / d);
308 if (c->stop % d > 0)
309 fprintf(f, ".%06i", c->stop % d);
a2eb5ea7 310
e78fce48 311 if (c->repeat > 0 && !(c->stop > 0 && c->repeat == d))
7c250321 312 fprintf(f, "/%i", c->repeat / d);
a2eb5ea7 313 if (c->repeat % d > 0)
7c250321 314 fprintf(f, ".%06i", c->repeat % d);
36697dc0 315
a2eb5ea7 316 if (c->next) {
36697dc0 317 fputc(',', f);
436dd70f 318 format_chain(f, space, c->next, usec);
36697dc0
LP
319 }
320}
321
322int calendar_spec_to_string(const CalendarSpec *c, char **p) {
323 char *buf = NULL;
324 size_t sz = 0;
325 FILE *f;
dacd6cee 326 int r;
36697dc0
LP
327
328 assert(c);
329 assert(p);
330
331 f = open_memstream(&buf, &sz);
332 if (!f)
333 return -ENOMEM;
334
489464d0 335 if (c->weekdays_bits > 0 && c->weekdays_bits <= BITS_WEEKDAYS) {
36697dc0
LP
336 format_weekdays(f, c);
337 fputc(' ', f);
338 }
339
436dd70f 340 format_chain(f, 4, c->year, false);
36697dc0 341 fputc('-', f);
436dd70f 342 format_chain(f, 2, c->month, false);
8ea80351 343 fputc(c->end_of_month ? '~' : '-', f);
436dd70f 344 format_chain(f, 2, c->day, false);
36697dc0 345 fputc(' ', f);
436dd70f 346 format_chain(f, 2, c->hour, false);
36697dc0 347 fputc(':', f);
436dd70f 348 format_chain(f, 2, c->minute, false);
36697dc0 349 fputc(':', f);
482f3b54 350 format_chain(f, 2, c->microsecond, true);
36697dc0 351
51ffa239
HV
352 if (c->utc)
353 fputs(" UTC", f);
21b3a0fc
LP
354 else if (IN_SET(c->dst, 0, 1)) {
355
356 /* If daylight saving is explicitly on or off, let's show the used timezone. */
357
358 tzset();
359
360 if (!isempty(tzname[c->dst])) {
361 fputc(' ', f);
362 fputs(tzname[c->dst], f);
363 }
364 }
51ffa239 365
dacd6cee
LP
366 r = fflush_and_check(f);
367 if (r < 0) {
36697dc0
LP
368 free(buf);
369 fclose(f);
dacd6cee 370 return r;
36697dc0
LP
371 }
372
373 fclose(f);
374
375 *p = buf;
376 return 0;
377}
378
379static int parse_weekdays(const char **p, CalendarSpec *c) {
380 static const struct {
381 const char *name;
382 const int nr;
383 } day_nr[] = {
384 { "Monday", 0 },
385 { "Mon", 0 },
386 { "Tuesday", 1 },
387 { "Tue", 1 },
388 { "Wednesday", 2 },
389 { "Wed", 2 },
390 { "Thursday", 3 },
391 { "Thu", 3 },
392 { "Friday", 4 },
393 { "Fri", 4 },
394 { "Saturday", 5 },
395 { "Sat", 5 },
396 { "Sunday", 6 },
397 { "Sun", 6 }
398 };
399
400 int l = -1;
401 bool first = true;
402
403 assert(p);
404 assert(*p);
405 assert(c);
406
407 for (;;) {
408 unsigned i;
409
36697dc0
LP
410 for (i = 0; i < ELEMENTSOF(day_nr); i++) {
411 size_t skip;
412
413 if (!startswith_no_case(*p, day_nr[i].name))
414 continue;
415
416 skip = strlen(day_nr[i].name);
417
418 if ((*p)[skip] != '-' &&
e638d050 419 (*p)[skip] != '.' &&
36697dc0
LP
420 (*p)[skip] != ',' &&
421 (*p)[skip] != ' ' &&
422 (*p)[skip] != 0)
423 return -EINVAL;
424
425 c->weekdays_bits |= 1 << day_nr[i].nr;
426
427 if (l >= 0) {
428 int j;
429
430 if (l > day_nr[i].nr)
431 return -EINVAL;
432
433 for (j = l + 1; j < day_nr[i].nr; j++)
434 c->weekdays_bits |= 1 << j;
435 }
436
437 *p += skip;
438 break;
439 }
440
441 /* Couldn't find this prefix, so let's assume the
442 weekday was not specified and let's continue with
443 the date */
444 if (i >= ELEMENTSOF(day_nr))
445 return first ? 0 : -EINVAL;
446
447 /* We reached the end of the string */
448 if (**p == 0)
449 return 0;
450
451 /* We reached the end of the weekday spec part */
452 if (**p == ' ') {
453 *p += strspn(*p, " ");
454 return 0;
455 }
456
e638d050
DC
457 if (**p == '.') {
458 if (l >= 0)
459 return -EINVAL;
460
461 if ((*p)[1] != '.')
462 return -EINVAL;
463
464 l = day_nr[i].nr;
6bae2fd4 465 *p += 2;
e638d050
DC
466
467 /* Support ranges with "-" for backwards compatibility */
468 } else if (**p == '-') {
36697dc0
LP
469 if (l >= 0)
470 return -EINVAL;
471
472 l = day_nr[i].nr;
6bae2fd4
DC
473 *p += 1;
474
475 } else if (**p == ',') {
36697dc0 476 l = -1;
6bae2fd4
DC
477 *p += 1;
478 }
479
482f3b54 480 /* Allow a trailing comma but not an open range */
6bae2fd4
DC
481 if (**p == 0 || **p == ' ') {
482 *p += strspn(*p, " ");
483 return l < 0 ? 0 : -EINVAL;
484 }
36697dc0 485
36697dc0
LP
486 first = false;
487 }
488}
489
c0aebb4b 490static int parse_component_decimal(const char **p, bool usec, int *res) {
436dd70f
HV
491 unsigned long value;
492 const char *e = NULL;
493 char *ee = NULL;
494 int r;
36697dc0 495
9dfa81a0
DC
496 if (!isdigit(**p))
497 return -EINVAL;
498
36697dc0 499 errno = 0;
436dd70f 500 value = strtoul(*p, &ee, 10);
8333c77e 501 if (errno > 0)
36697dc0 502 return -errno;
436dd70f 503 if (ee == *p)
36697dc0 504 return -EINVAL;
436dd70f 505 e = ee;
36697dc0 506
436dd70f
HV
507 if (usec) {
508 if (value * USEC_PER_SEC / USEC_PER_SEC != value)
36697dc0
LP
509 return -ERANGE;
510
436dd70f 511 value *= USEC_PER_SEC;
436dd70f 512
60bf5836
DC
513 /* One "." is a decimal point, but ".." is a range separator */
514 if (e[0] == '.' && e[1] != '.') {
515 unsigned add;
36ff0c97 516
436dd70f
HV
517 e++;
518 r = parse_fractional_part_u(&e, 6, &add);
519 if (r < 0)
520 return r;
521
522 if (add + value < value)
523 return -ERANGE;
524 value += add;
525 }
526 }
527
c0aebb4b
DC
528 if (value > INT_MAX)
529 return -ERANGE;
530
436dd70f
HV
531 *p = e;
532 *res = value;
533
534 return 0;
535}
536
32b52369
DC
537static int const_chain(int value, CalendarComponent **c) {
538 CalendarComponent *cc = NULL;
539
540 assert(c);
541
542 cc = new0(CalendarComponent, 1);
543 if (!cc)
544 return -ENOMEM;
545
e78fce48
DC
546 cc->start = value;
547 cc->stop = -1;
32b52369
DC
548 cc->repeat = 0;
549 cc->next = *c;
550
551 *c = cc;
552
553 return 0;
554}
555
436dd70f 556static int prepend_component(const char **p, bool usec, CalendarComponent **c) {
c0aebb4b 557 int r, start, stop = -1, repeat = 0;
436dd70f 558 CalendarComponent *cc;
436dd70f
HV
559 const char *e;
560
561 assert(p);
562 assert(c);
563
564 e = *p;
565
e78fce48 566 r = parse_component_decimal(&e, usec, &start);
436dd70f
HV
567 if (r < 0)
568 return r;
569
a2eb5ea7
DC
570 if (e[0] == '.' && e[1] == '.') {
571 e += 2;
e78fce48 572 r = parse_component_decimal(&e, usec, &stop);
a2eb5ea7
DC
573 if (r < 0)
574 return r;
575
576 repeat = usec ? USEC_PER_SEC : 1;
577 }
578
436dd70f
HV
579 if (*e == '/') {
580 e++;
581 r = parse_component_decimal(&e, usec, &repeat);
582 if (r < 0)
583 return r;
584
585 if (repeat == 0)
586 return -ERANGE;
36697dc0
LP
587 }
588
8ea80351 589 if (*e != 0 && *e != ' ' && *e != ',' && *e != '-' && *e != '~' && *e != ':')
36697dc0
LP
590 return -EINVAL;
591
592 cc = new0(CalendarComponent, 1);
593 if (!cc)
594 return -ENOMEM;
595
e78fce48
DC
596 cc->start = start;
597 cc->stop = stop;
36697dc0
LP
598 cc->repeat = repeat;
599 cc->next = *c;
600
601 *p = e;
602 *c = cc;
603
604 if (*e ==',') {
605 *p += 1;
436dd70f 606 return prepend_component(p, usec, c);
36697dc0
LP
607 }
608
609 return 0;
610}
611
436dd70f 612static int parse_chain(const char **p, bool usec, CalendarComponent **c) {
36697dc0
LP
613 const char *t;
614 CalendarComponent *cc = NULL;
615 int r;
616
617 assert(p);
618 assert(c);
619
620 t = *p;
621
622 if (t[0] == '*') {
436dd70f
HV
623 if (usec) {
624 r = const_chain(0, c);
625 if (r < 0)
626 return r;
627 (*c)->repeat = USEC_PER_SEC;
628 } else
629 *c = NULL;
630
36697dc0 631 *p = t + 1;
36697dc0
LP
632 return 0;
633 }
634
436dd70f 635 r = prepend_component(&t, usec, &cc);
36697dc0
LP
636 if (r < 0) {
637 free_chain(cc);
638 return r;
639 }
640
641 *p = t;
642 *c = cc;
643 return 0;
644}
645
36697dc0
LP
646static int parse_date(const char **p, CalendarSpec *c) {
647 const char *t;
648 int r;
649 CalendarComponent *first, *second, *third;
650
651 assert(p);
652 assert(*p);
653 assert(c);
654
655 t = *p;
656
657 if (*t == 0)
658 return 0;
659
436dd70f 660 r = parse_chain(&t, false, &first);
36697dc0
LP
661 if (r < 0)
662 return r;
663
664 /* Already the end? A ':' as separator? In that case this was a time, not a date */
665 if (*t == 0 || *t == ':') {
666 free_chain(first);
667 return 0;
668 }
669
8ea80351
DC
670 if (*t == '~')
671 c->end_of_month = true;
672 else if (*t != '-') {
36697dc0
LP
673 free_chain(first);
674 return -EINVAL;
675 }
676
677 t++;
436dd70f 678 r = parse_chain(&t, false, &second);
36697dc0
LP
679 if (r < 0) {
680 free_chain(first);
681 return r;
682 }
683
684 /* Got two parts, hence it's month and day */
685 if (*t == ' ' || *t == 0) {
686 *p = t + strspn(t, " ");
687 c->month = first;
688 c->day = second;
689 return 0;
fc2371c7
DC
690 } else if (c->end_of_month) {
691 free_chain(first);
692 free_chain(second);
8ea80351 693 return -EINVAL;
fc2371c7 694 }
36697dc0 695
8ea80351
DC
696 if (*t == '~')
697 c->end_of_month = true;
698 else if (*t != '-') {
36697dc0
LP
699 free_chain(first);
700 free_chain(second);
701 return -EINVAL;
702 }
703
704 t++;
436dd70f 705 r = parse_chain(&t, false, &third);
36697dc0
LP
706 if (r < 0) {
707 free_chain(first);
708 free_chain(second);
709 return r;
710 }
711
8ea80351 712 /* Got three parts, hence it is year, month and day */
36697dc0
LP
713 if (*t == ' ' || *t == 0) {
714 *p = t + strspn(t, " ");
715 c->year = first;
716 c->month = second;
717 c->day = third;
718 return 0;
719 }
720
721 free_chain(first);
722 free_chain(second);
723 free_chain(third);
724 return -EINVAL;
725}
726
519cffec 727static int parse_calendar_time(const char **p, CalendarSpec *c) {
36697dc0
LP
728 CalendarComponent *h = NULL, *m = NULL, *s = NULL;
729 const char *t;
730 int r;
731
732 assert(p);
733 assert(*p);
734 assert(c);
735
736 t = *p;
737
408a51e1
DC
738 /* If no time is specified at all, then this means 00:00:00 */
739 if (*t == 0)
740 goto null_hour;
36697dc0 741
436dd70f 742 r = parse_chain(&t, false, &h);
36697dc0
LP
743 if (r < 0)
744 goto fail;
745
746 if (*t != ':') {
747 r = -EINVAL;
748 goto fail;
749 }
750
751 t++;
436dd70f 752 r = parse_chain(&t, false, &m);
36697dc0
LP
753 if (r < 0)
754 goto fail;
755
756 /* Already at the end? Then it's hours and minutes, and seconds are 0 */
c0df71fa
DC
757 if (*t == 0)
758 goto null_second;
36697dc0
LP
759
760 if (*t != ':') {
761 r = -EINVAL;
762 goto fail;
763 }
764
765 t++;
436dd70f 766 r = parse_chain(&t, true, &s);
36697dc0
LP
767 if (r < 0)
768 goto fail;
769
770 /* At the end? Then it's hours, minutes and seconds */
771 if (*t == 0)
772 goto finish;
773
774 r = -EINVAL;
775 goto fail;
776
777null_hour:
778 r = const_chain(0, &h);
779 if (r < 0)
780 goto fail;
781
782 r = const_chain(0, &m);
783 if (r < 0)
784 goto fail;
785
786null_second:
787 r = const_chain(0, &s);
788 if (r < 0)
789 goto fail;
790
791finish:
792 *p = t;
793 c->hour = h;
794 c->minute = m;
436dd70f
HV
795 c->microsecond = s;
796
36697dc0
LP
797 return 0;
798
799fail:
800 free_chain(h);
801 free_chain(m);
802 free_chain(s);
803 return r;
804}
805
806int calendar_spec_from_string(const char *p, CalendarSpec **spec) {
21b3a0fc 807 const char *utc;
36697dc0
LP
808 CalendarSpec *c;
809 int r;
810
811 assert(p);
812 assert(spec);
813
36697dc0
LP
814 c = new0(CalendarSpec, 1);
815 if (!c)
816 return -ENOMEM;
21b3a0fc 817 c->dst = -1;
36697dc0 818
078efddd
HV
819 utc = endswith_no_case(p, " UTC");
820 if (utc) {
821 c->utc = true;
822 p = strndupa(p, utc - p);
21b3a0fc
LP
823 } else {
824 const char *e = NULL;
825 int j;
826
827 tzset();
828
829 /* Check if the local timezone was specified? */
830 for (j = 0; j <= 1; j++) {
831 if (isempty(tzname[j]))
832 continue;
833
834 e = endswith_no_case(p, tzname[j]);
835 if(!e)
836 continue;
837 if (e == p)
838 continue;
839 if (e[-1] != ' ')
840 continue;
841
842 break;
843 }
844
845 /* Found one of the two timezones specified? */
846 if (IN_SET(j, 0, 1)) {
847 p = strndupa(p, e - p - 1);
848 c->dst = j;
849 }
078efddd 850 }
51ffa239 851
04773cb5
DC
852 if (isempty(p)) {
853 r = -EINVAL;
854 goto fail;
855 }
856
272ac205 857 if (strcaseeq(p, "minutely")) {
436dd70f 858 r = const_chain(0, &c->microsecond);
272ac205
DM
859 if (r < 0)
860 goto fail;
861
862 } else if (strcaseeq(p, "hourly")) {
36697dc0
LP
863 r = const_chain(0, &c->minute);
864 if (r < 0)
865 goto fail;
436dd70f 866 r = const_chain(0, &c->microsecond);
36697dc0
LP
867 if (r < 0)
868 goto fail;
869
b43d1d01 870 } else if (strcaseeq(p, "daily")) {
36697dc0
LP
871 r = const_chain(0, &c->hour);
872 if (r < 0)
873 goto fail;
874 r = const_chain(0, &c->minute);
875 if (r < 0)
876 goto fail;
436dd70f 877 r = const_chain(0, &c->microsecond);
36697dc0
LP
878 if (r < 0)
879 goto fail;
880
b43d1d01 881 } else if (strcaseeq(p, "monthly")) {
36697dc0
LP
882 r = const_chain(1, &c->day);
883 if (r < 0)
884 goto fail;
885 r = const_chain(0, &c->hour);
886 if (r < 0)
887 goto fail;
888 r = const_chain(0, &c->minute);
889 if (r < 0)
890 goto fail;
436dd70f 891 r = const_chain(0, &c->microsecond);
36697dc0
LP
892 if (r < 0)
893 goto fail;
894
dbfd41e2
LP
895 } else if (strcaseeq(p, "annually") ||
896 strcaseeq(p, "yearly") ||
897 strcaseeq(p, "anually") /* backwards compatibility */ ) {
898
13516818
LP
899 r = const_chain(1, &c->month);
900 if (r < 0)
901 goto fail;
902 r = const_chain(1, &c->day);
903 if (r < 0)
904 goto fail;
905 r = const_chain(0, &c->hour);
906 if (r < 0)
907 goto fail;
908 r = const_chain(0, &c->minute);
909 if (r < 0)
910 goto fail;
436dd70f 911 r = const_chain(0, &c->microsecond);
13516818
LP
912 if (r < 0)
913 goto fail;
914
b43d1d01 915 } else if (strcaseeq(p, "weekly")) {
36697dc0
LP
916
917 c->weekdays_bits = 1;
918
919 r = const_chain(0, &c->hour);
920 if (r < 0)
921 goto fail;
922 r = const_chain(0, &c->minute);
923 if (r < 0)
924 goto fail;
436dd70f 925 r = const_chain(0, &c->microsecond);
dbfd41e2
LP
926 if (r < 0)
927 goto fail;
928
929 } else if (strcaseeq(p, "quarterly")) {
930
931 r = const_chain(1, &c->month);
932 if (r < 0)
933 goto fail;
934 r = const_chain(4, &c->month);
935 if (r < 0)
936 goto fail;
937 r = const_chain(7, &c->month);
938 if (r < 0)
939 goto fail;
940 r = const_chain(10, &c->month);
941 if (r < 0)
942 goto fail;
943 r = const_chain(1, &c->day);
944 if (r < 0)
945 goto fail;
946 r = const_chain(0, &c->hour);
947 if (r < 0)
948 goto fail;
949 r = const_chain(0, &c->minute);
950 if (r < 0)
951 goto fail;
436dd70f 952 r = const_chain(0, &c->microsecond);
dbfd41e2
LP
953 if (r < 0)
954 goto fail;
955
956 } else if (strcaseeq(p, "biannually") ||
957 strcaseeq(p, "bi-annually") ||
958 strcaseeq(p, "semiannually") ||
959 strcaseeq(p, "semi-annually")) {
960
961 r = const_chain(1, &c->month);
962 if (r < 0)
963 goto fail;
964 r = const_chain(7, &c->month);
965 if (r < 0)
966 goto fail;
967 r = const_chain(1, &c->day);
968 if (r < 0)
969 goto fail;
970 r = const_chain(0, &c->hour);
971 if (r < 0)
972 goto fail;
973 r = const_chain(0, &c->minute);
974 if (r < 0)
975 goto fail;
436dd70f 976 r = const_chain(0, &c->microsecond);
36697dc0
LP
977 if (r < 0)
978 goto fail;
979
980 } else {
981 r = parse_weekdays(&p, c);
982 if (r < 0)
983 goto fail;
984
985 r = parse_date(&p, c);
986 if (r < 0)
987 goto fail;
988
519cffec 989 r = parse_calendar_time(&p, c);
36697dc0
LP
990 if (r < 0)
991 goto fail;
992
993 if (*p != 0) {
994 r = -EINVAL;
995 goto fail;
996 }
997 }
998
999 r = calendar_spec_normalize(c);
1000 if (r < 0)
1001 goto fail;
1002
1003 if (!calendar_spec_valid(c)) {
1004 r = -EINVAL;
1005 goto fail;
1006 }
1007
1008 *spec = c;
1009 return 0;
1010
1011fail:
1012 calendar_spec_free(c);
1013 return r;
1014}
1015
60bf5836 1016static int find_end_of_month(struct tm *tm, bool utc, int day) {
a2eb5ea7
DC
1017 struct tm t = *tm;
1018
1019 t.tm_mon++;
1020 t.tm_mday = 1 - day;
1021
c477ff14 1022 if (mktime_or_timegm(&t, utc) < 0 ||
a2eb5ea7
DC
1023 t.tm_mon != tm->tm_mon)
1024 return -1;
1025
1026 return t.tm_mday;
1027}
1028
8ea80351
DC
1029static int find_matching_component(const CalendarSpec *spec, const CalendarComponent *c,
1030 struct tm *tm, int *val) {
482f3b54 1031 const CalendarComponent *p = c;
e78fce48 1032 int start, stop, d = -1;
36697dc0
LP
1033 bool d_set = false;
1034 int r;
1035
1036 assert(val);
1037
1038 if (!c)
1039 return 0;
1040
1041 while (c) {
e78fce48
DC
1042 start = c->start;
1043 stop = c->stop;
a2eb5ea7 1044
8ea80351 1045 if (spec->end_of_month && p == spec->day) {
e78fce48
DC
1046 start = find_end_of_month(tm, spec->utc, start);
1047 stop = find_end_of_month(tm, spec->utc, stop);
a2eb5ea7 1048
e78fce48
DC
1049 if (stop > 0)
1050 SWAP_TWO(start, stop);
a2eb5ea7 1051 }
8ea80351 1052
e78fce48 1053 if (start >= *val) {
36697dc0 1054
e78fce48
DC
1055 if (!d_set || start < d) {
1056 d = start;
36697dc0
LP
1057 d_set = true;
1058 }
1059
1060 } else if (c->repeat > 0) {
1061 int k;
1062
e78fce48 1063 k = start + c->repeat * ((*val - start + c->repeat - 1) / c->repeat);
36697dc0 1064
e78fce48 1065 if ((!d_set || k < d) && (stop < 0 || k <= stop)) {
36697dc0
LP
1066 d = k;
1067 d_set = true;
1068 }
1069 }
1070
482f3b54 1071 c = c->next;
36697dc0
LP
1072 }
1073
1074 if (!d_set)
1075 return -ENOENT;
1076
1077 r = *val != d;
1078 *val = d;
1079 return r;
1080}
1081
51ffa239 1082static bool tm_out_of_bounds(const struct tm *tm, bool utc) {
36697dc0
LP
1083 struct tm t;
1084 assert(tm);
1085
1086 t = *tm;
1087
c477ff14 1088 if (mktime_or_timegm(&t, utc) < 0)
36697dc0
LP
1089 return true;
1090
f6e7d66b
DC
1091 /*
1092 * Set an upper bound on the year so impossible dates like "*-02-31"
1093 * don't cause find_next() to loop forever. tm_year contains years
1094 * since 1900, so adjust it accordingly.
1095 */
1096 if (tm->tm_year + 1900 > MAX_YEAR)
1097 return true;
1098
36697dc0
LP
1099 /* Did any normalization take place? If so, it was out of bounds before */
1100 return
1101 t.tm_year != tm->tm_year ||
1102 t.tm_mon != tm->tm_mon ||
1103 t.tm_mday != tm->tm_mday ||
1104 t.tm_hour != tm->tm_hour ||
1105 t.tm_min != tm->tm_min ||
1106 t.tm_sec != tm->tm_sec;
1107}
1108
51ffa239 1109static bool matches_weekday(int weekdays_bits, const struct tm *tm, bool utc) {
36697dc0
LP
1110 struct tm t;
1111 int k;
1112
489464d0 1113 if (weekdays_bits < 0 || weekdays_bits >= BITS_WEEKDAYS)
36697dc0
LP
1114 return true;
1115
1116 t = *tm;
c477ff14 1117 if (mktime_or_timegm(&t, utc) < 0)
36697dc0
LP
1118 return false;
1119
1120 k = t.tm_wday == 0 ? 6 : t.tm_wday - 1;
1121 return (weekdays_bits & (1 << k));
1122}
1123
436dd70f 1124static int find_next(const CalendarSpec *spec, struct tm *tm, usec_t *usec) {
36697dc0 1125 struct tm c;
436dd70f 1126 int tm_usec;
36697dc0
LP
1127 int r;
1128
1129 assert(spec);
1130 assert(tm);
1131
1132 c = *tm;
436dd70f 1133 tm_usec = *usec;
36697dc0
LP
1134
1135 for (;;) {
1136 /* Normalize the current date */
ea3894c1 1137 (void) mktime_or_timegm(&c, spec->utc);
21b3a0fc 1138 c.tm_isdst = spec->dst;
36697dc0
LP
1139
1140 c.tm_year += 1900;
8ea80351 1141 r = find_matching_component(spec, spec->year, &c, &c.tm_year);
36697dc0
LP
1142 c.tm_year -= 1900;
1143
1144 if (r > 0) {
1145 c.tm_mon = 0;
1146 c.tm_mday = 1;
436dd70f 1147 c.tm_hour = c.tm_min = c.tm_sec = tm_usec = 0;
36697dc0 1148 }
e308ddca 1149 if (r < 0)
36697dc0 1150 return r;
e308ddca
LP
1151 if (tm_out_of_bounds(&c, spec->utc))
1152 return -ENOENT;
36697dc0
LP
1153
1154 c.tm_mon += 1;
8ea80351 1155 r = find_matching_component(spec, spec->month, &c, &c.tm_mon);
36697dc0
LP
1156 c.tm_mon -= 1;
1157
1158 if (r > 0) {
1159 c.tm_mday = 1;
436dd70f 1160 c.tm_hour = c.tm_min = c.tm_sec = tm_usec = 0;
36697dc0 1161 }
51ffa239 1162 if (r < 0 || tm_out_of_bounds(&c, spec->utc)) {
313cefa1 1163 c.tm_year++;
36697dc0
LP
1164 c.tm_mon = 0;
1165 c.tm_mday = 1;
436dd70f 1166 c.tm_hour = c.tm_min = c.tm_sec = tm_usec = 0;
36697dc0
LP
1167 continue;
1168 }
1169
8ea80351 1170 r = find_matching_component(spec, spec->day, &c, &c.tm_mday);
36697dc0 1171 if (r > 0)
436dd70f 1172 c.tm_hour = c.tm_min = c.tm_sec = tm_usec = 0;
51ffa239 1173 if (r < 0 || tm_out_of_bounds(&c, spec->utc)) {
313cefa1 1174 c.tm_mon++;
36697dc0 1175 c.tm_mday = 1;
436dd70f 1176 c.tm_hour = c.tm_min = c.tm_sec = tm_usec = 0;
36697dc0
LP
1177 continue;
1178 }
1179
51ffa239 1180 if (!matches_weekday(spec->weekdays_bits, &c, spec->utc)) {
36697dc0 1181 c.tm_mday++;
436dd70f 1182 c.tm_hour = c.tm_min = c.tm_sec = tm_usec = 0;
36697dc0
LP
1183 continue;
1184 }
1185
8ea80351 1186 r = find_matching_component(spec, spec->hour, &c, &c.tm_hour);
36697dc0 1187 if (r > 0)
a022d76e 1188 c.tm_min = c.tm_sec = tm_usec = 0;
51ffa239 1189 if (r < 0 || tm_out_of_bounds(&c, spec->utc)) {
313cefa1 1190 c.tm_mday++;
436dd70f 1191 c.tm_hour = c.tm_min = c.tm_sec = tm_usec = 0;
36697dc0
LP
1192 continue;
1193 }
1194
8ea80351 1195 r = find_matching_component(spec, spec->minute, &c, &c.tm_min);
36697dc0 1196 if (r > 0)
a022d76e 1197 c.tm_sec = tm_usec = 0;
51ffa239 1198 if (r < 0 || tm_out_of_bounds(&c, spec->utc)) {
313cefa1 1199 c.tm_hour++;
436dd70f 1200 c.tm_min = c.tm_sec = tm_usec = 0;
36697dc0
LP
1201 continue;
1202 }
1203
436dd70f 1204 c.tm_sec = c.tm_sec * USEC_PER_SEC + tm_usec;
8ea80351 1205 r = find_matching_component(spec, spec->microsecond, &c, &c.tm_sec);
436dd70f
HV
1206 tm_usec = c.tm_sec % USEC_PER_SEC;
1207 c.tm_sec /= USEC_PER_SEC;
1208
51ffa239 1209 if (r < 0 || tm_out_of_bounds(&c, spec->utc)) {
313cefa1 1210 c.tm_min++;
436dd70f 1211 c.tm_sec = tm_usec = 0;
36697dc0
LP
1212 continue;
1213 }
1214
36697dc0 1215 *tm = c;
436dd70f 1216 *usec = tm_usec;
36697dc0
LP
1217 return 0;
1218 }
1219}
1220
1221int calendar_spec_next_usec(const CalendarSpec *spec, usec_t usec, usec_t *next) {
1222 struct tm tm;
1223 time_t t;
1224 int r;
436dd70f 1225 usec_t tm_usec;
36697dc0
LP
1226
1227 assert(spec);
1228 assert(next);
1229
1bb4b028
LP
1230 if (usec > USEC_TIMESTAMP_FORMATTABLE_MAX)
1231 return -EINVAL;
1232
436dd70f
HV
1233 usec++;
1234 t = (time_t) (usec / USEC_PER_SEC);
51ffa239 1235 assert_se(localtime_or_gmtime_r(&t, &tm, spec->utc));
436dd70f 1236 tm_usec = usec % USEC_PER_SEC;
36697dc0 1237
436dd70f 1238 r = find_next(spec, &tm, &tm_usec);
36697dc0
LP
1239 if (r < 0)
1240 return r;
1241
51ffa239 1242 t = mktime_or_timegm(&tm, spec->utc);
c477ff14 1243 if (t < 0)
36697dc0
LP
1244 return -EINVAL;
1245
436dd70f 1246 *next = (usec_t) t * USEC_PER_SEC + tm_usec;
36697dc0
LP
1247 return 0;
1248}