Are we currently in the OnTime Period?

	//Time Now: 11:00
	//
	//	OnTime	OffTime	State
	//	10:00	22:00	On
	//	08:00	09:00	Off
	//	10:00	08:00	On
	//	23:00	12:00	On
	//	20:00	23:00	Off

	//Can be done with DateTime objects, or simple int's like this:
	//int DateTimeNow = (RtcHour * 60) + RtcMinute;
	//int DateTimeOnTime = (StartTimeHours * 60) + StartTimeMins;
	//int DateTimeOffTime = (EndTimeHours * 60) + EndTimeMins;

	bool WeAreOn = false;
	if (DateTimeNow >= DateTimeOnTime)
	{
		if (DateTimeNow <= DateTimeOffTime)
		{
			WeAreOn = true;
		}
		else if (DateTimeOnTime > DateTimeOffTime)
		{
			WeAreOn = true;
		}
		else
		{
			WeAreOn = false;
		}
	}
	else
	{
		if (
			(DateTimeOnTime > DateTimeOffTime) &&
			(DateTimeNow < DateTimeOffTime)
		)
		{
			WeAreOn = true;
		}
	}
Feel free to comment if you can add help to this page or point out issues and solutions you have found. I do not provide support on this site, if you need help with a problem head over to stack overflow.

Comments

Your email address will not be published. Required fields are marked *