简体   繁体   中英

What's the best way to compare the equality of two DateTime's in C#… but only to a certain precision?

I have two datetimes, one from a timestamp, and another I'm generating in code. I need to test their equality, and would love to do it without too many expressions. Here's an example of my two dates:

DateTime expireTimeStampUTC = 
    DateTime.Parse(UTCValueFromDatabase));
DateTime expectedExpireTime = 
    DateTime.UtcNow.AddHours(NumberOfExpectedHoursInConfig);

This is too high precision of a test:

if (expireTimeStampUTC.Equals(expectedExpireTime)){}

I don't care if they're exact to the second, just the hour.

Could it possibly be the best solution to do something compound like this:

if (expireTimeStampUTC.Date.Equals(expectedExpireTime.Date))
{
    if (!expireTimeStampUTC.Hour.Equals(expectedExpireTime.Hour))
    {
        pass = false;
    }
}

I'm not the most experienced with C#... is there some elegent way to do this?

If the problem you are having is because of them is a database type, you could convert to that type and compare there, we lose/gain about a millisecond converting to SQLDateTimes on roughly 1/3 of DB saves.

If not, compare the unit you actually care about:

DateTime dt1 = DateTime.UtcNow;
DateTime dt2 = DateTime.UtcNow.AddMinutes(59); // or 1 or 61 for test values;

// if the dates are in the same hour (12:10 == 12:50, 1:58 != 2:02)
if(dt1.Hour == dt2.Hour) // result

or if you care that they are within an hour time span

// if the dates are within one hour of each other (1:58 == 2:02, 3:30 != 4:45)
if((dt1 - dt2).Duration() < TimeSpan.FromHours(1)) // result

Here subtracting dates generates a time span, the duration is the 'absolute value' and then we create the limit explicitly from a unit we care about (FromHours) and compare.

The last line is as clean as I can think to do equality within a particular time span.

How about finding the difference between the two hours, and seeing if it's below a certain threshold (say 3600 seconds for an hour)?

var diff = expireTimeStamp.Subtract(expectedExpireTime).TotalSeconds;
pass = Math.Abs(diff) < 3600;

Subtract them. Check whether the resulting TimeSpan is within a certain maximum range.

Construct new DateTime objects and compare them. In C#, there's very little penalty for constructing "throwaway" objects in this way.

I experienced the problem when trying to write unit tests which committed then retrieved business objects. I was trying to use the "StartTime" property of my object to ensure the object could be retrieved and encountered this problem. The "StartTime" value committed in the database lost 6 digits of precision in the Ticks value!

Here's how I rewrote my test condition so my test would perform correctly and pass. Relevant line is second-to-last line of block.

DateTime start = DateTime.Now;
NewEventInfo testEvent1 = FakeEvent("test01action", start); //plus other params for testing
mServiceClient.AddEvent(testEvent1);

EventInfo[] eventInfos = null; //code to get back events within time window

Assert.IsNotEmpty(eventInfos);
Assert.GreaterOrEqual(eventInfos.Length, 1);

EventInfo resultEvent1 = eventInfos.FirstOrDefault(e => 
e.Action == "test01action" &&
Math.Abs(e.StartTime.Subtract(testEvent1.StartTime).TotalMilliseconds) < 1000); //checks dates are within 1 sec
Assert.IsNotNull(resultEvent1);

This way I could be sure the object was the one committed by the unit test, as the EventInfo.StartTime property only uses date times with precision of 1 second.

EDIT: Added Math.Abs(around DateTime diff) to ensure absolute value being compared to 1000.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM