diff --git a/code/espurna/ntp_timelib.h b/code/espurna/ntp_timelib.h index 3dce5535..689f95b0 100644 --- a/code/espurna/ntp_timelib.h +++ b/code/espurna/ntp_timelib.h @@ -5,81 +5,15 @@ Part of NTP MODULE */ // Based on https://github.com/PaulStoffregen/time +// Avoid doing any math (elapsed..., numberOf... functions and etc.), +// simply expect POSIX time API usage, and provide bare minimum to simplify `tm` access #pragma once +#include #include #include -constexpr time_t daysPerWeek = 7; - -constexpr time_t secondsPerMinute = 60; -constexpr time_t secondsPerHour = 3600; -constexpr time_t secondsPerDay = secondsPerHour * 24; -constexpr time_t secondsPerWeek = daysPerWeek * secondsPerDay; - -constexpr time_t secondsPerYear = secondsPerWeek * 52; -constexpr time_t secondsY2K = 946684800; // the time at the start of y2k - -// wall clock values -template -constexpr const T numberOfSeconds(T ts) { - return (ts % (T)secondsPerMinute); -} - -template -constexpr const T numberOfMinutes(T ts) { - return ((ts / (T)secondsPerMinute) % (T)secondsPerMinute); -} - -template -constexpr const time_t numberOfHours(T ts) { - return ((ts % (T)secondsPerDay) / (T)secondsPerHour); -} - -// week starts with sunday as number 1, monday as 2 etc. -constexpr const int dayOfWeek(time_t ts) { - return ((ts / secondsPerDay + 4) % daysPerWeek) + 1; -} - -// the number of days since 0 (Jan 1 1970 in case of time_t values) -constexpr const int elapsedDays(uint32_t ts) { - return (ts / secondsPerDay); -} - -// the number of seconds since last midnight -constexpr const uint32_t elapsedSecsToday(uint32_t ts) { - return (ts % (uint32_t)secondsPerDay); -} - -// note that week starts on day 1 -constexpr const uint32_t elapsedSecsThisWeek(uint32_t ts) { - return elapsedSecsToday(ts) + ((dayOfWeek(ts) - 1) * (uint32_t)secondsPerDay); -} - -// The following methods are used in calculating alarms and assume the clock is set to a date later than Jan 1 1971 -// Always set the correct time before settting alarms - -// time at the start of the given day -constexpr const time_t previousMidnight(time_t ts) { - return ((ts / secondsPerDay) * secondsPerDay); -} - -// time at the end of the given day -constexpr const time_t nextMidnight(time_t ts) { - return previousMidnight(ts) + secondsPerDay; -} - -// time at the start of the week for the given time -constexpr const time_t previousSunday(time_t ts) { - return ts - elapsedSecsThisWeek(ts); -} - -// time at the end of the week for the given time -constexpr const time_t nextSunday(time_t ts) { - return previousSunday(ts) + secondsPerWeek; -} - int utc_hour(time_t ts); int utc_minute(time_t ts); int utc_second(time_t ts); diff --git a/code/espurna/scheduler.cpp b/code/espurna/scheduler.cpp index 3d756b1e..83201d5c 100644 --- a/code/espurna/scheduler.cpp +++ b/code/espurna/scheduler.cpp @@ -203,6 +203,10 @@ NtpCalendarWeekday _schGetWeekday(time_t timestamp, int daybefore) { #else +constexpr time_t secondsPerMinute = 60; +constexpr time_t secondsPerHour = 3600; +constexpr time_t secondsPerDay = secondsPerHour * 24; + NtpCalendarWeekday _schGetWeekday(time_t timestamp, int daybefore) { tm utc_time; tm local_time; diff --git a/code/espurna/utils.cpp b/code/espurna/utils.cpp index e290d5cc..1c0e9ef3 100644 --- a/code/espurna/utils.cpp +++ b/code/espurna/utils.cpp @@ -284,16 +284,19 @@ namespace Heartbeat { } void infoUptime() { - const auto uptime [[gnu::unused]] = getUptime(); - #if NTP_SUPPORT - DEBUG_MSG_P( - PSTR("[MAIN] Uptime: %02dd %02dh %02dm %02ds\n"), - elapsedDays(uptime), numberOfHours(uptime), - numberOfMinutes(uptime), numberOfSeconds(uptime) - ); - #else - DEBUG_MSG_P(PSTR("[MAIN] Uptime: %lu seconds\n"), uptime); - #endif // NTP_SUPPORT +#if NTP_SUPPORT + time_t uptime = getUptime(); + tm spec; + gmtime_r(&uptime, &spec); + + DEBUG_MSG_P( + PSTR("[MAIN] Uptime: %02dy %02dd %02dh %02dm %02ds\n"), + (spec.tm_year - 70), spec.tm_yday, spec.tm_hour, + spec.tm_min, spec.tm_sec + ); +#else + DEBUG_MSG_P(PSTR("[MAIN] Uptime: %lu seconds\n"), uptime); +#endif // NTP_SUPPORT } void heartbeat() {