Try this;
Set the downtime offset to (6,23,30,0) this is a Saturday at 11:30pm
Set the duration to 4hrs.
Set the StartofWeek to 6/18/2006 a Sunday
Set the time to check to (2006,6,18,0,30,0) this is Sunday at 12:30am
So with these values the downtime is from Sat at 11:30pm to Sun at
3:30am.
Your code will return a "no" for anytime after midnight on Saturday,
because once a new week starts it places Saturday's offset at the end
of this new week. Remember you based your calculations from a beginning
of the week.
This is why you also need to check the previouse week in the
calculation. Easy enough just subtract 7 days and do the check again.
If either results are greater than zero and less than the duration then
we are in a downtime window.
I added a few line to your test,
public bool TestTime(DateTime StartOfWeek, DateTime time)
{
DateTime dtDowntimeStart = StartOfWeek + StartOffset;
TimeSpan ThisWeek = time - dtDowntimeStart;
DateTime dtDowntimeLastWeek =
dtDowntimeStart.Subtract(TimeSpan.FromDays(7));
TimeSpan LastWeek = time - dtDowntimeLastWeek;
if (ThisWeek >TimeSpan.Zero && ThisWeek < Duration){return (true);}
if (LastWeek > TimeSpan.Zero && LastWeek < Duration){return
(true);}
return (false);
}
Not Nonsense,
AJS
(E-Mail Removed) wrote:
> ajs wrote:
> > There is one one
> > issue that your code didn't account for.
>
> Nonsense.
>
> >
> > What if the downtime was on a Saturday at 11:00pm for 4 hrs, If I read
> > your code correctly, if I check for a down time just 2 hours later
> > (Sunday at 1:00 am) this would not show that I am currently in a down
> > time.
>
> Of course it will.
> >> TimeSpan range = time - dtDowntimeStart;
>
> time & dtDowntimeStart are full datetime objects, and so, range
> will be a proper TimeSpan object, giving the difference in days, hours,
> minutes etc.
>
> Truth,
> James