]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/analyze/analyze-calendar.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
4 #include "analyze-calendar.h"
5 #include "calendarspec.h"
6 #include "format-table.h"
7 #include "terminal-util.h"
9 static int test_calendar_one(usec_t n
, const char *p
) {
10 _cleanup_(calendar_spec_freep
) CalendarSpec
*spec
= NULL
;
11 _cleanup_(table_unrefp
) Table
*table
= NULL
;
12 _cleanup_free_
char *t
= NULL
;
16 r
= calendar_spec_from_string(p
, &spec
);
18 log_error_errno(r
, "Failed to parse calendar specification '%s': %m", p
);
19 time_parsing_hint(p
, /* calendar= */ false, /* timestamp= */ true, /* timespan= */ true);
23 r
= calendar_spec_to_string(spec
, &t
);
25 return log_error_errno(r
, "Failed to format calendar specification '%s': %m", p
);
27 table
= table_new_vertical();
31 assert_se(cell
= table_get_cell(table
, 0, 0));
32 r
= table_set_ellipsize_percent(table
, cell
, 100);
36 assert_se(cell
= table_get_cell(table
, 0, 1));
37 r
= table_set_ellipsize_percent(table
, cell
, 100);
42 r
= table_add_many(table
,
43 TABLE_FIELD
, "Original form",
46 return table_log_add_error(r
);
49 r
= table_add_many(table
,
50 TABLE_FIELD
, "Normalized form",
53 return table_log_add_error(r
);
55 for (unsigned i
= 0; i
< arg_iterations
; i
++) {
58 r
= calendar_spec_next_usec(spec
, n
, &next
);
61 r
= table_add_many(table
,
62 TABLE_FIELD
, "Next elapse",
63 TABLE_STRING
, "never",
64 TABLE_SET_COLOR
, ansi_highlight_yellow());
66 return table_log_add_error(r
);
71 return log_error_errno(r
, "Failed to determine next elapse for '%s': %m", p
);
74 r
= table_add_many(table
,
75 TABLE_FIELD
, "Next elapse",
76 TABLE_TIMESTAMP
, next
,
77 TABLE_SET_COLOR
, ansi_highlight_blue());
79 return table_log_add_error(r
);
81 int k
= DECIMAL_STR_WIDTH(i
+ 1);
88 r
= table_add_cell_stringf_full(table
, NULL
, TABLE_FIELD
, "Iteration #%u", i
+1);
90 return table_log_add_error(r
);
92 r
= table_add_many(table
,
93 TABLE_TIMESTAMP
, next
,
94 TABLE_SET_COLOR
, ansi_highlight_blue());
96 return table_log_add_error(r
);
99 if (!in_utc_timezone()) {
100 r
= table_add_many(table
,
101 TABLE_FIELD
, "(in UTC)",
102 TABLE_TIMESTAMP_UTC
, next
);
104 return table_log_add_error(r
);
107 r
= table_add_many(table
,
108 TABLE_FIELD
, "From now",
109 TABLE_TIMESTAMP_RELATIVE
, next
);
111 return table_log_add_error(r
);
116 return table_print(table
, NULL
);
119 int verb_calendar(int argc
, char *argv
[], void *userdata
) {
123 if (arg_base_time
!= USEC_INFINITY
)
126 n
= now(CLOCK_REALTIME
); /* We want to use the same "base" for all expressions */
128 STRV_FOREACH(p
, strv_skip(argv
, 1)) {
131 k
= test_calendar_one(n
, *p
);