Added datetime to ASCII function.

This commit is contained in:
Steve Karg
2024-04-23 10:26:30 -05:00
parent e73520a974
commit de6287bedb
3 changed files with 41 additions and 2 deletions
+30 -2
View File
@@ -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;
}
+2
View File
@@ -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);
+9
View File
@@ -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,