Skip to content
Snippets Groups Projects
Commit e3d90b12 authored by Janusz Dziedzic's avatar Janusz Dziedzic
Browse files

use common timestamp code

parent 59a3bb0e
No related branches found
No related tags found
1 merge request!291use common timestamp code
Pipeline #132137 passed
......@@ -27,7 +27,7 @@ exec_cmd git clone -b devel https://dev.iopsys.eu/iopsys/libeasy.git
cd libeasy
exec_cmd make CFLAGS+="-I/usr/include/libnl3"
exec_cmd cp -a libeasy*.so* /usr/lib
exec_cmd cp -a easy.h event.h utils.h if_utils.h debug.h hlist.h bufutil.h cryptutil.h ecc_cryptutil.h /usr/include/easy/
exec_cmd cp -a easy.h event.h utils.h if_utils.h debug.h hlist.h bufutil.h cryptutil.h ecc_cryptutil.h timestamp.h /usr/include/easy/
# libwifiutils
......
......@@ -151,165 +151,6 @@ int hwaddr_from_ip(char *ifname, char *ipstr, unsigned char *hw)
return 0;
}
void timestamp_reset(struct timespec *ts)
{
ts->tv_sec = 0;
ts->tv_nsec = 0;
}
void timestamp_update(struct timespec *ts)
{
if (clock_gettime(CLOCK_REALTIME, ts) < 0) {
ts->tv_sec = 0;
ts->tv_nsec = 0;
}
}
int timestamp_invalid(struct timespec *ts)
{
return ts->tv_sec == 0 && ts->tv_nsec == 0;
}
/* Time difference in seconds */
uint32_t timestamp_elapsed_sec(const struct timespec *ts)
{
struct timespec now;
uint32_t elapsed;
if (ts->tv_sec == 0)
return -1;
/* seconds and nanoseconds since the Epoch */
if (clock_gettime(CLOCK_REALTIME, &now) < 0)
now.tv_sec = 0;
elapsed = now.tv_sec - ts->tv_sec;
return (elapsed > 0) ? elapsed : 0;
}
/* check if timestamp expired */
int timestamp_expired(struct timespec *a, unsigned int tmo_ms)
{
struct timespec now;
unsigned long diff_ns = 0, diff_s = 0;
/* seconds and nanoseconds since the Epoch */
if (clock_gettime(CLOCK_REALTIME, &now) < 0)
return -1;
diff_s = now.tv_sec - a->tv_sec;
diff_ns = now.tv_nsec - a->tv_nsec;
if ((long)diff_ns < 0) {
diff_ns += 1000000000UL;
diff_s--;
}
if (diff_s * 1000 + diff_ns / 1000000 >= tmo_ms)
return 1;
return 0;
}
char *time_to_timestamp(const time_t *t, char *tsp)
{
char tmpbuf[64] = {0};
struct tm res;
char sign;
long int toff, toff_hour, toff_min;
if (!tsp)
return NULL;
/* E.g. "2019-02-11T06:42:31.23039-08:00" */
localtime_r(t, &res);
tzset();
toff = timezone;
sign = toff > 0 ? '-' : '+';
toff *= -1L;
toff_hour = toff / 3600;
toff_min = (toff % 3600) / 60;
snprintf(tmpbuf, sizeof(tmpbuf), "%04d-%02d-%02dT%02d:%02d:%02d%c%02ld:%02ld",
res.tm_year + 1900, res.tm_mon + 1, res.tm_mday,
res.tm_hour, res.tm_min, res.tm_sec,
sign, toff_hour, toff_min);
snprintf(tsp, 64, "%s", tmpbuf);
return tsp;
}
/* get time adjustment seconds (time(tzone) - time(UTC) secs) */
static long int timestamp_get_off_sec(const char *tsp)
{
char *tzone;
long int toff = 0, sign;
int toff_hour, toff_min;
/* Example timestamp: "2019-02-11T06:42:31-08:00" */
tzone = strchr(tsp, '+');
if (!tzone) {
tzone = strrchr(tsp, '-'); /* last occurence */
sign = -1L;
} else {
sign = +1L;
}
if (tzone) {
sscanf(tzone+1, "%02d:%02d", &toff_hour, &toff_min);
toff = toff_hour * 3600 + toff_min * 60; // seconds
toff *= -sign;
}
return toff;
}
/* Returns time alligned to UTC+0 */
time_t timestamp_to_time(const char *tsp)
{
struct tm tm_time;
time_t res;
/* Example timestamp: "2019-02-11T06:42:31-08:00" */
memset(&tm_time, 0, sizeof(tm_time));
strptime(tsp, "%Y-%m-%dT%H:%M:%S", &tm_time);
tzset();
res = mktime(&tm_time);
/* Allign by toff to get UTC+0 */
res += timestamp_get_off_sec(tsp);
return res;
}
struct timespec time_to_timespec(time_t t)
{
struct timespec res = {};
res.tv_sec = t;
res.tv_nsec = 0;
return res;
}
/* converts timestamp string to timespec struct
* adj_rtime true: adjust for realtime (ignore off time)
*/
struct timespec timestamp_to_timespec(const char *tsp, bool adj_rtime)
{
time_t tt = 0;
tt = timestamp_to_time(tsp);
if (adj_rtime)
tt -= timestamp_get_off_sec(tsp);
return time_to_timespec(tt);
}
/** list utility functions */
/**
......
......@@ -42,16 +42,6 @@ unsigned char *hwaddr_aton(const char *macstr, unsigned char *mac);
char *hwaddr_ntoa(const unsigned char *mac, char *macstr);
int hwaddr_from_ip(char *ifname, char *ipstr, unsigned char *hw);
void timestamp_reset(struct timespec *ts);
void timestamp_update(struct timespec *ts);
int timestamp_invalid(struct timespec *ts);
uint32_t timestamp_elapsed_sec(const struct timespec *ts);
int timestamp_expired(struct timespec *a, unsigned int tmo_ms);
char *time_to_timestamp(const time_t *t, char *tsp);
time_t timestamp_to_time(const char *tsp);
struct timespec time_to_timespec(time_t *t);
struct timespec timestamp_to_timespec(const char *tsp, bool adj_rtime);
/* bytes from-to hexstring helper functions */
int hex2byte(const char *hex);
unsigned char *strtob(char *str, int len, unsigned char *bytes);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment