简体   繁体   English

C ++-用std :: difftime编写调度程序-如何检查白天发生的时间(HH:MM:SS)?

[英]C++ - writing a scheduler with std::difftime - how to check for a time (HH:MM:SS) occurence during the day?

I try to have my program figure if an event is getting reached during the day 如果白天有活动到达,我会尝试让我的程序显示

for instance, I want to be able to create events at 10:00:00 so that a task gets executed at that moment in the day (only once) 例如,我希望能够在10:00:00创建事件,以便在一天中的这一时刻(仅一次)执行任务

so I have this function that can tell the time has passed, but it will always return true after 10:00 (time parameter) 所以我有一个可以告诉时间已经过去的函数,但是它将始终在10:00(时间参数)之后返回true。

bool Tools::passedTimeToday(std::time_t time)
{
    auto now = std::chrono::system_clock::now();
    std::time_t _now = std::chrono::system_clock::to_time_t(now);
    if (std::difftime(_now,time)<0)
        return false;
    else
        return true;
}

how do I check the time has passed only once ? 我如何检查时间只过去了一次?

do I use some sort of epsilon around that time ? 那时我会使用某种epsilon吗? what value shoud I use for that epsilon ? 我应该为那个epsilon使用什么值?

    double delta = std::difftime(_now,time);
    if ( (delta<0) && (delta>-epsilon) )
    {
        ...

I mean it could work, but what if my program tests that condition too late (bigger than epsilon) ? 我的意思是它可以工作,但是如果我的程序测试该条件太晚 (比epsilon大)怎么办?

I thought about using a boolean flag instead (bool processed), but then evey time I run the program, it would also run all tasks that happened around that time 我曾考虑过要使用布尔标志(经过布尔处理),但是在我运行程序时,它也将运行该时间发生的所有任务

any help appreciated 任何帮助表示赞赏

thanks 谢谢

  1. Create a list with the scheduled tasks and their next run time sorted by the next run time. 创建一个列表,其中包含计划的任务及其下一个运行时间(按下一个运行时间排序)。

  2. When the current time is after the next run time of the first task in the list, remove it from the list, execute the task and then add it back to the list with the run time incremented by a day (or whatever repetition period you need). 当当前时间在列表中第一个任务的下一个运行时间之后时,将其从列表中删除,执行任务,然后将其添加回列表,运行时间增加一天(或所需的任何重复周期) )。

  3. Go to step 2 unless the list is empty. 除非列表为空,否则请转到步骤2。

Here is my way of doing it 这是我的方法

I only admit tasks within a 5 seconds period 我只接受5秒内的任务

bool Tools::passedTimeToday(std::time_t time)
{
    auto now = std::chrono::system_clock::now();
    std::time_t _now = std::chrono::system_clock::to_time_t(now);
    double delta = std::difftime(_now,time);

    if ( (delta>0) && (delta<5) )
        return true;
    else
        return false;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 格式化时间 c++ dd/mm/yyyy hh:ss - Formatting time c++ dd/mm/yyyy hh:ss 如何将日期时间存储、打印和解析为 yyyyMMdd-HH:MM:SS.nnnnnnnn C++ - How to store, print and parse date time as yyyyMMdd-HH:MM:SS.nnnnnnnnn C++ 如何在 C++ 中将秒更改为 HH:MM:SS 格式? - How to change seconds into HH:MM:SS format in C++? 如何在 C++ 中获取 hh:mm:ss.uuuuuu 时间戳? - How to get hh:mm:ss.uuuuuu timestamp in c++? 如何在C ++中将格式化字符串HH:MM:SS转换为秒 - How to convert formatted string HH:MM:SS to seconds in C++ 为什么 time_of_day 和 hh_mm_ss 在 C++20 中是两种不同的类型? - Why are time_of_day and hh_mm_ss two different types in C++20? 如何使用将 stable_clock 时间格式化为 HH:MM:SS.Milliseconds<chrono> 来自 c++ stdl?</chrono> - How to format steady_clock time into HH:MM:SS.Milliseconds using <chrono>from c++ stdl? 将时间转换为本地时间:hh_ss_mm 模板参数问题 (C++ 14) - Converting time to local zone time: issue with hh_ss_mm template args (C++ 14) 如何将YYYY / MM / DD HH:MM:SS转换为std :: chrono :: system_clock :: time_point? - How do I convert YYYY/MM/DD HH:MM:SS to std::chrono::system_clock::time_point? 如何比较“月日期hh:mm:ss”格式的两个时间戳来检查+ ve或-ve值 - How to compare two time stamp in format “Month Date hh:mm:ss” to check +ve or -ve value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM