Added timestamp format "epoch-usec"

This timestamp format will print the seconds since epoch along with
subdivision in microseconds.

Example:
 [1748009585.087083] tio v3.9-8-g2fb788f-dirty
 [1748009585.087156] Press ctrl-t q to quit
 [1748009585.087683] Connected to /dev/ttyUSB0
This commit is contained in:
Maximilian Seesslen 2025-05-23 16:14:16 +02:00 committed by Martin Lund
parent 2fb788f817
commit 7e61a34df3
8 changed files with 28 additions and 5 deletions

View file

@ -150,6 +150,8 @@ Set timestamp format to any of the following timestamp formats:
ISO8601 format ("YYYY-MM-DDThh:mm:ss.sss") ISO8601 format ("YYYY-MM-DDThh:mm:ss.sss")
.IP "\fBepoch" .IP "\fBepoch"
Seconds since Unix epoch (1970-01-01) Seconds since Unix epoch (1970-01-01)
.IP "\fBepoch-usec"
Seconds since Unix epoch (1970-01-01) with subdivision in microseconds
.PP .PP
Default format is \fB24hour\fR Default format is \fB24hour\fR
.RE .RE

View file

@ -116,6 +116,8 @@ OPTIONS
epoch Seconds since Unix epoch (1970-01-01) epoch Seconds since Unix epoch (1970-01-01)
epoch-usec Seconds since Unix epoch (1970-01-01) with subdivision microseconds
Default format is 24hour Default format is 24hour
--timestamp-timeout <ms> --timestamp-timeout <ms>

View file

@ -89,7 +89,7 @@ _tio()
return 0 return 0
;; ;;
--timestamp-format) --timestamp-format)
COMPREPLY=( $(compgen -W "24hour 24hour-start 24hour-delta iso8601" -- ${cur}) ) COMPREPLY=( $(compgen -W "24hour 24hour-start 24hour-delta iso8601 epoch epoch-usec" -- ${cur}) )
return 0 return 0
;; ;;
-c | --color) -c | --color)

View file

@ -207,7 +207,7 @@ static void config_parse_keys(GKeyFile *key_file, char *group)
config_get_bool(key_file, group, "timestamp", (bool*) &option.timestamp); config_get_bool(key_file, group, "timestamp", (bool*) &option.timestamp);
if (option.timestamp != TIMESTAMP_NONE) if (option.timestamp != TIMESTAMP_NONE)
{ {
config_get_string(key_file, group, "timestamp-format", &string, "24hour", "24hour-start", "24hour-delta", "iso8601", "epoch", NULL); config_get_string(key_file, group, "timestamp-format", &string, "24hour", "24hour-start", "24hour-delta", "iso8601", "epoch", "epoch-usec", NULL);
if (string != NULL) if (string != NULL)
{ {
option_parse_timestamp(string, &option.timestamp); option_parse_timestamp(string, &option.timestamp);

View file

@ -400,6 +400,10 @@ const char* option_timestamp_format_to_string(timestamp_t timestamp)
return "epoch"; return "epoch";
break; break;
case TIMESTAMP_EPOCH_USEC:
return "epoch-usec";
break;
default: default:
return "unknown"; return "unknown";
break; break;
@ -430,6 +434,10 @@ void option_parse_timestamp(const char *arg, timestamp_t *timestamp)
{ {
*timestamp = TIMESTAMP_EPOCH; *timestamp = TIMESTAMP_EPOCH;
} }
else if (strcmp(arg, "epoch-usec") == 0)
{
*timestamp = TIMESTAMP_EPOCH_USEC;
}
else else
{ {
tio_error_print("Invalid timestamp '%s'", arg); tio_error_print("Invalid timestamp '%s'", arg);

View file

@ -75,6 +75,7 @@ char *timestamp_current_time(void)
len = strftime(time_string, sizeof(time_string), "%Y-%m-%dT%H:%M:%S", tm); len = strftime(time_string, sizeof(time_string), "%Y-%m-%dT%H:%M:%S", tm);
break; break;
case TIMESTAMP_EPOCH: case TIMESTAMP_EPOCH:
case TIMESTAMP_EPOCH_USEC:
// "N.sss" (seconds since Unix epoch, 1970-01-01 00:00:00Z) // "N.sss" (seconds since Unix epoch, 1970-01-01 00:00:00Z)
tv = tv_now; tv = tv_now;
tm = localtime(&tv.tv_sec); tm = localtime(&tv.tv_sec);
@ -84,12 +85,18 @@ char *timestamp_current_time(void)
return NULL; return NULL;
} }
// Append milliseconds to all timestamps // Append millis-/microseconds to all timestamps
if (len) if (len)
{ {
len = snprintf(time_string + len, TIME_STRING_SIZE_MAX - len, ".%03ld", (long)tv.tv_usec / 1000); if ( option.timestamp == TIMESTAMP_EPOCH_USEC )
{
len = snprintf(time_string + len, TIME_STRING_SIZE_MAX - len, ".%06ld", (long)tv.tv_usec);
}
else
{
len = snprintf(time_string + len, TIME_STRING_SIZE_MAX - len, ".%03ld", (long)tv.tv_usec / 1000);
}
} }
// Save previous time value for next run // Save previous time value for next run
tv_previous = tv_now; tv_previous = tv_now;

View file

@ -29,6 +29,7 @@ typedef enum
TIMESTAMP_24HOUR_DELTA, TIMESTAMP_24HOUR_DELTA,
TIMESTAMP_ISO8601, TIMESTAMP_ISO8601,
TIMESTAMP_EPOCH, TIMESTAMP_EPOCH,
TIMESTAMP_EPOCH_USEC,
TIMESTAMP_END, TIMESTAMP_END,
} timestamp_t; } timestamp_t;

View file

@ -1100,6 +1100,9 @@ void handle_command_sequence(char input_char, char *output_char, bool *forward)
case TIMESTAMP_EPOCH: case TIMESTAMP_EPOCH:
tio_printf("Switched timestamp mode to epoch"); tio_printf("Switched timestamp mode to epoch");
break; break;
case TIMESTAMP_EPOCH_USEC:
tio_printf("Switched timestamp mode to epoch with subdivision in microseconds");
break;
case TIMESTAMP_END: case TIMESTAMP_END:
option.timestamp = TIMESTAMP_NONE; option.timestamp = TIMESTAMP_NONE;
tio_printf("Switched timestamp mode off"); tio_printf("Switched timestamp mode off");