| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include "alloc-util.h" |
| 4 |
#include "analyze.h" |
| 5 |
#include "analyze-calendar.h" |
| 6 |
#include "ansi-color.h" |
| 7 |
#include "calendarspec.h" |
| 8 |
#include "errno-util.h" |
| 9 |
#include "format-table.h" |
| 10 |
#include "log.h" |
| 11 |
#include "string-util.h" |
| 12 |
#include "strv.h" |
| 13 |
#include "time-util.h" |
| 14 |
|
| 15 |
static int test_calendar_one(usec_t n, const char *p) { |
| 16 |
_cleanup_(calendar_spec_freep) CalendarSpec *spec = NULL; |
| 17 |
_cleanup_(table_unrefp) Table *table = NULL; |
| 18 |
_cleanup_free_ char *t = NULL; |
| 19 |
TableCell *cell; |
| 20 |
int r; |
| 21 |
|
| 22 |
r = calendar_spec_from_string(p, &spec); |
| 23 |
if (r < 0) { |
| 24 |
log_error_errno(r, "Failed to parse calendar specification '%s': %m", p); |
| 25 |
time_parsing_hint(p, /* calendar= */ false, /* timestamp= */ true, /* timespan= */ true); |
| 26 |
return r; |
| 27 |
} |
| 28 |
|
| 29 |
r = calendar_spec_to_string(spec, &t); |
| 30 |
if (r < 0) |
| 31 |
return log_error_errno(r, "Failed to format calendar specification '%s': %m", p); |
| 32 |
|
| 33 |
table = table_new_vertical(); |
| 34 |
if (!table) |
| 35 |
return log_oom(); |
| 36 |
|
| 37 |
assert_se(cell = table_get_cell(table, 0, 0)); |
| 38 |
(void) table_set_ellipsize_percent(table, cell, 100); |
| 39 |
|
| 40 |
assert_se(cell = table_get_cell(table, 0, 1)); |
| 41 |
(void) table_set_ellipsize_percent(table, cell, 100); |
| 42 |
|
| 43 |
if (!streq(t, p)) { |
| 44 |
r = table_add_many(table, |
| 45 |
TABLE_FIELD, "Original form", |
| 46 |
TABLE_STRING, p); |
| 47 |
if (r < 0) |
| 48 |
return table_log_add_error(r); |
| 49 |
} |
| 50 |
|
| 51 |
r = table_add_many(table, |
| 52 |
TABLE_FIELD, "Normalized form", |
| 53 |
TABLE_STRING, t); |
| 54 |
if (r < 0) |
| 55 |
return table_log_add_error(r); |
| 56 |
|
| 57 |
for (unsigned i = 0; i < arg_iterations; i++) { |
| 58 |
usec_t next; |
| 59 |
|
| 60 |
r = calendar_spec_next_usec(spec, n, &next); |
| 61 |
if (r == -ENOENT) { |
| 62 |
if (i == 0) { |
| 63 |
r = table_add_many(table, |
| 64 |
TABLE_FIELD, "Next elapse", |
| 65 |
TABLE_STRING, "never", |
| 66 |
TABLE_SET_COLOR, ansi_highlight_yellow()); |
| 67 |
if (r < 0) |
| 68 |
return table_log_add_error(r); |
| 69 |
} |
| 70 |
break; |
| 71 |
} |
| 72 |
if (r < 0) |
| 73 |
return log_error_errno(r, "Failed to determine next elapse for '%s': %m", p); |
| 74 |
|
| 75 |
if (i == 0) { |
| 76 |
r = table_add_many(table, |
| 77 |
TABLE_FIELD, "Next elapse", |
| 78 |
TABLE_TIMESTAMP, next, |
| 79 |
TABLE_SET_COLOR, ansi_highlight_blue()); |
| 80 |
if (r < 0) |
| 81 |
return table_log_add_error(r); |
| 82 |
} else { |
| 83 |
int k = DECIMAL_STR_WIDTH(i + 1); |
| 84 |
|
| 85 |
if (k < 8) |
| 86 |
k = 8 - k; |
| 87 |
else |
| 88 |
k = 0; |
| 89 |
|
| 90 |
r = table_add_cell_stringf_full(table, NULL, TABLE_FIELD, "Iteration #%u", i+1); |
| 91 |
if (r < 0) |
| 92 |
return table_log_add_error(r); |
| 93 |
|
| 94 |
r = table_add_many(table, |
| 95 |
TABLE_TIMESTAMP, next, |
| 96 |
TABLE_SET_COLOR, ansi_highlight_blue()); |
| 97 |
if (r < 0) |
| 98 |
return table_log_add_error(r); |
| 99 |
} |
| 100 |
|
| 101 |
if (!in_utc_timezone()) { |
| 102 |
r = table_add_many(table, |
| 103 |
TABLE_FIELD, "(in UTC)", |
| 104 |
TABLE_TIMESTAMP_UTC, next); |
| 105 |
if (r < 0) |
| 106 |
return table_log_add_error(r); |
| 107 |
} |
| 108 |
|
| 109 |
r = table_add_many(table, |
| 110 |
TABLE_FIELD, "From now", |
| 111 |
TABLE_TIMESTAMP_RELATIVE, next); |
| 112 |
if (r < 0) |
| 113 |
return table_log_add_error(r); |
| 114 |
|
| 115 |
n = next; |
| 116 |
} |
| 117 |
|
| 118 |
return table_print_or_warn(table); |
| 119 |
} |
| 120 |
|
| 121 |
int verb_calendar(int argc, char *argv[], uintptr_t _data, void *userdata) { |
| 122 |
int r = 0; |
| 123 |
usec_t n; |
| 124 |
|
| 125 |
if (arg_base_time != USEC_INFINITY) |
| 126 |
n = arg_base_time; |
| 127 |
else |
| 128 |
n = now(CLOCK_REALTIME); /* We want to use the same "base" for all expressions */ |
| 129 |
|
| 130 |
STRV_FOREACH(p, strv_skip(argv, 1)) { |
| 131 |
RET_GATHER(r, test_calendar_one(n, *p)); |
| 132 |
|
| 133 |
if (p[1]) |
| 134 |
putchar('\n'); |
| 135 |
} |
| 136 |
|
| 137 |
return r; |
| 138 |
} |