首页 > 解决方案 > 如何比较两个时间时间戳

问题描述

我需要比较文件修改时间和当前本地时间的两次时间戳。

更改文件时间戳后,我想通过持续监控运行以下语句 = while 循环

我尝试使用 ctime 转换将文件的 time_t 值和当前本地时间转换为字符串,但是如何比较两个 ctime 值?而且我还尝试将 ctime 字符串存储到 char* 并尝试比较以下字符串但没有结果

struct stat attr;
stat(path, &attr);
ctime(&attr.st_mtime) /*  modified time of a file in a string format */

time_t  timev;
time(&timev); 
ctime(&timev) ;

if (ctime(&timev) != ctime(&attr.st_mtime) ){
printf("enter code here statements");
}
else{
printf("enter code here statements");
}


char* string;
char* buffertime1 ;

string = ctime(&attr.st_mtime) ;
buffertime1 = ctime(&timev) ;

if (string !=buffertime1  ){
printf(enter code here statements);
}
else{
printf(enter code here statements);
}

 int result = strcmp(buffertime1, buffertime);

 if (result == 0){
 std::cout << "Both Strings are equal" << std::endl;
  }
 else{
  std::cout << "Both Strings are not equal";
  }

标签: c++

解决方案


推荐阅读