From e46df744ce2dbb989d98df94df4b3f85f7b681d6 Mon Sep 17 00:00:00 2001 From: mikee47 Date: Tue, 23 Apr 2024 20:30:47 +0100 Subject: [PATCH] SystemClock_NTP sample: Timezone code relates to system, move out of class --- samples/SystemClock_NTP/app/NtpClientDemo.cpp | 66 +++++++++++-------- samples/SystemClock_NTP/app/application.cpp | 3 +- .../SystemClock_NTP/include/NtpClientDemo.h | 16 ++--- 3 files changed, 43 insertions(+), 42 deletions(-) diff --git a/samples/SystemClock_NTP/app/NtpClientDemo.cpp b/samples/SystemClock_NTP/app/NtpClientDemo.cpp index 7ed6c6ec1d..8fb026c185 100644 --- a/samples/SystemClock_NTP/app/NtpClientDemo.cpp +++ b/samples/SystemClock_NTP/app/NtpClientDemo.cpp @@ -1,9 +1,44 @@ #include +#include +#include #include -const TimeChangeRule NtpClientDemo::dstStart = {"BST", Last, Sun, Mar, 1, 60}; -const TimeChangeRule NtpClientDemo::stdStart = {"GMT", Last, Sun, Oct, 2, 0}; -Timezone NtpClientDemo::tz(dstStart, stdStart); +namespace +{ +/* + * For handling local/UTC time conversions + * This is for the UK, amend as required + */ +const TimeChangeRule dstStart{"BST", Last, Sun, Mar, 1, 60}; +const TimeChangeRule stdStart{"GMT", Last, Sun, Oct, 2, 0}; +Timezone tz(dstStart, stdStart); + +/* + * We use the y/m/d from local time for sunrise/sunset calculations, and the solar calculator + * returns the time from midnight in UTC for that day. We therefore need to adjust this + * to account for timezone and daylight savings. + */ +time_t getNextSunriseSet(bool isSunrise) +{ + auto timeNow = SystemClock.now(eTZ_Local); + DateTime dt(timeNow); + dt.Hour = 0; + dt.Minute = 0; + dt.Second = 0; + SolarCalculator calc; + int offset_secs = SECS_PER_MIN * calc.sunRiseSet(isSunrise, dt.Year, dt.Month + 1, dt.Day); + + time_t t = tz.toLocal(dt + offset_secs); + + // If time has already passed, then make it tomorrow + if(t < timeNow) { + t = tz.toLocal(dt + offset_secs + SECS_PER_DAY); + } + + return t; +} + +} // namespace void NtpClientDemo::ntpResult(NtpClient& client, time_t ntpTime) { @@ -30,28 +65,3 @@ void NtpClientDemo::ntpResult(NtpClient& client, time_t ntpTime) Serial << _F("Next sunrise at ") << sunrise.toShortTimeString() << _F(", sunset at ") << sunset.toShortTimeString() << endl; } - -/* - * We use the y/m/d from local time for sunrise/sunset calculations, and the solar calculator - * returns the time from midnight in UTC for that day. We therefore need to adjust this - * to account for timezone and daylight savings. - */ -time_t NtpClientDemo::getNextSunriseSet(bool isSunrise) -{ - auto timeNow = SystemClock.now(eTZ_Local); - DateTime dt(timeNow); - dt.Hour = 0; - dt.Minute = 0; - dt.Second = 0; - SolarCalculator calc; - int offset_secs = SECS_PER_MIN * calc.sunRiseSet(isSunrise, dt.Year, dt.Month + 1, dt.Day); - - time_t t = tz.toLocal(dt + offset_secs); - - // If time has already passed, then make it tomorrow - if(t < timeNow) { - t = tz.toLocal(dt + offset_secs + SECS_PER_DAY); - } - - return t; -} diff --git a/samples/SystemClock_NTP/app/application.cpp b/samples/SystemClock_NTP/app/application.cpp index ecd3121802..077fa39682 100644 --- a/samples/SystemClock_NTP/app/application.cpp +++ b/samples/SystemClock_NTP/app/application.cpp @@ -9,8 +9,7 @@ namespace { -#pragma GCC diagnostic ignored "-Wunused-function" -void onNtpReceive(NtpClient& client, time_t timestamp); +[[maybe_unused]] void onNtpReceive(NtpClient& client, time_t timestamp); SimpleTimer printTimer; diff --git a/samples/SystemClock_NTP/include/NtpClientDemo.h b/samples/SystemClock_NTP/include/NtpClientDemo.h index 9f6dbbfed0..037bf76cb2 100644 --- a/samples/SystemClock_NTP/include/NtpClientDemo.h +++ b/samples/SystemClock_NTP/include/NtpClientDemo.h @@ -1,26 +1,18 @@ #pragma once -#include +#include +#include #include class NtpClientDemo { public: - NtpClientDemo() : ntpcp(nullptr, 30, NtpTimeResultDelegate(&NtpClientDemo::ntpResult, this)) + NtpClientDemo() : client(nullptr, 30, NtpTimeResultDelegate(&NtpClientDemo::ntpResult, this)) { } void ntpResult(NtpClient& client, time_t ntpTime); - time_t getNextSunriseSet(bool isSunrise); - private: - NtpClient ntpcp; - /* - * For handling local/UTC time conversions - * This is for the UK, amend as required - */ - static const TimeChangeRule dstStart; - static const TimeChangeRule stdStart; - static Timezone tz; + NtpClient client; };