66 lines
1.6 KiB
C++
66 lines
1.6 KiB
C++
#include <time.h>
|
|
#include <stdlib.h>
|
|
|
|
#include <tfe_utils.h>
|
|
#include "tfe_timestamp.h"
|
|
|
|
// 1 s = 1000 ms
|
|
// 1 ms = 1000 us
|
|
// 1 us = 1000 ns
|
|
|
|
struct timestamp
|
|
{
|
|
struct timespec timestamp;
|
|
uint64_t update_interval_ms;
|
|
};
|
|
|
|
struct timestamp *timestamp_new(uint64_t update_interval_ms)
|
|
{
|
|
struct timestamp *ts = (struct timestamp *)calloc(1, sizeof(struct timestamp));
|
|
ts->update_interval_ms = update_interval_ms;
|
|
|
|
timestamp_update(ts);
|
|
TFE_LOG_DEBUG(g_default_logger, "%s: TIMESTAMP->update_interval_ms : %lu", LOG_TAG_TIMESTAMP, timestamp_update_interval_ms(ts));
|
|
TFE_LOG_DEBUG(g_default_logger, "%s: TIMESTAMP->current_sec : %lu", LOG_TAG_TIMESTAMP, timestamp_get_sec(ts));
|
|
TFE_LOG_DEBUG(g_default_logger, "%s: TIMESTAMP->current_msec : %lu", LOG_TAG_TIMESTAMP, timestamp_get_msec(ts));
|
|
|
|
return ts;
|
|
}
|
|
|
|
void timestamp_free(struct timestamp *ts)
|
|
{
|
|
if (ts)
|
|
{
|
|
free(ts);
|
|
ts = NULL;
|
|
}
|
|
}
|
|
|
|
void timestamp_update(struct timestamp *ts)
|
|
{
|
|
struct timespec temp;
|
|
clock_gettime(CLOCK_MONOTONIC, &temp);
|
|
ATOMIC_SET(&(ts->timestamp.tv_sec), temp.tv_sec);
|
|
ATOMIC_SET(&(ts->timestamp.tv_nsec), temp.tv_nsec);
|
|
}
|
|
|
|
uint64_t timestamp_update_interval_ms(struct timestamp *ts)
|
|
{
|
|
return ts->update_interval_ms;
|
|
}
|
|
|
|
uint64_t timestamp_get_sec(struct timestamp *ts)
|
|
{
|
|
uint64_t sec = ATOMIC_READ(&(ts->timestamp.tv_sec));
|
|
|
|
return sec;
|
|
}
|
|
|
|
uint64_t timestamp_get_msec(struct timestamp *ts)
|
|
{
|
|
uint64_t sec = ATOMIC_READ(&(ts->timestamp.tv_sec));
|
|
uint64_t nsec = ATOMIC_READ(&(ts->timestamp.tv_nsec));
|
|
|
|
return sec * 1000 + nsec / 1000000;
|
|
}
|