diff --git a/src/misc.c b/src/misc.c index b4555ea..1a61eb9 100644 --- a/src/misc.c +++ b/src/misc.c @@ -35,17 +35,17 @@ char *current_time(void) { static char time_string[TIME_STRING_SIZE_MAX]; - static struct timeval tv_start; + static struct timeval tv, tv_now, tv_start, tv_previous; static bool first = true; struct tm *tm; - struct timeval tv; size_t len; - gettimeofday(&tv, NULL); + // Get current time value + gettimeofday(&tv_now, NULL); if (first) { - tv_start = tv; + tv_start = tv_now; first = false; } @@ -55,19 +55,26 @@ char *current_time(void) case TIMESTAMP_NONE: case TIMESTAMP_24HOUR: // "hh:mm:ss.sss" (24 hour format) - tm = localtime(&tv.tv_sec); + tm = localtime(&tv_now.tv_sec); len = strftime(time_string, sizeof(time_string), "%H:%M:%S", tm); break; case TIMESTAMP_24HOUR_START: // "hh:mm:ss.sss" (24 hour format relative to start time) - timersub(&tv, &tv_start, &tv); + timersub(&tv_now, &tv_start, &tv); + tv.tv_sec -= 3600; // Why is this needed?? + tm = localtime(&tv.tv_sec); + len = strftime(time_string, sizeof(time_string), "%H:%M:%S", tm); + break; + case TIMESTAMP_24HOUR_DELTA: + // "hh:mm:ss.sss" (24 hour format relative to previous time stamp) + timersub(&tv_now, &tv_previous, &tv); tv.tv_sec -= 3600; // Why is this needed?? tm = localtime(&tv.tv_sec); len = strftime(time_string, sizeof(time_string), "%H:%M:%S", tm); break; case TIMESTAMP_ISO8601: // "YYYY-MM-DDThh:mm:ss.sss" (ISO-8601) - tm = localtime(&tv.tv_sec); + tm = localtime(&tv_now.tv_sec); len = strftime(time_string, sizeof(time_string), "%Y-%m-%dT%H:%M:%S", tm); break; default: @@ -80,6 +87,9 @@ char *current_time(void) len = snprintf(time_string + len, TIME_STRING_SIZE_MAX - len, ".%03ld", (long)tv.tv_usec / 1000); } + // Save previous time value for next run + tv_previous = tv_now; + return (len < TIME_STRING_SIZE_MAX) ? time_string : NULL; } diff --git a/src/options.c b/src/options.c index 85782c9..bc8d010 100644 --- a/src/options.c +++ b/src/options.c @@ -117,6 +117,10 @@ const char* timestamp_token(enum timestamp_t timestamp) return "24hour-start"; break; + case TIMESTAMP_24HOUR_DELTA: + return "24hour-delta"; + break; + case TIMESTAMP_ISO8601: return "iso8601"; break; @@ -133,10 +137,22 @@ enum timestamp_t timestamp_option_parse(const char *arg) if (arg != NULL) { - if (strcmp(arg, "24hour-start") == 0) + if (strcmp(arg, "none") == 0) + { + return TIMESTAMP_NONE; + } + else if (strcmp(arg, "24hour") == 0) + { + return TIMESTAMP_24HOUR; + } + else if (strcmp(arg, "24hour-start") == 0) { return TIMESTAMP_24HOUR_START; } + else if (strcmp(arg, "24hour-delta") == 0) + { + return TIMESTAMP_24HOUR_DELTA; + } else if (strcmp(arg, "iso8601") == 0) { return TIMESTAMP_ISO8601; diff --git a/src/options.h b/src/options.h index cc905bf..63476b0 100644 --- a/src/options.h +++ b/src/options.h @@ -31,6 +31,7 @@ enum timestamp_t TIMESTAMP_NONE, TIMESTAMP_24HOUR, TIMESTAMP_24HOUR_START, + TIMESTAMP_24HOUR_DELTA, TIMESTAMP_ISO8601, TIMESTAMP_END, }; diff --git a/src/tty.c b/src/tty.c index f1efa69..8843058 100644 --- a/src/tty.c +++ b/src/tty.c @@ -355,6 +355,9 @@ void handle_command_sequence(char input_char, char previous_char, char *output_c case TIMESTAMP_24HOUR_START: tio_printf("Switched to 24hour-start timestamp mode"); break; + case TIMESTAMP_24HOUR_DELTA: + tio_printf("Switched to 24hour-delta timestamp mode"); + break; case TIMESTAMP_ISO8601: tio_printf("Switched to iso8601 timestamp mode"); break;