]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/analyze/analyze-timespan.c
8d7cd2ddd5924632952c023cc5f6d13c90cb045e
[thirdparty/systemd.git] / src / analyze / analyze-timespan.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include "analyze.h"
4 #include "analyze-timespan.h"
5 #include "calendarspec.h"
6 #include "format-table.h"
7 #include "glyph-util.h"
8 #include "strv.h"
9 #include "terminal-util.h"
10
11 int verb_timespan(int argc, char *argv[], void *userdata) {
12 char **input_timespan;
13
14 STRV_FOREACH(input_timespan, strv_skip(argv, 1)) {
15 _cleanup_(table_unrefp) Table *table = NULL;
16 usec_t output_usecs;
17 TableCell *cell;
18 int r;
19
20 r = parse_time(*input_timespan, &output_usecs, USEC_PER_SEC);
21 if (r < 0) {
22 log_error_errno(r, "Failed to parse time span '%s': %m", *input_timespan);
23 time_parsing_hint(*input_timespan, /* calendar= */ true, /* timestamp= */ true, /* timespan= */ false);
24 return r;
25 }
26
27 table = table_new("name", "value");
28 if (!table)
29 return log_oom();
30
31 table_set_header(table, false);
32
33 assert_se(cell = table_get_cell(table, 0, 0));
34 r = table_set_ellipsize_percent(table, cell, 100);
35 if (r < 0)
36 return r;
37
38 r = table_set_align_percent(table, cell, 100);
39 if (r < 0)
40 return r;
41
42 assert_se(cell = table_get_cell(table, 0, 1));
43 r = table_set_ellipsize_percent(table, cell, 100);
44 if (r < 0)
45 return r;
46
47 r = table_add_many(table,
48 TABLE_STRING, "Original:",
49 TABLE_STRING, *input_timespan);
50 if (r < 0)
51 return table_log_add_error(r);
52
53 r = table_add_cell_stringf(table, NULL, "%ss:", special_glyph(SPECIAL_GLYPH_MU));
54 if (r < 0)
55 return table_log_add_error(r);
56
57 r = table_add_many(table,
58 TABLE_UINT64, output_usecs,
59 TABLE_STRING, "Human:",
60 TABLE_TIMESPAN, output_usecs,
61 TABLE_SET_COLOR, ansi_highlight());
62 if (r < 0)
63 return table_log_add_error(r);
64
65 r = table_print(table, NULL);
66 if (r < 0)
67 return r;
68
69 if (input_timespan[1])
70 putchar('\n');
71 }
72
73 return 0;
74 }