PC Review


Reply
Thread Tools Rate Thread

DateTime Comparision

 
 
ajs
Guest
Posts: n/a
 
      12th Jun 2006
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

 
Reply With Quote
 
 
 
 
ajs
Guest
Posts: n/a
 
      12th Jun 2006
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.


ajs wrote:
> 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


 
Reply With Quote
 
Saad Rehmani
Guest
Posts: n/a
 
      12th Jun 2006
DateTime overloads the >, <, ==, !=, +, - operators


> 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
> AJ



 
Reply With Quote
 
james.curran@gmail.com
Guest
Posts: n/a
 
      12th Jun 2006
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.

ajs wrote:
> 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


 
Reply With Quote
 
ajs
Guest
Posts: n/a
 
      15th Jun 2006
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

(E-Mail Removed) wrote:
> 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.
>
> ajs wrote:
> > 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


 
Reply With Quote
 
james.curran@gmail.com
Guest
Posts: n/a
 
      16th Jun 2006
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

 
Reply With Quote
 
ajs
Guest
Posts: n/a
 
      19th Jun 2006
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


 
Reply With Quote
 
james.curran@gmail.com
Guest
Posts: n/a
 
      20th Jun 2006
I see your point. Sorry about doubting you.

ajs wrote:
> 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


 
Reply With Quote
 
ajs
Guest
Posts: n/a
 
      20th Jun 2006
No problem, Your code helped me figure out a few things.

-AJS

(E-Mail Removed) wrote:
> I see your point. Sorry about doubting you.
>
> ajs wrote:
> > 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


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
build Dictionary< DateTime , decimal > from List < DateTime > John A Grandy Microsoft C# .NET 1 5th Feb 2009 03:08 AM
Millisecond values missing when inserting datetime into datetime column of sql Server Manikandan Microsoft C# .NET 4 18th Jul 2007 08:59 PM
How can I save a DateTime from my C# program into a SQL Server (datetime) database column. Steve Kershaw Microsoft ASP .NET 5 29th Aug 2006 03:10 AM
DateTime conversion to its sqlServer datetime float representation. A.Neves Microsoft C# .NET 1 20th Apr 2006 01:01 PM
DateTime problem: how to create datetime format hh:mm:ss AM/PM without the date mimi Microsoft C# .NET 1 6th Aug 2004 08:28 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 02:55 AM.