From de6287bedb7aad024010ed74cca6f9ad8fc5ba07 Mon Sep 17 00:00:00 2001 From: Steve Karg Date: Tue, 23 Apr 2024 10:26:30 -0500 Subject: [PATCH] Added datetime to ASCII function. --- src/bacnet/datetime.c | 32 ++++++++++++++++++++++++++++++-- src/bacnet/datetime.h | 2 ++ test/bacnet/datetime/src/main.c | 9 +++++++++ 3 files changed, 41 insertions(+), 2 deletions(-) diff --git a/src/bacnet/datetime.c b/src/bacnet/datetime.c index a1ef8777..4785643a 100644 --- a/src/bacnet/datetime.c +++ b/src/bacnet/datetime.c @@ -1431,9 +1431,9 @@ int datetime_time_to_ascii(BACNET_TIME *btime, char *str, size_t str_size) } /** - * @brief Parse an ascii string for the date+time 2021/12/31 23:59:59.99 + * @brief Parse an ascii string for the date+time 2021/12/31-23:59:59.99 * @param bdate - #BACNET_DATE_TIME structure - * @param argv - C string with date+time formatted 2021/12/31 23:59:59.99 + * @param argv - C string with date+time formatted 2021/12/31-23:59:59.99 * @return true if parsed successfully */ bool datetime_init_ascii(BACNET_DATE_TIME *bdatetime, const char *ascii) @@ -1457,3 +1457,31 @@ bool datetime_init_ascii(BACNET_DATE_TIME *bdatetime, const char *ascii) return status; } + +/** + * @brief Print the date and time as an ascii string 2021/12/31-23:59:59.99 + * @param btime - pointer to a BACnetTime + * @param str - pointer to the string, or NULL for length only + * @param str_size - size of the string, or 0 for length only + * @return number of characters printed + */ +int datetime_to_ascii(BACNET_DATE_TIME *bdatetime, char *str, size_t str_size) +{ + int str_len = 0; + + if (!bdatetime) { + return 0; + } + /* 2021/12/31-23:59:59.99 */ + str_len = snprintf(str, str_size, + "%04u/%02u/%02u-%02u:%02u:%02u.%02u", + (unsigned)bdatetime->date.year, + (unsigned)bdatetime->date.month, + (unsigned)bdatetime->date.day, + (unsigned)bdatetime->time.hour, + (unsigned)bdatetime->time.min, + (unsigned)bdatetime->time.sec, + (unsigned)bdatetime->time.hundredths); + + return str_len; +} diff --git a/src/bacnet/datetime.h b/src/bacnet/datetime.h index 14595954..77ccc64a 100644 --- a/src/bacnet/datetime.h +++ b/src/bacnet/datetime.h @@ -257,6 +257,8 @@ BACNET_STACK_EXPORT int datetime_time_to_ascii(BACNET_TIME *btime, char *str, size_t str_size); BACNET_STACK_EXPORT bool datetime_init_ascii(BACNET_DATE_TIME *bdatetime, const char *ascii); +BACNET_STACK_EXPORT +int datetime_to_ascii(BACNET_DATE_TIME *bdatetime, char *str, size_t str_size); BACNET_STACK_EXPORT int bacapp_encode_datetime(uint8_t *apdu, BACNET_DATE_TIME *value); diff --git a/test/bacnet/datetime/src/main.c b/test/bacnet/datetime/src/main.c index 39807851..56cbdde9 100644 --- a/test/bacnet/datetime/src/main.c +++ b/test/bacnet/datetime/src/main.c @@ -590,6 +590,15 @@ static void testDatetimeCodec(void) } diff = datetime_compare(&datetimeOut, &datetimeIn); zassert_equal(diff, 0, NULL); + /* test datetime stringify */ + status = datetime_init_ascii(&datetimeIn, "1904/2/1-5:06:07.8"); + zassert_true(status, NULL); + str_len = datetime_to_ascii(&datetimeIn, str, sizeof(str)); + zassert_true(str_len > 0, NULL); + status = datetime_init_ascii(&datetimeOut, str); + zassert_true(status, NULL); + diff = datetime_compare(&datetimeOut, &datetimeIn); + zassert_equal(diff, 0, NULL); } static void testDatetimeConvertUTCSpecific(BACNET_DATE_TIME *utc_time,