Skip to content

Commit

Permalink
SystemClock_NTP sample: Timezone code relates to system, move out of …
Browse files Browse the repository at this point in the history
…class
  • Loading branch information
mikee47 authored and mikee47 committed Jun 19, 2024
1 parent 3c84c9f commit e46df74
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 42 deletions.
66 changes: 38 additions & 28 deletions samples/SystemClock_NTP/app/NtpClientDemo.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,44 @@
#include <NtpClientDemo.h>
#include <SystemClock.h>
#include <HardwareSerial.h>
#include <SolarCalculator.h>

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)
{
Expand All @@ -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;
}
3 changes: 1 addition & 2 deletions samples/SystemClock_NTP/app/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
16 changes: 4 additions & 12 deletions samples/SystemClock_NTP/include/NtpClientDemo.h
Original file line number Diff line number Diff line change
@@ -1,26 +1,18 @@
#pragma once

#include <SmingCore.h>
#include <Network/NtpClient.h>
#include <SystemClock.h>
#include <Timezone.h>

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

0 comments on commit e46df74

Please sign in to comment.