473,406 Members | 2,954 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

DateTime Comparision

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

Jun 12 '06 #1
8 4612
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.
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


Jun 12 '06 #2
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
AJS

Jun 12 '06 #3
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


Jun 12 '06 #4
ajs
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

ja**********@gmail.com 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


Jun 15 '06 #5
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

Jun 16 '06 #6
ajs
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

ja**********@gmail.com 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


Jun 19 '06 #7
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


Jun 20 '06 #8
ajs
No problem, Your code helped me figure out a few things.

-AJS

ja**********@gmail.com 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


Jun 20 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: Donnal Walter | last post by:
I was very surprised to discover that >>> import datetime >>> x = datetime.date(2004, 9, 14) >>> y = datetime.datetime(2004, 9, 14, 6, 43, 15) >>> print x == y True How can these two...
2
by: Florian Lindner | last post by:
Hello, I've read the chapter in the Python documentation, but I'm interested in a a more in-depth comparision. Especially regarding how pythonic it is and how well it performs and looks under...
1
by: John Black | last post by:
Hi, In using find_if() you need to name a comparision function which normally is a static function, but sometimes it is really inconvinient for this, let's take this example, class MyClass{...
2
by: Ashwin Kambli | last post by:
Hi, I am doing a study on the performance comparision of C# and Java. Links to any articles on this topic will be greatly appreciated. Thanking you, Ashwin
9
by: jaym1212 | last post by:
Execution of the following simple code results in variable z being assigned the value of 1 ... x = 234; y = 234; z = (x == y); .... but I wanted z to be 234. What is the most efficient...
3
by: kd | last post by:
Hi All, How to perform case-insensitive comparision of strings? Would there be some kind of an indicator, which when set to true, would allow case-insenitive comparision of strings using...
2
by: nirav.lulla | last post by:
I have been given the task to come up with Requirements, Comparision and Migration document from Shadow Direct to DB2 Connect. I am very new much to all this, but kind of know a little bit about...
3
by: abctech | last post by:
I have an Html page, user enters a Date (dd-mm-yyyy) here. There's a servlet connected in the backend for processing this submitted information, it must have a method to compare this entered date...
3
by: ChrisB | last post by:
Hello, I was wondering what the easiest way is to compare two DateTime objects and not have the time components be included in the comparision. For, example, if time1 = 10/01/07 9:00 am, and...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.