Difficulty comparing differences in DateTimes - c#

I have a DateTime variable called "lastActivated", that is set to DateTime.Now when the Form_Activated event fires. The goal is to make sure something doesn't happen within the first 1 second of a user clicking the screen from another screen.
DateTime? lastActivate = null; //used to determine the last time the screen was focused.
private void Form1_Activated(object sender, EventArgs e)
{
lastActivate = DateTime.Now;
}
The code for determining whether it has been longer than 1 second looks like
TimeSpan oneSec = new TimeSpan(0,0,1);
if (lastActivate == null || (TimeSpan)(lastActivate - DateTime.Now) > oneSec)
{
//stuff
}
The above if statement always, ALWAYS fails. Even when the values are:
lastActivate {11/30/2013 10:23:21 AM} System.DateTime?
now {11/30/2013 10:32:48 AM} System.DateTime
(I made a temp value DateTime now = DateTime.Now so I could paste the value here, since I couldn't directly access DateTime.Now's value)
Does anyone have any suggestions on what I'm doing wrong, and what I should change to get it to accomplish the goal I am after?
Thanks!

You can test the time between two dates by doing this:
var lastActivated = DateTime.Now;
if (DateTime.Now.Subtract(lastActivated).TotalSeconds > 1)
{
// Do whatever you need.
}
DateTime.Subtract(DateTime) returns a TimeSpan of the time difference between the two given dates.

Last active date is less than current time, you should subtract it from current time:
if (!lastActivate.HasValue || (DateTime.Now - lastActivate.Value).TotalSeconds > 1)
{
//stuff
}

Problem : You are subtracting in reverse order, you should subtract the lastActivate Time from the current Time(DateTime.Now).
otherwise you will always get -ve value when you subtract DateTime.Now from lastActivate as lastActivate time is always less than the Current Time (DateTime.Now).
Solution :
This:
(TimeSpan)(lastActivate - DateTime.Now)
Should be :
(TimeSpan)(DateTime.Now - lastActivate)
Complete Code:
TimeSpan oneSec = new TimeSpan(0,0,1);
if (lastActivate == null || (TimeSpan)(DateTime.Now - lastActivate) > oneSec)
{
//stuff
}

Related

Comparing timespan object if it's between hours in C#

I want to filter an array of objects. The goal is to filter the objects between two timespan objects:
TimeSpan tsFrom = TimeSpan.Parse("16:00");
TimeSpan tsTo = TimeSpan.Parse("00:59");
For example I have this object:
TimeSpan dateObject = TimeSpan.Parse("22:05:22");
The problem comes when I try to compare if the dateObject is between tsFrom and tsTo:
if (dateObject > tsFrom && dateObject < tsTo)
{
// do something ....
}
This won't work for cases like that. Do you have any ideas how I can make this work ?
You're wanting to works with times of day. The TimeSpan data type works with time spans (somewhat obvious to say). These are distinct concepts.
Times of day are precisely what motivated the creation of the new TimeOnly type in .NET 6. It has an IsBetween method:
Determines if a time falls within the range provided. Supports both "normal" ranges such as 10:00-12:00, and ranges that span midnight such as 23:00-01:00.
Note that IsBetween use the more sensible convention of inclusive start, exclusive end which means that you'd use 01:00 as the to moment and not accidentally exclude e.g. 00:59:17 from your period
For older versions, I'd suggest you realise that if To is less than From (e.g. it's representing a period that crosses midnight) you should check:
if ((tsTo > tsFrom && dateObject >= tsFrom && dateObject < tsTo) ||
(tsFrom > tsTo && (dateObject < tsTo || dateObject >= tsFrom)))
{
}
Note, again, that this logic is assuming inclusive From and exclusive To (Your original logic treated both ends as exclusive which is rarely correct)
The problem with this one is that you're trying to use the timeSpan method in the wrong way, if I got it right you are trying to check if the dateObject time comes between tsFrom and tsTo, the problem is that you are trying to refer to a different day with the tsTo, something that timespan can not handle.
I fixed it using dateTime to specify at least the day the time is taken from, i suggest changing the code to this
string datevalue = #"15/03/2021 16:00:00";
DateTime start = DateTime.Parse(datevalue);
datevalue = #"16/03/2021 00:59:00";
DateTime end = DateTime.Parse(datevalue);
datevalue = #"15/03/2021 22:05:22";
DateTime obj = DateTime.Parse(datevalue);
if (obj > start && obj < end)
{
//Do something
}

How to know if current time is between two timespans?

I have two time spans like so:
TimeSpan Starttime : 16:37:00
TimeSpan EndTime: 17:37:00
current time:
DateTime currentDate = DateTime.Now;
TimeSpan now = currentDate.TimeOfDay;
Problem:
I can't figure out how to know if the current time is between starttime and endtime. i want to send mesages only between those two timespans.
How do i do this?
My attempt:
if(startTime.Hours < now.Hours && endTime.Hours > now.Hours)
// do stuff
This does not cover all scenarios since I need it to be exactly between starttime and endtime to the last second but I dont know how to do this.
You can just use:
if (startTime < now && now < endTime)
Note that:
This doesn't check the date; doesn't look like that's an issue here
Depending on why you're doing this, you may want to consider intervals such as "10pm-2am" at which point you effectively want to reverse the logic
In most cases, it's worth making the lower-bound inclusive and the upper-bound exclusive, e.g.
if (startTime <= now && now < endTime)
That's useful because then you can have several intervals where the end of one interval is the start of the next interval, and any one time is in exactly one interval.
To handle the "10pm-2am" example, you'd want something like:
if (interval.StartTime < interval.EndTime)
{
// Normal case, e.g. 8am-2pm
return interval.StartTime <= candidateTime && candidateTime < interval.EndTime;
}
else
{
// Reverse case, e.g. 10pm-2am
return interval.StartTime <= candidateTime || candidateTime < interval.EndTime;
}

DateTime.Compare(start, end) resulting weired in my system

In the above picture you can see that the start and end value is same. but the compare method is returning -1, which means start time is less than end time. How is this possible?
I have tried sample values in a console application to test comapre method, & its working fine. I think here may be some internal value of datetime object is not matching. But couldn't find.
Here is the code.
DateTime start = Convert.ToDateTime(pi.StartTime), end = Convert.ToDateTime(pi.EndTime);
int t1 = DateTime.Compare(start, end);
if (t1 == 0)
{
MessageBox.Show("Start Time and End Time are same.");
return;
}
else if (t1 == 1)
{
MessageBox.Show("Start Time is greater then end time.");
return;
}
I suggest comparison with tolerance, e.g. trimming off milliseconds:
int t1 = DateTime.Compare(
new DateTime(start.Ticks - (start.Ticks % TimeSpan.TicksPerSecond), start.Kind),
new DateTime(end.Ticks - (end.Ticks % TimeSpan.TicksPerSecond), end.Kind));
Just after the posting of Question, I checked each properties and found that there are difference of 127 miliseconds. WOW! And then I convert my system datetime to string and then again converting to datetime (as the milisecond become 0). so everything works fine now. So is it ok what I am doing?
No. By doing a conversion you do not communicate intent. The one reading your code will not understand why you did like that.
A much better way would be:
var difference = date1.Substract(date2).TotalSeconds;
return Math.Abs(difference) < 1;
Because then you show in the code that you accept a small difference (and how large difference you allow).
From MSDN
Compares two instances of DateTime and returns an integer that
indicates whether the first instance is earlier than, the same as, or
later than the second instance.
Then it says :
To determine the relationship of t1 to t2, the Compare method compares
the Ticks property of t1 and t2 but ignores their Kind property.
Before comparing DateTime objects, ensure that the objects represent
times in the same time zone.
And that s why you're having that result : here's an example :
Edit :
using System;
public class Example
{
public static void Main()
{
DateTime date1 = new DateTime(2009, 8, 1, 0, 0, 0);
DateTime date2 = new DateTime(2009, 8, 1, 0, 0, 0);
date2 = date2.AddMilliseconds(2);
int result = DateTime.Compare(date1, date2);
string relationship;
if (result < 0)
relationship = "is earlier than";
else if (result == 0)
relationship = "is the same time as";
else
relationship = "is later than";
Console.WriteLine("{0} {1} {2}", date1, relationship, date2);
}
}
// The example displays the following output:
// 8/1/2009 12:00:00 AM is earlier than 8/1/2009 12:00:00 AM
Hope that helped.
You can compare
DateTime.Compare(
start.AddMilliseconds(-start.Millisecond),
end.AddMilliseconds(-end.Millisecond)
);
or even better with an extension method
DateTime.Compare(start.TrimMilliseconds(), stop.TrimMilliseconds())
public static class DateTimeExtensions
{
public static DateTime TrimMilliseconds(this DateTime date)
{
return date.AddMilliseconds(-date.Millisecond);
}
}
please not that DateTime values are immutable so you are comparing two different DateTime values. start and end are not modified and are still differnt. you can avoid that with trimming the milliseconds during assignment
var start = DateTime.Now.TrimMilliseconds();

TimeSpan Time Calculation, Minutes - ho to convert time span to int

I have the folowing code,
now the question is what is the best way to preform this:
also take in notice the "minAdd" can pass 60, meaning 90min add (an hour an half etc)
thanks,
int minAdd = Convert.ToInt16(txtMinAdd.text);
DateTime now = DateTime.Now;
DateTime nextEvent = DateTime.Now.AddMinutes(minAdd);
TimeSpan diff = now - nextEvent;
if (diff > minAdd) -------------- PROBLEM HERE
{
//act here
}
EDIT: As noted by Reed, the code you've shown is pretty pointless. I assume you actually want to get nextEvent from somewhere else.
I suspect you just want:
if (diff.TotalMinutes > minAdd)
{
}
Or you could use:
TimeSpan minTimeSpan = TimeSpan.FromMinutes(Convert.ToInt16(txtMinAdd.text));
...
if (diff > minTimeSpan)
{
}
Since diff is based on nextEvent, which is based exactly on minAdd, there is no reason for this check - it will never be true.
Also, in your code, diff will always be negative if minAdd is positive, as you're subtracting a future event (nextEvent) from DateTime.Now.
If you are trying to schedule an event to occur at a point in time, you may want to consider using a Timer, and scheduling the timer to occur at some point in time based on the event time:
DateTime nextEvent = DateTime.Now.AddMinutes(minAdd);
TimeSpan diff = nextEvent - DateTime.Now;
// Schedule a timer to occur here
someTimer.Interval = diff.TotalMilliseconds; // Timer intervals are typically in ms

Determine if current time between multiple time spans

I'm having trouble figuring out which tradings session any particular time is in.
There are four possible sessions, show in this picture taken from ForexFactory.com
I have this method that I need to check is currentTime is during the specified trading session.
public bool IsTradingSession(TradingSession tradingSession, DateTime currentTime)
{
//currentTime is in local time.
//Regular session is 5PM - next day 5PM, this is the session in the picture.
//Irregular sessions also occur for example late open (3AM - same day 5PM) or early close (5PM - next day 11AM)
DateTime sessionStart = Exchange.ToLocalTime(Exchange.CurrentSessionOpen);
DateTime sessionEnd = Exchange.ToLocalTime(Exchange.CurrentSessionClose);
if(tradingSession == TradingSession.Sydney)
return ....... ? true : false;
if(tradingSession == TradingSession.Tokyo)
return ....... ? true : false;
if(tradingSession == TradingSession.London)
return ....... ? true : false;
if (tradingSession == TradingSession.NewYork)
return ....... ? true : false;
return false;
}
Use:
bool isSydneySession = IsTradingSession(TradingSession.Sydney, CurrentTime);
bool isTokyoSession = IsTradingSession(TradingSession.Tokyo, CurrentTime);
bool isLondonSession = IsTradingSession(TradingSession.London, CurrentTime);
bool isNewYorkSession = IsTradingSession(TradingSession.NewYork, CurrentTime);
Thank you
I'd suggest writing a simple function for each trading session, which takes a DateTime and returns a bool indicating if it's open at that time.
var sydneyOpen = new TimeSpan(17, 0, 0);
var sydneyClose = new TimeSpan(2, 0, 0);
Func<DateTime, bool> isOpenInSydney = d =>
(d.TimeOfDay > sydneyOpen || d.TimeOfDay < sydneyClose);
// same for other markets, write a function to check against two times
Then place these into a Dictionary<TradingSession, Func> like this for generic retrieval...
var marketHours = new Dictionary<TradingSession, Func<DateTime, bool>>();
marketHours.Add(TradingSession.Sydney, isOpenInSydney);
// add other markets...
And then your existing method simply selects the appropriate function for the given TradingSession and applies it
public bool IsTradingSession(TradingSession tradingSession, DateTime currentTime)
{
var functionForSession = marketHours[tradingSession];
return functionForSession(currentTime);
}
I don't believe you need UTC time here as long as your application only runs in a single timezone, but daylight savings might cause problems.
A nice way to account for the problem of trading sessions which cover two days, as opposed to just one day, is to write a helper that precisely considers whether it's a 'cross-day' trading session and applies a different rule for you:
private bool IsBetween(DateTime now, TimeSpan open, TimeSpan close)
{
var nowTime = now.TimeOfDay;
return (open < close
// if open is before close, then now must be between them
? (nowTime > open && nowTime < close)
// otherwise now must be *either* after open or before close
: (nowTime > open || nowTime < close));
}
and then
var sydneyOpen = new TimeSpan(17, 0, 0);
var sydneyClose = new TimeSpan(2, 0, 0);
Func<DateTime, bool> isOpenInSydney = d => IsBetween(d, sydneyOpen, sydneyClose);
You can compare with > & < or compare ticks.
See related questions: Check if datetime instance falls in between other two datetime objects
To avoid the multiple if statements, you could also create a TradingSession object with start and end time and define a property/function to check if in session. When I have big switch or if blocks, it usually indicates a missed OO opportunity :)
TradingSession sydneySession = new TradingSession
{
StartTimeUtc = ...;
EndTimeUtc = ...;
}
The trading session object could then have a property IsInSession.
public bool IsInSession
{
get {
return DateTime.UTCNow >= StartTimeUtc && DateTime.UTCNow <= EndTimeUtc;
}
}
This uses UTC time to eliminate time zone issues.
You need to normalize your local times to UTC. You can then compare times across regions.
For each trading session, you need to know the session start and end time in UTC.
You need the current time in UTC. DateTime.UtcNow, for example.
You can then perform range comparisons for each trading session window:
if(tradingSession == TradingSession.Sydney)
return currentTimeUtc >= sydneyStartTimeUtc && currentTimeUtc <= sydneyEndTimeUtc;
etc...
If you're trying to validate that a transaction time is valid for a transaction on a particular trading session, this will work fine.
If however you're trying to figure out what trading session a trade belongs to based on its time, you will have multiple answers sometimes because the trading sessions overlap. Multiple trading sessions may be valid for a given time.

Categories