Skip to content

Commit 284e7ab

Browse files
committed
Try out ZonedClock
1 parent 610cbed commit 284e7ab

File tree

2 files changed

+61
-4
lines changed

2 files changed

+61
-4
lines changed

samples/SystemClock_NTP/app/application.cpp

Lines changed: 60 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#include <SmingCore.h>
22
#include <NtpClientDemo.h>
3+
#include <ZonedClock.h>
4+
#include <tzdata.h>
35

46
// If you want, you can define WiFi settings globally in Eclipse Environment Variables
57
#ifndef WIFI_SSID
@@ -13,6 +15,11 @@ namespace
1315

1416
SimpleTimer printTimer;
1517

18+
ZonedClock london = TZ::Europe::London();
19+
ZonedClock berlin = TZ::Europe::Berlin();
20+
ZonedClock moscow = TZ::Europe::Moscow();
21+
ZonedClock sydney = TZ::Australia::Sydney();
22+
1623
// Option 1
1724
// Use this option if you want to have full control of NtpTime client
1825
// Default : no automatic NTP requests sent, no automatic update of SystemTime
@@ -45,10 +52,30 @@ NtpClientDemo* demo;
4552
// or use
4653
// ntpClientDemo dm1;
4754

55+
void printClock(const ZonedClock& clock)
56+
{
57+
Serial << clock.getLocation() << ": " << clock.now().toString();
58+
if(clock.getTimezone().hasDaylightSavings()) {
59+
auto change = clock.getNextChange();
60+
Serial << _F(", Next Change ") << change.toString() << " (" << change.toUtc().toString() << ")" << endl;
61+
} else {
62+
Serial << _F(" (no daylight savings)") << endl;
63+
}
64+
}
65+
4866
void onPrintSystemTime()
4967
{
50-
Serial << _F("Local Time: ") << SystemClock.getSystemTimeString(eTZ_Local) << _F(", UTC Time: ")
51-
<< SystemClock.getSystemTimeString(eTZ_UTC) << endl;
68+
Serial << _F("\0337" // Save cursor position
69+
"\033[H" // Home
70+
"\033[0r" // Set scrolling region
71+
);
72+
printClock(london);
73+
printClock(berlin);
74+
printClock(moscow);
75+
printClock(sydney);
76+
Serial << _F("\033[6r" // Set scrolling region
77+
"\0338" // Restore cursor position
78+
);
5279
}
5380

5481
// Called when time has been received by NtpClient (option 1 or 2)
@@ -69,6 +96,8 @@ void connectFail(const String& ssid, MacAddress bssid, WifiDisconnectReason reas
6996

7097
void gotIP(IpAddress ip, IpAddress netmask, IpAddress gateway)
7198
{
99+
Serial << _F("Connected as ") << ip << endl;
100+
72101
// Set specific parameters if started by option 1 or 2
73102
// Set client to do automatic time requests every 60 seconds.
74103
// NOTE: you should have longer interval in a real world application
@@ -99,7 +128,35 @@ void init()
99128
{
100129
Serial.begin(SERIAL_BAUD_RATE);
101130
Serial.systemDebugOutput(true); // Allow debug print to serial
102-
Serial.println(_F("Sming. Let's do smart things!"));
131+
132+
if(0) {
133+
DateTime dt1;
134+
135+
auto printChange = [&]() {
136+
auto change = london.getTimezone().getNextChange(dt1);
137+
Serial << dt1.toFullDateTimeString() << " -> " << change.toString() << endl;
138+
};
139+
140+
dt1.setTime(1774746000 - 1);
141+
printChange();
142+
143+
dt1.setTime(0, 0, 0, 31, dtDecember, 2025);
144+
printChange();
145+
dt1.setTime(0, 0, 0, 1, dtJanuary, 2026);
146+
printChange();
147+
dt1.setTime(0, 0, 1, 29, dtMarch, 2026);
148+
printChange();
149+
dt1.setTime(0, 0, 1, 25, dtOctober, 2026);
150+
printChange();
151+
152+
abort();
153+
}
154+
155+
Serial << _F("\033[H" // Home
156+
"\033[0J" // Erase screen
157+
"\033[5B" // Down 5 lines
158+
)
159+
<< _F("Sming. Let's do smart things!") << endl;
103160

104161
// Station - WiFi client
105162
WifiStation.enable(true);

0 commit comments

Comments
 (0)