-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
examples/rust-matter-light: Make periph_rtc feature optional. If not …
…supported, get seconds since UNIX epoch from ztimer_sec
- Loading branch information
Showing
2 changed files
with
19 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,24 @@ | ||
#include <stdint.h> | ||
#include "modules.h" | ||
|
||
#if IS_USED(MODULE_PERIPH_RTC) | ||
#include "periph/rtc.h" | ||
#include <time.h> | ||
#else | ||
#include "ztimer.h" | ||
#endif | ||
|
||
// RIOT_EPOCH(2020) in seconds since UNIX Epoch | ||
#define RIOT_EPOCH_SECS 1577833200; | ||
|
||
uint32_t get_seconds_since_unix_epoch(void) { | ||
// Get time since RIOT_EPOCH from RTC | ||
#if IS_USED(MODULE_PERIPH_RTC) | ||
// Get seconds since RIOT_EPOCH from RTC | ||
struct tm now; | ||
rtc_get_time(&now); | ||
// Add to RIOT_EPOCH to get current seconds since UNIX epoch | ||
return rtc_mktime(&now) + RIOT_EPOCH_SECS; | ||
// Add to RIOT_EPOCH to get seconds since UNIX epoch | ||
return RIOT_EPOCH_SECS + rtc_mktime(&now); | ||
#else | ||
// Get elapsed seconds from ztimer | ||
return RIOT_EPOCH_SECS + ztimer_now(ZTIMER_SEC); | ||
#endif | ||
} |