diff --git a/src/agent_tlv.c b/src/agent_tlv.c index e6fece07260cf10dbea23acb12f3bfd4330555cb..8d164c916afa7632b670187c4935d5da51d6f11d 100644 --- a/src/agent_tlv.c +++ b/src/agent_tlv.c @@ -3078,37 +3078,6 @@ char *get_timestamp_old(time_t *t, char **tbuf) return *tbuf; } -char *get_timestamp(time_t *t, char *tbuf) -{ - char tmpbuf[64] = {0}; - struct tm res; - char sign; - long int toff, toff_hour, toff_min; - const time_t now = time(t); - - if (!tbuf) - return NULL; - - /* E.g. "2019-02-11T06:42:31.23039-08:00" */ - - localtime_r(&now, &res); - tzset(); - toff = timezone; - sign = toff > 0 ? '-' : '+'; - toff *= -1L; - - toff_hour = toff / 3600; - toff_min = (toff % 3600) / 60; - - snprintf(tmpbuf, 63, "%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(tbuf, 64, "%s", tmpbuf); - return tbuf; -} - int agent_gen_timestamp_tlv(struct agent *agent, struct cmdu_buff *frm) { int ret; @@ -3123,7 +3092,7 @@ int agent_gen_timestamp_tlv(struct agent *agent, struct cmdu_buff *frm) /* Define the TLV */ t->type = MAP_TLV_TIMESTAMP; data = (struct tlv_timestamp *) t->data; - get_timestamp(NULL, tsp); + time_to_timestr(NULL, tsp); data->len = strlen(tsp); memcpy(data->timestamp, (uint8_t *)tsp, data->len); t->len = sizeof(*data) + data->len; @@ -3230,7 +3199,7 @@ int agent_gen_ch_scan_response_tlv(struct agent *a, struct cmdu_buff *cmdu, trace("\t INFO: radio " MACFMT ", opclass %d, channel %d\n", MAC2STR(radio_mac), opclass_id, ch->channel); - get_timestamp(NULL, tsp); + time_to_timestr(NULL, tsp); memcpy(ch->tsp, tsp, strlen(tsp)); t = cmdu_reserve_scan_response_tlv(cmdu, reserve_len, tsp, radio_mac, opclass_id, ch, status, &offset); diff --git a/src/timer.c b/src/timer.c index 9fd2846e4e3bb74810562cbec9198d0d6aff7417..e96d413bf430d1616c3fc8cb133746ea99b1f7e2 100644 --- a/src/timer.c +++ b/src/timer.c @@ -13,6 +13,8 @@ #include <libubox/uloop.h> #include <stdint.h> +#include <stdio.h> +#include <string.h> #include <sys/time.h> #include <time.h> @@ -96,3 +98,90 @@ bool timestamp_less_or_equal(const struct timespec *lhs, const struct timespec * { return !timestamp_less_than(rhs, lhs); } + +char *time_to_timestr(const time_t *t, char *tsp) +{ + char tmpbuf[64] = {0}; + struct tm res; + char sign; + long toff, toff_hour, toff_min; + time_t tt; + + if (!tsp) + return NULL; + + if (!t) + /* use now */ + time(&tt); + else + tt = *t; + + /* E.g. "2019-02-11T06:42:31.23039-08:00" */ + + localtime_r(&tt, &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 timestamp_get_off_sec(const char *tsp) +{ + char *tzone; + 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) { + int ret = 0; + + ret = sscanf(tzone+1, "%02d:%02d", &toff_hour, &toff_min); + if (ret == 2) { + toff = toff_hour * 3600 + toff_min * 60; // seconds + toff *= -sign; + } + } + + return toff; +} + +/* Returns time alligned to UTC+0 */ +time_t timestr_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; +} diff --git a/src/timer.h b/src/timer.h index 9e7b3274f61bed9a7bcc88885796faed04500c8e..3324ad9ab681d4333893d9dbd4cd2487fed53158 100644 --- a/src/timer.h +++ b/src/timer.h @@ -34,5 +34,7 @@ bool timestamp_greater_than(const struct timespec *lhs, const struct timespec *r bool timestamp_greater_or_equal(const struct timespec *lhs, const struct timespec *rhs); bool timestamp_less_or_equal(const struct timespec *lhs, const struct timespec *rhs); +char *time_to_timestr(const time_t *t, char *tsp); +time_t timestr_to_time(const char *tsp); #endif /* ATIMER_H */