time.h函数在部分工具链版本中工作异常的技术问询
Arduino 高版本GCC工具链下时间设置异常问题
问题代码
#include <Arduino.h> #include <time.h> #include <sys/time.h> void setup() { Serial.begin(115200); struct timeval tv = { .tv_sec = 25, .tv_usec = 0 }; settimeofday(&tv, NULL); while(true) { time_t now; time(&now); printf("Current epoch time: %ld\n", now); delay(1000); } } void loop(){}
问题现象
通过settimeofday()将系统epoch时间设置为25,再用time(&now)读取并打印时间,不同GCC工具链版本编译后表现差异极大:
- GCC 12.2.0/11.2.0版本编译:初始打印随机值(示例为69),随后周期性在1080和1081之间切换
- GCC 8.4.0版本编译:打印值从25开始正常每秒递增1
GCC 12.2.0/11.2.0编译后输出
Current epoch time: 69 Current epoch time: 1081 Current epoch time: 1081 Current epoch time: 1081 Current epoch time: 1081
GCC 8.4.0编译后输出
Current epoch time: 25 Current epoch time: 26 Current epoch time: 27 Current epoch time: 28 Current epoch time: 29
内容的提问来源于stack exchange,提问作者Mahyar Shokraeian
相关产品推荐
相关产品推荐

