C/C++判断当前时间是否在设定范围内

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;
            }
    } 
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
### 回答1: 要判断当前时间是否在某个时间段之内,可以使用编程语言中提供的时间函数来实现。比如在Python中,可以使用datetime模块中的datetime和timedelta类,具体步骤如下: 1. 获取当前时间:使用datetime类中的now()方法获取当前时间。 2. 定义时间段:使用datetime类中的strptime()方法将字符串形式的时间转换为datetime类型。 3. 判断当前时间是否时间段之内:使用比较运算符(大于等于和小于等于)判断。 4. 返回判断结果:如果当前时间时间段之内,返回True,否则返回False。 下面是一个示例代码,判断当前时间是否在8点到18点之间: import datetime # 获取当前时间 now = datetime.datetime.now() # 定义时间段 start_time = datetime.datetime.strptime('08:00:00', '%H:%M:%S') end_time = datetime.datetime.strptime('18:00:00', '%H:%M:%S') # 判断当前时间是否时间段内 if start_time <= now <= end_time: print('当前时间在8点到18点之间') else: print('当前时间不在8点到18点之间') 根据实际情况,可以修改start_time和end_time的值来定义不同的时间段,从而判断当前时间是否在指定的时间段内。 ### 回答2: 为了判断当前时间是否在一个时间段内,我们需要首先获取当前时间和待判断时间段。对于时间的表示,一般使用时间戳(timestamp),它是指Unix时间或者Epoch时间,表示从1970年1月1日零点起经过的秒数。 对于时间段的表示,我们可以使用开始时间和结束时间两个时间戳来描述。假设我们需要判断当前时间是否在2021年7月1日10点到11点这个时间段内,可以这样设置时间段: 开始时间:2021-07-01 10:00:00,转换成时间戳为1625145600 结束时间:2021-07-01 11:00:00,转换成时间戳为1625149200 接下来,我们比较当前时间时间段的开始时间和结束时间。 方法1:使用time模块 Python中的time模块提供了获取当前时间的函数time.time(),返回当前时间时间戳。我们可以使用这个函数获取当前时间时间戳,并与待判断时间段的开始时间和结束时间进行比较。代码如下: import time start_time = 1625145600 # 开始时间时间戳 end_time = 1625149200 # 结束时间时间戳 # 获取当前时间时间戳 current_time = time.time() # 判断当前时间是否时间段 if start_time <= current_time <= end_time: print("当前时间时间段内") else: print("当前时间不在时间段内") 方法2:使用datetime模块 Python中的datetime模块提供了获取当前时间的函数datetime.now(),返回当前的日期和时间。我们可以将待判断时间段的开始时间和结束时间转换成datetime对象,并与当前时间进行比较。代码如下: import datetime start_time = datetime.datetime.fromtimestamp(1625145600) # 开始时间的datetime对象 end_time = datetime.datetime.fromtimestamp(1625149200) # 结束时间的datetime对象 # 获取当前时间的datetime对象 current_time = datetime.datetime.now() # 判断当前时间是否时间段 if start_time <= current_time <= end_time: print("当前时间时间段内") else: print("当前时间不在时间段内") 以上两种方法都可以判断当前时间是否在一个时间段内,具体使用哪种方法取决于实际需求。如果需要进行更多的时间计算或格式化输出,可以使用datetime模块;如果只需要比较时间戳大小,则可以使用time模块。 ### 回答3: 判断当前时间是否在某个时间段,可以使用 Python 的 datetime 模块,通过获取当前时间时间段的起止时间,比较两者的大小,来判断当前时间是否在该时间段内。具体步骤如下: 1. 导入 datetime 模块: ```python import datetime ``` 2. 获取当前时间: ```python now_time = datetime.datetime.now() ``` 3. 定义时间段的起止时间: ```python start_time = datetime.datetime(now_time.year, now_time.month, now_time.day, 9, 0, 0) end_time = datetime.datetime(now_time.year, now_time.month, now_time.day, 17, 0, 0) ``` 4. 判断当前时间是否时间段内: ```python if start_time <= now_time <= end_time: print("当前时间时间段内") else: print("当前时间不在时间段内") ``` 完整代码示例: ```python import datetime now_time = datetime.datetime.now() start_time = datetime.datetime(now_time.year, now_time.month, now_time.day, 9, 0, 0) end_time = datetime.datetime(now_time.year, now_time.month, now_time.day, 17, 0, 0) if start_time <= now_time <= end_time: print("当前时间时间段内") else: print("当前时间不在时间段内") ``` 其中,datetime.datetime.now() 返回的是当前时间;datetime.datetime(year, month, day, hour, minute, second) 可以通过指定年、月、日、时、分、秒来创建一个 datetime 对象。通过比较起止时间当前时间的大小,来判断当前时间是否时间段内。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值