With the code below I want to execute a task if the day is saturday and if the minute is >= 14.
There is 2 things I wonder here.
The code below is an infinite loop that never stops. So it checks for if this criteria is true but when the criteria is true nothing happens.

And more important. This cant be the way to do this because the processor is peeking all the time since it is a loop that is running.
How is it possible to check for this criteria without letting the processor "work"/peek ?

int Day;
int Minute;

time_t now = time(0);
struct tm* tm = localtime(&now);

for( int i = 0; i < 5; i++)
	i = i - 1;

	Day = tm->tm_wday;
	Minute = tm->tm_min;

		if( Day == 6 && Minute >= 14 )
			//Execute task

Check out this related thread

If you only want something done on Saturday (when dow == 6) then write a bit of code that will call Sleep() and put your program asleep for 24 hours, then check again. Make sure the program waiks up again at midnight so that the next checks will work.

Once the day is 6 keep checking once a minute until the hour is 0 and minute is 14.

windows.h has a Sleep() function, this will allow you to stall the application for a short period of time each loop, thus reducing the processor usage to 0 or almost.


Here a link that gives a code example to:
"create a task that is scheduled to execute Notepad on a weekly basis. The task contains a weekly trigger that specifies a start boundary, a weeks interval, and a day of the week for the task to start on. The task also contains an action that specifies the task to execute Notepad."

Also this looks more attractive, if you're creating this for a windows environment:

typedef struct _SYSTEMTIME {
WORD wYear;
WORD wMonth;
WORD wDayOfWeek;
WORD wDay;
WORD wHour;
WORD wMinute;
WORD wSecond;
WORD wMilliseconds;
A sample for displaying date and time using SYSTEMTIME is as follows. This program displays the current Coordinated Universal date and Time, using GetSystemTime function.
#include <Windows.h>
#include <stdio.h>
void main()
printf("Year:%d\nMonth:%d\nDate:%d\nHour:%d\nMin:%d\nSecond:% d\n" ,st.wYear,st.wMonth,st.wDay,st.wHour,st.wMinute,st.wSecond);

Edited 3 Years Ago by happygeek: fixed formatting

Or just do a difftime() between now and next Saturday @00:14, then call Sleep() for that many seconds.

One calculation, one call to Sleep().
5 lines of code, and it's sorted.

great idea -- lot simplier than my suggestion

That also seems more efficient than what I had in mind, which was running a task that would check systemdate for saturday and execute "whatever" once on that day.

Thanks all for the replies :) I think I find the difftime() interesting to check.
I will check this out now.
I found a SleepFunction under Thread :: Sleep();


int Day;
int Minute;

time_t now = time(0);
struct tm* tm = localtime(&now);

Day = tm->tm_wday;
Minute = tm->tm_min;

	 Thread::Sleep(240000); //4 Minutes Sleep or other calculated interval

	if( Day == 6 && Minute >= 15 )
This question has already been answered. Start a new discussion instead.