fix timestamp_to_date, etc. for nanosecond timestamps

This commit is contained in:
F. Duncanh
2023-02-15 02:17:49 -05:00
parent 28489d5658
commit 2f85d64571
2 changed files with 8 additions and 7 deletions

View File

@@ -300,7 +300,7 @@ raop_ntp_thread(void *arg)
(struct sockaddr *) &raop_ntp->remote_saddr, &raop_ntp->remote_saddr_len);
if (response_len < 0) {
timeout_counter++;
char time[28];
char time[30];
int level = (timeout_counter == 1 ? LOGGER_DEBUG : LOGGER_ERR);
ntp_timestamp_to_time(send_time, time, sizeof(time));
logger_log(raop_ntp->logger, level, "raop_ntp receive timeout %d (limit %d) (request sent %s)",

View File

@@ -21,6 +21,7 @@
#include <assert.h>
#include <time.h>
#include <stdint.h>
#define SECOND_IN_NSECS 1000000000UL
char *
utils_strsep(char **stringp, const char *delim)
@@ -218,21 +219,21 @@ char *utils_data_to_text(const char *data, int datalen) {
}
void ntp_timestamp_to_time(uint64_t ntp_timestamp, char *timestamp, size_t maxsize) {
time_t rawtime = (time_t) (ntp_timestamp / 1000000);
time_t rawtime = (time_t) (ntp_timestamp / SECOND_IN_NSECS);
struct tm ts = *localtime(&rawtime);
assert(maxsize > 26);
assert(maxsize > 29);
#ifdef _WIN32 /*modification for compiling for Windows */
strftime(timestamp, 20, "%Y-%m-%d %H:%M:%S", &ts);
#else
strftime(timestamp, 20, "%F %T", &ts);
#endif
snprintf(timestamp + 19, 8,".%6.6u", (unsigned int) ntp_timestamp % 1000000);
snprintf(timestamp + 19, 11,".%9.9lu", (unsigned long) ntp_timestamp % SECOND_IN_NSECS);
}
void ntp_timestamp_to_seconds(uint64_t ntp_timestamp, char *timestamp, size_t maxsize) {
time_t rawtime = (time_t) (ntp_timestamp / 1000000);
time_t rawtime = (time_t) (ntp_timestamp / SECOND_IN_NSECS);
struct tm ts = *localtime(&rawtime);
assert(maxsize > 9);
assert(maxsize > 12);
strftime(timestamp, 3, "%S", &ts);
snprintf(timestamp + 2, 8,".%6.6u", (unsigned int) ntp_timestamp % 1000000);
snprintf(timestamp + 2, 11,".%9.9lu", (unsigned long) ntp_timestamp % SECOND_IN_NSECS);
}