From 788a61a7128568178103bb987f373056d5b835bd Mon Sep 17 00:00:00 2001 From: Matteo Cypriani Date: Tue, 15 Mar 2011 13:50:00 +0100 Subject: [PATCH] [lib] Add timestamp_to_string() --- libowlps/libowlps.c | 15 ++++++++++++++- libowlps/owlps.h | 3 +++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/libowlps/libowlps.c b/libowlps/libowlps.c index 5d3a239..4b4151e 100644 --- a/libowlps/libowlps.c +++ b/libowlps/libowlps.c @@ -15,7 +15,7 @@ BOOL run = TRUE ; /* * Converts a MAC address from bytes to string. - * /!\ You *must* manually free the returned string /!\ + * /!\ You *must* manually free the returned string. /!\ */ char* owl_mac_bytes_to_string(uint8_t *mac_binary) { @@ -147,6 +147,19 @@ uint64_t owl_timestamp_to_ms(TIMESTAMP d) +/* + * Converts a TIMESTAMP date value into a printable string. + * 'dst' must be an allocated array of at least TIMESTAMP_STR_LEN + * characters. + */ +void owl_timestamp_to_string(char *dst, TIMESTAMP src) +{ + snprintf(dst, TIMESTAMP_STR_LEN, "%"PRIu32".%"PRIu32, + src.tv_sec, src.tv_nsec) ; +} + + + /* * Returns the time (in milliseconds) between two dates. */ diff --git a/libowlps/owlps.h b/libowlps/owlps.h index 5404e2a..ca66e4e 100644 --- a/libowlps/owlps.h +++ b/libowlps/owlps.h @@ -66,6 +66,8 @@ typedef struct _TIMESTAMP uint32_t tv_sec ; uint32_t tv_nsec ; } TIMESTAMP ; +// Length of a TIMESTAMP when converted to string: +#define TIMESTAMP_STR_LEN 22 // 22 = 10 digits, '.', 10 digits, '\0' /* Message sent by the listener to the aggregator */ @@ -230,6 +232,7 @@ uint_fast8_t owl_frequency_to_channel(uint_fast16_t channel) ; int owl_timestamp_now(TIMESTAMP *now) ; TIMESTAMP owl_timespec_to_timestamp(const struct timespec d) ; TIMESTAMP owl_timeval_to_timestamp(const struct timeval d) ; +void owl_timestamp_to_string(char *dst, TIMESTAMP src) ; uint64_t owl_timestamp_to_ms(TIMESTAMP date) ; uint_fast32_t owl_time_elapsed(TIMESTAMP sup, TIMESTAMP inf) ; TIMESTAMP owl_hton_timestamp(TIMESTAMP date) ;