]>
Commit | Line | Data |
---|---|---|
db9ecf05 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
36697dc0 | 2 | |
b5efdb8a | 3 | #include "alloc-util.h" |
36697dc0 | 4 | #include "calendarspec.h" |
f035bb1b | 5 | #include "env-util.h" |
4bbccb02 | 6 | #include "errno-util.h" |
07630cea | 7 | #include "string-util.h" |
36697dc0 | 8 | |
f035bb1b | 9 | static void _test_one(int line, const char *input, const char *output) { |
36697dc0 | 10 | CalendarSpec *c; |
aaa87092 | 11 | _cleanup_free_ char *p = NULL, *q = NULL; |
36697dc0 | 12 | usec_t u; |
36697dc0 LP |
13 | int r; |
14 | ||
15 | assert_se(calendar_spec_from_string(input, &c) >= 0); | |
16 | ||
17 | assert_se(calendar_spec_to_string(c, &p) >= 0); | |
f035bb1b | 18 | log_info("line %d: \"%s\" → \"%s\"", line, input, p); |
36697dc0 LP |
19 | |
20 | assert_se(streq(p, output)); | |
36697dc0 LP |
21 | |
22 | u = now(CLOCK_REALTIME); | |
23 | r = calendar_spec_next_usec(c, u, &u); | |
04f5c018 | 24 | log_info("Next: %s", r < 0 ? strerror_safe(r) : FORMAT_TIMESTAMP(u)); |
aaa87092 | 25 | calendar_spec_free(c); |
36697dc0 | 26 | |
aaa87092 LP |
27 | assert_se(calendar_spec_from_string(p, &c) >= 0); |
28 | assert_se(calendar_spec_to_string(c, &q) >= 0); | |
36697dc0 | 29 | calendar_spec_free(c); |
aaa87092 LP |
30 | |
31 | assert_se(streq(q, p)); | |
36697dc0 | 32 | } |
f035bb1b | 33 | #define test_one(input, output) _test_one(__LINE__, input, output) |
36697dc0 | 34 | |
f035bb1b | 35 | static void _test_next(int line, const char *input, const char *new_tz, usec_t after, usec_t expect) { |
9d5bd70d HV |
36 | CalendarSpec *c; |
37 | usec_t u; | |
38 | char *old_tz; | |
9d5bd70d HV |
39 | int r; |
40 | ||
41 | old_tz = getenv("TZ"); | |
42 | if (old_tz) | |
43 | old_tz = strdupa(old_tz); | |
44 | ||
47b0b657 | 45 | if (!isempty(new_tz)) |
f035bb1b | 46 | new_tz = strjoina(":", new_tz); |
437f48a4 | 47 | |
f035bb1b | 48 | assert_se(set_unset_env("TZ", new_tz, true) == 0); |
9d5bd70d HV |
49 | tzset(); |
50 | ||
51 | assert_se(calendar_spec_from_string(input, &c) >= 0); | |
52 | ||
f035bb1b | 53 | log_info("line %d: \"%s\" new_tz=%s", line, input, strnull(new_tz)); |
9d5bd70d HV |
54 | |
55 | u = after; | |
56 | r = calendar_spec_next_usec(c, after, &u); | |
0086ef19 | 57 | log_info("At: %s", r < 0 ? strerror_safe(r) : FORMAT_TIMESTAMP_STYLE(u, TIMESTAMP_US)); |
f5fbe71d | 58 | if (expect != USEC_INFINITY) |
9d5bd70d HV |
59 | assert_se(r >= 0 && u == expect); |
60 | else | |
61 | assert(r == -ENOENT); | |
62 | ||
63 | calendar_spec_free(c); | |
64 | ||
f035bb1b | 65 | assert_se(set_unset_env("TZ", old_tz, true) == 0); |
9d5bd70d HV |
66 | tzset(); |
67 | } | |
f035bb1b | 68 | #define test_next(input, new_tz, after, expect) _test_next(__LINE__, input,new_tz,after,expect) |
9d5bd70d | 69 | |
21b3a0fc LP |
70 | static void test_timestamp(void) { |
71 | char buf[FORMAT_TIMESTAMP_MAX]; | |
72 | _cleanup_free_ char *t = NULL; | |
73 | CalendarSpec *c; | |
74 | usec_t x, y; | |
75 | ||
76 | /* Ensure that a timestamp is also a valid calendar specification. Convert forth and back */ | |
77 | ||
78 | x = now(CLOCK_REALTIME); | |
79 | ||
f035bb1b ZJS |
80 | assert_se(format_timestamp_style(buf, sizeof buf, x, TIMESTAMP_US)); |
81 | log_info("%s", buf); | |
21b3a0fc LP |
82 | assert_se(calendar_spec_from_string(buf, &c) >= 0); |
83 | assert_se(calendar_spec_to_string(c, &t) >= 0); | |
84 | calendar_spec_free(c); | |
f035bb1b | 85 | log_info("%s", t); |
21b3a0fc LP |
86 | |
87 | assert_se(parse_timestamp(t, &y) >= 0); | |
88 | assert_se(y == x); | |
89 | } | |
90 | ||
250517d2 ZJS |
91 | static void test_hourly_bug_4031(void) { |
92 | CalendarSpec *c; | |
93 | usec_t n, u, w; | |
250517d2 ZJS |
94 | int r; |
95 | ||
96 | assert_se(calendar_spec_from_string("hourly", &c) >= 0); | |
97 | n = now(CLOCK_REALTIME); | |
98 | assert_se((r = calendar_spec_next_usec(c, n, &u)) >= 0); | |
99 | ||
0086ef19 ZJS |
100 | log_info("Now: %s (%"PRIu64")", FORMAT_TIMESTAMP_STYLE(n, TIMESTAMP_US), n); |
101 | log_info("Next hourly: %s (%"PRIu64")", r < 0 ? strerror_safe(r) : FORMAT_TIMESTAMP_STYLE(u, TIMESTAMP_US), u); | |
250517d2 ZJS |
102 | |
103 | assert_se((r = calendar_spec_next_usec(c, u, &w)) >= 0); | |
0086ef19 | 104 | log_info("Next hourly: %s (%"PRIu64")", r < 0 ? strerror_safe(r) : FORMAT_TIMESTAMP_STYLE(w, TIMESTAMP_US), w); |
250517d2 ZJS |
105 | |
106 | assert_se(n < u); | |
107 | assert_se(u <= n + USEC_PER_HOUR); | |
108 | assert_se(u < w); | |
109 | assert_se(w <= u + USEC_PER_HOUR); | |
a5d5c0d2 EV |
110 | |
111 | calendar_spec_free(c); | |
250517d2 ZJS |
112 | } |
113 | ||
36697dc0 LP |
114 | int main(int argc, char* argv[]) { |
115 | CalendarSpec *c; | |
116 | ||
e638d050 DC |
117 | test_one("Sat,Thu,Mon-Wed,Sat-Sun", "Mon..Thu,Sat,Sun *-*-* 00:00:00"); |
118 | test_one("Sat,Thu,Mon..Wed,Sat..Sun", "Mon..Thu,Sat,Sun *-*-* 00:00:00"); | |
36697dc0 LP |
119 | test_one("Mon,Sun 12-*-* 2,1:23", "Mon,Sun 2012-*-* 01,02:23:00"); |
120 | test_one("Wed *-1", "Wed *-*-01 00:00:00"); | |
121 | test_one("Wed-Wed,Wed *-1", "Wed *-*-01 00:00:00"); | |
e638d050 | 122 | test_one("Wed..Wed,Wed *-1", "Wed *-*-01 00:00:00"); |
36697dc0 | 123 | test_one("Wed, 17:48", "Wed *-*-* 17:48:00"); |
6bae2fd4 | 124 | test_one("Wednesday,", "Wed *-*-* 00:00:00"); |
e638d050 DC |
125 | test_one("Wed-Sat,Tue 12-10-15 1:2:3", "Tue..Sat 2012-10-15 01:02:03"); |
126 | test_one("Wed..Sat,Tue 12-10-15 1:2:3", "Tue..Sat 2012-10-15 01:02:03"); | |
36697dc0 LP |
127 | test_one("*-*-7 0:0:0", "*-*-07 00:00:00"); |
128 | test_one("10-15", "*-10-15 00:00:00"); | |
129 | test_one("monday *-12-* 17:00", "Mon *-12-* 17:00:00"); | |
a2eb5ea7 DC |
130 | test_one("Mon,Fri *-*-3,1,2 *:30:45", "Mon,Fri *-*-01,02,03 *:30:45"); |
131 | test_one("12,14,13,12:20,10,30", "*-*-* 12,13,14:10,20,30:00"); | |
36697dc0 LP |
132 | test_one("mon,fri *-1/2-1,3 *:30:45", "Mon,Fri *-01/2-01,03 *:30:45"); |
133 | test_one("03-05 08:05:40", "*-03-05 08:05:40"); | |
134 | test_one("08:05:40", "*-*-* 08:05:40"); | |
135 | test_one("05:40", "*-*-* 05:40:00"); | |
136 | test_one("Sat,Sun 12-05 08:05:40", "Sat,Sun *-12-05 08:05:40"); | |
137 | test_one("Sat,Sun 08:05:40", "Sat,Sun *-*-* 08:05:40"); | |
138 | test_one("2003-03-05 05:40", "2003-03-05 05:40:00"); | |
139 | test_one("2003-03-05", "2003-03-05 00:00:00"); | |
140 | test_one("03-05", "*-03-05 00:00:00"); | |
141 | test_one("hourly", "*-*-* *:00:00"); | |
142 | test_one("daily", "*-*-* 00:00:00"); | |
143 | test_one("monthly", "*-*-01 00:00:00"); | |
144 | test_one("weekly", "Mon *-*-* 00:00:00"); | |
dbfd41e2 LP |
145 | test_one("minutely", "*-*-* *:*:00"); |
146 | test_one("quarterly", "*-01,04,07,10-01 00:00:00"); | |
147 | test_one("semi-annually", "*-01,07-01 00:00:00"); | |
148 | test_one("annually", "*-01-01 00:00:00"); | |
36697dc0 | 149 | test_one("*:2/3", "*-*-* *:02/3:00"); |
9d5bd70d | 150 | test_one("2015-10-25 01:00:00 uTc", "2015-10-25 01:00:00 UTC"); |
48d26c01 IK |
151 | test_one("2015-10-25 01:00:00 Asia/Vladivostok", "2015-10-25 01:00:00 Asia/Vladivostok"); |
152 | test_one("weekly Pacific/Auckland", "Mon *-*-* 00:00:00 Pacific/Auckland"); | |
f22554a0 HV |
153 | test_one("2016-03-27 03:17:00.4200005", "2016-03-27 03:17:00.420001"); |
154 | test_one("2016-03-27 03:17:00/0.42", "2016-03-27 03:17:00/0.420000"); | |
9904dc00 DC |
155 | test_one("9..11,13:00,30", "*-*-* 09..11,13:00,30:00"); |
156 | test_one("1..3-1..3 1..3:1..3", "*-01..03-01..03 01..03:01..03:00"); | |
a2eb5ea7 | 157 | test_one("00:00:1.125..2.125", "*-*-* 00:00:01.125000..02.125000"); |
9904dc00 DC |
158 | test_one("00:00:1.0..3.8", "*-*-* 00:00:01..03"); |
159 | test_one("00:00:01..03", "*-*-* 00:00:01..03"); | |
a2eb5ea7 | 160 | test_one("00:00:01/2,02..03", "*-*-* 00:00:01/2,02..03"); |
8ea80351 DC |
161 | test_one("*-*~1 Utc", "*-*~01 00:00:00 UTC"); |
162 | test_one("*-*~05,3 ", "*-*~03,05 00:00:00"); | |
163 | test_one("*-*~* 00:00:00", "*-*-* 00:00:00"); | |
408a51e1 DC |
164 | test_one("Monday", "Mon *-*-* 00:00:00"); |
165 | test_one("Monday *-*-*", "Mon *-*-* 00:00:00"); | |
166 | test_one("*-*-*", "*-*-* 00:00:00"); | |
3215e35c | 167 | test_one("*:*:*", "*-*-* *:*:*"); |
c0df71fa DC |
168 | test_one("*:*", "*-*-* *:*:00"); |
169 | test_one("12:*", "*-*-* 12:*:00"); | |
170 | test_one("*:30", "*-*-* *:30:00"); | |
a2eb5ea7 DC |
171 | test_one("93..00-*-*", "1993..2000-*-* 00:00:00"); |
172 | test_one("00..07-*-*", "2000..2007-*-* 00:00:00"); | |
173 | test_one("*:20..39/5", "*-*-* *:20..35/5:00"); | |
174 | test_one("00:00:20..40/1", "*-*-* 00:00:20..40"); | |
963e3d83 | 175 | test_one("*~03/1,03..05", "*-*~03/1,03..05 00:00:00"); |
d80e5b74 ZJS |
176 | /* UNIX timestamps are always UTC */ |
177 | test_one("@1493187147", "2017-04-26 06:12:27 UTC"); | |
178 | test_one("@1493187147 UTC", "2017-04-26 06:12:27 UTC"); | |
179 | test_one("@0", "1970-01-01 00:00:00 UTC"); | |
180 | test_one("@0 UTC", "1970-01-01 00:00:00 UTC"); | |
c9c9f6f4 LP |
181 | test_one("*:05..05", "*-*-* *:05:00"); |
182 | test_one("*:05..10/6", "*-*-* *:05:00"); | |
9d5bd70d HV |
183 | |
184 | test_next("2016-03-27 03:17:00", "", 12345, 1459048620000000); | |
185 | test_next("2016-03-27 03:17:00", "CET", 12345, 1459041420000000); | |
186 | test_next("2016-03-27 03:17:00", "EET", 12345, -1); | |
187 | test_next("2016-03-27 03:17:00 UTC", NULL, 12345, 1459048620000000); | |
188 | test_next("2016-03-27 03:17:00 UTC", "", 12345, 1459048620000000); | |
189 | test_next("2016-03-27 03:17:00 UTC", "CET", 12345, 1459048620000000); | |
190 | test_next("2016-03-27 03:17:00 UTC", "EET", 12345, 1459048620000000); | |
f22554a0 HV |
191 | test_next("2016-03-27 03:17:00.420000001 UTC", "EET", 12345, 1459048620420000); |
192 | test_next("2016-03-27 03:17:00.4200005 UTC", "EET", 12345, 1459048620420001); | |
193 | test_next("2015-11-13 09:11:23.42", "EET", 12345, 1447398683420000); | |
194 | test_next("2015-11-13 09:11:23.42/1.77", "EET", 1447398683420000, 1447398685190000); | |
195 | test_next("2015-11-13 09:11:23.42/1.77", "EET", 1447398683419999, 1447398683420000); | |
a022d76e | 196 | test_next("Sun 16:00:00", "CET", 1456041600123456, 1456066800000000); |
f6e7d66b | 197 | test_next("*-04-31", "", 12345, -1); |
8ea80351 DC |
198 | test_next("2016-02~01 UTC", "", 12345, 1456704000000000); |
199 | test_next("Mon 2017-05~01..07 UTC", "", 12345, 1496016000000000); | |
200 | test_next("Mon 2017-05~07/1 UTC", "", 12345, 1496016000000000); | |
a2eb5ea7 DC |
201 | test_next("2017-08-06 9,11,13,15,17:00 UTC", "", 1502029800000000, 1502031600000000); |
202 | test_next("2017-08-06 9..17/2:00 UTC", "", 1502029800000000, 1502031600000000); | |
203 | test_next("2016-12-* 3..21/6:00 UTC", "", 1482613200000001, 1482634800000000); | |
48d26c01 | 204 | test_next("2017-09-24 03:30:00 Pacific/Auckland", "", 12345, 1506177000000000); |
f035bb1b | 205 | /* Due to daylight saving time - 2017-09-24 02:30:00 does not exist */ |
48d26c01 IK |
206 | test_next("2017-09-24 02:30:00 Pacific/Auckland", "", 12345, -1); |
207 | test_next("2017-04-02 02:30:00 Pacific/Auckland", "", 12345, 1491053400000000); | |
f035bb1b | 208 | /* Confirm that even though it's a time change here (backward) 02:30 happens only once */ |
48d26c01 IK |
209 | test_next("2017-04-02 02:30:00 Pacific/Auckland", "", 1491053400000000, -1); |
210 | test_next("2017-04-02 03:30:00 Pacific/Auckland", "", 12345, 1491060600000000); | |
f035bb1b | 211 | /* Confirm that timezones in the Spec work regardless of current timezone */ |
48d26c01 IK |
212 | test_next("2017-09-09 20:42:00 Pacific/Auckland", "", 12345, 1504946520000000); |
213 | test_next("2017-09-09 20:42:00 Pacific/Auckland", "EET", 12345, 1504946520000000); | |
129cb6e2 ZJS |
214 | /* Check that we don't start looping if mktime() moves us backwards */ |
215 | test_next("Sun *-*-* 01:00:00 Europe/Dublin", "", 1616412478000000, 1617494400000000); | |
216 | test_next("Sun *-*-* 01:00:00 Europe/Dublin", "IST", 1616412478000000, 1617494400000000); | |
36697dc0 LP |
217 | |
218 | assert_se(calendar_spec_from_string("test", &c) < 0); | |
04773cb5 | 219 | assert_se(calendar_spec_from_string(" utc", &c) < 0); |
408a51e1 | 220 | assert_se(calendar_spec_from_string(" ", &c) < 0); |
36697dc0 LP |
221 | assert_se(calendar_spec_from_string("", &c) < 0); |
222 | assert_se(calendar_spec_from_string("7", &c) < 0); | |
223 | assert_se(calendar_spec_from_string("121212:1:2", &c) < 0); | |
f22554a0 HV |
224 | assert_se(calendar_spec_from_string("2000-03-05.23 00:00:00", &c) < 0); |
225 | assert_se(calendar_spec_from_string("2000-03-05 00:00.1:00", &c) < 0); | |
226 | assert_se(calendar_spec_from_string("00:00:00/0.00000001", &c) < 0); | |
32b52369 | 227 | assert_se(calendar_spec_from_string("00:00:00.0..00.9", &c) < 0); |
8ea80351 DC |
228 | assert_se(calendar_spec_from_string("2016~11-22", &c) < 0); |
229 | assert_se(calendar_spec_from_string("*-*~5/5", &c) < 0); | |
6bae2fd4 DC |
230 | assert_se(calendar_spec_from_string("Monday.. 12:00", &c) < 0); |
231 | assert_se(calendar_spec_from_string("Monday..", &c) < 0); | |
9dfa81a0 DC |
232 | assert_se(calendar_spec_from_string("-00:+00/-5", &c) < 0); |
233 | assert_se(calendar_spec_from_string("00:+00/-5", &c) < 0); | |
234 | assert_se(calendar_spec_from_string("2016- 11- 24 12: 30: 00", &c) < 0); | |
a2eb5ea7 DC |
235 | assert_se(calendar_spec_from_string("*~29", &c) < 0); |
236 | assert_se(calendar_spec_from_string("*~16..31", &c) < 0); | |
237 | assert_se(calendar_spec_from_string("12..1/2-*", &c) < 0); | |
a2eb5ea7 DC |
238 | assert_se(calendar_spec_from_string("20/4:00", &c) < 0); |
239 | assert_se(calendar_spec_from_string("00:00/60", &c) < 0); | |
c0aebb4b DC |
240 | assert_se(calendar_spec_from_string("00:00:2300", &c) < 0); |
241 | assert_se(calendar_spec_from_string("00:00:18446744073709551615", &c) < 0); | |
5ee45c6d | 242 | assert_se(calendar_spec_from_string("@88588582097858858", &c) == -ERANGE); |
36697dc0 | 243 | |
21b3a0fc | 244 | test_timestamp(); |
250517d2 | 245 | test_hourly_bug_4031(); |
21b3a0fc | 246 | |
36697dc0 LP |
247 | return 0; |
248 | } |