mirror of
https://github.com/tio/tio.git
synced 2026-05-01 14:57:59 +02:00
Consolidate timestamp implementation in one file
This commit is contained in:
parent
a85cfcf3ef
commit
ba2d49d2f7
10 changed files with 202 additions and 139 deletions
64
src/misc.c
64
src/misc.c
|
|
@ -25,75 +25,11 @@
|
|||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
#include <sys/time.h>
|
||||
#include <errno.h>
|
||||
#include "error.h"
|
||||
#include "print.h"
|
||||
#include "options.h"
|
||||
|
||||
#define TIME_STRING_SIZE_MAX 24
|
||||
|
||||
char *current_time(void)
|
||||
{
|
||||
static char time_string[TIME_STRING_SIZE_MAX];
|
||||
static struct timeval tv, tv_now, tv_start, tv_previous;
|
||||
static bool first = true;
|
||||
struct tm *tm;
|
||||
size_t len;
|
||||
|
||||
// Get current time value
|
||||
gettimeofday(&tv_now, NULL);
|
||||
|
||||
if (first)
|
||||
{
|
||||
tv_start = tv_now;
|
||||
first = false;
|
||||
}
|
||||
|
||||
// Add formatted timestap
|
||||
switch (option.timestamp)
|
||||
{
|
||||
case TIMESTAMP_NONE:
|
||||
case TIMESTAMP_24HOUR:
|
||||
// "hh:mm:ss.sss" (24 hour format)
|
||||
tv = tv_now;
|
||||
tm = localtime(&tv.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_now, &tv_start, &tv);
|
||||
tm = gmtime(&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);
|
||||
tm = gmtime(&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)
|
||||
tv = tv_now;
|
||||
tm = localtime(&tv.tv_sec);
|
||||
len = strftime(time_string, sizeof(time_string), "%Y-%m-%dT%H:%M:%S", tm);
|
||||
break;
|
||||
default:
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Append milliseconds to all timestamps
|
||||
if (len)
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
void delay(long ms)
|
||||
{
|
||||
struct timespec ts;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue