forked from bxparks/AceTime
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHelloSystemClock.ino
More file actions
60 lines (49 loc) · 1.75 KB
/
HelloSystemClock.ino
File metadata and controls
60 lines (49 loc) · 1.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/*
* A program to demonstrate the use of AceTime SystemClock. It should print the
* following on the SERIAL_PORT_MONITOR port every 2 seconds:
*
* 2019-06-17T19:50:00-07:00[America/Los_Angeles]
* 2019-06-17T19:50:02-07:00[America/Los_Angeles]
* 2019-06-17T19:50:04-07:00[America/Los_Angeles]
* ...
*/
#include <AceTime.h>
using namespace ace_time;
using namespace ace_time::clock;
// ZoneProcessor instance should be created statically at initialization time.
static BasicZoneProcessor pacificProcessor;
static SystemClockLoop systemClock(nullptr /*reference*/, nullptr /*backup*/);
void setup() {
#if ! defined(UNIX_HOST_DUINO)
delay(1000);
#endif
SERIAL_PORT_MONITOR.begin(115200);
while (!SERIAL_PORT_MONITOR); // Wait until ready - Leonardo/Micro
systemClock.setup();
// Creating timezones is cheap, so we can create them on the fly as needed.
auto pacificTz = TimeZone::forZoneInfo(&zonedb::kZoneAmerica_Los_Angeles,
&pacificProcessor);
// Set the SystemClock using these components.
auto pacificTime = ZonedDateTime::forComponents(
2019, 6, 17, 19, 50, 0, pacificTz);
systemClock.setNow(pacificTime.toEpochSeconds());
}
void printCurrentTime() {
acetime_t now = systemClock.getNow();
// Create Pacific Time and print.
auto pacificTz = TimeZone::forZoneInfo(&zonedb::kZoneAmerica_Los_Angeles,
&pacificProcessor);
auto pacificTime = ZonedDateTime::forEpochSeconds(now, pacificTz);
pacificTime.printTo(SERIAL_PORT_MONITOR);
SERIAL_PORT_MONITOR.println();
}
// Do NOT use delay(), it breaks systemClock.loop()
void loop() {
static acetime_t prevNow = systemClock.getNow();
systemClock.loop();
acetime_t now = systemClock.getNow();
if (now - prevNow >= 2) {
printCurrentTime();
prevNow = now;
}
}