1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2025-12-27 23:41:18 +01:00

periph_common/rtc: add rtc_tm_compare()

Add an easy way to compare two points in time.
This commit is contained in:
Benjamin Valentin 2019-11-04 16:16:07 +01:00 committed by Benjamin Valentin
parent b8b7606a7c
commit 513a3a7d59
2 changed files with 33 additions and 0 deletions

View File

@ -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

View File

@ -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;
}