tags:

views:

596

answers:

4

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?

A: 

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

overslacked
Again, I'd love to see an example in code.
danieltalsky
+5  A: 

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;
Matt Hamilton
Excellent... that was the answer so stupidly obvious I would kick myself for not thinking of it. Thanks. And with a code sample too. Magical.
danieltalsky
Although, no "var" in C# ;)
danieltalsky
Maybe not in *your* C#! Time to upgrade to Visual Studio 2008, mate! :)
Matt Hamilton
var (C# Reference): http://msdn.microsoft.com/en-us/library/bb383973.aspx
Ken Browning
Wild. I think my dev lead would shoot me if I started using that.
danieltalsky
Nothing harmful about "var". In my code snippet above it's totally obvious what the "diff" variable means, and you don't care about its compile-time type. (Is it Double? Int? Long? Who cares?)
Matt Hamilton
+4  A: 

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

recursive
DateTime values support the subtraction operation. you get a TimeSpan, you check if it is smaller than the specified interval.
Cheeso
That's pretty much what I just said.
recursive
+5  A: 

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.Now;
DateTime dt2 = DateTime.Now.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 == dt1.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.

Colin Dabritz
Actually I think this is actually better style. I think it's clearer what's happening. I've never changed the "right answer" token before, but I think in this case, this is the better solution.
danieltalsky
Fair enough. I think it's six one/half a dozen the other. The "Duration()" syntax might be more readable, but it has the (admittedly negligible) overhead of newing up a TimeSpan on both sides of the comparison.
Matt Hamilton