Reputation: 33
I am making a function to check time fall between a time range in 24hr format, However there is some thing wrong with my code , can any one point out how to fix ?
My code:
bool isDoTime(int starthour, int startminute, int endhour, int endminute)
{
TimeSpan start = new TimeSpan(starthour, startminute, 0);
TimeSpan end = new TimeSpan(endhour, endminute, 0);
TimeSpan add24h = new TimeSpan(24, 0, 0);
TimeSpan now = DateTime.Now.TimeOfDay;
if (starthour > endhour || (endhour == starthour && endminute <= startminute))
{
end += add24h;
}
if ((now > start) && (now < end))
{
return true;
}
return false;
}
Problem: i want to return true when current time between 20:30 - 3:30 , however when i run my code as below. the condition is return true only from 8:30 to 00:00 , not true from 00:00 - 3:30
if (isDoTime(20,30,3,30) //return true from 20:30 - 3:30
{
//dosomething
}
Upvotes: 3
Views: 3749
Reputation: 22442
Split up in one check if it spans across midninght, and one for same day.
TimeSpan start = new TimeSpan(starthour, startminute, 0);
TimeSpan end = new TimeSpan(endhour, endminute, 0);
TimeSpan now = DateTime.Now.TimeOfDay;
//The readable version:
if(start>end){
//Must check if after start (before midnight) or before end (after midnight)
if((now > start) || (now < end)){
return true;
{
}
else
{
//Simple check - span is within same day
if ((now > start) && (now < end))
{
return true;
}
}
return false;
The short/cryptic version:
return start > end ? (now > start) || (now < end) : (now > start) && (now < end);
Upvotes: 5
Reputation: 137128
I think you'd be better off using DateTime
however, you will still need to check that if the start time is greater than the end time and add 24 hours in that case.
Your method would start:
bool isDoTime(int starthour, int startminute, int endhour, int endminute)
{
DateTime start = new DateTime(0, 0, 0, starthour, startminute, 0);
DateTime end = new DateTime(0, 0, 0, endhour, endminute, 0);
if (start > end)
{
end.AddDays(1);
}
DateTime now = DateTime.Now;
return start < now && now < end;
}
Though you might want <=
tests depending on your logic
(Unless it's your logic of where you're adding 24 hours of course - does that code execute?)
Upvotes: 1
Reputation: 416
[TestFixture]
public class Class1
{
private DateTime _now;
[SetUp]
public void SetUp()
{
_now = DateTime.MinValue.AddDays(1).AddHours(2); //02.01.0001 02:00:00
}
[Test]
public void TestCase()
{
Assert.IsTrue(IsDoTime(20, 30, 3, 30));
}
bool IsDoTime(int starthour, int startminute, int endhour, int endminute)
{
var start1 = DateTime.MinValue.AddHours(starthour).AddMinutes(startminute); //01.01.0001 20:30:00
var end1 = endhour < starthour
? DateTime.MinValue.AddDays(1).AddHours(endhour).AddMinutes(endminute) //02.01.0001 03:30:00
: DateTime.MinValue.AddHours(endhour).AddMinutes(endminute);
return ((_now > start1) && (_now < end1));
}
}
Upvotes: 0
Reputation: 46098
You want to use DateTime
structures rather than integers. You can also generalise it to arbitary DateTimes. If secondTime
is less than firstTime
, it adds 1 day to secondTime
.
public bool IsBetween(this DateTime thisTime, DateTime firstTime, DateTime secondTime) {
if (secondTime < firstTime)
secondTime = secondTime.AddDays(1);
return firstTime < thisTime && thisTime < secondTime);
}
// to use...
bool isDoTime = DateTime.Now.IsBetween(firstTime, secondTime);
Upvotes: 3
Reputation: 60694
It will be much easier to make to use DateTime's as parameters here, and avoid the whole problem of manually checking:
bool isDoTime(DateTime starttime, DateTime endtime)
{
if (DateTime.Now > starttime && DateTime.Now < endtime)
{
return true;
}
return false;
}
Upvotes: 0