DateTime Comparision

  • Thread starter Thread starter ajs
  • Start date Start date
A

ajs

I have a process that verifies that a server is running. If the server
stops responding I send an email to a support person.

Now I want to add a downtime indicator so that my code does not send
mail if the sever has a scheduled downtime say, Sunday at 23:00 for 4
hrs.

Is there an easy way to see if the current time is within that window?

Thanks
AJS
 
One other thing to note is that the downtime window is weekly "Every
Sunday at 23:00 for 4hrs". So I don't have a specific day/month/year.
 
DateTime time = DateTime.Now;
if (time.DayOfWeek == DayOfWeek.Sunday && time.Hour == 23) ||
(time.DayOfWeek == DayOfWeek.Monday && time.Hour < 3)
{

But you probably want a more extendable solution.

I speent awy too long on this:

public class Downtime
{
private TimeSpan StartOffset;
private TimeSpan Duration;
public Downtime(TimeSpan off, TimeSpan dur)
{
StartOffset = off;
Duration = dur;
}
public bool TestTime(DateTime StartOfWeek, DateTime time)
{
DateTime dtDowntimeStart = StartOfWeek + StartOffset;
TimeSpan range = time - dtDowntimeStart;

return (range > TimeSpan.Zero && range < Duration);
}
}

To use:
public static void Main()
{
Downtime d1 = new Downtime(new TimeSpan(1, 16, 30, 0),
new TimeSpan(0, 4, 0, 0));
DateTime StartOfWeek = new DateTime(2006, 6, 11);
DateTime time = DateTime.Now;
if (d1.TestTime(StartOfWeek, time))
{
Console.WriteLine("Yes");
}
}

This first parameter to the Downtime constructor is the offset of the
beginning of the downtime from midnight Sunday morning (eg, the one
there is for Monday at 4:30PM)
The second parameter is the duration.

StartOfWeek is the date of the sunday of the current week.
 
Thanks for the reply,

I used an Idea from your code to solve my problem. There is one one
issue that your code didn't account for.

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.

What I did was I took the downtime Saturday at 11:00pm and created a
LastWeek, ThisWeek and NextWeek DateTime Values. This gave me a 3 week
span of time to cover the overlaps.

Then I compared Now to each of the downtime values. So if any of them
are within the duration value, then I am in a window.

Of course this assumes a downtime of less than 1 week.

AJS
 
ajs said:
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.
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
 
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
 
Back
Top