forked from bkobkobko/SparkCoreLibraries
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSparkTime.cpp
More file actions
426 lines (359 loc) · 11.7 KB
/
SparkTime.cpp
File metadata and controls
426 lines (359 loc) · 11.7 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
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
/* Spark Time by Brian Ogilvie
Inspired by Arduino Time by Michael Margolis
Copyright (c) 2014 Brian Ogilvie. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
- Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
- Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
TODO: Fractional second handling for NTP and millis();
Translation for other languages
*/
#include "SparkTime.h"
SparkTime::SparkTime()
{
_UDPClient = NULL;
_timezone = -5;
_useDST = true;
_useEuroDSTRule = false;
_syncedOnce = false;
_interval = 60UL * 60UL;
_localPort = 2390;
}
void SparkTime::begin(UDP * UDPClient, const char * NTPServer) {
_UDPClient = UDPClient;
memcpy(_serverName, NTPServer, strlen(NTPServer)+1);
}
void SparkTime::begin(UDP * UDPClient) {
_UDPClient = UDPClient;
const char NTPServer[] = "pool.ntp.org";
memcpy(_serverName, NTPServer, strlen(NTPServer)+1);
}
bool SparkTime::hasSynced() {
return _syncedOnce;
}
void SparkTime::setUseDST(bool value) {
_useDST = value;
}
void SparkTime::setUseEuroDSTRule(bool value) {
_useEuroDSTRule = value;
}
uint8_t SparkTime::hour(uint32_t tnow) {
uint8_t hTemp = ((tnow+timeZoneDSTOffset(tnow)) % 86400UL)/3600UL;
return hTemp;
}
uint8_t SparkTime::minute(uint32_t tnow) {
return (((tnow+timeZoneDSTOffset(tnow)) % 3600) / 60);
}
uint8_t SparkTime::second(uint32_t tnow) {
return ((tnow+timeZoneDSTOffset(tnow)) % 60);
}
uint8_t SparkTime::dayOfWeek(uint32_t tnow) {
uint32_t dayNum = (tnow + timeZoneDSTOffset(tnow)-SPARKTIMEEPOCHSTART)/SPARKTIMESECPERDAY;
//Unix epoch day 0 was a thursday
return ((dayNum+4)%7);
}
uint8_t SparkTime::day(uint32_t tnow) {
uint32_t dayNum = (tnow+timeZoneDSTOffset(tnow)-SPARKTIMEBASESTART)/SPARKTIMESECPERDAY;
uint32_t tempYear = SPARKTIMEBASEYEAR;
uint8_t tempMonth = 0;
while(dayNum >= YEARSIZE(tempYear)) {
dayNum -= YEARSIZE(tempYear);
tempYear++;
}
while(dayNum >= _monthLength[LEAPYEAR(tempYear)][tempMonth]) {
dayNum -= _monthLength[LEAPYEAR(tempYear)][tempMonth];
tempMonth++;
}
dayNum++; // correct for zero-base
return (uint8_t)dayNum;
}
uint8_t SparkTime::month(uint32_t tnow) {
uint32_t dayNum = (tnow+timeZoneDSTOffset(tnow)-SPARKTIMEBASESTART)/SPARKTIMESECPERDAY;
uint32_t tempYear = SPARKTIMEBASEYEAR;
uint8_t tempMonth = 0;
while(dayNum >= YEARSIZE(tempYear)) {
dayNum -= YEARSIZE(tempYear);
tempYear++;
}
while(dayNum >= _monthLength[LEAPYEAR(tempYear)][tempMonth]) {
dayNum -= _monthLength[LEAPYEAR(tempYear)][tempMonth];
tempMonth++;
}
tempMonth++;
return tempMonth;
}
uint32_t SparkTime::year(uint32_t tnow) {
uint32_t dayNum = (tnow+timeZoneDSTOffset(tnow)-SPARKTIMEBASESTART)/SPARKTIMESECPERDAY;
uint32_t tempYear = SPARKTIMEBASEYEAR;
while(dayNum >= YEARSIZE(tempYear)) {
dayNum -= YEARSIZE(tempYear);
tempYear++;
}
return tempYear;
}
String SparkTime::hourString(uint32_t tnow) {
return String(_digits[hour(tnow)]);
}
String SparkTime::hour12String(uint32_t tnow) {
uint8_t tempHour = hour(tnow);
if (tempHour>12) {
tempHour -= 12;
}
if (tempHour == 0) {
tempHour = 12;
}
return String(_digits[tempHour]);
}
String SparkTime::minuteString(uint32_t tnow) {
return String(_digits[minute(tnow)]);
}
String SparkTime::secondString(uint32_t tnow) {
return String(_digits[second(tnow)]);
}
String SparkTime::AMPMString(uint32_t tnow) {
uint8_t tempHour = hour(tnow);
if (tempHour<12) {
return String("AM");
} else {
return String("PM");
}
}
String SparkTime::dayOfWeekShortString(uint32_t tnow) {
return String(_days_short[dayOfWeek(tnow)]);
}
String SparkTime::dayOfWeekString(uint32_t tnow) {
return String(_days[dayOfWeek(tnow)]);
}
String SparkTime::dayString(uint32_t tnow) {
return String(_digits[day(tnow)]);
}
String SparkTime::monthString(uint32_t tnow) {
return String(_digits[month(tnow)]);
}
String SparkTime::monthNameShortString(uint32_t tnow) {
return String(_months_short[month(tnow)-1]);
}
String SparkTime::monthNameString(uint32_t tnow) {
return String(_months[month(tnow)-1]);
}
String SparkTime::yearShortString(uint32_t tnow) {
uint32_t tempYear = year(tnow)%100;
if (tempYear<10) {
return String(_digits[tempYear]);
} else {
return String(tempYear);
}
}
String SparkTime::yearString(uint32_t tnow) {
return String(year(tnow));
}
String SparkTime::ISODateString(uint32_t tnow) {
String ISOString;
ISOString += yearString(tnow);
ISOString += "-";
ISOString += monthString(tnow);
ISOString += "-";
ISOString += dayString(tnow);
ISOString += "T";
ISOString += hourString(tnow);
ISOString += ":";
ISOString += minuteString(tnow);
ISOString += ":";
ISOString += secondString(tnow);
int32_t offset = timeZoneDSTOffset(tnow)/3600L;
// Guard against timezone problems
if (offset>-24 && offset<24) {
if (offset < 0) {
ISOString = ISOString + "-" + _digits[-offset] + "00";
} else {
ISOString = ISOString + "+" + _digits[offset] + "00";
}
}
return ISOString;
}
String SparkTime::ISODateUTCString(uint32_t tnow) {
uint32_t savedTimeZone = _timezone;
bool savedUseDST = _useDST;
_timezone = 0;
_useDST = false;
String ISOString;
ISOString += yearString(tnow);
ISOString += "-";
ISOString += monthString(tnow);
ISOString += "-";
ISOString += dayString(tnow);
ISOString += "T";
ISOString += hourString(tnow);
ISOString += ":";
ISOString += minuteString(tnow);
ISOString += ":";
ISOString += secondString(tnow);
ISOString += "Z";
_timezone = savedTimeZone;
_useDST = savedUseDST;
return ISOString;
}
uint32_t SparkTime::now() {
if (!_syncedOnce && !_isSyncing) {
updateNTPTime();
}
if (!_syncedOnce) { // fail!
return SPARKTIMEBASEYEAR; // Jan 1, 2014
}
return nowNoUpdate();
}
uint32_t SparkTime::nowNoUpdate() {
uint32_t mTime = millis();
//unsigned long mFracSec = (mTime%1000); // 0 to 999 miliseconds
//unsigned long nowFrac = _lastSyncNTPFrac + mFracSec*2^22/1000;
uint32_t nowSec = _lastSyncNTPTime + ((mTime - _lastSyncMillisTime)/1000);
if (_lastSyncMillisTime>mTime) { // has wrapped
nowSec = nowSec + SPARKTIMEWRAPSECS;
}
if (nowSec >= (_lastSyncNTPTime + _interval)) {
updateNTPTime();
}
return nowSec;
}
uint32_t SparkTime::nowEpoch() {
return (now()-SPARKTIMEEPOCHSTART);
}
uint32_t SparkTime::lastNTPTime() {
return _lastSyncNTPTime;
}
void SparkTime::setNTPInvterval(uint32_t intervalMinutes) {
const uint32_t fiveMinutes = 5UL * 60UL;
uint32_t interval = intervalMinutes * 60UL;
_interval = max(fiveMinutes, interval);
}
void SparkTime::setTimeZone(int32_t hoursOffset) {
_timezone = hoursOffset;
}
bool SparkTime::isUSDST(uint32_t tnow) {
// 2am 2nd Sunday in March to 2am 1st Sunday in November
// can't use offset here
bool result = false;
uint32_t dayNum = (tnow+_timezone*3600UL-SPARKTIMEBASESTART)/SPARKTIMESECPERDAY;
uint32_t tempYear = SPARKTIMEBASEYEAR;
uint8_t tempMonth = 0;
uint8_t tempHour = ((tnow+_timezone*3600UL) % 86400UL)/3600UL;
while(dayNum >= YEARSIZE(tempYear)) {
dayNum -= YEARSIZE(tempYear);
tempYear++;
}
while(dayNum >= _monthLength[LEAPYEAR(tempYear)][tempMonth]) {
dayNum -= _monthLength[LEAPYEAR(tempYear)][tempMonth];
tempMonth++;
}
tempMonth++;
dayNum++; // correct for zero-base
if (tempMonth>3 && tempMonth<11) {
result = true;
} else if (tempMonth == 3) {
if ((dayNum == _usDSTStart[tempYear-SPARKTIMEBASEYEAR] && tempHour >=2) ||
(dayNum > _usDSTStart[tempYear-SPARKTIMEBASEYEAR])) {
result = true;
}
} else if (tempMonth == 11) {
if (!((dayNum == _usDSTEnd[tempYear-SPARKTIMEBASEYEAR] && tempHour >=2) ||
(dayNum > _usDSTEnd[tempYear-SPARKTIMEBASEYEAR]))) {
result = true;
}
}
return result;
}
bool SparkTime::isEuroDST(uint32_t tnow) {
// 1am last Sunday in March to 1am on last Sunday October
// can't use offset here
bool result = false;
uint32_t dayNum = (tnow+_timezone*3600UL-SPARKTIMEBASESTART)/SPARKTIMESECPERDAY;
uint32_t tempYear = SPARKTIMEBASEYEAR;
uint8_t tempMonth = 0;
uint8_t tempHour = ((tnow+_timezone*3600UL) % 86400UL)/3600UL;
while(dayNum >= YEARSIZE(tempYear)) {
dayNum -= YEARSIZE(tempYear);
tempYear++;
}
while(dayNum >= _monthLength[LEAPYEAR(tempYear)][tempMonth]) {
dayNum -= _monthLength[LEAPYEAR(tempYear)][tempMonth];
tempMonth++;
}
tempMonth++;
dayNum++; // correct for zero-base
if (tempMonth>3 && tempMonth<10) {
result = true;
} else if (tempMonth == 3) {
if ((dayNum == _EuDSTStart[tempYear-SPARKTIMEBASEYEAR] && tempHour >=1) ||
(dayNum > _EuDSTStart[tempYear-SPARKTIMEBASEYEAR])) {
result = true;
}
} else if (tempMonth == 10) {
if (!((dayNum == _EuDSTEnd[tempYear-SPARKTIMEBASEYEAR] && tempHour >=1) ||
(dayNum > _EuDSTEnd[tempYear-SPARKTIMEBASEYEAR]))) {
result = true;
}
}
return result;
}
void SparkTime::updateNTPTime() {
//digitalWrite(D7,HIGH);
_isSyncing = true;
_UDPClient->begin(_localPort);
memset(_packetBuffer, 0, SPARKTIMENTPSIZE);
_packetBuffer[0] = 0b11100011; // LI, Version, Mode
_packetBuffer[1] = 0; // Stratum, or type of clock
_packetBuffer[2] = 6; // Polling Interval
_packetBuffer[3] = 0xEC; // Peer Clock Precision
// 4-11 are zero
_packetBuffer[12] = 49;
_packetBuffer[13] = 0x4E;
_packetBuffer[14] = 49;
_packetBuffer[15] = 52;
_UDPClient->beginPacket(_serverName, 123); //NTP requests are to port 123
_UDPClient->write(_packetBuffer,SPARKTIMENTPSIZE);
_UDPClient->endPacket();
//gather the local offset close to the send time
uint32_t localmsec = millis();
int32_t retries = 0;
int32_t bytesrecv = _UDPClient->parsePacket();
while(bytesrecv == 0 && retries < 1000) {
bytesrecv = _UDPClient->parsePacket();
retries++;
}
if (bytesrecv>0) {
_UDPClient->read(_packetBuffer,SPARKTIMENTPSIZE);
// Handle Kiss-of-death
if (_packetBuffer[1]==0) {
_interval = max(_interval * 2, 24UL*60UL*60UL);
}
_lastSyncNTPTime = _packetBuffer[40] << 24 | _packetBuffer[41] << 16 | _packetBuffer[42] << 8 | _packetBuffer[43];
_lastSyncNTPFrac = _packetBuffer[44] << 24 | _packetBuffer[45] << 16 | _packetBuffer[46] << 8 | _packetBuffer[47];
_lastSyncMillisTime = localmsec;
_syncedOnce = true;
}
//digitalWrite(D7,LOW);
_UDPClient->stop();
_isSyncing = false;
}
int32_t SparkTime::timeZoneDSTOffset(uint32_t tnow) {
int32_t result = _timezone*3600UL;
if ((_useDST && (!_useEuroDSTRule && isUSDST(tnow))) ||
(_useDST && (_useEuroDSTRule && isEuroDST(tnow)))) {
result += 3600UL;
}
return result;
}