diff --git a/cores/esp8266/time.c b/cores/esp8266/time.c index 27f6640ca1..58c6b64d90 100644 --- a/cores/esp8266/time.c +++ b/cores/esp8266/time.c @@ -17,6 +17,7 @@ */ #include +#include #include #include "sntp.h" @@ -36,8 +37,6 @@ extern uint64_t micros64(); // time gap in seconds from 01.01.1900 (NTP time) to 01.01.1970 (UNIX time) #define DIFF1900TO1970 2208988800UL -static int s_daylightOffset_sec = 0; -static long s_timezone_sec = 0; static bool s_bootTimeSet = false; static uint64_t s_bootTime_us = 0; @@ -73,12 +72,29 @@ void configTime(int timezone, int daylightOffset_sec, const char* server1, const setServer(1, server2); setServer(2, server3); - s_timezone_sec = timezone; - s_daylightOffset_sec = daylightOffset_sec; + //s_timezone_sec = timezone; + //s_daylightOffset_sec = daylightOffset_sec; sntp_set_timezone(timezone/3600); + sntp_set_daylight(daylightOffset_sec); sntp_init(); } +int settimeofday(const struct timeval* tv, const struct timezone* tz) +{ + if (tz) /*before*/ + { + sntp_set_timezone(tz->tz_minuteswest / 60); + // apparently tz->tz_dsttime is a bitfield and should not be further used (cf man) + sntp_set_daylight(0); + } + if (tv) /* after*/ + { + sntp_set_system_time(tv->tv_sec); + // ignore tv->usec + } + return 0; +} + int clock_gettime(clockid_t unused, struct timespec *tp) { (void) unused; diff --git a/libraries/esp8266/examples/NTP-TZ-DST/NTP-TZ-DST.ino b/libraries/esp8266/examples/NTP-TZ-DST/NTP-TZ-DST.ino new file mode 100644 index 0000000000..ddbbc56292 --- /dev/null +++ b/libraries/esp8266/examples/NTP-TZ-DST/NTP-TZ-DST.ino @@ -0,0 +1,93 @@ + +#include // time() ctime() +#include // struct timeval +#include + +//////////////////////////////////////////////////////// + +#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); +} + diff --git a/tools/sdk/lib/liblwip2.a b/tools/sdk/lib/liblwip2.a index 4c3ef868b9..a1be08c447 100644 Binary files a/tools/sdk/lib/liblwip2.a and b/tools/sdk/lib/liblwip2.a differ diff --git a/tools/sdk/lwip2/builder b/tools/sdk/lwip2/builder index 9b6966aea4..b0cad9fbd4 160000 --- a/tools/sdk/lwip2/builder +++ b/tools/sdk/lwip2/builder @@ -1 +1 @@ -Subproject commit 9b6966aea4c66ff5d1c6c077dee2641f848c4fdb +Subproject commit b0cad9fbd4283dbde61cd8caf8af8a934ea3d147 diff --git a/tools/sdk/lwip2/include/arch/cc.h b/tools/sdk/lwip2/include/arch/cc.h index 98046e42c0..834aedcc79 100644 --- a/tools/sdk/lwip2/include/arch/cc.h +++ b/tools/sdk/lwip2/include/arch/cc.h @@ -51,11 +51,17 @@ extern "C" #ifdef __cplusplus } #endif - -void sntp_set_system_time (uint32_t t); - #endif // defined(LWIP_BUILD) +#ifdef __cplusplus +extern "C" +{ +#endif +void sntp_set_system_time (uint32_t t); // also provided to user +#ifdef __cplusplus +} +#endif + #include "mem.h" // useful for os_malloc used in esp-arduino's mDNS typedef uint32_t sys_prot_t; // not really used diff --git a/tools/sdk/lwip2/include/lwipopts.h b/tools/sdk/lwip2/include/lwipopts.h index a9c1c2a5db..3863e3b88a 100644 --- a/tools/sdk/lwip2/include/lwipopts.h +++ b/tools/sdk/lwip2/include/lwipopts.h @@ -2986,7 +2986,10 @@ -------------------------------------------------- */ #define SNTP_SERVER_DNS 1 // SNTP support DNS names through sntp_setservername / sntp_getservername -#define SNTP_SERVER_ADDRESS "pool.ntp.org" // default +// if SNTP_SERVER_ADDRESS is defined, it always overrides user's config +// so we do not define it. sntp server can come from dhcp server, or by +// user. +//#define SNTP_SERVER_ADDRESS "pool.ntp.org" // default #define SNTP_GET_SERVERS_FROM_DHCP 1 #define SNTP_SET_SYSTEM_TIME(t) (sntp_set_system_time(t)) // implemented in lwip2-sntp.c