tags:

views:

175

answers:

3

It seems that there are several possible ways to determine if a given System.DateTime represents midnight. What are the pros and cons of each? Is one more readable or perform better than the others?

EDIT: I believe that readability is more important than performance until profiling shows that there is an issue. That is why I asked about both.

Example 1

Public Function IsMidnight(ByVal value As Date) As Boolean
    Return value.TimeOfDay = TimeSpan.FromHours(0)
End Function

Example 2

Public Function IsMidnight(ByVal value As Date) As Boolean
    Return value.CompareTo(value.[Date]) = 0
End Function
+2  A: 

Depends. Do you want to check for exactly midnight to the second, or just that it's the midnight hour?

For the midnight hour

Public Function IsMidnightHour(ByVal date as Date) As Boolean
  return date.Hour = 0
End Function

For midnight hour and minute

Public Function IsMidnightHourAndMinute(ByVal date as Date) As Boolean
  return date.Hour = 0 AndAlso date.Minute = 0
End Function

For purely simply, exactly midnight you can use your example. However that will include checks up to the millisecond which may not be what you want.

JaredPar
I am looking for exactly midnight to the millisecond.
Eric Weilnau
@Eric, then Marc's answer is the most efficient way.
JaredPar
+5  A: 

I'd check (using C# for the example):

bool isMidnight = value.TimeOfDay.Ticks == 0;

IMO, this is easier than using FromHours etc, and doesn't involve any extra multiplication (since Ticks defined the TimeSpan - all the other properties are calculated).

Marc Gravell
I'd think that the ticks measurement would hardly ever evaluate to true. Executing a couple of statements would involve a few ticks.Might be easier to check for minutes.
Cerebrus
It would evaluate to true for all the times that represent *true* midnight. You wouldn't call it on `DateTime.Now`, obviously, but it does the job as stated...
Marc Gravell
More importantly, it exactly matches the "Return value.TimeOfDay = TimeSpan.FromHours(0)" from the original question. Checking for minutes etc **doesn't**
Marc Gravell
@Cerebrus, see the comments on my answer. OP is looking for exactly midnight down to the tick
JaredPar
@Cerebrus, whether or not that is a good approach though is contextual. For actual captured times, probably not. But if the user is entering times or filtering is ocurring it may be perfectly OK
JaredPar
+2  A: 

This is a bit of micro-optimizing, either method would work fine.

Anyhow, I think this is what would perform best:

Public Function IsMidnight(ByVal value As Date) As Boolean
   Return value.TimeOfDay.Ticks = 0
End Function

If you want understandable code, perhaps this is best:

Public Function IsMidnight(ByVal value As Date) As Boolean
   Return value.TimeOfDay = TimeSpan.Zero
End Function
Guffa
Actually, those should perform more or less equally, since Ticks is used as an internal value in TimeSpan. So (timespan == timespan) is more or less the same as (timespan.ticks == timespan.ticks) which in both cases will be (timespan.ticks == 0)
Lars Mæhlum
Yes, the == operator is implemented to compare the Ticks properties, so TimeSpan.Zero.Ticks is just a longer route to get to zero. :)
Guffa