0

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)

so I have this function that can tell the time has passed, but it will always return true after 10:00 (time parameter)

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 ? what value shoud I use for that 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) ?

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

user7082181
  • 354
  • 3
  • 15
  • Use a flag somewhere that is false until it detects that the time has elapsed. – mathematician1975 Mar 06 '19 at 10:18
  • use a state machine – fjardon Mar 06 '19 at 10:21
  • Why wouldn't use a task scheduler instead of manage it in your program ? – Zoma Mar 06 '19 at 10:24
  • every time I run the program, it would run all the tasks already passed...so far , I'll use both a flag and epsilon...another solutions would be to use a Thread sleeping every second, but then I would have to tests all the tasks in the list which could take some time for a really long schedule...it's actualy a game script – user7082181 Mar 06 '19 at 10:26
  • Actually it's for a video game, I already wrote the scheduler, it uses ext triggers, the only feature I miss is a "run @ time" feature – user7082181 Mar 06 '19 at 10:27

2 Answers2

0
  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.

Alan Birtles
  • 32,622
  • 4
  • 31
  • 60
  • ok but it does not solve the fact that all tasks defined before the current time will be ran at each program launch..even if they were supposed to run hours ago...so an arbitrary epsilon check in inevitable...also what if I dont want to remove events from the list – user7082181 Mar 06 '19 at 12:20
0

Here is my way of doing it

I only admit tasks within a 5 seconds period

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;
}
user7082181
  • 354
  • 3
  • 15