1 /* 2 * Copyright (c) 2023 Bjarki Arge Andreasen 3 * Copyright (c) 2024 Andrew Featherstone 4 * 5 * SPDX-License-Identifier: Apache-2.0 6 */ 7 8 #include <stdbool.h> 9 #include <stdint.h> 10 11 #include <zephyr/drivers/rtc.h> 12 13 #include "rtc_utils.h" 14 rtc_utils_validate_rtc_time(const struct rtc_time * timeptr,uint16_t mask)15bool rtc_utils_validate_rtc_time(const struct rtc_time *timeptr, uint16_t mask) 16 { 17 if ((mask & RTC_ALARM_TIME_MASK_SECOND) && (timeptr->tm_sec < 0 || timeptr->tm_sec > 59)) { 18 return false; 19 } 20 21 if ((mask & RTC_ALARM_TIME_MASK_MINUTE) && (timeptr->tm_min < 0 || timeptr->tm_min > 59)) { 22 return false; 23 } 24 25 if ((mask & RTC_ALARM_TIME_MASK_HOUR) && (timeptr->tm_hour < 0 || timeptr->tm_hour > 23)) { 26 return false; 27 } 28 29 if ((mask & RTC_ALARM_TIME_MASK_MONTH) && (timeptr->tm_mon < 0 || timeptr->tm_mon > 11)) { 30 return false; 31 } 32 33 if ((mask & RTC_ALARM_TIME_MASK_MONTHDAY) && 34 (timeptr->tm_mday < 1 || timeptr->tm_mday > 31)) { 35 return false; 36 } 37 38 if ((mask & RTC_ALARM_TIME_MASK_YEAR) && (timeptr->tm_year < 0 || timeptr->tm_year > 199)) { 39 return false; 40 } 41 42 if ((mask & RTC_ALARM_TIME_MASK_WEEKDAY) && 43 (timeptr->tm_wday < 0 || timeptr->tm_wday > 6)) { 44 return false; 45 } 46 47 if ((mask & RTC_ALARM_TIME_MASK_YEARDAY) && 48 (timeptr->tm_yday < 0 || timeptr->tm_yday > 365)) { 49 return false; 50 } 51 52 if ((mask & RTC_ALARM_TIME_MASK_NSEC) && 53 (timeptr->tm_nsec < 0 || timeptr->tm_nsec > 999999999)) { 54 return false; 55 } 56 57 return true; 58 } 59