Added a datetime_is_valid() function for checking a valid calendar date.
This commit is contained in:
@@ -87,6 +87,10 @@ extern "C" {
|
|||||||
uint8_t minute,
|
uint8_t minute,
|
||||||
uint8_t seconds,
|
uint8_t seconds,
|
||||||
uint8_t hundredths);
|
uint8_t hundredths);
|
||||||
|
/* utility test for validity */
|
||||||
|
bool datetime_is_valid(
|
||||||
|
BACNET_DATE *bdate,
|
||||||
|
BACNET_TIME *btime);
|
||||||
|
|
||||||
/* utility comparison functions:
|
/* utility comparison functions:
|
||||||
if the date/times are the same, return is 0
|
if the date/times are the same, return is 0
|
||||||
@@ -113,7 +117,7 @@ extern "C" {
|
|||||||
BACNET_DATE_TIME * datetime1,
|
BACNET_DATE_TIME * datetime1,
|
||||||
BACNET_DATE_TIME * datetime2);
|
BACNET_DATE_TIME * datetime2);
|
||||||
|
|
||||||
/* utility add function */
|
/* utility add or subtract minutes function */
|
||||||
void datetime_add_minutes(
|
void datetime_add_minutes(
|
||||||
BACNET_DATE_TIME * bdatetime,
|
BACNET_DATE_TIME * bdatetime,
|
||||||
int32_t minutes);
|
int32_t minutes);
|
||||||
|
|||||||
@@ -82,18 +82,33 @@ static uint8_t month_days(
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static bool date_is_valid(
|
||||||
|
uint16_t year,
|
||||||
|
uint8_t month,
|
||||||
|
uint8_t day)
|
||||||
|
{
|
||||||
|
bool status = false; /* true if value date */
|
||||||
|
uint8_t monthdays; /* days in a month */
|
||||||
|
|
||||||
|
|
||||||
|
monthdays = month_days(year, month);
|
||||||
|
if ((year >= 1900) && (monthdays) && (day >= 1) && (day <= monthdays)) {
|
||||||
|
status = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return status;
|
||||||
|
}
|
||||||
|
|
||||||
static uint32_t days_since_epoch(
|
static uint32_t days_since_epoch(
|
||||||
uint16_t year,
|
uint16_t year,
|
||||||
uint8_t month,
|
uint8_t month,
|
||||||
uint8_t day)
|
uint8_t day)
|
||||||
{
|
{
|
||||||
uint32_t days = 0; /* return value */
|
uint32_t days = 0; /* return value */
|
||||||
uint8_t monthdays; /* days in a month */
|
|
||||||
uint16_t years = 0; /* loop counter for years */
|
uint16_t years = 0; /* loop counter for years */
|
||||||
uint8_t months = 0; /* loop counter for months */
|
uint8_t months = 0; /* loop counter for months */
|
||||||
|
|
||||||
monthdays = month_days(year, month);
|
if (date_is_valid(year, month, day)) {
|
||||||
if ((year >= 1900) && (monthdays) && (day >= 1) && (day <= monthdays)) {
|
|
||||||
for (years = 1900; years < year; years++) {
|
for (years = 1900; years < year; years++) {
|
||||||
days += 365;
|
days += 365;
|
||||||
if (is_leap_year(years))
|
if (is_leap_year(years))
|
||||||
@@ -144,7 +159,6 @@ static void days_since_epoch_into_ymd(
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/* Jan 1, 1900 is a Monday */
|
/* Jan 1, 1900 is a Monday */
|
||||||
/* wday 1=Monday...7=Sunday */
|
/* wday 1=Monday...7=Sunday */
|
||||||
static uint8_t day_of_week(
|
static uint8_t day_of_week(
|
||||||
@@ -155,6 +169,50 @@ static uint8_t day_of_week(
|
|||||||
return (uint8_t) ((days_since_epoch(year, month, day) % 7) + 1);
|
return (uint8_t) ((days_since_epoch(year, month, day) % 7) + 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static bool time_is_valid(
|
||||||
|
uint8_t hour,
|
||||||
|
uint8_t min,
|
||||||
|
uint8_t sec,
|
||||||
|
uint8_t hundredths)
|
||||||
|
{
|
||||||
|
bool status = false;
|
||||||
|
|
||||||
|
if ((hour < 24) && (min < 60) && (sec < 60) && (hundredths < 100)) {
|
||||||
|
status = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return status;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Determines if a given date and time is valid for calendar
|
||||||
|
*
|
||||||
|
* @param bdate - pointer to a BACNET_DATE structure
|
||||||
|
* @param btime - pointer to a BACNET_TIME structure
|
||||||
|
*
|
||||||
|
* @return true if the date and time are valid
|
||||||
|
*/
|
||||||
|
bool datetime_is_valid(
|
||||||
|
BACNET_DATE *bdate,
|
||||||
|
BACNET_TIME *btime)
|
||||||
|
{
|
||||||
|
bool status = false; /* return value */
|
||||||
|
|
||||||
|
/* get the number of days in the month, and check for valid month too */
|
||||||
|
if (bdate) {
|
||||||
|
status = date_is_valid(bdate->year, bdate->month, bdate->day);
|
||||||
|
if (status && btime) {
|
||||||
|
status = time_is_valid(btime->hour, btime->min, btime->sec,
|
||||||
|
btime->hundredths);
|
||||||
|
} else {
|
||||||
|
status = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return status;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/* if the date1 is the same as date2, return is 0
|
/* if the date1 is the same as date2, return is 0
|
||||||
if date1 is after date2, returns positive
|
if date1 is after date2, returns positive
|
||||||
if date1 is before date2, returns negative */
|
if date1 is before date2, returns negative */
|
||||||
|
|||||||
Reference in New Issue
Block a user