473,666 Members | 2,575 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 4625
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.Sunda y && time.Hour == 23) ||
(time.DayOfWeek == DayOfWeek.Monda y && 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(TimeSp an off, TimeSpan dur)
{
StartOffset = off;
Duration = dur;
}
public bool TestTime(DateTi me 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(St artOfWeek, time))
{
Console.WriteLi ne("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**********@gm ail.com wrote:
DateTime time = DateTime.Now;
if (time.DayOfWeek == DayOfWeek.Sunda y && time.Hour == 23) ||
(time.DayOfWeek == DayOfWeek.Monda y && 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(TimeSp an off, TimeSpan dur)
{
StartOffset = off;
Duration = dur;
}
public bool TestTime(DateTi me 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(St artOfWeek, time))
{
Console.WriteLi ne("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(DateTi me StartOfWeek, DateTime time)
{
DateTime dtDowntimeStart = StartOfWeek + StartOffset;
TimeSpan ThisWeek = time - dtDowntimeStart ;
DateTime dtDowntimeLastW eek =
dtDowntimeStart .Subtract(TimeS pan.FromDays(7) );
TimeSpan LastWeek = time - dtDowntimeLastW eek;
if (ThisWeek >TimeSpan.Zer o && ThisWeek < Duration){retur n (true);}
if (LastWeek > TimeSpan.Zero && LastWeek < Duration){retur n
(true);}
return (false);
}

Not Nonsense,

AJS

ja**********@gm ail.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**********@gm ail.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
13119
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 objects be considered equal? Is there a *general* way to test for date != datetime as well as 4.5 != 4.6?
2
1472
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 Windows. I've some C++ experiences with Qt, so I'm very interested to have PyQt compared to wxWindows and Tk. How fast does PyQt catches up with the versiones released from Trolltech? etc.. Thx, Florian
1
1732
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{ vector<int> myVec; void func(); };
2
2987
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
1469
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 method (min CPU cycles) of doing this? (I realize it could be accomplished as follows)
3
1636
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 if/select case, etc; after performing the case-insenisitive compare, the indicator can be reset to allow the conventional comparision of strings? kd
2
2661
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 both of them. There has been a lot of pressure from business to move to DB2 Connect from Shadow Direct which the company is currently using since almost 7 years. I would greatly appreciate if any one of you can help or send me some comparision...
3
4271
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 with the dates obtained from the backend table which again are retrieved as strings (dd-mm-yyyy). Can anyone suggest how to carry out comparision between these date strings? Eg: String Entered Date: "28-02-2007" String Backend Date:...
3
6216
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 time2 = 10/1/07 10:00 am, time1.CompareTo(time2) == 0 should evaluate to true. Thanks, Chris
0
8454
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8363
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8787
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8645
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6203
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5672
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4200
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4372
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2013
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.