Add "epoch" timestamp option

Add an option that prints the timestamp as the number of seconds since
the Unix epoch.
This commit is contained in:
Steve Marple 2024-08-23 12:11:05 +01:00
parent cdc773100c
commit 20f50281eb
6 changed files with 23 additions and 1 deletions

View file

@ -148,6 +148,8 @@ Set timestamp format to any of the following timestamp formats:
24-hour format relative to previous timestamp
.IP "\fBiso8601"
ISO8601 format ("YYYY-MM-DDThh:mm:ss.sss")
.IP "\fBepoch"
Seconds since Unix epoch (1970-01-01)
.PP
Default format is \fB24hour\fR
.RE

View file

@ -114,6 +114,8 @@ OPTIONS
iso8601 ISO8601 format ("YYYY-MM-DDThh:mm:ss.sss")
epoch Seconds since Unix epoch (1970-01-01)
Default format is 24hour
--timestamp-timeout <ms>

View file

@ -392,6 +392,10 @@ const char* option_timestamp_format_to_string(timestamp_t timestamp)
return "iso8601";
break;
case TIMESTAMP_EPOCH:
return "epoch";
break;
default:
return "unknown";
break;
@ -418,6 +422,10 @@ void option_parse_timestamp(const char *arg, timestamp_t *timestamp)
{
*timestamp = TIMESTAMP_ISO8601;
}
else if (strcmp(arg, "epoch") == 0)
{
*timestamp = TIMESTAMP_EPOCH;
}
else
{
tio_error_print("Invalid timestamp '%s'", arg);
@ -1151,7 +1159,7 @@ void options_parse_final(int argc, char *argv[])
if ( ((strncmp("COM", option.target, 3) == 0)
|| (strncmp("com", option.target, 3) == 0) )
&& (sscanf(option.target + 3, "%hhu", &portnum) == 1)
&& (portnum > 0) )
&& (portnum > 0) )
{
asprintf(&tty_win, "/dev/ttyS%hhu", portnum - 1);
option.target = tty_win;

View file

@ -76,6 +76,12 @@ char *timestamp_current_time(void)
tm = localtime(&tv.tv_sec);
len = strftime(time_string, sizeof(time_string), "%Y-%m-%dT%H:%M:%S", tm);
break;
case TIMESTAMP_EPOCH:
// "1223" (seconds since Unix epoch, 1970-01-01 00:00:00Z)
tv = tv_now;
tm = localtime(&tv.tv_sec);
len = strftime(time_string, sizeof(time_string), "%s", tm);
break;
default:
return NULL;
}

View file

@ -28,6 +28,7 @@ typedef enum
TIMESTAMP_24HOUR_START,
TIMESTAMP_24HOUR_DELTA,
TIMESTAMP_ISO8601,
TIMESTAMP_EPOCH,
TIMESTAMP_END,
} timestamp_t;

View file

@ -1079,6 +1079,9 @@ void handle_command_sequence(char input_char, char *output_char, bool *forward)
case TIMESTAMP_ISO8601:
tio_printf("Switched timestamp mode to iso8601");
break;
case TIMESTAMP_EPOCH:
tio_printf("Switched timestamp mode to epoch");
break;
case TIMESTAMP_END:
option.timestamp = TIMESTAMP_NONE;
tio_printf("Switched timestamp mode off");