用数字比较替换字符串比较

master
huangyongan 3 years ago
parent 55fa29f359
commit 0e23357a52

@ -2,6 +2,7 @@
#include <boost/filesystem.hpp> #include <boost/filesystem.hpp>
#include <vector> #include <vector>
#include <string> #include <string>
#include <algorithm>
#define K_VISION_DIR "/hard/k_vision" #define K_VISION_DIR "/hard/k_vision"
#define AI_VISION_DIR "/hard/ai_vision" #define AI_VISION_DIR "/hard/ai_vision"
@ -92,7 +93,7 @@ std::vector<std::string> get_search_paths(const std::string& strFileName, int nB
return vs; return vs;
} }
std::vector<std::string> cvt_filename_to_datetime(const std::string& strFileName, int nBeginTime, int nEndTime) std::vector<long long> get_file_timestamp_range(const std::string& strFileName, int nBeginTime, int nEndTime)
{ {
std::vector<std::string> vs; std::vector<std::string> vs;
if (strFileName.empty() || nBeginTime > nEndTime) if (strFileName.empty() || nBeginTime > nEndTime)
@ -115,18 +116,18 @@ std::vector<std::string> cvt_filename_to_datetime(const std::string& strFileName
tt[2] = tt[0] + nEndTime; tt[2] = tt[0] + nEndTime;
tm* tm_begin = localtime(&tt[1]); tm* tm_begin = localtime(&tt[1]);
char szBegin[255]; char szBegin[255];
snprintf(szBegin, sizeof(szBegin),"%d%02d%02d_%02d%02d%02d", snprintf(szBegin, sizeof(szBegin),"%d%02d%02d%02d%02d%02d",
tm_begin->tm_year + 1900, tm_begin->tm_mon + 1, tm_begin->tm_mday, tm_begin->tm_hour, tm_begin->tm_min, tm_begin->tm_sec); tm_begin->tm_year + 1900, tm_begin->tm_mon + 1, tm_begin->tm_mday, tm_begin->tm_hour, tm_begin->tm_min, tm_begin->tm_sec);
tm* tm_end = localtime(&tt[2]); tm* tm_end = localtime(&tt[2]);
char szEnd[255]; char szEnd[255];
snprintf(szEnd, sizeof(szEnd),"%d%02d%02d_%02d%02d%02d", snprintf(szEnd, sizeof(szEnd),"%d%02d%02d%02d%02d%02d",
tm_end->tm_year + 1900, tm_end->tm_mon + 1, tm_end->tm_mday, tm_end->tm_hour, tm_end->tm_min, tm_end->tm_sec); tm_end->tm_year + 1900, tm_end->tm_mon + 1, tm_end->tm_mday, tm_end->tm_hour, tm_end->tm_min, tm_end->tm_sec);
vs.push_back(szBegin); vs.push_back(std::atoll(szBegin));
vs.push_back(szEnd); vs.push_back(std::atoll(szEnd));
return vs; return vs;
} }
std::string get_file_timestamp_str(const std::string& strPath) long long get_file_timestamp(const std::string& strPath)
{ {
std::string str; std::string str;
if (strPath.empty()) if (strPath.empty())
@ -135,7 +136,8 @@ std::string get_file_timestamp_str(const std::string& strPath)
} }
str = path.filename().string(); str = path.filename().string();
auto strTime = str.substr(0, 15); auto strTime = str.substr(0, 15);
return strTime; std::remove(strTime.begin(), strTime.end(), '_');
return std::atoll(strTime.c_str());
} }
int main(int argc, char* argv[]) { int main(int argc, char* argv[]) {
@ -171,7 +173,7 @@ int main(int argc, char* argv[]) {
for (auto it:vec_file_name) for (auto it:vec_file_name)
{ {
++j; ++j;
auto vec_datetime = cvt_filename_to_datetime(it, nParam[0], nParam[1]); auto vec_datetime = get_file_timestamp_range(it, nParam[0], nParam[1]);
if (vec_datetime.empty()) if (vec_datetime.empty())
{ {
continue; continue;
@ -182,8 +184,8 @@ int main(int argc, char* argv[]) {
auto vec_file_path = get_file_paths(dir); auto vec_file_path = get_file_paths(dir);
for (auto path:vec_file_path) for (auto path:vec_file_path)
{ {
auto strTime = get_file_timestamp_str(path); auto timestamp = get_file_timestamp(path);
if (strTime >= vec_datetime.front() && strTime <= vec_datetime.back()) if (timestamp >= vec_datetime.front() && timestamp <= vec_datetime.back())
{ {
vec_matched_path.push_back(path); vec_matched_path.push_back(path);
} }

Loading…
Cancel
Save