3#include <libtock/peripherals/rtc.h>
6static const uint8_t days_in_month[] = {31, 28, 31, 30, 31, 30,
7 31, 31, 30, 31, 30, 31};
10static inline int is_leap_year(uint16_t year) {
11 return ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0));
15static inline uint8_t get_feb_days(uint16_t year) {
16 return is_leap_year(year) ? 29 : 28;
20 libtock_rtc_date_t date = {};
21 libtocksync_rtc_get_date(&date);
26 libtock_rtc_date_t date = {};
28 libtocksync_rtc_set_date(&date);
33 const uint16_t epoch_year = 1970;
34 uint32_t total_seconds = 0;
38 for (year = epoch_year; year < date->year; year++) {
39 total_seconds += is_leap_year(year) ? 366 : 365;
41 total_seconds *= 86400;
45 for (uint8_t month = 0; month < (date->month - 1); month++) {
46 days += days_in_month[month];
48 if (month == 1 && is_leap_year(date->year)) {
52 total_seconds += days * 86400;
55 total_seconds += (date->day - 1) * 86400;
58 total_seconds += (uint32_t)date->hour * 3600;
59 total_seconds += (uint32_t)date->minute * 60;
60 total_seconds += date->seconds;
66 const uint16_t epoch_year = 1970;
68 uint32_t days =
epoch / 86400;
69 uint32_t seconds =
epoch % 86400;
72 uint16_t year = epoch_year;
74 uint16_t days_in_year = is_leap_year(year) ? 366 : 365;
75 if (days < days_in_year) {
86 uint8_t month_days = days_in_month[month];
87 if (month == 1 && is_leap_year(year)) {
91 if (days < month_days) {
98 date->month = month + 1;
101 date->day = days + 1;
104 date->hour = seconds / 3600;
107 date->minute = seconds / 60;
108 date->seconds = seconds % 60;
uint32_t epoch(void)
Gets the current unix epoch timestamp.
void set_epoch(uint32_t epoch)
Sets the current unix epoch timestamp.
uint32_t rtc_date_to_epoch(const libtock_rtc_date_t *date)
Converts tock time to unix epoch time.
void epoch_to_rtc_date(uint32_t epoch, libtock_rtc_date_t *date)
Convert unix epoch to tock time.