You can do it this way:
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 is a lot going on so it is worth an explanation. The function MultiploSeguinte
Calculates the next multiple (or the number itself if it is a multiple) of a number depending on the’m parameter'.
The function CeilMinutos
uses MultiploSeguinte
but works with the structure TimeSpan
which is the structure that Voce is interested in.
I have made use of extension methods that allow you to access static methods as if they were instantiation methods. In practice this allows you to call tempo.CeilMinutos
instead of TempoUtils.CeilMinutos(tempo, 30)
take a look in that reply, I think it solves.
– rLinhares
Always for the biggest? 12:21 turns to 12:30 or 12:20?
– Leandro Angelo
This always goes on and on.
– Brayan