It's a lot used method of rounding to add half of desired precision and then cut the decimals. Examples:
Desired precision: 0.1 ( / 2 -> add 0.05 )
1.44 + 0.05 = 1.49 -> cut last -> 1.4
1.46 + 0.05 = 1.51 -> cut last -> 1.5
Desired precision: 0.01 ( / 2 -> add 0.005 )
1.443 + 0.005 = 1.448 -> cut last -> 1.44
1.465 + 0.005 = 1.470 -> cut last -> 1.47
Desired precision: 1minute -> add 0.5min or 30s or 30000 ticks* (assumed 1s = 1000 ticks). Unit doesn't matter as long as you keep units the same in all calculations.
1min 25s + 30s = 1min 55s -> cut -> 1min
1min 35s + 30s = 2min 05s -> cut -> 2min
So with datetime it's the same. In order to round by minute, you may add half a minute and then cut the rest away.
Ticks are one way of representing time. So just take the tick-count of a minute (new TimeSpan(0, 0, 1, 0)
), divide it by two to get the half way and then add it to your time.
Now all you need to do is to cut the rest away and you can do it by diving your time with the precision (tick count of 1 minute). When you divide by 1 minute, everything that's smaller than a minute will be in decimals. Tick is an integer so it "forgets" all the decimals, thus cutting them away. Now all you need to do is make the value valid again by multiplying it with the same value you divided it with. Decimals are already lost at this point, so... TADAA: Rounded by minute.