I'm trying to emulate the way Google Calendar and Outlook handle time selection where all time entries are 30 minutes apart. I almost have every thing working with functional tests except for one thing. My problem is that I can't figure out how to ensure I'm always rounding the current date up to 30 minutes.
I'm using the following code:
protected DateTime GetStartTime()
{
var spanTicks = TimeSpan.FromMinutes(30).Ticks;
var ticks = (Time.Now.Ticks + spanTicks - 1) / spanTicks;
return new DateTime(ticks * spanTicks);
}
This works for the majority of the cases except for 12:00am (where I would expect this to return 12:30am) and say 8:56am (where I would expect it to return 9:00am).
How can I fix this?
Let .NET take care of all the special cases for you: