In terms of efficiency, you should hardly be worrying about that. But I think you could make this a little more human readable / self-explanatory and a little less complicated than it is / looks.
/// <summary>
/// Returns the period of time left before the specified hour is due to elapse
/// </summary>
/// <param name="hour">And integer representing an hour,
/// where 0 is midnight, 12 is midday, 23 is eleven et cetera</param>
/// <returns>A TimeSpan representing the calculated time period</returns>
public static TimeSpan GetTimeUntilNextHour(int hour)
{
var currentTime = DateTime.Now;
var desiredTime = new DateTime(DateTime.Now.Year,
DateTime.Now.Month, DateTime.Now.Day, hour, 0, 0);
var timeDifference = (currentTime - desiredTime);
var timePeriod = currentTime.Hour >= hour ?
(desiredTime.AddDays(1) - currentTime) :
-timeDifference;
return timePeriod;
}
In hindsight and largely due to my attention being drawn by comments, this could be shorter, by omitting the timeDifference
variable assignment and calculating in-line, further, we don't need to negate the value of either; and also we can return
rather than assign timePeriod
:
var currentTime = DateTime.Now;
var desiredTime = new DateTime(DateTime.Now.Year,
DateTime.Now.Month, DateTime.Now.Day, hour, 0, 0);
return currentTime.Hour >= hour ?
(desiredTime.AddDays(1) - currentTime) :
desiredTime - currentTime;
A few notes here to qualify my statement...
Lines aren't expensive, but if you must have this on eight lines you could still do so - I've purposely dropped desiredTime
to cover two lines, not strictly to stay on the safe side of history (though it helps), but just to maintain flow - where line endings are relative rather than sporadic, think of a paperback book.
I'd recommend naming things aptly, that way sometimes people don't necessarily need examine how the code does what it does, which allows them to either A) scan and find what they're looking or B) figure it out without having to wonder what was going through that guys head.
Use of implicitly typed variables (var
), these are available from C# 3.0 onwards; this feature allows us to omit the specific type name completely when assigning a value / reference as part of the declaration - just be sure to use this wisely, clarity should maintained for the reader. You didn't specify which language version you're using, so this may not be available to you, but bear it in mind for the future.
DateTime
provides operators to that allow us to work with them directly in this instance, so no need to bother with FromTicks
, TotalMilliseconds
, TotalTicks
, TimeOfDay
et cetera.
As a final note: an empty statement (;
) produces a compiler warning, these shouldn't be ignored and, in any place I know, you'd at least have to explain yourself if someone came across this; it could also produce a compile-time error depending on the environment - that and the preceding condition are not only redundant in this case but neither are they indicative of anything, so much so that even you felt obliged to leave a comment.