]>
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("name", "value");
31 table_set_header(table
, false);
33 assert_se(cell
= table_get_cell(table
, 0, 0));
34 r
= table_set_ellipsize_percent(table
, cell
, 100);
38 r
= table_set_align_percent(table
, cell
, 100);
42 assert_se(cell
= table_get_cell(table
, 0, 1));
43 r
= table_set_ellipsize_percent(table
, cell
, 100);
48 r
= table_add_many(table
,
49 TABLE_STRING
, "Original form:",
52 return table_log_add_error(r
);
55 r
= table_add_many(table
,
56 TABLE_STRING
, "Normalized form:",
59 return table_log_add_error(r
);
61 for (unsigned i
= 0; i
< arg_iterations
; i
++) {
64 r
= calendar_spec_next_usec(spec
, n
, &next
);
67 r
= table_add_many(table
,
68 TABLE_STRING
, "Next elapse:",
69 TABLE_STRING
, "never",
70 TABLE_SET_COLOR
, ansi_highlight_yellow());
72 return table_log_add_error(r
);
77 return log_error_errno(r
, "Failed to determine next elapse for '%s': %m", p
);
80 r
= table_add_many(table
,
81 TABLE_STRING
, "Next elapse:",
82 TABLE_TIMESTAMP
, next
,
83 TABLE_SET_COLOR
, ansi_highlight_blue());
85 return table_log_add_error(r
);
87 int k
= DECIMAL_STR_WIDTH(i
+ 1);
94 r
= table_add_cell_stringf(table
, NULL
, "Iter. #%u:", i
+1);
96 return table_log_add_error(r
);
98 r
= table_add_many(table
,
99 TABLE_TIMESTAMP
, next
,
100 TABLE_SET_COLOR
, ansi_highlight_blue());
102 return table_log_add_error(r
);
105 if (!in_utc_timezone()) {
106 r
= table_add_many(table
,
107 TABLE_STRING
, "(in UTC):",
108 TABLE_TIMESTAMP_UTC
, next
);
110 return table_log_add_error(r
);
113 r
= table_add_many(table
,
114 TABLE_STRING
, "From now:",
115 TABLE_TIMESTAMP_RELATIVE
, next
);
117 return table_log_add_error(r
);
122 return table_print(table
, NULL
);
125 int verb_calendar(int argc
, char *argv
[], void *userdata
) {
129 if (arg_base_time
!= USEC_INFINITY
)
132 n
= now(CLOCK_REALTIME
); /* We want to use the same "base" for all expressions */
134 STRV_FOREACH(p
, strv_skip(argv
, 1)) {
137 k
= test_calendar_one(n
, *p
);