我正在为嵌入式 ARM 设备编写 C+ 应用程序。
我的应用程序接收格式为 RFC3339 和 UTC/Zulu 的日期和时间字符串。例如"2020-12-14T23:18:13Z"
我需要以秒为单位获取时间,将其与当前时间(以秒为单位)进行比较,如果这些时间之间的差异大于 8 秒,则采取行动。
我已经包含了一个第三方头文件来帮助,date.h
从这里:https ://howardhinnant.github.io/date/date.html
我不得不说我仍在努力从日期和时间字符串中正确获取秒数并进行比较。在我的情况下它总是为零。到目前为止,这是我一直在尝试的:
#include <iostream>
#include <string>
#include <sstream>
#include "date.h"
int main()
{
std::string ts = "2020-11-14T22:14:16Z"; // Literal string to test at first
std::istringstream infile1{ts};
sys_seconds tps;
infile1 >> parse("%FT%T%Ez", tps);
auto stampSinceEpoch_s = tps.time_since_epoch();
// Get the current time
auto now = system_clock::now();
auto now_s = time_point_cast<seconds>(now);
auto nowSinceEpoch_s = now_s.time_since_epoch();
if ( (nowSinceEpoch_s .count() - stampSinceEpoch_s.count()) > 8)
{
// Time difference is greater than 8 seconds, take action
}
}
任何帮助是极大的赞赏。