forked from esp8266/Arduino
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathNTP-TZ-DST.ino
93 lines (72 loc) · 2.36 KB
/
NTP-TZ-DST.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
#include <time.h> // time() ctime()
#include <sys/time.h> // struct timeval
#include <ESP8266WiFi.h>
////////////////////////////////////////////////////////
#define SSID "open"
#define SSIDPWD ""
#define TZ 1 // utc+TZ in hours
#define DST_MN 60 // use 60 for summer time in some countries
#define NTP0_OR_LOCAL1 1 // 0:use NTP 1:fake external RTC
#define RTC_TEST 1510592825 // 1510592825 = Monday 13 November 2017 17:07:05 UTC
////////////////////////////////////////////////////////
#define TZ_MN ((TZ)*60)
#define TZ_SEC ((TZ)*3600)
#define DST_SEC ((DST_MN)*60)
void setup() {
Serial.begin(115200);
#if NTP0_OR_LOCAL1
// local
ESP.eraseConfig();
time_t rtc = RTC_TEST;
timeval tv = { rtc, 0 };
timezone tz = { TZ_MN + DST_MN, 0 };
settimeofday(&tv, &tz);
#else
// NTP
configTime(TZ_SEC, DST_SEC, "pool.ntp.org");
WiFi.mode(WIFI_STA);
WiFi.begin(SSID, SSIDPWD);
// don't wait
#endif
}
// for testing purpose:
extern "C" int clock_gettime(clockid_t unused, struct timespec *tp);
void loop() {
// same result as with time()
timeval tv;
gettimeofday(&tv, NULL);
Serial.print("gtod:");
Serial.print((uint32_t)tv.tv_sec);
#if 0
// but usec is wrong
Serial.print("/");
Serial.print((uint32_t)tv.tv_usec);
#endif
// same result as with millis()/1000
timespec tp;
clock_gettime(0, &tp);
Serial.print(" -- clock:");
Serial.print((uint32_t)tp.tv_sec);
#if 0
// nsec is wrong
Serial.print("/");
Serial.print((uint32_t)tp.tv_nsec/1000000);
Serial.print("ms");
#endif
// gives EPOCH+tz+dst
time_t now = time(nullptr);
Serial.print(" -- time:");
Serial.print((uint32_t)now);
// from boot:
Serial.print(" -- millis:");
Serial.print((uint32_t)millis());
Serial.print(" -- micros:");
Serial.print((uint32_t)micros());
// human readable
Serial.print(" -- ctime:(UTC+");
Serial.print((uint32_t)(TZ*60 + DST_MN));
Serial.print("mn)");
Serial.print(ctime(&now));
// simple drifting loop
delay(1000);
}