]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/analyze/analyze-timestamp.c
strv: make iterator in STRV_FOREACH() declaread in the loop
[thirdparty/systemd.git] / src / analyze / analyze-timestamp.c
CommitLineData
503ccaaa
LP
1/* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3#include "analyze.h"
4#include "analyze-timestamp.h"
5#include "format-table.h"
6#include "terminal-util.h"
7
8static int test_timestamp_one(const char *p) {
9 _cleanup_(table_unrefp) Table *table = NULL;
10 TableCell *cell;
11 usec_t usec;
12 int r;
13
14 r = parse_timestamp(p, &usec);
15 if (r < 0) {
16 log_error_errno(r, "Failed to parse \"%s\": %m", p);
17 time_parsing_hint(p, /* calendar= */ true, /* timestamp= */ false, /* timespan= */ true);
18 return r;
19 }
20
21 table = table_new("name", "value");
22 if (!table)
23 return log_oom();
24
25 table_set_header(table, false);
26
27 assert_se(cell = table_get_cell(table, 0, 0));
28 r = table_set_ellipsize_percent(table, cell, 100);
29 if (r < 0)
30 return r;
31
32 r = table_set_align_percent(table, cell, 100);
33 if (r < 0)
34 return r;
35
36 assert_se(cell = table_get_cell(table, 0, 1));
37 r = table_set_ellipsize_percent(table, cell, 100);
38 if (r < 0)
39 return r;
40
41 r = table_add_many(table,
42 TABLE_STRING, "Original form:",
43 TABLE_STRING, p,
44 TABLE_STRING, "Normalized form:",
45 TABLE_TIMESTAMP, usec,
46 TABLE_SET_COLOR, ansi_highlight_blue());
47 if (r < 0)
48 return table_log_add_error(r);
49
50 if (!in_utc_timezone()) {
51 r = table_add_many(table,
52 TABLE_STRING, "(in UTC):",
53 TABLE_TIMESTAMP_UTC, usec);
54 if (r < 0)
55 return table_log_add_error(r);
56 }
57
58 r = table_add_cell(table, NULL, TABLE_STRING, "UNIX seconds:");
59 if (r < 0)
60 return table_log_add_error(r);
61
62 if (usec % USEC_PER_SEC == 0)
63 r = table_add_cell_stringf(table, NULL, "@%"PRI_USEC,
64 usec / USEC_PER_SEC);
65 else
66 r = table_add_cell_stringf(table, NULL, "@%"PRI_USEC".%06"PRI_USEC"",
67 usec / USEC_PER_SEC,
68 usec % USEC_PER_SEC);
69 if (r < 0)
70 return r;
71
72 r = table_add_many(table,
73 TABLE_STRING, "From now:",
74 TABLE_TIMESTAMP_RELATIVE, usec);
75 if (r < 0)
76 return table_log_add_error(r);
77
78 return table_print(table, NULL);
79}
80
ef38bedb 81int verb_timestamp(int argc, char *argv[], void *userdata) {
503ccaaa 82 int ret = 0, r;
503ccaaa
LP
83
84 STRV_FOREACH(p, strv_skip(argv, 1)) {
85 r = test_timestamp_one(*p);
86 if (ret == 0 && r < 0)
87 ret = r;
88
89 if (*(p + 1))
90 putchar('\n');
91 }
92
93 return ret;
94}