From 2f85d64571920959d4740e399cb8c29be82f52e8 Mon Sep 17 00:00:00 2001 From: "F. Duncanh" Date: Wed, 15 Feb 2023 02:17:49 -0500 Subject: [PATCH] fix timestamp_to_date, etc. for nanosecond timestamps --- lib/raop_ntp.c | 2 +- lib/utils.c | 13 +++++++------ 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/lib/raop_ntp.c b/lib/raop_ntp.c index 494f705..7fd5361 100644 --- a/lib/raop_ntp.c +++ b/lib/raop_ntp.c @@ -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)", diff --git a/lib/utils.c b/lib/utils.c index 633d01a..230dd15 100644 --- a/lib/utils.c +++ b/lib/utils.c @@ -21,6 +21,7 @@ #include #include #include +#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); }