]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-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/>.
22 #include "alloc-util.h"
23 #include "calendarspec.h"
24 #include "string-util.h"
27 static void test_one(const char *input
, const char *output
) {
29 _cleanup_free_
char *p
= NULL
, *q
= NULL
;
31 char buf
[FORMAT_TIMESTAMP_MAX
];
34 assert_se(calendar_spec_from_string(input
, &c
) >= 0);
36 assert_se(calendar_spec_to_string(c
, &p
) >= 0);
37 printf("\"%s\" → \"%s\"\n", input
, p
);
39 assert_se(streq(p
, output
));
41 u
= now(CLOCK_REALTIME
);
42 r
= calendar_spec_next_usec(c
, u
, &u
);
43 printf("Next: %s\n", r
< 0 ? strerror(-r
) : format_timestamp(buf
, sizeof(buf
), u
));
44 calendar_spec_free(c
);
46 assert_se(calendar_spec_from_string(p
, &c
) >= 0);
47 assert_se(calendar_spec_to_string(c
, &q
) >= 0);
48 calendar_spec_free(c
);
50 assert_se(streq(q
, p
));
53 static void test_next(const char *input
, const char *new_tz
, usec_t after
, usec_t expect
) {
57 char buf
[FORMAT_TIMESTAMP_MAX
];
60 old_tz
= getenv("TZ");
62 old_tz
= strdupa(old_tz
);
65 assert_se(setenv("TZ", new_tz
, 1) >= 0);
67 assert_se(unsetenv("TZ") >= 0);
70 assert_se(calendar_spec_from_string(input
, &c
) >= 0);
72 printf("\"%s\"\n", input
);
75 r
= calendar_spec_next_usec(c
, after
, &u
);
76 printf("At: %s\n", r
< 0 ? strerror(-r
) : format_timestamp_us(buf
, sizeof(buf
), u
));
77 if (expect
!= (usec_t
)-1)
78 assert_se(r
>= 0 && u
== expect
);
82 calendar_spec_free(c
);
85 assert_se(setenv("TZ", old_tz
, 1) >= 0);
87 assert_se(unsetenv("TZ") >= 0);
91 int main(int argc
, char* argv
[]) {
94 test_one("Sat,Thu,Mon-Wed,Sat-Sun", "Mon-Thu,Sat,Sun *-*-* 00:00:00");
95 test_one("Mon,Sun 12-*-* 2,1:23", "Mon,Sun 2012-*-* 01,02:23:00");
96 test_one("Wed *-1", "Wed *-*-01 00:00:00");
97 test_one("Wed-Wed,Wed *-1", "Wed *-*-01 00:00:00");
98 test_one("Wed, 17:48", "Wed *-*-* 17:48:00");
99 test_one("Wed-Sat,Tue 12-10-15 1:2:3", "Tue-Sat 2012-10-15 01:02:03");
100 test_one("*-*-7 0:0:0", "*-*-07 00:00:00");
101 test_one("10-15", "*-10-15 00:00:00");
102 test_one("monday *-12-* 17:00", "Mon *-12-* 17:00:00");
103 test_one("Mon,Fri *-*-3,1,2 *:30:45", "Mon,Fri *-*-01,02,03 *:30:45");
104 test_one("12,14,13,12:20,10,30", "*-*-* 12,13,14:10,20,30:00");
105 test_one("mon,fri *-1/2-1,3 *:30:45", "Mon,Fri *-01/2-01,03 *:30:45");
106 test_one("03-05 08:05:40", "*-03-05 08:05:40");
107 test_one("08:05:40", "*-*-* 08:05:40");
108 test_one("05:40", "*-*-* 05:40:00");
109 test_one("Sat,Sun 12-05 08:05:40", "Sat,Sun *-12-05 08:05:40");
110 test_one("Sat,Sun 08:05:40", "Sat,Sun *-*-* 08:05:40");
111 test_one("2003-03-05 05:40", "2003-03-05 05:40:00");
112 test_one("2003-03-05", "2003-03-05 00:00:00");
113 test_one("03-05", "*-03-05 00:00:00");
114 test_one("hourly", "*-*-* *:00:00");
115 test_one("daily", "*-*-* 00:00:00");
116 test_one("monthly", "*-*-01 00:00:00");
117 test_one("weekly", "Mon *-*-* 00:00:00");
118 test_one("minutely", "*-*-* *:*:00");
119 test_one("quarterly", "*-01,04,07,10-01 00:00:00");
120 test_one("semi-annually", "*-01,07-01 00:00:00");
121 test_one("annually", "*-01-01 00:00:00");
122 test_one("*:2/3", "*-*-* *:02/3:00");
123 test_one("2015-10-25 01:00:00 uTc", "2015-10-25 01:00:00 UTC");
124 test_one("2016-03-27 03:17:00.4200005", "2016-03-27 03:17:00.420001");
125 test_one("2016-03-27 03:17:00/0.42", "2016-03-27 03:17:00/0.420000");
126 test_one("2016-03-27 03:17:00/0.42", "2016-03-27 03:17:00/0.420000");
128 test_next("2016-03-27 03:17:00", "", 12345, 1459048620000000);
129 test_next("2016-03-27 03:17:00", "CET", 12345, 1459041420000000);
130 test_next("2016-03-27 03:17:00", "EET", 12345, -1);
131 test_next("2016-03-27 03:17:00 UTC", NULL
, 12345, 1459048620000000);
132 test_next("2016-03-27 03:17:00 UTC", "", 12345, 1459048620000000);
133 test_next("2016-03-27 03:17:00 UTC", "CET", 12345, 1459048620000000);
134 test_next("2016-03-27 03:17:00 UTC", "EET", 12345, 1459048620000000);
135 test_next("2016-03-27 03:17:00.420000001 UTC", "EET", 12345, 1459048620420000);
136 test_next("2016-03-27 03:17:00.4200005 UTC", "EET", 12345, 1459048620420001);
137 test_next("2015-11-13 09:11:23.42", "EET", 12345, 1447398683420000);
138 test_next("2015-11-13 09:11:23.42/1.77", "EET", 1447398683420000, 1447398685190000);
139 test_next("2015-11-13 09:11:23.42/1.77", "EET", 1447398683419999, 1447398683420000);
141 assert_se(calendar_spec_from_string("test", &c
) < 0);
142 assert_se(calendar_spec_from_string("", &c
) < 0);
143 assert_se(calendar_spec_from_string("7", &c
) < 0);
144 assert_se(calendar_spec_from_string("121212:1:2", &c
) < 0);
145 assert_se(calendar_spec_from_string("2000-03-05.23 00:00:00", &c
) < 0);
146 assert_se(calendar_spec_from_string("2000-03-05 00:00.1:00", &c
) < 0);
147 assert_se(calendar_spec_from_string("00:00:00/0.00000001", &c
) < 0);