-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathListZones.ino
203 lines (168 loc) · 5.62 KB
/
ListZones.ino
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/*
* A program to list the zones of a ZoneManager sorted by UTC offset and name,
* illustrating the ZoneSorterByName and ZoneSorterByOffsetAndName classes.
*
* By default, this sorts all zones and links by UTC offset first, then by name.
* On EpoxyDuino, the --sort command line flag allows sorting by name:
*
* $ ./ListZones [--sort (offset | name)]
* Sorted 594 zones in 8 millis
* UTC-12:00 Etc/GMT+12
* UTC-11:00 Etc/GMT+11
* ...
* UTC+14:00 Etc/GMT-14
* UTC+14:00 Pacific/Kiritimati
*/
#if defined(EPOXY_DUINO)
#include <stdio.h> // fprintf()
#include <string.h> // strcmp()
#endif
#include <Arduino.h>
#include <AceCommon.h> // PrintStr<>
#include <AceTime.h>
using namespace ace_time;
using namespace ace_time::zonedbx;
//---------------------------------------------------------------------------
// Sorting option.
//---------------------------------------------------------------------------
enum class SortOrder : uint8_t {
kNone,
kOffsetAndName,
kName
};
// User-defined option parameters.
SortOrder sortOrder = SortOrder::kOffsetAndName;
//---------------------------------------------------------------------------
// Parse command line flags and arguments.
//---------------------------------------------------------------------------
#if defined(EPOXY_DUINO)
/** Print usage and exit with status code. (0 means success). */
static void usage_and_exit(int status) {
fprintf(stderr,
"Usage: ListZones [--sort offset|name]\n"
);
exit(status);
}
/** Shift the command line arguments to the left by one position. */
static void shift(int& argc, const char* const*& argv) {
argc--;
argv++;
}
/** Compare 2 C-strings. */
static bool arg_equals(const char* s, const char* t) {
return strcmp(s, t) == 0;
}
/**
* Parse command line flags.
* Returns the index of the first argument after the flags.
*/
static int parse_flags(int argc, const char* const* argv) {
int argc_original = argc;
shift(argc, argv);
while (argc > 0) {
if (arg_equals(argv[0], "--sort")) {
shift(argc, argv);
if (argc == 0) usage_and_exit(1);
if (arg_equals(argv[0], "offset")) {
sortOrder = SortOrder::kOffsetAndName;
} else if (arg_equals(argv[0], "name")) {
sortOrder = SortOrder::kName;
} else {
fprintf(stderr, "Unknown option '%s'\n", argv[0]);
usage_and_exit(1);
}
} else if (arg_equals(argv[0], "--")) {
shift(argc, argv);
break;
} else if (arg_equals(argv[0], "--help")) {
usage_and_exit(0);
break;
} else if (strncmp(argv[0], "-", 1) == 0) {
fprintf(stderr, "Unknonwn flag '%s'\n", argv[0]);
usage_and_exit(1);
} else {
break;
}
shift(argc, argv);
}
if (sortOrder == SortOrder::kNone) {
fprintf(stderr, "Must provide --sort\n");
usage_and_exit(1);
}
return argc_original - argc;
}
#endif
//---------------------------------------------------------------------------
// Print out the zones (and links) after sorting them by UTC offset, then by
// name.
//---------------------------------------------------------------------------
ExtendedZoneProcessorCache<1> zoneProcessorCache;
ExtendedZoneManager zoneManager(
zonedbx::kZoneAndLinkRegistrySize,
zonedbx::kZoneAndLinkRegistry,
zoneProcessorCache
);
void printZones(uint16_t elapsedMillis, uint16_t indexes[], uint16_t size) {
SERIAL_PORT_MONITOR.print("Sorted ");
SERIAL_PORT_MONITOR.print(size);
SERIAL_PORT_MONITOR.print(" zones in ");
SERIAL_PORT_MONITOR.print(elapsedMillis);
SERIAL_PORT_MONITOR.println(" millis");
// Print each zone in the form of:
// "UTC-08:00 America/Los_Angeles"
for (uint16_t i = 0; i < size; i++) {
ExtendedZone zone = zoneManager.getZoneForIndex(indexes[i]);
TimeOffset stdOffset = zone.stdOffset();
SERIAL_PORT_MONITOR.print(F("UTC"));
stdOffset.printTo(SERIAL_PORT_MONITOR);
SERIAL_PORT_MONITOR.print(' ');
zone.printNameTo(SERIAL_PORT_MONITOR);
SERIAL_PORT_MONITOR.println();
}
}
void printZonesSortedByOffsetAndName() {
uint16_t indexes[zonedbx::kZoneAndLinkRegistrySize];
ZoneSorterByOffsetAndName<ExtendedZoneManager> zoneSorter(zoneManager);
zoneSorter.fillIndexes(indexes, zonedbx::kZoneAndLinkRegistrySize);
uint16_t startMillis = millis();
zoneSorter.sortIndexes(indexes, zonedbx::kZoneAndLinkRegistrySize);
uint16_t elapsedMillis = millis() - startMillis;
printZones(elapsedMillis, indexes, zonedbx::kZoneAndLinkRegistrySize);
}
void printZonesSortedByName() {
uint16_t indexes[zonedbx::kZoneAndLinkRegistrySize];
ZoneSorterByName<ExtendedZoneManager> zoneSorter(zoneManager);
zoneSorter.fillIndexes(indexes, zonedbx::kZoneAndLinkRegistrySize);
uint16_t startMillis = millis();
zoneSorter.sortIndexes(indexes, zonedbx::kZoneAndLinkRegistrySize);
uint16_t elapsedMillis = millis() - startMillis;
printZones(elapsedMillis, indexes, zonedbx::kZoneAndLinkRegistrySize);
}
//---------------------------------------------------------------------------
void setup() {
#if ! defined(EPOXY_DUINO)
delay(1000); // wait to prevent garbage on SERIAL_PORT_MONITOR
#endif
SERIAL_PORT_MONITOR.begin(115200);
while (!SERIAL_PORT_MONITOR); // Wait until ready - Leonardo/Micro
#if defined(EPOXY_DUINO)
SERIAL_PORT_MONITOR.setLineModeUnix();
#endif
#if defined(EPOXY_DUINO)
/*int args = */parse_flags(epoxy_argc, epoxy_argv);
#endif
if (sortOrder == SortOrder::kOffsetAndName) {
printZonesSortedByOffsetAndName();
} else if (sortOrder == SortOrder::kName) {
printZonesSortedByName();
} else {
#if defined(EPOXY_DUINO)
fprintf(stderr, "Invalid sortOrder\n");
usage_and_exit(1);
#endif
}
#if defined(EPOXY_DUINO)
exit(0);
#endif
}
void loop() {}