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
verb_timespan(int argc,char * argv[],void * userdata)11 int verb_timespan(int argc, char *argv[], void *userdata) {
12 STRV_FOREACH(input_timespan, strv_skip(argv, 1)) {
13 _cleanup_(table_unrefp) Table *table = NULL;
14 usec_t output_usecs;
15 TableCell *cell;
16 int r;
17
18 r = parse_time(*input_timespan, &output_usecs, USEC_PER_SEC);
19 if (r < 0) {
20 log_error_errno(r, "Failed to parse time span '%s': %m", *input_timespan);
21 time_parsing_hint(*input_timespan, /* calendar= */ true, /* timestamp= */ true, /* timespan= */ false);
22 return r;
23 }
24
25 table = table_new("name", "value");
26 if (!table)
27 return log_oom();
28
29 table_set_header(table, false);
30
31 assert_se(cell = table_get_cell(table, 0, 0));
32 r = table_set_ellipsize_percent(table, cell, 100);
33 if (r < 0)
34 return r;
35
36 r = table_set_align_percent(table, cell, 100);
37 if (r < 0)
38 return r;
39
40 assert_se(cell = table_get_cell(table, 0, 1));
41 r = table_set_ellipsize_percent(table, cell, 100);
42 if (r < 0)
43 return r;
44
45 r = table_add_many(table,
46 TABLE_STRING, "Original:",
47 TABLE_STRING, *input_timespan);
48 if (r < 0)
49 return table_log_add_error(r);
50
51 r = table_add_cell_stringf(table, NULL, "%ss:", special_glyph(SPECIAL_GLYPH_MU));
52 if (r < 0)
53 return table_log_add_error(r);
54
55 r = table_add_many(table,
56 TABLE_UINT64, output_usecs,
57 TABLE_STRING, "Human:",
58 TABLE_TIMESPAN, output_usecs,
59 TABLE_SET_COLOR, ansi_highlight());
60 if (r < 0)
61 return table_log_add_error(r);
62
63 r = table_print(table, NULL);
64 if (r < 0)
65 return r;
66
67 if (input_timespan[1])
68 putchar('\n');
69 }
70
71 return 0;
72 }
73