[FIXED] DateTime: Differenz zwischen Stunde und Ganzzahl

Ausgabe

Ich habe einige Fehler in meinem Code in den 2 Zeilen, in denen ein Kommentar darüber steht:

import java.time.temporal.ChronoUnit
import java.time.LocalTime

import scala.concurrent.duration._
val t = LocalTime.now()


def toStart(t: LocalTime) = {
  
  val start = LocalTime.of(9, 0)  
  val midEnd = LocalTime.of(13, 0)  
  val midStart = LocalTime.of(14, 0)  
  val end = LocalTime.of(18, 0)
  
  if (t.isBefore(start)) 0.hours
  
  // if (9 > myHour < 13 ==> myHour + 9 Hours, I wrote: - 9.hours instead of + 4.hours
  else if (t.isBefore(midEnd)) t.until(midEnd, ChronoUnit.MILLIS).millis - 9.hours
         
  else if (t.isBefore(midStart)) 4.hours
  
  //  if (14 > myHour < 18  Then (myhour - 14) + 4
  else if (t.isBefore(end)) t.until(end, ChronoUnit.MILLIS).millis
 
  else 8.hours
}

 
implicit class formatter(d: FiniteDuration) {
  def withMinutes = {    
    val l = d.toMinutes    
    s"${l / 60}:${l % 60}"
  }
  def withSeconds = s"${d.toHours}:${d.toMinutes % 60}:${d.toSeconds % 60}"
}

Der Test der Funktion ToStart ist in diesen zwei Fällen falsch:

scala> toStart(LocalTime.of(9, 30, 24)).withSeconds
res89: String = -5:-30:-24

scala> toStart(LocalTime.of(12, 30, 32)).withSeconds
res90: String = -8:-30:-32

scala> toStart(LocalTime.of(14, 30, 45)).withSeconds
res92: String = 3:29:15

scala> toStart(LocalTime.of(16, 22, 44)).withSeconds
res93: String = 1:37:16

Wie kann ich meinen Code ändern, um das beste Ergebnis zu finden?

Lösung

Der Code sollte meiner Antwort hier ähnlich sein , aber Sie müssen verstehen, was ich getan habe. Sie müssen auf jeden Fall die von mir verwendeten API-Aufrufe überprüfen, aber ich habe einige zusätzliche Kommentare hinzugefügt:

import java.time.temporal.ChronoUnit
import java.time.LocalTime

import scala.concurrent.duration._

val t = LocalTime.now()


// 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)
// here we define duration of first half a day: diff between start of a day and midEnd (end of first half)
val firstHalf = start.until(midEnd, ChronoUnit.MILLIS).millis
// here we define duration of second half a day: diff between start of second half a day and end of a day
val secondHalf = midStart.until(end, ChronoUnit.MILLIS).millis

def toStart(t: LocalTime) = {
  // when checked time is before start of a day
  if (t.isBefore(start)) 0.hours
  // otherwise when checked time is before end of first half (will be diff between start time and checked time)
  else if (t.isBefore(midEnd)) start.until(t, ChronoUnit.MILLIS).millis
  // otherwise when checked time is before start of second half (will be duration of first half)
  else if (t.isBefore(midStart)) firstHalf
  // otherwise when checked time is before end of a day (will be duration of first half + duration of diff between checked time and start of second half)
  else if (t.isBefore(end)) firstHalf + midStart.until(t, ChronoUnit.MILLIS).millis
  // otherwise sum of durations 
  else firstHalf + secondHalf
}

// 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}"
  }
}

toStart(t).withMinutes
toStart(LocalTime.of(9, 30)).withMinutes
toStart(LocalTime.of(12, 30)).withMinutes
toStart(LocalTime.of(13, 30)).withMinutes
toStart(LocalTime.of(14, 30)).withMinutes

Verbringen Sie etwas Zeit und überprüfen Sie java.timeapi (insbesondere LocalTime.until). Überprüfen Sie FiniteDurationapi, um das von .millismir verwendete Suffix zu verstehen


Beantwortet von –
Evgeny


Antwort geprüft von –
Candace Johnson (FixError Volunteer)

0 Shares:
Leave a Reply

Your email address will not be published. Required fields are marked *

You May Also Like