Commit 80b4e484c17ced202ec2377959e2e869cea24ec9

Authored by Hu Chunming
1 parent 1e5961fe

优化时间检查代码

src/ai_platform/mvpt.cpp
... ... @@ -6,6 +6,7 @@
6 6 #include <mutex>
7 7 #include <chrono>
8 8 #include <thread>
  9 +#include <ctime>
9 10  
10 11 #include "../decoder/interface/DecoderManager.h"
11 12 #include "../decoder/interface/utiltools.hpp"
... ... @@ -615,17 +616,13 @@ bool CMultiSourceProcess::save_obj_pic(ObjectInfo&amp; res_obj)
615 616 }
616 617  
617 618 bool CMultiSourceProcess::CheckTime() {
618   - struct tm* info;
619   - int nYear, nMonth, nDay;
620   - time_t raw;
621   - time(&raw);
622   - info = localtime(&raw);
623   - nYear = info->tm_year + 1900;
624   - nMonth = info->tm_mon + 1;
625   - nDay = info->tm_mday;
626   - if (nYear == 2026 && nMonth <= 3)
  619 +
  620 + long long deadline_ts = helpers::timer::get_ts_by_day_ms(2026, 4, 1);
  621 + long long cur_ts = helpers::timer::get_cur_time_ms();
  622 +
  623 + if (cur_ts < deadline_ts)
627 624 {
628   - return true;
  625 + return true;
629 626 }
630 627 else
631 628 {
... ...
src/helpers/time_helper.hpp
... ... @@ -17,6 +17,8 @@
17 17 #include <thread>
18 18 #include <unordered_map>
19 19 #include <spdlog/spdlog.h>
  20 +#include <ctime>
  21 +
20 22  
21 23 using namespace std;
22 24  
... ... @@ -57,6 +59,31 @@ namespace helpers
57 59 return std::string(buf);
58 60 }
59 61  
  62 + static long long get_ts_by_day_ms(int year, int month, int day)
  63 + {
  64 + // 初始化tm结构体
  65 + std::tm t = {};
  66 + t.tm_year = year - 1900; // 年从1900年开始计数
  67 + t.tm_mon = month - 1; // 月份从0开始计数,3月是第4个月
  68 + t.tm_mday = day; // 日
  69 + t.tm_hour = 0; // 小时
  70 + t.tm_min = 0; // 分钟
  71 + t.tm_sec = 0; // 秒
  72 +
  73 + // 使用mktime转换为time_t(如果需要)
  74 + std::time_t timestamp = std::mktime(&t); // 或者直接使用下面的chrono方法跳过这一步
  75 + if (timestamp == -1) {
  76 + return -1; // 或者抛出异常等错误处理方式
  77 + }
  78 +
  79 + // 使用chrono转换(如果不需要先转换为time_t)
  80 + auto timepoint = std::chrono::system_clock::from_time_t(timestamp);
  81 + auto duration = timepoint.time_since_epoch();
  82 + auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(duration).count();
  83 +
  84 + return ms;
  85 + }
  86 +
60 87 } // namespace timer
61 88 } // namespace helpers
62 89  
... ...
src/util/util_tools.cpp deleted
1   -#include "util_tools.h"
2   -
3   -using namespace std;
4   -
5   -namespace UtilTools{
6   - long long get_cur_time_ms(){
7   - chrono::time_point<chrono::system_clock, chrono::milliseconds> tpMicro
8   - = chrono::time_point_cast<chrono::milliseconds>(chrono::system_clock::now());
9   -
10   - return tpMicro.time_since_epoch().count();
11   - }
12   -}
13 0 \ No newline at end of file
src/util/util_tools.h deleted
1   -#include <chrono>
2   -
3   -
4   -namespace UtilTools{
5   - long long get_cur_time_ms();
6   -}