time_t AVRecord::GetRecordDurationSEC() { time_t endTime; time(&endTime); return (endTime - this->startTime); } linux C++使用time()记录时间时,如何解决修改了系统时间后,该记录的实时时间不被打乱
时间: 2024-03-26 16:40:39 浏览: 86
在 Linux 中,time_t 类型表示的是 UTC 时间,即协调世界时。当系统时间被修改后,time_t 类型的时间也会随之改变。因此,在记录时间时,应该使用系统启动到现在的时间,而不是使用系统时间。可以使用 gettimeofday 函数获取系统启动到现在的时间,然后计算出相对时间。
下面是一个示例代码:
```
#include <sys/time.h>
#include <time.h>
time_t AVRecord::GetRecordDurationSEC() {
struct timeval now;
gettimeofday(&now, NULL);
return (now.tv_sec - this->startTime.tv_sec);
}
```
在这个代码中,使用 gettimeofday 函数获取当前时间,然后计算出与启动时间之间的时间差,返回秒数。这种方式可以避免系统时间被修改后,记录的时间不准确的问题。
相关问题
time_t startTime; time(&(this->startTime)); time_t AVRecord::GetRecordDurationSEC() { time_t endTime; time(&endTime); return (endTime - this->startTime); } 实时 记录时间时,记录时间时,如何解决修改了系统时间后,该记录的实时时间不被打乱
如果系统时间被修改后,实时记录时间可能会有误。为了解决这个问题,可以通过以下两种方法:
1.使用网络时间协议(NTP)来同步系统时间。NTP会自动从互联网上的时间服务器获取准确的时间,并校准本地系统时间。这样可以确保系统时间始终正确。
2.在记录时间的时候,不使用系统时间而是使用一个单独的计时器来记录时间。这个计时器可以使用硬件时钟或者系统计时器来实现。这样,无论系统时间是否被修改,记录的实时时间都不会受到影响。
综上所述,建议使用第一种方法来同步系统时间,以确保记录的时间准确无误。
foreach (QString str, list) { qDebug() << str; const QString studentID = str; for(auto& studentID : students){ auto studentIt = m_students.find(studentID); if(studentIt == m_students.end()){ qDebug() << "student is not exist"; return; } auto it = studentIt.value()->m_workMap.find(actName); if(it != studentIt.value()->m_workMap.end()){ qDebug() << "activity repeat"; return; } activity* act = new activity; act->m_name = actName; act->m_startTime = time; act->m_endTime = -1; act->m_type = type; act->m_data.push_back(data); act->m_week.push_back(week); studentIt.value()->m_workMap.insert(act->m_name, act); log(m_currentPeople, "发布了活动[" + actName + "]给[" + studentIt.value()->m_name + "]"); } }
这段代码中使用了两个名为`studentID`的变量,一个是foreach语句中的迭代变量,另一个是for循环中的auto& studentID。这会导致编译错误,因为在同一作用域中不能有同名变量。如果你想在for循环中使用迭代变量,可以将其更改为不同的名称,例如:
```
foreach (QString str, list) {
qDebug() << str;
const QString studentID = str;
for(auto& id : students){
auto studentIt = m_students.find(id);
if(studentIt == m_students.end()){
qDebug() << "student is not exist";
return;
}
auto it = studentIt.value()->m_workMap.find(actName);
if(it != studentIt.value()->m_workMap.end()){
qDebug() << "activity repeat";
return;
}
activity* act = new activity;
act->m_name = actName;
act->m_startTime = time;
act->m_endTime = -1;
act->m_type = type;
act->m_data.push_back(data);
act->m_week.push_back(week);
studentIt.value()->m_workMap.insert(act->m_name, act);
log(m_currentPeople, "发布了活动[" + actName + "]给[" + studentIt.value()->m_name + "]");
}
}
```
这样就能避免同名变量的问题了。
阅读全文
相关推荐
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)