bool test(string currentTime, string time1, string time2) {
const std::regex pattern(R"((\d+):(\d+):(\d+))");
int one_day_second = 86400;
// 匹配当前时间
std::smatch match;
std::regex_match(currentTime, match, pattern);
int current_hour = std::stoi(match[1]);
int current_minute = std::stoi(match[2]);
int current_second = std::stoi(match[3]);
// 匹配时间点 1
std::regex_match(time1, match, pattern);
int start_hour1 = std::stoi(match[1]);
int start_minute1 = std::stoi(match[2]);
int start_second1 = std::stoi(match[3]);
// 匹配时间点 2
std::regex_match(time2, match, pattern);
int end_hour2 = std::stoi(match[1]);
int end_minute2 = std::stoi(match[2]);
int end_second2 = std::stoi(match[3]);
int current_total_seconds = current_hour * 3600 + current_minute * 60 + current_second;
int start_total_seconds1 = start_hour1 * 3600 + start_minute1 * 60 + start_second1;
int start_total_seconds2 = end_hour2 * 3600 + end_minute2 * 60 + end_second2;
// 判断当前时间是否在两个时间点之间
if ((start_hour1 < end_hour2) || (start_hour1 == end_hour2 && start_minute1 <= end_minute2)
|| (start_minute1 == end_minute2 && start_second1 <= end_second2))
{
if (start_total_seconds1 <= current_total_seconds && current_total_seconds <= start_total_seconds2)
{
return true;
}
}
else
{
double ad_total_seconds2 = start_total_seconds2 + one_day_second;
double ad_current_total_seconds = current_total_seconds + one_day_second;
if ((start_total_seconds1 <= current_total_seconds && current_total_seconds <= ad_total_seconds2)
|| ad_current_total_seconds <= ad_total_seconds2)
{
return true;
}
}
}
C/C++判断当前时间是否在设定范围内
最新推荐文章于 2024-04-01 11:32:45 发布