]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/calendarspec.c
2 This file is part of systemd.
4 Copyright 2012 Lennart Poettering
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.
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.
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/>.
29 #include "alloc-util.h"
30 #include "calendarspec.h"
33 #include "parse-util.h"
34 #include "string-util.h"
36 #define BITS_WEEKDAYS 127
40 static void free_chain(CalendarComponent
*c
) {
50 void calendar_spec_free(CalendarSpec
*c
) {
59 free_chain(c
->minute
);
60 free_chain(c
->microsecond
);
65 static int component_compare(const void *_a
, const void *_b
) {
66 CalendarComponent
* const *a
= _a
, * const *b
= _b
;
68 if ((*a
)->value
< (*b
)->value
)
70 if ((*a
)->value
> (*b
)->value
)
73 if ((*a
)->range_end
< (*b
)->range_end
)
75 if ((*a
)->range_end
> (*b
)->range_end
)
78 if ((*a
)->repeat
< (*b
)->repeat
)
80 if ((*a
)->repeat
> (*b
)->repeat
)
86 static void normalize_chain(CalendarComponent
**c
) {
88 CalendarComponent
**b
, *i
, **j
, *next
;
92 for (i
= *c
; i
; i
= i
->next
) {
96 * While we're counting the chain, also normalize `range_end`
97 * so the length of the range is a multiple of `repeat`
99 if (i
->range_end
> i
->value
)
100 i
->range_end
-= (i
->range_end
- i
->value
) % i
->repeat
;
107 j
= b
= alloca(sizeof(CalendarComponent
*) * n
);
108 for (i
= *c
; i
; i
= i
->next
)
111 qsort(b
, n
, sizeof(CalendarComponent
*), component_compare
);
116 /* Drop non-unique entries */
117 for (k
= n
-1; k
> 0; k
--) {
118 if (b
[k
-1]->value
== next
->value
&&
119 b
[k
-1]->repeat
== next
->repeat
) {
131 static void fix_year(CalendarComponent
*c
) {
132 /* Turns 12 → 2012, 89 → 1989 */
135 CalendarComponent
*n
= c
->next
;
137 if (c
->value
>= 0 && c
->value
< 70)
140 if (c
->range_end
>= 0 && c
->range_end
< 70)
141 c
->range_end
+= 2000;
143 if (c
->value
>= 70 && c
->value
< 100)
146 if (c
->range_end
>= 70 && c
->range_end
< 100)
147 c
->range_end
+= 1900;
153 int calendar_spec_normalize(CalendarSpec
*c
) {
156 if (c
->weekdays_bits
<= 0 || c
->weekdays_bits
>= BITS_WEEKDAYS
)
157 c
->weekdays_bits
= -1;
159 if (c
->end_of_month
&& !c
->day
)
160 c
->end_of_month
= false;
164 normalize_chain(&c
->year
);
165 normalize_chain(&c
->month
);
166 normalize_chain(&c
->day
);
167 normalize_chain(&c
->hour
);
168 normalize_chain(&c
->minute
);
169 normalize_chain(&c
->microsecond
);
174 _pure_
static bool chain_valid(CalendarComponent
*c
, int from
, int to
, bool end_of_month
) {
178 /* Forbid dates more than 28 days from the end of the month */
182 if (c
->value
< from
|| c
->value
> to
)
186 * c->repeat must be short enough so at least one repetition may
187 * occur before the end of the interval. For dates scheduled
188 * relative to the end of the month, c->value and c->range_end
189 * correspond to the Nth last day of the month.
191 if (c
->range_end
>= 0) {
192 if (c
->range_end
< from
|| c
->range_end
> to
)
195 if (c
->value
+ c
->repeat
> c
->range_end
)
198 if (end_of_month
&& c
->value
- c
->repeat
< from
)
201 if (!end_of_month
&& c
->value
+ c
->repeat
> to
)
206 return chain_valid(c
->next
, from
, to
, end_of_month
);
211 _pure_
bool calendar_spec_valid(CalendarSpec
*c
) {
214 if (c
->weekdays_bits
> BITS_WEEKDAYS
)
217 if (!chain_valid(c
->year
, MIN_YEAR
, MAX_YEAR
, false))
220 if (!chain_valid(c
->month
, 1, 12, false))
223 if (!chain_valid(c
->day
, 1, 31, c
->end_of_month
))
226 if (!chain_valid(c
->hour
, 0, 23, false))
229 if (!chain_valid(c
->minute
, 0, 59, false))
232 if (!chain_valid(c
->microsecond
, 0, 60*USEC_PER_SEC
-1, false))
238 static void format_weekdays(FILE *f
, const CalendarSpec
*c
) {
239 static const char *const days
[] = {
250 bool need_comma
= false;
254 assert(c
->weekdays_bits
> 0 && c
->weekdays_bits
<= BITS_WEEKDAYS
);
256 for (x
= 0, l
= -1; x
< (int) ELEMENTSOF(days
); x
++) {
258 if (c
->weekdays_bits
& (1 << x
)) {
273 fputs(x
> l
+ 2 ? ".." : ",", f
);
281 if (l
>= 0 && x
> l
+ 1) {
282 fputs(x
> l
+ 2 ? ".." : ",", f
);
287 static void format_chain(FILE *f
, int space
, const CalendarComponent
*c
, bool usec
) {
288 int d
= usec
? (int) USEC_PER_SEC
: 1;
297 assert(c
->value
>= 0);
299 fprintf(f
, "%0*i", space
, c
->value
/ d
);
300 if (c
->value
% d
> 0)
301 fprintf(f
, ".%06i", c
->value
% d
);
303 if (c
->range_end
> 0)
304 fprintf(f
, "..%0*i", space
, c
->range_end
/ d
);
305 if (c
->range_end
% d
> 0)
306 fprintf(f
, ".%06i", c
->range_end
% d
);
308 if (c
->repeat
> 0 && !(c
->range_end
> 0 && c
->repeat
== d
))
309 fprintf(f
, "/%i", c
->repeat
/ d
);
310 if (c
->repeat
% d
> 0)
311 fprintf(f
, ".%06i", c
->repeat
% d
);
315 format_chain(f
, space
, c
->next
, usec
);
319 int calendar_spec_to_string(const CalendarSpec
*c
, char **p
) {
320 CalendarComponent
*cc
;
329 f
= open_memstream(&buf
, &sz
);
333 if (c
->weekdays_bits
> 0 && c
->weekdays_bits
<= BITS_WEEKDAYS
) {
334 format_weekdays(f
, c
);
338 format_chain(f
, 4, c
->year
, false);
340 format_chain(f
, 2, c
->month
, false);
341 fputc(c
->end_of_month
? '~' : '-', f
);
342 format_chain(f
, 2, c
->day
, false);
344 format_chain(f
, 2, c
->hour
, false);
346 format_chain(f
, 2, c
->minute
, false);
350 if (cc
&& !cc
->value
&& cc
->repeat
== USEC_PER_SEC
&& !cc
->next
)
353 format_chain(f
, 2, c
->microsecond
, true);
357 else if (IN_SET(c
->dst
, 0, 1)) {
359 /* If daylight saving is explicitly on or off, let's show the used timezone. */
363 if (!isempty(tzname
[c
->dst
])) {
365 fputs(tzname
[c
->dst
], f
);
369 r
= fflush_and_check(f
);
382 static int parse_weekdays(const char **p
, CalendarSpec
*c
) {
383 static const struct {
413 for (i
= 0; i
< ELEMENTSOF(day_nr
); i
++) {
416 if (!startswith_no_case(*p
, day_nr
[i
].name
))
419 skip
= strlen(day_nr
[i
].name
);
421 if ((*p
)[skip
] != '-' &&
428 c
->weekdays_bits
|= 1 << day_nr
[i
].nr
;
433 if (l
> day_nr
[i
].nr
)
436 for (j
= l
+ 1; j
< day_nr
[i
].nr
; j
++)
437 c
->weekdays_bits
|= 1 << j
;
444 /* Couldn't find this prefix, so let's assume the
445 weekday was not specified and let's continue with
447 if (i
>= ELEMENTSOF(day_nr
))
448 return first
? 0 : -EINVAL
;
450 /* We reached the end of the string */
454 /* We reached the end of the weekday spec part */
456 *p
+= strspn(*p
, " ");
470 /* Support ranges with "-" for backwards compatibility */
471 } else if (**p
== '-') {
478 } else if (**p
== ',') {
483 /* Allow a trailing comma but not an open range */
484 if (**p
== 0 || **p
== ' ') {
485 *p
+= strspn(*p
, " ");
486 return l
< 0 ? 0 : -EINVAL
;
493 static int parse_component_decimal(const char **p
, bool usec
, unsigned long *res
) {
495 const char *e
= NULL
;
503 value
= strtoul(*p
, &ee
, 10);
508 if ((unsigned long) (int) value
!= value
)
513 if (value
* USEC_PER_SEC
/ USEC_PER_SEC
!= value
)
516 value
*= USEC_PER_SEC
;
520 /* This is the start of a range, not a fractional part */
525 r
= parse_fractional_part_u(&e
, 6, &add
);
529 if (add
+ value
< value
)
542 static int const_chain(int value
, CalendarComponent
**c
) {
543 CalendarComponent
*cc
= NULL
;
547 cc
= new0(CalendarComponent
, 1);
561 static int prepend_component(const char **p
, bool usec
, CalendarComponent
**c
) {
562 unsigned long value
, range_end
= -1, repeat
= 0;
563 CalendarComponent
*cc
;
572 r
= parse_component_decimal(&e
, usec
, &value
);
576 if (e
[0] == '.' && e
[1] == '.') {
578 r
= parse_component_decimal(&e
, usec
, &range_end
);
582 repeat
= usec
? USEC_PER_SEC
: 1;
587 r
= parse_component_decimal(&e
, usec
, &repeat
);
595 if (*e
!= 0 && *e
!= ' ' && *e
!= ',' && *e
!= '-' && *e
!= '~' && *e
!= ':')
598 cc
= new0(CalendarComponent
, 1);
603 cc
->range_end
= range_end
;
612 return prepend_component(p
, usec
, c
);
618 static int parse_chain(const char **p
, bool usec
, CalendarComponent
**c
) {
620 CalendarComponent
*cc
= NULL
;
630 r
= const_chain(0, c
);
633 (*c
)->repeat
= USEC_PER_SEC
;
641 r
= prepend_component(&t
, usec
, &cc
);
652 static int parse_date(const char **p
, CalendarSpec
*c
) {
655 CalendarComponent
*first
, *second
, *third
;
666 r
= parse_chain(&t
, false, &first
);
670 /* Already the end? A ':' as separator? In that case this was a time, not a date */
671 if (*t
== 0 || *t
== ':') {
677 c
->end_of_month
= true;
678 else if (*t
!= '-') {
684 r
= parse_chain(&t
, false, &second
);
690 /* Got two parts, hence it's month and day */
691 if (*t
== ' ' || *t
== 0) {
692 *p
= t
+ strspn(t
, " ");
696 } else if (c
->end_of_month
) {
703 c
->end_of_month
= true;
704 else if (*t
!= '-') {
711 r
= parse_chain(&t
, false, &third
);
718 /* Got three parts, hence it is year, month and day */
719 if (*t
== ' ' || *t
== 0) {
720 *p
= t
+ strspn(t
, " ");
733 static int parse_calendar_time(const char **p
, CalendarSpec
*c
) {
734 CalendarComponent
*h
= NULL
, *m
= NULL
, *s
= NULL
;
744 /* If no time is specified at all, then this means 00:00:00 */
748 r
= parse_chain(&t
, false, &h
);
758 r
= parse_chain(&t
, false, &m
);
762 /* Already at the end? Then it's hours and minutes, and seconds are 0 */
772 r
= parse_chain(&t
, true, &s
);
776 /* At the end? Then it's hours, minutes and seconds */
784 r
= const_chain(0, &h
);
788 r
= const_chain(0, &m
);
793 r
= const_chain(0, &s
);
812 int calendar_spec_from_string(const char *p
, CalendarSpec
**spec
) {
820 c
= new0(CalendarSpec
, 1);
825 utc
= endswith_no_case(p
, " UTC");
828 p
= strndupa(p
, utc
- p
);
830 const char *e
= NULL
;
835 /* Check if the local timezone was specified? */
836 for (j
= 0; j
<= 1; j
++) {
837 if (isempty(tzname
[j
]))
840 e
= endswith_no_case(p
, tzname
[j
]);
851 /* Found one of the two timezones specified? */
852 if (IN_SET(j
, 0, 1)) {
853 p
= strndupa(p
, e
- p
- 1);
863 if (strcaseeq(p
, "minutely")) {
864 r
= const_chain(0, &c
->microsecond
);
868 } else if (strcaseeq(p
, "hourly")) {
869 r
= const_chain(0, &c
->minute
);
872 r
= const_chain(0, &c
->microsecond
);
876 } else if (strcaseeq(p
, "daily")) {
877 r
= const_chain(0, &c
->hour
);
880 r
= const_chain(0, &c
->minute
);
883 r
= const_chain(0, &c
->microsecond
);
887 } else if (strcaseeq(p
, "monthly")) {
888 r
= const_chain(1, &c
->day
);
891 r
= const_chain(0, &c
->hour
);
894 r
= const_chain(0, &c
->minute
);
897 r
= const_chain(0, &c
->microsecond
);
901 } else if (strcaseeq(p
, "annually") ||
902 strcaseeq(p
, "yearly") ||
903 strcaseeq(p
, "anually") /* backwards compatibility */ ) {
905 r
= const_chain(1, &c
->month
);
908 r
= const_chain(1, &c
->day
);
911 r
= const_chain(0, &c
->hour
);
914 r
= const_chain(0, &c
->minute
);
917 r
= const_chain(0, &c
->microsecond
);
921 } else if (strcaseeq(p
, "weekly")) {
923 c
->weekdays_bits
= 1;
925 r
= const_chain(0, &c
->hour
);
928 r
= const_chain(0, &c
->minute
);
931 r
= const_chain(0, &c
->microsecond
);
935 } else if (strcaseeq(p
, "quarterly")) {
937 r
= const_chain(1, &c
->month
);
940 r
= const_chain(4, &c
->month
);
943 r
= const_chain(7, &c
->month
);
946 r
= const_chain(10, &c
->month
);
949 r
= const_chain(1, &c
->day
);
952 r
= const_chain(0, &c
->hour
);
955 r
= const_chain(0, &c
->minute
);
958 r
= const_chain(0, &c
->microsecond
);
962 } else if (strcaseeq(p
, "biannually") ||
963 strcaseeq(p
, "bi-annually") ||
964 strcaseeq(p
, "semiannually") ||
965 strcaseeq(p
, "semi-annually")) {
967 r
= const_chain(1, &c
->month
);
970 r
= const_chain(7, &c
->month
);
973 r
= const_chain(1, &c
->day
);
976 r
= const_chain(0, &c
->hour
);
979 r
= const_chain(0, &c
->minute
);
982 r
= const_chain(0, &c
->microsecond
);
987 r
= parse_weekdays(&p
, c
);
991 r
= parse_date(&p
, c
);
995 r
= parse_calendar_time(&p
, c
);
1005 r
= calendar_spec_normalize(c
);
1009 if (!calendar_spec_valid(c
)) {
1018 calendar_spec_free(c
);
1022 static int find_end_of_month(struct tm
*tm
, bool utc
, int day
)
1027 t
.tm_mday
= 1 - day
;
1029 if (mktime_or_timegm(&t
, utc
) == (time_t) -1 ||
1030 t
.tm_mon
!= tm
->tm_mon
)
1036 static int find_matching_component(const CalendarSpec
*spec
, const CalendarComponent
*c
,
1037 struct tm
*tm
, int *val
) {
1038 const CalendarComponent
*n
, *p
= c
;
1054 if (spec
->end_of_month
&& p
== spec
->day
) {
1055 v
= find_end_of_month(tm
, spec
->utc
, v
);
1056 e
= find_end_of_month(tm
, spec
->utc
, e
);
1064 if (!d_set
|| v
< d
) {
1069 } else if (c
->repeat
> 0) {
1072 k
= v
+ c
->repeat
* ((*val
- v
+ c
->repeat
- 1) / c
->repeat
);
1074 if ((!d_set
|| k
< d
) && (e
< 0 || k
<= e
)) {
1091 static bool tm_out_of_bounds(const struct tm
*tm
, bool utc
) {
1097 if (mktime_or_timegm(&t
, utc
) == (time_t) -1)
1101 * Set an upper bound on the year so impossible dates like "*-02-31"
1102 * don't cause find_next() to loop forever. tm_year contains years
1103 * since 1900, so adjust it accordingly.
1105 if (tm
->tm_year
+ 1900 > MAX_YEAR
)
1108 /* Did any normalization take place? If so, it was out of bounds before */
1110 t
.tm_year
!= tm
->tm_year
||
1111 t
.tm_mon
!= tm
->tm_mon
||
1112 t
.tm_mday
!= tm
->tm_mday
||
1113 t
.tm_hour
!= tm
->tm_hour
||
1114 t
.tm_min
!= tm
->tm_min
||
1115 t
.tm_sec
!= tm
->tm_sec
;
1118 static bool matches_weekday(int weekdays_bits
, const struct tm
*tm
, bool utc
) {
1122 if (weekdays_bits
< 0 || weekdays_bits
>= BITS_WEEKDAYS
)
1126 if (mktime_or_timegm(&t
, utc
) == (time_t) -1)
1129 k
= t
.tm_wday
== 0 ? 6 : t
.tm_wday
- 1;
1130 return (weekdays_bits
& (1 << k
));
1133 static int find_next(const CalendarSpec
*spec
, struct tm
*tm
, usec_t
*usec
) {
1145 /* Normalize the current date */
1146 (void) mktime_or_timegm(&c
, spec
->utc
);
1147 c
.tm_isdst
= spec
->dst
;
1150 r
= find_matching_component(spec
, spec
->year
, &c
, &c
.tm_year
);
1156 c
.tm_hour
= c
.tm_min
= c
.tm_sec
= tm_usec
= 0;
1160 if (tm_out_of_bounds(&c
, spec
->utc
))
1164 r
= find_matching_component(spec
, spec
->month
, &c
, &c
.tm_mon
);
1169 c
.tm_hour
= c
.tm_min
= c
.tm_sec
= tm_usec
= 0;
1171 if (r
< 0 || tm_out_of_bounds(&c
, spec
->utc
)) {
1175 c
.tm_hour
= c
.tm_min
= c
.tm_sec
= tm_usec
= 0;
1179 r
= find_matching_component(spec
, spec
->day
, &c
, &c
.tm_mday
);
1181 c
.tm_hour
= c
.tm_min
= c
.tm_sec
= tm_usec
= 0;
1182 if (r
< 0 || tm_out_of_bounds(&c
, spec
->utc
)) {
1185 c
.tm_hour
= c
.tm_min
= c
.tm_sec
= tm_usec
= 0;
1189 if (!matches_weekday(spec
->weekdays_bits
, &c
, spec
->utc
)) {
1191 c
.tm_hour
= c
.tm_min
= c
.tm_sec
= tm_usec
= 0;
1195 r
= find_matching_component(spec
, spec
->hour
, &c
, &c
.tm_hour
);
1197 c
.tm_min
= c
.tm_sec
= tm_usec
= 0;
1198 if (r
< 0 || tm_out_of_bounds(&c
, spec
->utc
)) {
1200 c
.tm_hour
= c
.tm_min
= c
.tm_sec
= tm_usec
= 0;
1204 r
= find_matching_component(spec
, spec
->minute
, &c
, &c
.tm_min
);
1206 c
.tm_sec
= tm_usec
= 0;
1207 if (r
< 0 || tm_out_of_bounds(&c
, spec
->utc
)) {
1209 c
.tm_min
= c
.tm_sec
= tm_usec
= 0;
1213 c
.tm_sec
= c
.tm_sec
* USEC_PER_SEC
+ tm_usec
;
1214 r
= find_matching_component(spec
, spec
->microsecond
, &c
, &c
.tm_sec
);
1215 tm_usec
= c
.tm_sec
% USEC_PER_SEC
;
1216 c
.tm_sec
/= USEC_PER_SEC
;
1218 if (r
< 0 || tm_out_of_bounds(&c
, spec
->utc
)) {
1220 c
.tm_sec
= tm_usec
= 0;
1230 int calendar_spec_next_usec(const CalendarSpec
*spec
, usec_t usec
, usec_t
*next
) {
1240 t
= (time_t) (usec
/ USEC_PER_SEC
);
1241 assert_se(localtime_or_gmtime_r(&t
, &tm
, spec
->utc
));
1242 tm_usec
= usec
% USEC_PER_SEC
;
1244 r
= find_next(spec
, &tm
, &tm_usec
);
1248 t
= mktime_or_timegm(&tm
, spec
->utc
);
1249 if (t
== (time_t) -1)
1252 *next
= (usec_t
) t
* USEC_PER_SEC
+ tm_usec
;