You can do this as follows:
public static class TempoUtils{
private static int MultiploSeguinte(int n, int m){
var resto = n % m;
if(resto == 0) return n;
var faltam = m - resto;
return n + faltam;
}
public static TimeSpan CeilMinutos(this TimeSpan tempo, int multiplo){
var multiploSeguinte = MultiploSeguinte(tempo.Minutes, multiplo);
return new TimeSpan(tempo.Hours, multiploSeguinte, tempo.Seconds);
}
}
void Main()
{
var tempo = new TimeSpan(12, 21, 00);
tempo.CeilMinutos(30).Dump();
}
There's a lot going for it so it's worth an explanation. The MultiploSeguinte
function calculates the next multiple (or the number itself if it is a multiple) of a number depending on the 'm' parameter.
The CeilMinutos
function uses MultiploSeguinte
but works with the TimeSpan
structure which is the structure you are interested in.
I've made use of extension methods that allow you to access static methods as if they were instance methods. In practice this allows you to call tempo.CeilMinutos
instead of TempoUtils.CeilMinutos(tempo, 30)