完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
您好,我想告诉您一个问题,我在使用 ESP8266 内核中包含的 NTP DST(我使用的是 3.1.0 版)将系统日期初始化为当前日期(包括时区)时遇到了问题。我一直使用 NTP 客户端来管理日期和时区,而且我从未遇到过我描述的问题(我还附上了草图和日志)。
如果我使用 settime(以秒为单位的日期),now() 和 hour() 的值是不同的:now() 与当前时间一致,hour() 落后一小时。 如果我使用 setTime(时、分、秒、日、月、年),则 now() 的值比当前时间提前一小时,而 hour() 的值与当前时间一致。 这个谜团怎么解释。我哪里错了和/或我不明白 now() 和 hour() 是如何工作的? 感谢您的帮助, 代码是 代码:全选#ifndef STASSID #define STASSID \"xxxxxxxx\" // set your SSID #define STAPSK \"xxxxxxxx\" // set your wifi password #endif /* Configuration of NTP */ #define MY_NTP_SERVER \"europe.pool.ntp.org\" #define MY_TZ \"CET-1CEST,M3.5.0/02,M10.5.0/03\" #include // we need wifi to get internet access #include #include // time() ctime() /* Globals */ time_t dtora; tm tmt; // the structure tm holds time information in a more convient way time_t ora; char tmnow[9] = \"00:00:00\"; // \"hh:mm:ss\" char dt[11] = \"00.00.0000\"; static tm getDateTimeByParams(long time){ struct tm *newtime; const time_t tim = time; newtime = localtime(&tim); return *newtime; } /** * Input tm time format and return String with format pattern * by Renzo Mischianti <www.mischianti.org> */ static String getDateTimeStringByParams(tm *newtime, char* pattern = (char *)\"%d.%m.%Y %H:%M:%S\"){ char buffer[30]; strftime(buffer, 30, pattern, newtime); return buffer; } /** * Input time in epoch format format and return String with format pattern * by Renzo Mischianti <www.mischianti.org> */ static String getTimeGMY_HMS(long time, char* pattern = (char *)\"%d.%m.%Y-%H:%M:%S\"){ // struct tm *newtime; tm newtime; newtime = getDateTimeByParams(time); return getDateTimeStringByParams(&newtime, pattern); } /*------------------------------------------------------*/ uint32_t sntp_update_delay_MS_rfc_not_less_than_15000 () { Serial.println(F(\"\\ntime was updated! \")); upd_DateTime(); while ((tmt.tm_year + 1900) == 1970) { upd_DateTime(); } // return 12 * 60 * 60 * 1000UL; // 12 hours return 1 * 60 * 1000UL; // 1 minuti } /*------------------------------------------------------*/ void upd_DateTime() { Serial.println(\" \"); time(&dtora); // read the current time localtime_r(&dtora, &tmt); // update the structure tm with the current time if ((tmt.tm_year + 1900) == 1970) { return; } printf(\"local: %s\", asctime(localtime(&dtora))); Serial.println(\"\\n---------- setTime(local)\"); setTime(dtora); Serial.print(\"1. local : \");Serial.println(dtora); Serial.print(\"1. now() : \");Serial.println(now()); Serial.print(\"1. getTimeGMY_HMS(now()): \");Serial.println(getTimeGMY_HMS(now())); sprintf(tmnow, \"%02u:%02u:%02u\", hour(), minute(), second()); sprintf(dt, \"%02d.%02d.%04d\", day(), month(), year()); Serial.print(\"1. ora-data hour() : \");Serial.print(dt); Serial.print(\"-\"); Serial.println(tmnow); Serial.println(\" \"); Serial.println(\"---------- setTime(tmt.tm_hour, tmt.tm_min,...... \"); time(&dtora); // read the current time localtime_r(&dtora, &tmt); // update the structure tm with the current time setTime(tmt.tm_hour, tmt.tm_min, tmt.tm_sec, tmt.tm_mday, tmt.tm_mon + 1, tmt.tm_year + 1900); Serial.print(\"2. local : \");Serial.println(dtora); Serial.print(\"2. now() : \");Serial.println(now()); Serial.print(\"2. getTimeGMY_HMS(now()): \");Serial.println(getTimeGMY_HMS(now())); sprintf(tmnow, \"%02u:%02u:%02u\", hour(), minute(), second()); sprintf(dt, \"%02d.%02d.%04d\", day(), month(), year()); Serial.print(\"2. ora-data hour() : \");Serial.print(dt); Serial.print(\"-\"); Serial.println(tmnow); Serial.print(\"\\nwday\"); Serial.print(tmt.tm_wday); // days since Sunday 0-6 if (tmt.tm_isdst == 1) { // Daylight Saving Time flag Serial.print(\" - DST\"); Serial.println(\" - Fuso orario = 2\"); }else{ Serial.print(\" - standard\"); Serial.println(\" - Fuso orario = 1\"); } } void setup() { Serial.begin(115200); delay(2000); Serial.println(\"\\nTest NTP TZ DST \"); configTime(MY_TZ, MY_NTP_SERVER); // --> Here is the IMPORTANT ONE LINER needed in your sketch! // start network WiFi.persistent(false); WiFi.mode(WIFI_OFF); WiFi.mode(WIFI_STA); WiFi.begin(STASSID, STAPSK); while (WiFi.status() != WL_CONNECTED) { delay(200); Serial.print ( \".\" ); } Serial.println(\"\\nWiFi connected\"); // by default, the NTP will be started after 60 secs } void loop() { } 日志是 00:42:17.021 -> 00:42:17.021 -> 测试 NTP TZ DST 00:42:17.212 -> ..................... 00:42:20.832 ->时间更新了! 00:42:20.832 -> 00:42:20.832 -> 本地:Thu Mar 23 00:42:20 2023 00:42:20.878 -> 00:42:20.878 -> ---------- setTime (本地) 00:42:20.878 -> 1.本地:1679528540 00:42:20.878 -> 1.现在():1679528540 00:42:20.878 -> 1.getTimeGMY_HMS(现在()):23.03.2023-00 :42:20 00:42:20.878 -> 1.ora-data hour() : 22.03.2023-23:42:20 00:42:20.878 -> 00:42:20.878 -> ------ --- setTime(tmt.tm_hour, tmt.tm_min,...... 00:42:20.878 -> 2. local : 1679528540 00:42:20.878 -> 2.now() : 1679532140 00:42:20.878 -> 2.getTimeGMY_HMS(now()): 23.03.2023-01:42:20 00:42:20.878 -> 2.ora-数据小时():23.03.2023-00:42:20 00:42:20.878 -> 00:42:20.878 -> wday4 - 标准 - Fuso orario = 1 |
|
相关推荐
|
|
只有小组成员才能发言,加入小组>>
968 浏览 1 评论
553浏览 6评论
463浏览 5评论
有没有办法在不使用混杂模式的情况下实现Wifi驱动程序接收缓冲区访问中断呢?
447浏览 5评论
448浏览 4评论
418浏览 4评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-11-27 09:05 , Processed in 0.694953 second(s), Total 76, Slave 59 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号