Skip to content
Snippets Groups Projects
Commit 2875ccf9 authored by Filip Matusiak's avatar Filip Matusiak
Browse files

Implement timestamp_to_time utility function

parent 29465724
Branches
No related tags found
1 merge request!139Issue initial channel scan
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <signal.h> #include <signal.h>
#define __USE_XOPEN
#include <time.h> #include <time.h>
#include <unistd.h> #include <unistd.h>
#include <sys/types.h> #include <sys/types.h>
...@@ -158,6 +159,7 @@ uint32_t timestamp_elapsed_sec(struct timespec *ts) ...@@ -158,6 +159,7 @@ uint32_t timestamp_elapsed_sec(struct timespec *ts)
if (ts->tv_sec == 0) if (ts->tv_sec == 0)
return -1; return -1;
/* seconds and nanoseconds since the Epoch */
if (clock_gettime(CLOCK_REALTIME, &now) < 0) if (clock_gettime(CLOCK_REALTIME, &now) < 0)
now.tv_sec = 0; now.tv_sec = 0;
...@@ -172,6 +174,7 @@ int timestamp_expired(struct timespec *a, unsigned int tmo_ms) ...@@ -172,6 +174,7 @@ int timestamp_expired(struct timespec *a, unsigned int tmo_ms)
struct timespec now; struct timespec now;
unsigned long diff_ns = 0, diff_s = 0; unsigned long diff_ns = 0, diff_s = 0;
/* seconds and nanoseconds since the Epoch */
if (clock_gettime(CLOCK_REALTIME, &now) < 0) if (clock_gettime(CLOCK_REALTIME, &now) < 0)
return -1; return -1;
...@@ -218,6 +221,55 @@ char *time_to_timestamp(time_t *t, char *tsp) ...@@ -218,6 +221,55 @@ char *time_to_timestamp(time_t *t, char *tsp)
return tsp; return tsp;
} }
time_t timestamp_to_time(const char *tsp)
{
struct tm tm_time;
char *tzone;
time_t res;
long int toff, sign;
int toff_hour, toff_min;
/* 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);
tzone = strchr(tsp, '+');
if (!tzone) {
tzone = strchr(tsp, '-');
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;
res += toff;
}
return res;
}
struct timespec time_to_timespec(time_t t)
{
struct timespec res = {};
res.tv_sec = t;
res.tv_nsec = 0;
return res;
}
struct timespec tsp_to_timespec(const char *tsp)
{
return time_to_timespec(timestamp_to_time(tsp));
}
/** list utility functions */ /** list utility functions */
/** /**
* list_num_entries - gets number of entries on the list * list_num_entries - gets number of entries on the list
......
...@@ -67,6 +67,9 @@ static inline int timestamp_invalid(struct timespec *ts) ...@@ -67,6 +67,9 @@ static inline int timestamp_invalid(struct timespec *ts)
uint32_t timestamp_elapsed_sec(struct timespec *ts); uint32_t timestamp_elapsed_sec(struct timespec *ts);
int timestamp_expired(struct timespec *a, unsigned int tmo_ms); int timestamp_expired(struct timespec *a, unsigned int tmo_ms);
char *time_to_timestamp(time_t *t, char *tsp); char *time_to_timestamp(time_t *t, char *tsp);
time_t timestamp_to_time(const char *tsp);
struct timespec time_to_timespec(time_t *t);
struct timespec tsp_to_timespec(const char *tsp);
/* bytes from-to hexstring helper functions */ /* bytes from-to hexstring helper functions */
int hex2byte(const char *hex); int hex2byte(const char *hex);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment