I'm looking to do a difference between an schedule and Integer (this Integer is a number of hours).
I'm looking to compute number hours and minutes. I would like display
How can I change my code to compute hours and minutes ?
I'm looking to do a difference between an schedule and Integer (this Integer is a number of hours).
I'm looking to compute number hours and minutes. I would like display
How can I change my code to compute hours and minutes ?
Possible approach with comments inside
import java.time.temporal.ChronoUnit
import java.time.LocalTime
import scala.concurrent.duration._
val t = LocalTime.now()
def toEnd(t: LocalTime) = {
// start of the day
val start = LocalTime.of(9, 0)
// end of first half
val midEnd = LocalTime.of(13, 0)
// start of second half
val midStart = LocalTime.of(14, 0)
// end of the day
val end = LocalTime.of(18, 0)
// before start of the day
if (t.isBefore(start)) 8.hours
// first half
else if (t.isBefore(midEnd)) t.until(midEnd, ChronoUnit.MILLIS).millis + 4.hours
// in between
else if (t.isBefore(midStart)) 4.hours
// second half
else if (t.isBefore(end)) t.until(end, ChronoUnit.MILLIS).millis
// after the end
else 0.hours
}
// here you can add any specific format for evaluated duration
implicit class formatter(d: FiniteDuration) {
def withMinutes = {
// convert to minutes
val l = d.toMinutes
// format
s"${l / 60}:${l % 60}"
}
}
toEnd(t).withMinutes
toEnd(LocalTime.of(9, 30)).withMinutes
toEnd(LocalTime.of(12, 30)).withMinutes
toEnd(LocalTime.of(13, 30)).withMinutes
toEnd(LocalTime.of(14, 30)).withMinutes