| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include "analyze.h" |
| 4 |
#include "analyze-timestamp.h" |
| 5 |
#include "ansi-color.h" |
| 6 |
#include "errno-util.h" |
| 7 |
#include "format-table.h" |
| 8 |
#include "log.h" |
| 9 |
#include "strv.h" |
| 10 |
#include "time-util.h" |
| 11 |
|
| 12 |
static int test_timestamp_one(const char *p) { |
| 13 |
_cleanup_(table_unrefp) Table *table = NULL; |
| 14 |
TableCell *cell; |
| 15 |
usec_t usec; |
| 16 |
int r; |
| 17 |
|
| 18 |
r = parse_timestamp(p, &usec); |
| 19 |
if (r < 0) { |
| 20 |
log_error_errno(r, "Failed to parse \"%s\": %m", p); |
| 21 |
time_parsing_hint(p, /* calendar= */ true, /* timestamp= */ false, /* timespan= */ true); |
| 22 |
return r; |
| 23 |
} |
| 24 |
|
| 25 |
table = table_new_vertical(); |
| 26 |
if (!table) |
| 27 |
return log_oom(); |
| 28 |
|
| 29 |
assert_se(cell = table_get_cell(table, 0, 0)); |
| 30 |
(void) table_set_ellipsize_percent(table, cell, 100); |
| 31 |
|
| 32 |
assert_se(cell = table_get_cell(table, 0, 1)); |
| 33 |
(void) table_set_ellipsize_percent(table, cell, 100); |
| 34 |
|
| 35 |
r = table_add_many(table, |
| 36 |
TABLE_FIELD, "Original form", |
| 37 |
TABLE_STRING, p, |
| 38 |
TABLE_FIELD, "Normalized form", |
| 39 |
TABLE_TIMESTAMP, usec, |
| 40 |
TABLE_SET_COLOR, ansi_highlight_blue()); |
| 41 |
if (r < 0) |
| 42 |
return table_log_add_error(r); |
| 43 |
|
| 44 |
if (!in_utc_timezone()) { |
| 45 |
r = table_add_many(table, |
| 46 |
TABLE_FIELD, "(in UTC)", |
| 47 |
TABLE_TIMESTAMP_UTC, usec); |
| 48 |
if (r < 0) |
| 49 |
return table_log_add_error(r); |
| 50 |
} |
| 51 |
|
| 52 |
r = table_add_cell(table, NULL, TABLE_FIELD, "UNIX seconds"); |
| 53 |
if (r < 0) |
| 54 |
return table_log_add_error(r); |
| 55 |
|
| 56 |
if (usec % USEC_PER_SEC == 0) |
| 57 |
r = table_add_cell_stringf(table, NULL, "@%"PRI_USEC, |
| 58 |
usec / USEC_PER_SEC); |
| 59 |
else |
| 60 |
r = table_add_cell_stringf(table, NULL, "@%"PRI_USEC".%06"PRI_USEC"", |
| 61 |
usec / USEC_PER_SEC, |
| 62 |
usec % USEC_PER_SEC); |
| 63 |
if (r < 0) |
| 64 |
return table_log_add_error(r); |
| 65 |
|
| 66 |
r = table_add_many(table, |
| 67 |
TABLE_FIELD, "From now", |
| 68 |
TABLE_TIMESTAMP_RELATIVE, usec); |
| 69 |
if (r < 0) |
| 70 |
return table_log_add_error(r); |
| 71 |
|
| 72 |
return table_print_or_warn(table); |
| 73 |
} |
| 74 |
|
| 75 |
int verb_timestamp(int argc, char *argv[], uintptr_t _data, void *userdata) { |
| 76 |
int r = 0; |
| 77 |
|
| 78 |
char **args = strv_skip(argv, 1); |
| 79 |
STRV_FOREACH(arg, args) { |
| 80 |
if (arg != args) |
| 81 |
puts(""); |
| 82 |
|
| 83 |
RET_GATHER(r, test_timestamp_one(*arg)); |
| 84 |
} |
| 85 |
|
| 86 |
return r; |
| 87 |
} |