Skip to content

Commit

Permalink
format-table: add TABLE_TIMESTAMP_UTC and _RELATIVE
Browse files Browse the repository at this point in the history
  • Loading branch information
yuwata committed Jul 15, 2019
1 parent d8bfdbe commit c5bbb2b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
21 changes: 19 additions & 2 deletions src/shared/format-table.c
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,8 @@ static size_t table_data_size(TableDataType type, const void *data) {
return sizeof(bool);

case TABLE_TIMESTAMP:
case TABLE_TIMESTAMP_UTC:
case TABLE_TIMESTAMP_RELATIVE:
case TABLE_TIMESPAN:
case TABLE_TIMESPAN_MSEC:
return sizeof(usec_t);
Expand Down Expand Up @@ -720,6 +722,8 @@ int table_add_many_internal(Table *t, TableDataType first_type, ...) {
break;

case TABLE_TIMESTAMP:
case TABLE_TIMESTAMP_UTC:
case TABLE_TIMESTAMP_RELATIVE:
case TABLE_TIMESPAN:
case TABLE_TIMESPAN_MSEC:
buffer.usec = va_arg(ap, usec_t);
Expand Down Expand Up @@ -884,6 +888,8 @@ static int cell_data_compare(TableData *a, size_t index_a, TableData *b, size_t
return 0;

case TABLE_TIMESTAMP:
case TABLE_TIMESTAMP_UTC:
case TABLE_TIMESTAMP_RELATIVE:
return CMP(a->timestamp, b->timestamp);

case TABLE_TIMESPAN:
Expand Down Expand Up @@ -988,14 +994,23 @@ static const char *table_data_format(TableData *d) {
case TABLE_BOOLEAN:
return yes_no(d->boolean);

case TABLE_TIMESTAMP: {
case TABLE_TIMESTAMP:
case TABLE_TIMESTAMP_UTC:
case TABLE_TIMESTAMP_RELATIVE: {
_cleanup_free_ char *p;
char *ret;

p = new(char, FORMAT_TIMESTAMP_MAX);
if (!p)
return NULL;

if (!format_timestamp(p, FORMAT_TIMESTAMP_MAX, d->timestamp))
if (d->type == TABLE_TIMESTAMP)
ret = format_timestamp(p, FORMAT_TIMESTAMP_MAX, d->timestamp);
else if (d->type == TABLE_TIMESTAMP_UTC)
ret = format_timestamp_utc(p, FORMAT_TIMESTAMP_MAX, d->timestamp);
else
ret = format_timestamp_relative(p, FORMAT_TIMESTAMP_MAX, d->timestamp);
if (!ret)
return "n/a";

d->formatted = TAKE_PTR(p);
Expand Down Expand Up @@ -1637,6 +1652,8 @@ static int table_data_to_json(TableData *d, JsonVariant **ret) {
return json_variant_new_boolean(ret, d->boolean);

case TABLE_TIMESTAMP:
case TABLE_TIMESTAMP_UTC:
case TABLE_TIMESTAMP_RELATIVE:
if (d->timestamp == USEC_INFINITY)
return json_variant_new_null(ret);

Expand Down
2 changes: 2 additions & 0 deletions src/shared/format-table.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ typedef enum TableDataType {
TABLE_STRING,
TABLE_BOOLEAN,
TABLE_TIMESTAMP,
TABLE_TIMESTAMP_UTC,
TABLE_TIMESTAMP_RELATIVE,
TABLE_TIMESPAN,
TABLE_TIMESPAN_MSEC,
TABLE_SIZE,
Expand Down

0 comments on commit c5bbb2b

Please sign in to comment.