Browse Source

ntp: simplify uptime info, drop legacy timelib funcs & consts

mcspr-patch-1
Maxim Prokhorov 4 years ago
parent
commit
83edf629cc
3 changed files with 20 additions and 79 deletions
  1. +3
    -69
      code/espurna/ntp_timelib.h
  2. +4
    -0
      code/espurna/scheduler.cpp
  3. +13
    -10
      code/espurna/utils.cpp

+ 3
- 69
code/espurna/ntp_timelib.h View File

@ -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 <cstdint>
#include <time.h>
#include <sys/time.h>
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 <typename T>
constexpr const T numberOfSeconds(T ts) {
return (ts % (T)secondsPerMinute);
}
template <typename T>
constexpr const T numberOfMinutes(T ts) {
return ((ts / (T)secondsPerMinute) % (T)secondsPerMinute);
}
template <typename T>
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);


+ 4
- 0
code/espurna/scheduler.cpp View File

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


+ 13
- 10
code/espurna/utils.cpp View File

@ -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() {


Loading…
Cancel
Save