Add timestamp snprintf. Fix bacapp snprintf to account for string size zero behavior of snprintf.

This commit is contained in:
Steve Karg
2024-04-23 10:04:46 -05:00
parent 8729c00dbd
commit e73520a974
7 changed files with 299 additions and 142 deletions
+50 -1
View File
@@ -1344,6 +1344,30 @@ bool datetime_date_init_ascii(BACNET_DATE *bdate, const char *ascii)
return status;
}
/**
* @brief Print the date as an ascii string 2021/12/31
* @param bdate - pointer to a BACnetDate
* @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_date_to_ascii(BACNET_DATE *bdate, char *str, size_t str_size)
{
int str_len = 0;
if (!bdate) {
return 0;
}
/* 2021/12/31 */
str_len = snprintf(str, str_size,
"%04u/%02u/%02u",
(unsigned)bdate->year,
(unsigned)bdate->month,
(unsigned)bdate->day);
return str_len;
}
/**
* @brief Parse an ascii string for the time formatted 23:59:59.99
* @param btime - #BACNET_TIME structure
@@ -1381,6 +1405,31 @@ bool datetime_time_init_ascii(BACNET_TIME *btime, const char *ascii)
return status;
}
/**
* @brief Print the time as an ascii string 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_time_to_ascii(BACNET_TIME *btime, char *str, size_t str_size)
{
int str_len = 0;
if (!btime) {
return 0;
}
/* 23:59:59.99 */
str_len = snprintf(str, str_size,
"%02u:%02u:%02u.%02u",
(unsigned)btime->hour,
(unsigned)btime->min,
(unsigned)btime->sec,
(unsigned)btime->hundredths);
return str_len;
}
/**
* @brief Parse an ascii string for the date+time 2021/12/31 23:59:59.99
* @param bdate - #BACNET_DATE_TIME structure
@@ -1394,7 +1443,7 @@ bool datetime_init_ascii(BACNET_DATE_TIME *bdatetime, const char *ascii)
int hour = 0, min = 0, sec = 0, hundredths = 0;
int count = 0;
count = sscanf(ascii, "%4d/%3d/%3d %3d:%3d:%3d.%3d", &year, &month, &day,
count = sscanf(ascii, "%4d/%3d/%3d-%3d:%3d:%3d.%3d", &year, &month, &day,
&hour, &min, &sec, &hundredths);
if (count >= 3) {
datetime_set_date(