]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/basic/calendarspec.c
fix path in btrfs rule (#6844)
[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)
4b61c875 260 fputc_unlocked(',', f);
36697dc0 261 else
e638d050 262 need_comma = true;
36697dc0 263
4b61c875 264 fputs_unlocked(days[x], f);
36697dc0
LP
265 l = x;
266 }
267
268 } else if (l >= 0) {
269
270 if (x > l + 1) {
4b61c875
LP
271 fputs_unlocked(x > l + 2 ? ".." : ",", f);
272 fputs_unlocked(days[x-1], f);
36697dc0
LP
273 }
274
275 l = -1;
276 }
277 }
278
279 if (l >= 0 && x > l + 1) {
4b61c875
LP
280 fputs_unlocked(x > l + 2 ? ".." : ",", f);
281 fputs_unlocked(days[x-1], f);
36697dc0
LP
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) {
4b61c875 291 fputc_unlocked('*', f);
36697dc0
LP
292 return;
293 }
294
482f3b54 295 if (usec && c->start == 0 && c->repeat == USEC_PER_SEC && !c->next) {
4b61c875 296 fputc_unlocked('*', f);
482f3b54
DC
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) {
4b61c875 317 fputc_unlocked(',', 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 336 format_weekdays(f, c);
4b61c875 337 fputc_unlocked(' ', f);
36697dc0
LP
338 }
339
436dd70f 340 format_chain(f, 4, c->year, false);
4b61c875 341 fputc_unlocked('-', f);
436dd70f 342 format_chain(f, 2, c->month, false);
4b61c875 343 fputc_unlocked(c->end_of_month ? '~' : '-', f);
436dd70f 344 format_chain(f, 2, c->day, false);
4b61c875 345 fputc_unlocked(' ', f);
436dd70f 346 format_chain(f, 2, c->hour, false);
4b61c875 347 fputc_unlocked(':', f);
436dd70f 348 format_chain(f, 2, c->minute, false);
4b61c875 349 fputc_unlocked(':', f);
482f3b54 350 format_chain(f, 2, c->microsecond, true);
36697dc0 351
51ffa239 352 if (c->utc)
4b61c875 353 fputs_unlocked(" 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])) {
4b61c875
LP
361 fputc_unlocked(' ', f);
362 fputs_unlocked(tzname[c->dst], f);
21b3a0fc
LP
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
d80e5b74
ZJS
490static int parse_one_number(const char *p, const char **e, unsigned long *ret) {
491 char *ee = NULL;
492 unsigned long value;
493
494 errno = 0;
495 value = strtoul(p, &ee, 10);
496 if (errno > 0)
497 return -errno;
498 if (ee == p)
499 return -EINVAL;
500
501 *ret = value;
502 *e = ee;
503 return 0;
504}
505
c0aebb4b 506static int parse_component_decimal(const char **p, bool usec, int *res) {
436dd70f
HV
507 unsigned long value;
508 const char *e = NULL;
436dd70f 509 int r;
36697dc0 510
9dfa81a0
DC
511 if (!isdigit(**p))
512 return -EINVAL;
513
d80e5b74
ZJS
514 r = parse_one_number(*p, &e, &value);
515 if (r < 0)
516 return r;
36697dc0 517
436dd70f
HV
518 if (usec) {
519 if (value * USEC_PER_SEC / USEC_PER_SEC != value)
36697dc0
LP
520 return -ERANGE;
521
436dd70f 522 value *= USEC_PER_SEC;
436dd70f 523
60bf5836
DC
524 /* One "." is a decimal point, but ".." is a range separator */
525 if (e[0] == '.' && e[1] != '.') {
526 unsigned add;
36ff0c97 527
436dd70f
HV
528 e++;
529 r = parse_fractional_part_u(&e, 6, &add);
530 if (r < 0)
531 return r;
532
533 if (add + value < value)
534 return -ERANGE;
535 value += add;
536 }
537 }
538
c0aebb4b
DC
539 if (value > INT_MAX)
540 return -ERANGE;
541
436dd70f
HV
542 *p = e;
543 *res = value;
544
545 return 0;
546}
547
32b52369
DC
548static int const_chain(int value, CalendarComponent **c) {
549 CalendarComponent *cc = NULL;
550
551 assert(c);
552
553 cc = new0(CalendarComponent, 1);
554 if (!cc)
555 return -ENOMEM;
556
e78fce48
DC
557 cc->start = value;
558 cc->stop = -1;
32b52369
DC
559 cc->repeat = 0;
560 cc->next = *c;
561
562 *c = cc;
563
564 return 0;
565}
566
d80e5b74
ZJS
567static int calendarspec_from_time_t(CalendarSpec *c, time_t time) {
568 struct tm tm;
569 CalendarComponent *year = NULL, *month = NULL, *day = NULL, *hour = NULL, *minute = NULL, *us = NULL;
570 int r;
571
572 assert_se(gmtime_r(&time, &tm));
573
574 r = const_chain(tm.tm_year + 1900, &year);
575 if (r < 0)
576 return r;
577
578 r = const_chain(tm.tm_mon + 1, &month);
579 if (r < 0)
580 return r;
581
582 r = const_chain(tm.tm_mday, &day);
583 if (r < 0)
584 return r;
585
586 r = const_chain(tm.tm_hour, &hour);
587 if (r < 0)
588 return r;
589
590 r = const_chain(tm.tm_min, &minute);
591 if (r < 0)
592 return r;
593
594 r = const_chain(tm.tm_sec * USEC_PER_SEC, &us);
595 if (r < 0)
596 return r;
597
598 c->utc = true;
599 c->year = year;
600 c->month = month;
601 c->day = day;
602 c->hour = hour;
603 c->minute = minute;
604 c->microsecond = us;
605 return 0;
606}
607
436dd70f 608static int prepend_component(const char **p, bool usec, CalendarComponent **c) {
c0aebb4b 609 int r, start, stop = -1, repeat = 0;
436dd70f 610 CalendarComponent *cc;
436dd70f
HV
611 const char *e;
612
613 assert(p);
614 assert(c);
615
616 e = *p;
617
e78fce48 618 r = parse_component_decimal(&e, usec, &start);
436dd70f
HV
619 if (r < 0)
620 return r;
621
a2eb5ea7
DC
622 if (e[0] == '.' && e[1] == '.') {
623 e += 2;
e78fce48 624 r = parse_component_decimal(&e, usec, &stop);
a2eb5ea7
DC
625 if (r < 0)
626 return r;
627
628 repeat = usec ? USEC_PER_SEC : 1;
629 }
630
436dd70f
HV
631 if (*e == '/') {
632 e++;
633 r = parse_component_decimal(&e, usec, &repeat);
634 if (r < 0)
635 return r;
636
637 if (repeat == 0)
638 return -ERANGE;
36697dc0
LP
639 }
640
8ea80351 641 if (*e != 0 && *e != ' ' && *e != ',' && *e != '-' && *e != '~' && *e != ':')
36697dc0
LP
642 return -EINVAL;
643
644 cc = new0(CalendarComponent, 1);
645 if (!cc)
646 return -ENOMEM;
647
e78fce48
DC
648 cc->start = start;
649 cc->stop = stop;
36697dc0
LP
650 cc->repeat = repeat;
651 cc->next = *c;
652
653 *p = e;
654 *c = cc;
655
656 if (*e ==',') {
657 *p += 1;
436dd70f 658 return prepend_component(p, usec, c);
36697dc0
LP
659 }
660
661 return 0;
662}
663
436dd70f 664static int parse_chain(const char **p, bool usec, CalendarComponent **c) {
36697dc0
LP
665 const char *t;
666 CalendarComponent *cc = NULL;
667 int r;
668
669 assert(p);
670 assert(c);
671
672 t = *p;
673
674 if (t[0] == '*') {
436dd70f
HV
675 if (usec) {
676 r = const_chain(0, c);
677 if (r < 0)
678 return r;
679 (*c)->repeat = USEC_PER_SEC;
680 } else
681 *c = NULL;
682
36697dc0 683 *p = t + 1;
36697dc0
LP
684 return 0;
685 }
686
436dd70f 687 r = prepend_component(&t, usec, &cc);
36697dc0
LP
688 if (r < 0) {
689 free_chain(cc);
690 return r;
691 }
692
693 *p = t;
694 *c = cc;
695 return 0;
696}
697
36697dc0
LP
698static int parse_date(const char **p, CalendarSpec *c) {
699 const char *t;
700 int r;
701 CalendarComponent *first, *second, *third;
702
703 assert(p);
704 assert(*p);
705 assert(c);
706
707 t = *p;
708
709 if (*t == 0)
710 return 0;
711
d80e5b74
ZJS
712 /* @TIMESTAMP — UNIX time in seconds since the epoch */
713 if (*t == '@') {
714 unsigned long value;
715 time_t time;
716
717 r = parse_one_number(t + 1, &t, &value);
718 if (r < 0)
719 return r;
720
721 time = value;
722 if ((unsigned long) time != value)
723 return -ERANGE;
724
725 r = calendarspec_from_time_t(c, time);
726 if (r < 0)
727 return r;
728
729 *p = t;
730 return 1; /* finito, don't parse H:M:S after that */
731 }
732
436dd70f 733 r = parse_chain(&t, false, &first);
36697dc0
LP
734 if (r < 0)
735 return r;
736
737 /* Already the end? A ':' as separator? In that case this was a time, not a date */
738 if (*t == 0 || *t == ':') {
739 free_chain(first);
740 return 0;
741 }
742
8ea80351
DC
743 if (*t == '~')
744 c->end_of_month = true;
745 else if (*t != '-') {
36697dc0
LP
746 free_chain(first);
747 return -EINVAL;
748 }
749
750 t++;
436dd70f 751 r = parse_chain(&t, false, &second);
36697dc0
LP
752 if (r < 0) {
753 free_chain(first);
754 return r;
755 }
756
757 /* Got two parts, hence it's month and day */
758 if (*t == ' ' || *t == 0) {
759 *p = t + strspn(t, " ");
760 c->month = first;
761 c->day = second;
762 return 0;
fc2371c7
DC
763 } else if (c->end_of_month) {
764 free_chain(first);
765 free_chain(second);
8ea80351 766 return -EINVAL;
fc2371c7 767 }
36697dc0 768
8ea80351
DC
769 if (*t == '~')
770 c->end_of_month = true;
771 else if (*t != '-') {
36697dc0
LP
772 free_chain(first);
773 free_chain(second);
774 return -EINVAL;
775 }
776
777 t++;
436dd70f 778 r = parse_chain(&t, false, &third);
36697dc0
LP
779 if (r < 0) {
780 free_chain(first);
781 free_chain(second);
782 return r;
783 }
784
8ea80351 785 /* Got three parts, hence it is year, month and day */
36697dc0
LP
786 if (*t == ' ' || *t == 0) {
787 *p = t + strspn(t, " ");
788 c->year = first;
789 c->month = second;
790 c->day = third;
791 return 0;
792 }
793
794 free_chain(first);
795 free_chain(second);
796 free_chain(third);
797 return -EINVAL;
798}
799
519cffec 800static int parse_calendar_time(const char **p, CalendarSpec *c) {
36697dc0
LP
801 CalendarComponent *h = NULL, *m = NULL, *s = NULL;
802 const char *t;
803 int r;
804
805 assert(p);
806 assert(*p);
807 assert(c);
808
809 t = *p;
810
408a51e1
DC
811 /* If no time is specified at all, then this means 00:00:00 */
812 if (*t == 0)
813 goto null_hour;
36697dc0 814
436dd70f 815 r = parse_chain(&t, false, &h);
36697dc0
LP
816 if (r < 0)
817 goto fail;
818
819 if (*t != ':') {
820 r = -EINVAL;
821 goto fail;
822 }
823
824 t++;
436dd70f 825 r = parse_chain(&t, false, &m);
36697dc0
LP
826 if (r < 0)
827 goto fail;
828
829 /* Already at the end? Then it's hours and minutes, and seconds are 0 */
c0df71fa
DC
830 if (*t == 0)
831 goto null_second;
36697dc0
LP
832
833 if (*t != ':') {
834 r = -EINVAL;
835 goto fail;
836 }
837
838 t++;
436dd70f 839 r = parse_chain(&t, true, &s);
36697dc0
LP
840 if (r < 0)
841 goto fail;
842
843 /* At the end? Then it's hours, minutes and seconds */
844 if (*t == 0)
845 goto finish;
846
847 r = -EINVAL;
848 goto fail;
849
850null_hour:
851 r = const_chain(0, &h);
852 if (r < 0)
853 goto fail;
854
855 r = const_chain(0, &m);
856 if (r < 0)
857 goto fail;
858
859null_second:
860 r = const_chain(0, &s);
861 if (r < 0)
862 goto fail;
863
864finish:
865 *p = t;
866 c->hour = h;
867 c->minute = m;
436dd70f
HV
868 c->microsecond = s;
869
36697dc0
LP
870 return 0;
871
872fail:
873 free_chain(h);
874 free_chain(m);
875 free_chain(s);
876 return r;
877}
878
879int calendar_spec_from_string(const char *p, CalendarSpec **spec) {
21b3a0fc 880 const char *utc;
36697dc0
LP
881 CalendarSpec *c;
882 int r;
883
884 assert(p);
885 assert(spec);
886
36697dc0
LP
887 c = new0(CalendarSpec, 1);
888 if (!c)
889 return -ENOMEM;
21b3a0fc 890 c->dst = -1;
36697dc0 891
078efddd
HV
892 utc = endswith_no_case(p, " UTC");
893 if (utc) {
894 c->utc = true;
895 p = strndupa(p, utc - p);
21b3a0fc
LP
896 } else {
897 const char *e = NULL;
898 int j;
899
900 tzset();
901
902 /* Check if the local timezone was specified? */
903 for (j = 0; j <= 1; j++) {
904 if (isempty(tzname[j]))
905 continue;
906
907 e = endswith_no_case(p, tzname[j]);
d80e5b74 908 if (!e)
21b3a0fc
LP
909 continue;
910 if (e == p)
911 continue;
912 if (e[-1] != ' ')
913 continue;
914
915 break;
916 }
917
918 /* Found one of the two timezones specified? */
919 if (IN_SET(j, 0, 1)) {
920 p = strndupa(p, e - p - 1);
921 c->dst = j;
922 }
078efddd 923 }
51ffa239 924
04773cb5
DC
925 if (isempty(p)) {
926 r = -EINVAL;
927 goto fail;
928 }
929
272ac205 930 if (strcaseeq(p, "minutely")) {
436dd70f 931 r = const_chain(0, &c->microsecond);
272ac205
DM
932 if (r < 0)
933 goto fail;
934
935 } else if (strcaseeq(p, "hourly")) {
36697dc0
LP
936 r = const_chain(0, &c->minute);
937 if (r < 0)
938 goto fail;
436dd70f 939 r = const_chain(0, &c->microsecond);
36697dc0
LP
940 if (r < 0)
941 goto fail;
942
b43d1d01 943 } else if (strcaseeq(p, "daily")) {
36697dc0
LP
944 r = const_chain(0, &c->hour);
945 if (r < 0)
946 goto fail;
947 r = const_chain(0, &c->minute);
948 if (r < 0)
949 goto fail;
436dd70f 950 r = const_chain(0, &c->microsecond);
36697dc0
LP
951 if (r < 0)
952 goto fail;
953
b43d1d01 954 } else if (strcaseeq(p, "monthly")) {
36697dc0
LP
955 r = const_chain(1, &c->day);
956 if (r < 0)
957 goto fail;
958 r = const_chain(0, &c->hour);
959 if (r < 0)
960 goto fail;
961 r = const_chain(0, &c->minute);
962 if (r < 0)
963 goto fail;
436dd70f 964 r = const_chain(0, &c->microsecond);
36697dc0
LP
965 if (r < 0)
966 goto fail;
967
dbfd41e2
LP
968 } else if (strcaseeq(p, "annually") ||
969 strcaseeq(p, "yearly") ||
970 strcaseeq(p, "anually") /* backwards compatibility */ ) {
971
13516818
LP
972 r = const_chain(1, &c->month);
973 if (r < 0)
974 goto fail;
975 r = const_chain(1, &c->day);
976 if (r < 0)
977 goto fail;
978 r = const_chain(0, &c->hour);
979 if (r < 0)
980 goto fail;
981 r = const_chain(0, &c->minute);
982 if (r < 0)
983 goto fail;
436dd70f 984 r = const_chain(0, &c->microsecond);
13516818
LP
985 if (r < 0)
986 goto fail;
987
b43d1d01 988 } else if (strcaseeq(p, "weekly")) {
36697dc0
LP
989
990 c->weekdays_bits = 1;
991
992 r = const_chain(0, &c->hour);
993 if (r < 0)
994 goto fail;
995 r = const_chain(0, &c->minute);
996 if (r < 0)
997 goto fail;
436dd70f 998 r = const_chain(0, &c->microsecond);
dbfd41e2
LP
999 if (r < 0)
1000 goto fail;
1001
1002 } else if (strcaseeq(p, "quarterly")) {
1003
1004 r = const_chain(1, &c->month);
1005 if (r < 0)
1006 goto fail;
1007 r = const_chain(4, &c->month);
1008 if (r < 0)
1009 goto fail;
1010 r = const_chain(7, &c->month);
1011 if (r < 0)
1012 goto fail;
1013 r = const_chain(10, &c->month);
1014 if (r < 0)
1015 goto fail;
1016 r = const_chain(1, &c->day);
1017 if (r < 0)
1018 goto fail;
1019 r = const_chain(0, &c->hour);
1020 if (r < 0)
1021 goto fail;
1022 r = const_chain(0, &c->minute);
1023 if (r < 0)
1024 goto fail;
436dd70f 1025 r = const_chain(0, &c->microsecond);
dbfd41e2
LP
1026 if (r < 0)
1027 goto fail;
1028
1029 } else if (strcaseeq(p, "biannually") ||
1030 strcaseeq(p, "bi-annually") ||
1031 strcaseeq(p, "semiannually") ||
1032 strcaseeq(p, "semi-annually")) {
1033
1034 r = const_chain(1, &c->month);
1035 if (r < 0)
1036 goto fail;
1037 r = const_chain(7, &c->month);
1038 if (r < 0)
1039 goto fail;
1040 r = const_chain(1, &c->day);
1041 if (r < 0)
1042 goto fail;
1043 r = const_chain(0, &c->hour);
1044 if (r < 0)
1045 goto fail;
1046 r = const_chain(0, &c->minute);
1047 if (r < 0)
1048 goto fail;
436dd70f 1049 r = const_chain(0, &c->microsecond);
36697dc0
LP
1050 if (r < 0)
1051 goto fail;
1052
1053 } else {
1054 r = parse_weekdays(&p, c);
1055 if (r < 0)
1056 goto fail;
1057
1058 r = parse_date(&p, c);
1059 if (r < 0)
1060 goto fail;
1061
d80e5b74
ZJS
1062 if (r == 0) {
1063 r = parse_calendar_time(&p, c);
1064 if (r < 0)
1065 goto fail;
1066 }
36697dc0
LP
1067
1068 if (*p != 0) {
1069 r = -EINVAL;
1070 goto fail;
1071 }
1072 }
1073
1074 r = calendar_spec_normalize(c);
1075 if (r < 0)
1076 goto fail;
1077
1078 if (!calendar_spec_valid(c)) {
1079 r = -EINVAL;
1080 goto fail;
1081 }
1082
1083 *spec = c;
1084 return 0;
1085
1086fail:
1087 calendar_spec_free(c);
1088 return r;
1089}
1090
60bf5836 1091static int find_end_of_month(struct tm *tm, bool utc, int day) {
a2eb5ea7
DC
1092 struct tm t = *tm;
1093
1094 t.tm_mon++;
1095 t.tm_mday = 1 - day;
1096
c477ff14 1097 if (mktime_or_timegm(&t, utc) < 0 ||
a2eb5ea7
DC
1098 t.tm_mon != tm->tm_mon)
1099 return -1;
1100
1101 return t.tm_mday;
1102}
1103
8ea80351
DC
1104static int find_matching_component(const CalendarSpec *spec, const CalendarComponent *c,
1105 struct tm *tm, int *val) {
482f3b54 1106 const CalendarComponent *p = c;
e78fce48 1107 int start, stop, d = -1;
36697dc0
LP
1108 bool d_set = false;
1109 int r;
1110
1111 assert(val);
1112
1113 if (!c)
1114 return 0;
1115
1116 while (c) {
e78fce48
DC
1117 start = c->start;
1118 stop = c->stop;
a2eb5ea7 1119
8ea80351 1120 if (spec->end_of_month && p == spec->day) {
e78fce48
DC
1121 start = find_end_of_month(tm, spec->utc, start);
1122 stop = find_end_of_month(tm, spec->utc, stop);
a2eb5ea7 1123
e78fce48
DC
1124 if (stop > 0)
1125 SWAP_TWO(start, stop);
a2eb5ea7 1126 }
8ea80351 1127
e78fce48 1128 if (start >= *val) {
36697dc0 1129
e78fce48
DC
1130 if (!d_set || start < d) {
1131 d = start;
36697dc0
LP
1132 d_set = true;
1133 }
1134
1135 } else if (c->repeat > 0) {
1136 int k;
1137
e78fce48 1138 k = start + c->repeat * ((*val - start + c->repeat - 1) / c->repeat);
36697dc0 1139
e78fce48 1140 if ((!d_set || k < d) && (stop < 0 || k <= stop)) {
36697dc0
LP
1141 d = k;
1142 d_set = true;
1143 }
1144 }
1145
482f3b54 1146 c = c->next;
36697dc0
LP
1147 }
1148
1149 if (!d_set)
1150 return -ENOENT;
1151
1152 r = *val != d;
1153 *val = d;
1154 return r;
1155}
1156
51ffa239 1157static bool tm_out_of_bounds(const struct tm *tm, bool utc) {
36697dc0
LP
1158 struct tm t;
1159 assert(tm);
1160
1161 t = *tm;
1162
c477ff14 1163 if (mktime_or_timegm(&t, utc) < 0)
36697dc0
LP
1164 return true;
1165
f6e7d66b
DC
1166 /*
1167 * Set an upper bound on the year so impossible dates like "*-02-31"
1168 * don't cause find_next() to loop forever. tm_year contains years
1169 * since 1900, so adjust it accordingly.
1170 */
1171 if (tm->tm_year + 1900 > MAX_YEAR)
1172 return true;
1173
36697dc0
LP
1174 /* Did any normalization take place? If so, it was out of bounds before */
1175 return
1176 t.tm_year != tm->tm_year ||
1177 t.tm_mon != tm->tm_mon ||
1178 t.tm_mday != tm->tm_mday ||
1179 t.tm_hour != tm->tm_hour ||
1180 t.tm_min != tm->tm_min ||
1181 t.tm_sec != tm->tm_sec;
1182}
1183
51ffa239 1184static bool matches_weekday(int weekdays_bits, const struct tm *tm, bool utc) {
36697dc0
LP
1185 struct tm t;
1186 int k;
1187
489464d0 1188 if (weekdays_bits < 0 || weekdays_bits >= BITS_WEEKDAYS)
36697dc0
LP
1189 return true;
1190
1191 t = *tm;
c477ff14 1192 if (mktime_or_timegm(&t, utc) < 0)
36697dc0
LP
1193 return false;
1194
1195 k = t.tm_wday == 0 ? 6 : t.tm_wday - 1;
1196 return (weekdays_bits & (1 << k));
1197}
1198
436dd70f 1199static int find_next(const CalendarSpec *spec, struct tm *tm, usec_t *usec) {
36697dc0 1200 struct tm c;
436dd70f 1201 int tm_usec;
36697dc0
LP
1202 int r;
1203
1204 assert(spec);
1205 assert(tm);
1206
1207 c = *tm;
436dd70f 1208 tm_usec = *usec;
36697dc0
LP
1209
1210 for (;;) {
1211 /* Normalize the current date */
ea3894c1 1212 (void) mktime_or_timegm(&c, spec->utc);
21b3a0fc 1213 c.tm_isdst = spec->dst;
36697dc0
LP
1214
1215 c.tm_year += 1900;
8ea80351 1216 r = find_matching_component(spec, spec->year, &c, &c.tm_year);
36697dc0
LP
1217 c.tm_year -= 1900;
1218
1219 if (r > 0) {
1220 c.tm_mon = 0;
1221 c.tm_mday = 1;
436dd70f 1222 c.tm_hour = c.tm_min = c.tm_sec = tm_usec = 0;
36697dc0 1223 }
e308ddca 1224 if (r < 0)
36697dc0 1225 return r;
e308ddca
LP
1226 if (tm_out_of_bounds(&c, spec->utc))
1227 return -ENOENT;
36697dc0
LP
1228
1229 c.tm_mon += 1;
8ea80351 1230 r = find_matching_component(spec, spec->month, &c, &c.tm_mon);
36697dc0
LP
1231 c.tm_mon -= 1;
1232
1233 if (r > 0) {
1234 c.tm_mday = 1;
436dd70f 1235 c.tm_hour = c.tm_min = c.tm_sec = tm_usec = 0;
36697dc0 1236 }
51ffa239 1237 if (r < 0 || tm_out_of_bounds(&c, spec->utc)) {
313cefa1 1238 c.tm_year++;
36697dc0
LP
1239 c.tm_mon = 0;
1240 c.tm_mday = 1;
436dd70f 1241 c.tm_hour = c.tm_min = c.tm_sec = tm_usec = 0;
36697dc0
LP
1242 continue;
1243 }
1244
8ea80351 1245 r = find_matching_component(spec, spec->day, &c, &c.tm_mday);
36697dc0 1246 if (r > 0)
436dd70f 1247 c.tm_hour = c.tm_min = c.tm_sec = tm_usec = 0;
51ffa239 1248 if (r < 0 || tm_out_of_bounds(&c, spec->utc)) {
313cefa1 1249 c.tm_mon++;
36697dc0 1250 c.tm_mday = 1;
436dd70f 1251 c.tm_hour = c.tm_min = c.tm_sec = tm_usec = 0;
36697dc0
LP
1252 continue;
1253 }
1254
51ffa239 1255 if (!matches_weekday(spec->weekdays_bits, &c, spec->utc)) {
36697dc0 1256 c.tm_mday++;
436dd70f 1257 c.tm_hour = c.tm_min = c.tm_sec = tm_usec = 0;
36697dc0
LP
1258 continue;
1259 }
1260
8ea80351 1261 r = find_matching_component(spec, spec->hour, &c, &c.tm_hour);
36697dc0 1262 if (r > 0)
a022d76e 1263 c.tm_min = c.tm_sec = tm_usec = 0;
51ffa239 1264 if (r < 0 || tm_out_of_bounds(&c, spec->utc)) {
313cefa1 1265 c.tm_mday++;
436dd70f 1266 c.tm_hour = c.tm_min = c.tm_sec = tm_usec = 0;
36697dc0
LP
1267 continue;
1268 }
1269
8ea80351 1270 r = find_matching_component(spec, spec->minute, &c, &c.tm_min);
36697dc0 1271 if (r > 0)
a022d76e 1272 c.tm_sec = tm_usec = 0;
51ffa239 1273 if (r < 0 || tm_out_of_bounds(&c, spec->utc)) {
313cefa1 1274 c.tm_hour++;
436dd70f 1275 c.tm_min = c.tm_sec = tm_usec = 0;
36697dc0
LP
1276 continue;
1277 }
1278
436dd70f 1279 c.tm_sec = c.tm_sec * USEC_PER_SEC + tm_usec;
8ea80351 1280 r = find_matching_component(spec, spec->microsecond, &c, &c.tm_sec);
436dd70f
HV
1281 tm_usec = c.tm_sec % USEC_PER_SEC;
1282 c.tm_sec /= USEC_PER_SEC;
1283
51ffa239 1284 if (r < 0 || tm_out_of_bounds(&c, spec->utc)) {
313cefa1 1285 c.tm_min++;
436dd70f 1286 c.tm_sec = tm_usec = 0;
36697dc0
LP
1287 continue;
1288 }
1289
36697dc0 1290 *tm = c;
436dd70f 1291 *usec = tm_usec;
36697dc0
LP
1292 return 0;
1293 }
1294}
1295
1296int calendar_spec_next_usec(const CalendarSpec *spec, usec_t usec, usec_t *next) {
1297 struct tm tm;
1298 time_t t;
1299 int r;
436dd70f 1300 usec_t tm_usec;
36697dc0
LP
1301
1302 assert(spec);
1303 assert(next);
1304
1bb4b028
LP
1305 if (usec > USEC_TIMESTAMP_FORMATTABLE_MAX)
1306 return -EINVAL;
1307
436dd70f
HV
1308 usec++;
1309 t = (time_t) (usec / USEC_PER_SEC);
51ffa239 1310 assert_se(localtime_or_gmtime_r(&t, &tm, spec->utc));
436dd70f 1311 tm_usec = usec % USEC_PER_SEC;
36697dc0 1312
436dd70f 1313 r = find_next(spec, &tm, &tm_usec);
36697dc0
LP
1314 if (r < 0)
1315 return r;
1316
51ffa239 1317 t = mktime_or_timegm(&tm, spec->utc);
c477ff14 1318 if (t < 0)
36697dc0
LP
1319 return -EINVAL;
1320
436dd70f 1321 *next = (usec_t) t * USEC_PER_SEC + tm_usec;
36697dc0
LP
1322 return 0;
1323}