I also wanted to create a program that executes a function every x minutes or hours, and I found many examples, but to create it it was necessary to turn on and load the library, and for me it was inconvenient, and I made mine, not very logical)), but it works, you can see it below
using namespace std;
struct tm *addtime(struct tm *tm2)
{
time_t t = time(0);
struct tm * tm1 = localtime( & t );
cout << " Time begin : " << tm1->tm_hour << " : " << tm1->tm_min <<endl;
struct tm * aux = (struct tm*)malloc(sizeof (struct tm));
aux->tm_sec = tm1->tm_sec + tm2->tm_sec;
aux->tm_min = tm1->tm_min + tm2->tm_min + (aux->tm_sec / 60) ;
aux->tm_hour = tm1->tm_hour + tm2->tm_hour + (aux->tm_min / 60);
aux->tm_min %= 60;
aux->tm_sec %= 60;
return (aux);
}
bool verif_time(struct tm *tm1)
{
time_t t = time(0);
struct tm * now = localtime( & t );
if (tm1->tm_hour == now->tm_hour && tm1->tm_min == now->tm_min)
return true;
else
return false;
}
int main()
{
struct tm * after = (struct tm*)malloc(sizeof (struct tm));
after->tm_sec = 0;
after->tm_min = 1;
after->tm_hour = 0;
after = addtime(after);
cout << " After time" << after->tm_hour << ':' << after->tm_min<<endl;
while (true)
{
if (verif_time(after))
{
cout << "Hello " << after->tm_hour << " : " << after->tm_min<<endl;
after->tm_sec = 0;
after->tm_min = 1;
after->tm_hour = 0;
after = addtime(after);
cout << " After time" << after->tm_hour << ':' << after->tm_min<<endl;
}
}
}
Adr1k source
share