gpt4 book ai didi

c++ - 时间返回负值

转载 作者:行者123 更新时间:2023-11-28 06:17:32 24 4
gpt4 key购买 nike

我尝试使用 stm32f4 卡的时间函数返回 1970 年的秒数。

我配置了这些数据类型

RTC_HandleTypeDef RtcHandle;
RTC_DateTypeDef dateStruct;
RTC_TimeTypeDef timeStruct;

我调用

HAL_RTC_GetTime
HAL_RTC_GetDate

我调用这个函数

long SystemClock::getUnixTimestamp()
struct tm timeinfo;

//Setup a tm structure based on the RTC
timeinfo.tm_wday = dateStruct.WeekDay;
timeinfo.tm_mon = dateStruct.Month - 1;
timeinfo.tm_mday = dateStruct.Date;
timeinfo.tm_year = dateStruct.Year + 100;
timeinfo.tm_hour = timeStruct.Hours;
timeinfo.tm_min = timeStruct.Minutes;
timeinfo.tm_sec = timeStruct.Seconds;

time_t rawtime = mktime(&timeinfo);

trace_printf("Current date and time are: %s\n", ctime(&rawtime));
long x = time(&rawtime);
trace_printf("time %lu\n", x);
return x;
}

我明白了

Current date and time are: Wed Apr 29 22:46:00 2015
time 1430347560

5 second later, i do another call to HAL_RTC_GetTime, HAL_RTC_GetDate, getUnixTimestamp and i get

Current date and time are: Wed Apr 29 22:46:05 2015
time 1430347560

为什么时间没有修改?

最佳答案

使用

printf("time %d\n", time(&rawtime));

显示rawtime 是不合适的。 time_t 不一定是 int

这是我从 http://en.cppreference.com/w/cpp/chrono/c/time 复制的一些代码显示 time 返回的值:

#include <ctime>
#include <iostream>

int main()
{
std::time_t result = std::time(nullptr);
std::cout << std::asctime(std::localtime(&result))
<< result << " seconds since the Epoch\n";
}

关于c++ - 时间返回负值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29958333/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com