完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
您好,我想告诉您一个问题,我在使用 ESP8266 内核中包含的 NTP DST(我使用的是 3.1.0 版)将系统日期初始化为当前日期(包括时区)时遇到了问题。
在 settime(以秒为单位的日期)之后,如果我打印 now() 和 hour() 的值,它们是不同的:now() 与当前时间一致,hour() 落后一小时。 setTime(时、分、秒、日、月、年)后,now() 的值比当前时间提前一小时,而 hour() 的值与当前时间一致。 这个谜团怎么解释。我哪里错了和/或我不明白 now() 和 hour() 是如何工作的? 感谢您的帮助 代码:全选#include #include #include // Timezone definition #define MYTZ \"CET-1CEST,M3.5.0,M10.5.0/3\" /*--------------------------------------------------*/ 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); } char tmnow[9] = \"00:00:00\"; // \"hh:mm:ss\" char dt[11] = \"00.00.0000\"; void loop() { static uint32_t pTime; if (millis() - pTime > 1000) { pTime = millis(); time_t ora; tm tInfo; getLocalTime(&tInfo); //Update tInfo struct Serial.print(\"\\n tInfo.tm_mday : \"); Serial.println(tInfo.tm_mday); Serial.print(\" tInfo.tm_mon +1 : \"); Serial.println(tInfo.tm_mon+1); Serial.print(\" tInfo.tm_year+1900 : \"); Serial.println(tInfo.tm_year+1900); Serial.print(\" tInfo.tm_hour : \"); Serial.println(tInfo.tm_hour); Serial.print(\" tInfo.tm_min : \"); Serial.println(tInfo.tm_min); Serial.print(\" tInfo.tm_sec : \"); Serial.println(tInfo.tm_sec); Serial.print(\" tInfo.tm_wday : \"); Serial.println(tInfo.tm_wday); Serial.print(\" tInfo.tm_isdst : \"); Serial.println(tInfo.tm_isdst); ora = mktime(&tInfo); setTime(tInfo.tm_hour, tInfo.tm_min, tInfo.tm_sec, tInfo.tm_mday, tInfo.tm_mon + 1, tInfo.tm_year + 1900); Serial.println(\"\\n---------- setTime(tmt.tm_hour, tmt.tm_min,...... \"); Serial.print(\"\\n1. ora : \"); Serial.println(ora); Serial.print(\"1. now() : \"); Serial.println(now()); Serial.print(\"1. getTimeGMY_HMS(now()): \"); Serial.println(getTimeGMY_HMS(now())); Serial.print(\"1. getTimeGMY_HMS(ora) : \"); Serial.println(getTimeGMY_HMS(ora)); 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); sprintf(tmnow, \"%02u:%02u:%02u\", hour(now()), minute(now()), second(now())); sprintf(dt, \"%02d.%02d.%04d\", day(now()), month(now()), year(now())); Serial.print(\"1. ora-data hour(now()) : \"); Serial.print(dt); Serial.print(\"-\"); Serial.println(tmnow); Serial.print(\"1. ora : \");Serial.println(ctime(&ora)); setTime(ora); Serial.println(\"\\n---------- setTime(ora) --------------------\"); Serial.print(\"\\n2. ora : \"); Serial.println(ora); Serial.print(\"2. now() : \"); Serial.println(now()); Serial.print(\"2. getTimeGMY_HMS(now()): \"); Serial.println(getTimeGMY_HMS(now())); Serial.print(\"2. getTimeGMY_HMS(ora) : \"); Serial.println(getTimeGMY_HMS(ora)); 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); sprintf(tmnow, \"%02u:%02u:%02u\", hour(now()), minute(now()), second(now())); sprintf(dt, \"%02d.%02d.%04d\", day(now()), month(now()), year(now())); Serial.print(\"2. ora-data hour(now()) : \"); Serial.print(dt); Serial.print(\"-\"); Serial.println(tmnow); Serial.print(\"2. ora : \");Serial.println(ctime(&ora)); } delay(60000); // Increase simulation performance } void setup() { Serial.begin(115200); Serial.println(\"Connecting to WiFi \"); WiFi.begin(\"R_1_Mya_2.4\", \"1_Marina_0hkl\"); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print(\".\"); } Serial.print(\"\\nWiFi connected. \"); Serial.println(\"IP address: \"); Serial.println(WiFi.localIP().toString()); // Sync time with NTP server configTime(MYTZ, \"time.google.com\", \"time.windows.com\", \"pool.ntp.org\"); } |
|
相关推荐
|
|
只有小组成员才能发言,加入小组>>
310 浏览 1 评论
551浏览 6评论
461浏览 5评论
有没有办法在不使用混杂模式的情况下实现Wifi驱动程序接收缓冲区访问中断呢?
443浏览 5评论
445浏览 4评论
415浏览 4评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-11-23 12:14 , Processed in 0.686465 second(s), Total 73, Slave 56 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号