I have a timer that is only supposed to tick between x and x on weekdays.
I have used the following implementation.
void tmrMain_Tick(object sender, EventArgs e)
{
if (!(DateTime.Now.DayOfWeek == DayOfWeek.Saturday || DateTime.Now.DayOfWeek == DayOfWeek.Sunday))
{
if ((IsTimeOfDayBetween(DateTime.Now, new TimeSpan(8, 0, 0), new TimeSpan(18, 0, 0))))
{
if (pnlAssembly.Visible == true)
{
cmdShed.PerformClick();
}
else if (pnlShed.Visible == true)
{
cmdServices.PerformClick();
}
else if (pnlWeb.Visible == true)
{
cmdAssembly.PerformClick();
}
}
}
}
static bool IsTimeOfDayBetween(DateTime time,
TimeSpan startTime, TimeSpan endTime)
{
if (endTime == startTime)
{
return true;
}
else if (endTime < startTime)
{
return time.TimeOfDay <= endTime ||
time.TimeOfDay >= startTime;
}
else
{
return time.TimeOfDay >= startTime &&
time.TimeOfDay <= endTime;
}
}
Is this the way to go?
== true
bits. It's redundant. For example,if (pnlAssembly.Visisble) {
is more idiomatic as it's already a Boolean expression and reads more naturally. \$\endgroup\$