]>
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
)->start
< (*b
)->start
)
70 if ((*a
)->start
> (*b
)->start
)
73 if ((*a
)->stop
< (*b
)->stop
)
75 if ((*a
)->stop
> (*b
)->stop
)
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 `stop`
97 * so the length of the range is a multiple of `repeat`
99 if (i
->stop
> i
->start
)
100 i
->stop
-= (i
->stop
- i
->start
) % 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]->start
== next
->start
&&
119 b
[k
-1]->repeat
== next
->repeat
) {
131 static void fix_year(CalendarComponent
*c
) {
132 /* Turns 12 → 2012, 89 → 1989 */
135 if (c
->start
>= 0 && c
->start
< 70)
138 if (c
->stop
>= 0 && c
->stop
< 70)
141 if (c
->start
>= 70 && c
->start
< 100)
144 if (c
->stop
>= 70 && c
->stop
< 100)
151 int calendar_spec_normalize(CalendarSpec
*c
) {
154 if (c
->weekdays_bits
<= 0 || c
->weekdays_bits
>= BITS_WEEKDAYS
)
155 c
->weekdays_bits
= -1;
157 if (c
->end_of_month
&& !c
->day
)
158 c
->end_of_month
= false;
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
);
172 _pure_
static bool chain_valid(CalendarComponent
*c
, int from
, int to
, bool end_of_month
) {
176 /* Forbid dates more than 28 days from the end of the month */
180 if (c
->start
< from
|| c
->start
> to
)
184 * c->repeat must be short enough so at least one repetition may
185 * occur before the end of the interval. For dates scheduled
186 * relative to the end of the month, c->start and c->stop
187 * correspond to the Nth last day of the month.
190 if (c
->stop
< from
|| c
->stop
> to
)
193 if (c
->start
+ c
->repeat
> c
->stop
)
196 if (end_of_month
&& c
->start
- c
->repeat
< from
)
199 if (!end_of_month
&& c
->start
+ c
->repeat
> to
)
204 return chain_valid(c
->next
, from
, to
, end_of_month
);
209 _pure_
bool calendar_spec_valid(CalendarSpec
*c
) {
212 if (c
->weekdays_bits
> BITS_WEEKDAYS
)
215 if (!chain_valid(c
->year
, MIN_YEAR
, MAX_YEAR
, false))
218 if (!chain_valid(c
->month
, 1, 12, false))
221 if (!chain_valid(c
->day
, 1, 31, c
->end_of_month
))
224 if (!chain_valid(c
->hour
, 0, 23, false))
227 if (!chain_valid(c
->minute
, 0, 59, false))
230 if (!chain_valid(c
->microsecond
, 0, 60*USEC_PER_SEC
-1, false))
236 static void format_weekdays(FILE *f
, const CalendarSpec
*c
) {
237 static const char *const days
[] = {
248 bool need_comma
= false;
252 assert(c
->weekdays_bits
> 0 && c
->weekdays_bits
<= BITS_WEEKDAYS
);
254 for (x
= 0, l
= -1; x
< (int) ELEMENTSOF(days
); x
++) {
256 if (c
->weekdays_bits
& (1 << x
)) {
271 fputs(x
> l
+ 2 ? ".." : ",", f
);
279 if (l
>= 0 && x
> l
+ 1) {
280 fputs(x
> l
+ 2 ? ".." : ",", f
);
285 static void format_chain(FILE *f
, int space
, const CalendarComponent
*c
, bool usec
) {
286 int d
= usec
? (int) USEC_PER_SEC
: 1;
295 if (usec
&& c
->start
== 0 && c
->repeat
== USEC_PER_SEC
&& !c
->next
) {
300 assert(c
->start
>= 0);
302 fprintf(f
, "%0*i", space
, c
->start
/ d
);
303 if (c
->start
% d
> 0)
304 fprintf(f
, ".%06i", c
->start
% d
);
307 fprintf(f
, "..%0*i", space
, c
->stop
/ d
);
309 fprintf(f
, ".%06i", c
->stop
% d
);
311 if (c
->repeat
> 0 && !(c
->stop
> 0 && c
->repeat
== d
))
312 fprintf(f
, "/%i", c
->repeat
/ d
);
313 if (c
->repeat
% d
> 0)
314 fprintf(f
, ".%06i", c
->repeat
% d
);
318 format_chain(f
, space
, c
->next
, usec
);
322 int calendar_spec_to_string(const CalendarSpec
*c
, char **p
) {
331 f
= open_memstream(&buf
, &sz
);
335 if (c
->weekdays_bits
> 0 && c
->weekdays_bits
<= BITS_WEEKDAYS
) {
336 format_weekdays(f
, c
);
340 format_chain(f
, 4, c
->year
, false);
342 format_chain(f
, 2, c
->month
, false);
343 fputc(c
->end_of_month
? '~' : '-', f
);
344 format_chain(f
, 2, c
->day
, false);
346 format_chain(f
, 2, c
->hour
, false);
348 format_chain(f
, 2, c
->minute
, false);
350 format_chain(f
, 2, c
->microsecond
, true);
354 else if (IN_SET(c
->dst
, 0, 1)) {
356 /* If daylight saving is explicitly on or off, let's show the used timezone. */
360 if (!isempty(tzname
[c
->dst
])) {
362 fputs(tzname
[c
->dst
], f
);
366 r
= fflush_and_check(f
);
379 static int parse_weekdays(const char **p
, CalendarSpec
*c
) {
380 static const struct {
410 for (i
= 0; i
< ELEMENTSOF(day_nr
); i
++) {
413 if (!startswith_no_case(*p
, day_nr
[i
].name
))
416 skip
= strlen(day_nr
[i
].name
);
418 if ((*p
)[skip
] != '-' &&
425 c
->weekdays_bits
|= 1 << day_nr
[i
].nr
;
430 if (l
> day_nr
[i
].nr
)
433 for (j
= l
+ 1; j
< day_nr
[i
].nr
; j
++)
434 c
->weekdays_bits
|= 1 << j
;
441 /* Couldn't find this prefix, so let's assume the
442 weekday was not specified and let's continue with
444 if (i
>= ELEMENTSOF(day_nr
))
445 return first
? 0 : -EINVAL
;
447 /* We reached the end of the string */
451 /* We reached the end of the weekday spec part */
453 *p
+= strspn(*p
, " ");
467 /* Support ranges with "-" for backwards compatibility */
468 } else if (**p
== '-') {
475 } else if (**p
== ',') {
480 /* Allow a trailing comma but not an open range */
481 if (**p
== 0 || **p
== ' ') {
482 *p
+= strspn(*p
, " ");
483 return l
< 0 ? 0 : -EINVAL
;
490 static int parse_component_decimal(const char **p
, bool usec
, unsigned long *res
) {
492 const char *e
= NULL
;
500 value
= strtoul(*p
, &ee
, 10);
505 if ((unsigned long) (int) value
!= value
)
510 if (value
* USEC_PER_SEC
/ USEC_PER_SEC
!= value
)
513 value
*= USEC_PER_SEC
;
517 /* This is the start of a range, not a fractional part */
522 r
= parse_fractional_part_u(&e
, 6, &add
);
526 if (add
+ value
< value
)
539 static int const_chain(int value
, CalendarComponent
**c
) {
540 CalendarComponent
*cc
= NULL
;
544 cc
= new0(CalendarComponent
, 1);
558 static int prepend_component(const char **p
, bool usec
, CalendarComponent
**c
) {
559 unsigned long start
, stop
= -1, repeat
= 0;
560 CalendarComponent
*cc
;
569 r
= parse_component_decimal(&e
, usec
, &start
);
573 if (e
[0] == '.' && e
[1] == '.') {
575 r
= parse_component_decimal(&e
, usec
, &stop
);
579 repeat
= usec
? USEC_PER_SEC
: 1;
584 r
= parse_component_decimal(&e
, usec
, &repeat
);
592 if (*e
!= 0 && *e
!= ' ' && *e
!= ',' && *e
!= '-' && *e
!= '~' && *e
!= ':')
595 cc
= new0(CalendarComponent
, 1);
609 return prepend_component(p
, usec
, c
);
615 static int parse_chain(const char **p
, bool usec
, CalendarComponent
**c
) {
617 CalendarComponent
*cc
= NULL
;
627 r
= const_chain(0, c
);
630 (*c
)->repeat
= USEC_PER_SEC
;
638 r
= prepend_component(&t
, usec
, &cc
);
649 static int parse_date(const char **p
, CalendarSpec
*c
) {
652 CalendarComponent
*first
, *second
, *third
;
663 r
= parse_chain(&t
, false, &first
);
667 /* Already the end? A ':' as separator? In that case this was a time, not a date */
668 if (*t
== 0 || *t
== ':') {
674 c
->end_of_month
= true;
675 else if (*t
!= '-') {
681 r
= parse_chain(&t
, false, &second
);
687 /* Got two parts, hence it's month and day */
688 if (*t
== ' ' || *t
== 0) {
689 *p
= t
+ strspn(t
, " ");
693 } else if (c
->end_of_month
) {
700 c
->end_of_month
= true;
701 else if (*t
!= '-') {
708 r
= parse_chain(&t
, false, &third
);
715 /* Got three parts, hence it is year, month and day */
716 if (*t
== ' ' || *t
== 0) {
717 *p
= t
+ strspn(t
, " ");
730 static int parse_calendar_time(const char **p
, CalendarSpec
*c
) {
731 CalendarComponent
*h
= NULL
, *m
= NULL
, *s
= NULL
;
741 /* If no time is specified at all, then this means 00:00:00 */
745 r
= parse_chain(&t
, false, &h
);
755 r
= parse_chain(&t
, false, &m
);
759 /* Already at the end? Then it's hours and minutes, and seconds are 0 */
769 r
= parse_chain(&t
, true, &s
);
773 /* At the end? Then it's hours, minutes and seconds */
781 r
= const_chain(0, &h
);
785 r
= const_chain(0, &m
);
790 r
= const_chain(0, &s
);
809 int calendar_spec_from_string(const char *p
, CalendarSpec
**spec
) {
817 c
= new0(CalendarSpec
, 1);
822 utc
= endswith_no_case(p
, " UTC");
825 p
= strndupa(p
, utc
- p
);
827 const char *e
= NULL
;
832 /* Check if the local timezone was specified? */
833 for (j
= 0; j
<= 1; j
++) {
834 if (isempty(tzname
[j
]))
837 e
= endswith_no_case(p
, tzname
[j
]);
848 /* Found one of the two timezones specified? */
849 if (IN_SET(j
, 0, 1)) {
850 p
= strndupa(p
, e
- p
- 1);
860 if (strcaseeq(p
, "minutely")) {
861 r
= const_chain(0, &c
->microsecond
);
865 } else if (strcaseeq(p
, "hourly")) {
866 r
= const_chain(0, &c
->minute
);
869 r
= const_chain(0, &c
->microsecond
);
873 } else if (strcaseeq(p
, "daily")) {
874 r
= const_chain(0, &c
->hour
);
877 r
= const_chain(0, &c
->minute
);
880 r
= const_chain(0, &c
->microsecond
);
884 } else if (strcaseeq(p
, "monthly")) {
885 r
= const_chain(1, &c
->day
);
888 r
= const_chain(0, &c
->hour
);
891 r
= const_chain(0, &c
->minute
);
894 r
= const_chain(0, &c
->microsecond
);
898 } else if (strcaseeq(p
, "annually") ||
899 strcaseeq(p
, "yearly") ||
900 strcaseeq(p
, "anually") /* backwards compatibility */ ) {
902 r
= const_chain(1, &c
->month
);
905 r
= const_chain(1, &c
->day
);
908 r
= const_chain(0, &c
->hour
);
911 r
= const_chain(0, &c
->minute
);
914 r
= const_chain(0, &c
->microsecond
);
918 } else if (strcaseeq(p
, "weekly")) {
920 c
->weekdays_bits
= 1;
922 r
= const_chain(0, &c
->hour
);
925 r
= const_chain(0, &c
->minute
);
928 r
= const_chain(0, &c
->microsecond
);
932 } else if (strcaseeq(p
, "quarterly")) {
934 r
= const_chain(1, &c
->month
);
937 r
= const_chain(4, &c
->month
);
940 r
= const_chain(7, &c
->month
);
943 r
= const_chain(10, &c
->month
);
946 r
= const_chain(1, &c
->day
);
949 r
= const_chain(0, &c
->hour
);
952 r
= const_chain(0, &c
->minute
);
955 r
= const_chain(0, &c
->microsecond
);
959 } else if (strcaseeq(p
, "biannually") ||
960 strcaseeq(p
, "bi-annually") ||
961 strcaseeq(p
, "semiannually") ||
962 strcaseeq(p
, "semi-annually")) {
964 r
= const_chain(1, &c
->month
);
967 r
= const_chain(7, &c
->month
);
970 r
= const_chain(1, &c
->day
);
973 r
= const_chain(0, &c
->hour
);
976 r
= const_chain(0, &c
->minute
);
979 r
= const_chain(0, &c
->microsecond
);
984 r
= parse_weekdays(&p
, c
);
988 r
= parse_date(&p
, c
);
992 r
= parse_calendar_time(&p
, c
);
1002 r
= calendar_spec_normalize(c
);
1006 if (!calendar_spec_valid(c
)) {
1015 calendar_spec_free(c
);
1019 static int find_end_of_month(struct tm
*tm
, bool utc
, int day
)
1024 t
.tm_mday
= 1 - day
;
1026 if (mktime_or_timegm(&t
, utc
) == (time_t) -1 ||
1027 t
.tm_mon
!= tm
->tm_mon
)
1033 static int find_matching_component(const CalendarSpec
*spec
, const CalendarComponent
*c
,
1034 struct tm
*tm
, int *val
) {
1035 const CalendarComponent
*p
= c
;
1036 int start
, stop
, d
= -1;
1049 if (spec
->end_of_month
&& p
== spec
->day
) {
1050 start
= find_end_of_month(tm
, spec
->utc
, start
);
1051 stop
= find_end_of_month(tm
, spec
->utc
, stop
);
1054 SWAP_TWO(start
, stop
);
1057 if (start
>= *val
) {
1059 if (!d_set
|| start
< d
) {
1064 } else if (c
->repeat
> 0) {
1067 k
= start
+ c
->repeat
* ((*val
- start
+ c
->repeat
- 1) / c
->repeat
);
1069 if ((!d_set
|| k
< d
) && (stop
< 0 || k
<= stop
)) {
1086 static bool tm_out_of_bounds(const struct tm
*tm
, bool utc
) {
1092 if (mktime_or_timegm(&t
, utc
) == (time_t) -1)
1096 * Set an upper bound on the year so impossible dates like "*-02-31"
1097 * don't cause find_next() to loop forever. tm_year contains years
1098 * since 1900, so adjust it accordingly.
1100 if (tm
->tm_year
+ 1900 > MAX_YEAR
)
1103 /* Did any normalization take place? If so, it was out of bounds before */
1105 t
.tm_year
!= tm
->tm_year
||
1106 t
.tm_mon
!= tm
->tm_mon
||
1107 t
.tm_mday
!= tm
->tm_mday
||
1108 t
.tm_hour
!= tm
->tm_hour
||
1109 t
.tm_min
!= tm
->tm_min
||
1110 t
.tm_sec
!= tm
->tm_sec
;
1113 static bool matches_weekday(int weekdays_bits
, const struct tm
*tm
, bool utc
) {
1117 if (weekdays_bits
< 0 || weekdays_bits
>= BITS_WEEKDAYS
)
1121 if (mktime_or_timegm(&t
, utc
) == (time_t) -1)
1124 k
= t
.tm_wday
== 0 ? 6 : t
.tm_wday
- 1;
1125 return (weekdays_bits
& (1 << k
));
1128 static int find_next(const CalendarSpec
*spec
, struct tm
*tm
, usec_t
*usec
) {
1140 /* Normalize the current date */
1141 (void) mktime_or_timegm(&c
, spec
->utc
);
1142 c
.tm_isdst
= spec
->dst
;
1145 r
= find_matching_component(spec
, spec
->year
, &c
, &c
.tm_year
);
1151 c
.tm_hour
= c
.tm_min
= c
.tm_sec
= tm_usec
= 0;
1155 if (tm_out_of_bounds(&c
, spec
->utc
))
1159 r
= find_matching_component(spec
, spec
->month
, &c
, &c
.tm_mon
);
1164 c
.tm_hour
= c
.tm_min
= c
.tm_sec
= tm_usec
= 0;
1166 if (r
< 0 || tm_out_of_bounds(&c
, spec
->utc
)) {
1170 c
.tm_hour
= c
.tm_min
= c
.tm_sec
= tm_usec
= 0;
1174 r
= find_matching_component(spec
, spec
->day
, &c
, &c
.tm_mday
);
1176 c
.tm_hour
= c
.tm_min
= c
.tm_sec
= tm_usec
= 0;
1177 if (r
< 0 || tm_out_of_bounds(&c
, spec
->utc
)) {
1180 c
.tm_hour
= c
.tm_min
= c
.tm_sec
= tm_usec
= 0;
1184 if (!matches_weekday(spec
->weekdays_bits
, &c
, spec
->utc
)) {
1186 c
.tm_hour
= c
.tm_min
= c
.tm_sec
= tm_usec
= 0;
1190 r
= find_matching_component(spec
, spec
->hour
, &c
, &c
.tm_hour
);
1192 c
.tm_min
= c
.tm_sec
= tm_usec
= 0;
1193 if (r
< 0 || tm_out_of_bounds(&c
, spec
->utc
)) {
1195 c
.tm_hour
= c
.tm_min
= c
.tm_sec
= tm_usec
= 0;
1199 r
= find_matching_component(spec
, spec
->minute
, &c
, &c
.tm_min
);
1201 c
.tm_sec
= tm_usec
= 0;
1202 if (r
< 0 || tm_out_of_bounds(&c
, spec
->utc
)) {
1204 c
.tm_min
= c
.tm_sec
= tm_usec
= 0;
1208 c
.tm_sec
= c
.tm_sec
* USEC_PER_SEC
+ tm_usec
;
1209 r
= find_matching_component(spec
, spec
->microsecond
, &c
, &c
.tm_sec
);
1210 tm_usec
= c
.tm_sec
% USEC_PER_SEC
;
1211 c
.tm_sec
/= USEC_PER_SEC
;
1213 if (r
< 0 || tm_out_of_bounds(&c
, spec
->utc
)) {
1215 c
.tm_sec
= tm_usec
= 0;
1225 int calendar_spec_next_usec(const CalendarSpec
*spec
, usec_t usec
, usec_t
*next
) {
1235 t
= (time_t) (usec
/ USEC_PER_SEC
);
1236 assert_se(localtime_or_gmtime_r(&t
, &tm
, spec
->utc
));
1237 tm_usec
= usec
% USEC_PER_SEC
;
1239 r
= find_next(spec
, &tm
, &tm_usec
);
1243 t
= mktime_or_timegm(&tm
, spec
->utc
);
1244 if (t
== (time_t) -1)
1247 *next
= (usec_t
) t
* USEC_PER_SEC
+ tm_usec
;