diff --git a/drivers/include/periph/rtc.h b/drivers/include/periph/rtc.h index 016e922b06..7c87c91e7d 100644 --- a/drivers/include/periph/rtc.h +++ b/drivers/include/periph/rtc.h @@ -131,6 +131,22 @@ void rtc_poweroff(void); */ void rtc_tm_normalize(struct tm *time); +/** + * @brief Compare two time structs. + * + * @pre The time structs @p a and @p b are assumed to be normalized. + * Use @ref rtc_tm_normalize to normalize a struct tm that has been + * manually edited. + * + * @param[in] a The first time struct. + * @param[in] b The second time struct. + * + * @return an integer < 0 if a is earlier than b + * @return an integer > 0 if a is later than b + * @return 0 if a and b are equal + */ +int rtc_tm_compare(const struct tm *a, const struct tm *b); + #ifdef __cplusplus } #endif diff --git a/drivers/periph_common/rtc.c b/drivers/periph_common/rtc.c index 551bcd6e39..e2a5dc74fd 100644 --- a/drivers/periph_common/rtc.c +++ b/drivers/periph_common/rtc.c @@ -147,3 +147,20 @@ void rtc_tm_normalize(struct tm *t) t->tm_wday = _wday(t->tm_mday, t->tm_mon, t->tm_year + 1900); #endif } + +#define RETURN_IF_DIFFERENT(a, b, member) \ + if (a->member != b->member) { \ + return a->member-b->member; \ + } + +int rtc_tm_compare(const struct tm *a, const struct tm *b) +{ + RETURN_IF_DIFFERENT(a, b, tm_year); + RETURN_IF_DIFFERENT(a, b, tm_mon); + RETURN_IF_DIFFERENT(a, b, tm_mday); + RETURN_IF_DIFFERENT(a, b, tm_hour); + RETURN_IF_DIFFERENT(a, b, tm_min); + RETURN_IF_DIFFERENT(a, b, tm_sec); + + return 0; +}