-
Notifications
You must be signed in to change notification settings - Fork 14
/
NTP.h
executable file
·288 lines (254 loc) · 7.35 KB
/
NTP.h
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
/**
* NTP library for Arduino framewoek
* The MIT License (MIT)
* (c) 2022 sstaub
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#ifndef NTP_H
#define NTP_H
#include "Arduino.h"
#include <time.h>
#include <Udp.h>
#define SEVENTYYEARS 2208988800UL
#define UNIXOFFSET 946684800UL
#ifdef __AVR__
//#define POSIX_OFFSET NTP_OFFSET - SEVENTYYEARS// - UNIX_OFFSET// + 30 years
#define POSIX_OFFSET UNIXOFFSET
#else
#define POSIX_OFFSET -SEVENTYYEARS
#endif
#define NTP_PACKET_SIZE 48
#define NTP_PORT 123
#define SECS_PER_MINUTES 60
#define SECS_PER_DAY 86400
#define GMT_MESSAGE "GMT +/- offset"
#define RULE_DST_MESSAGE "no DST rule"
#define RULE_STD_MESSAGE "no STD rule"
enum week_t {Last, First, Second, Third, Fourth};
enum dow_t {Sun, Mon, Tue, Wed, Thu, Fri, Sat};
enum month_t {Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec};
class NTP {
public:
/**
* @brief constructor for the NTP object
*
* @param udp
*/
NTP(UDP& udp);
/**
* @brief destructor for the NTP object
*
*/
~NTP();
/**
* @brief starts the underlying UDP client with the default local port
*
* @param server NTP server host name
* @param serverIP NTP server IP address
*/
void begin(const char* server = "pool.ntp.org");
void begin(IPAddress serverIP);
/**
* @brief stops the underlying UDP client
*
*/
void stop();
/**
* @brief This should be called in the main loop of your application. By default an update from the NTP Server is only
* made every 60 seconds. This can be configured in the NTPTime constructor.
*
* @return true on success
* @return false on no update or update failure
*/
bool update();
/**
* @brief set the update interval
*
* @param updateInterval in ms, default = 60000ms
*/
void updateInterval(uint32_t interval);
/**
* @brief set the rule for DST (daylight saving time)
* start date of DST
*
* @param tzName name of the time zone
* @param week Last, First, Second, Third, Fourth (0 - 4)
* @param wday Sun, Mon, Tue, Wed, Thu, Fri, Sat (0 - 7)
* @param month Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec (0 -11)
* @param hour the local hour when rule chages
* @param tzOffset sum of summertime and timezone offset
*/
void ruleDST(const char* tzName, int8_t week, int8_t wday, int8_t month, int8_t hour, int tzOffset);
/**
* @brief get the DST time as a ctime string
*
* @return char* time string
*/
const char* ruleDST();
/**
* @brief set the rule for STD (standard day)
* end date of DST
*
* @param tzName name of the time zone
* @param week Last, First, Second, Third, Fourth (0 - 4)
* @param wday Sun, Mon, Tue, Wed, Thu, Fri, Sat (0 - 7)
* @param month Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec (0 -11)
* @param hour the local hour when rule chages
* @param tzOffset timezone offset
*/
void ruleSTD(const char* tzName, int8_t week, int8_t wday, int8_t month, int8_t hour, int tzOffset);
/**
* @brief get the STD time as a ctime string
*
* @return char* time string
*/
const char* ruleSTD();
/**
* @brief get the name of the timezone
*
* @return char* name of the timezone
*/
const char* tzName();
/**
* @brief set the timezone manually
* this should used if there is no DST!
*
* @param tzHours
* @param tzMinutes
*/
void timeZone(int8_t tzHours, int8_t tzMinutes = 0);
/**
* @brief set daylight saving manually!
* use in conjunction with timeZone, when there is no DST!
*
* @param dstZone
*/
void isDST(bool dstZone);
/**
* @brief returns the DST state
*
* @return int 1 if summertime, 0 no summertime
*/
bool isDST();
/**
* @brief get the Unix epoch timestamp
*
* @return time_t timestamp
*/
time_t epoch();
/**
* @brief get the year
*
* @return int year
*/
int16_t year();
/**
* @brief get the month
*
* @return int month, 1 = january
*/
int8_t month();
/**
* @brief get the day of a month
*
* @return int day
*/
int8_t day();
/**
* @brief get the day of a week
*
* @return int day of the week, 0 = sunday
*/
int8_t weekDay();
/**
* @brief get the hour of the day
*
* @return int
*/
int8_t hours();
/**
* @brief get the minutes of the hour
*
* @return int minutes
*/
int8_t minutes();
/**
* @brief get the seconds of a minute
*
* @return int seconds
*/
int8_t seconds();
/**
* @brief returns a formatted string
*
* @param format for strftime
* @return char* formated time string
*/
const char* formattedTime(const char *format);
/**
* @brief returns NTP timestamp
*
* @return uint32_t
*/
uint32_t ntp();
/**
* @brief returns UNIX timestamp
*
* @return uint32_t
*/
uint32_t utc();
private:
UDP *udp;
const char* server = nullptr;
IPAddress serverIP;
uint8_t ntpRequest[NTP_PACKET_SIZE]; // = {0xE3, 0x00, 0x06, 0xEC};
uint8_t ntpQuery[NTP_PACKET_SIZE];
time_t utcCurrent = 0;
time_t local = 0;
struct tm *current;
uint32_t interval = 60000;
uint32_t lastUpdate = 0;
uint8_t tzHours = 0;
uint8_t tzMinutes = 0;
int32_t timezoneOffset;
int16_t dstOffset = 0;
bool dstZone = true;
uint32_t timestamp;
uint32_t ntpTime = 0;
uint32_t utcTime = 0;
time_t utcSTD, utcDST;
time_t dstTime, stdTime;
uint16_t yearDST;
char timeString[64];
struct ruleDST {
char tzName[6]; // five chars max
int8_t week; // First, Second, Third, Fourth, or Last week of the month
int8_t wday; // day of week, 0 = Sun, 2 = Mon, ... 6 = Sat
int8_t month; // 0 = Jan, 1 = Feb, ... 11=Dec
int8_t hour; // 0 - 23
int tzOffset; // offset from UTC in minutes
} dstStart, dstEnd;
void init();
bool ntpUpdate();
time_t localTime();
void currentTime();
void beginDST();
time_t calcDateDST(struct ruleDST rule, int year);
bool summerTime();
};
#endif