scala DateTime:小时和整数之间的差异

kgsdhlau  于 2022-11-09  发布在  Scala
关注(0)|答案(1)|浏览(140)

我有一些错误,在我的代码中有两行,上面有一个注解:

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

在以下两种情况下,函数ToStart的测试为假:

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

如何更改代码以找到最佳结果?

sigwle7e

sigwle7e1#

代码应该类似于我对您的回答here,但是您需要理解我做了什么。您肯定需要检查我使用的API调用,但我添加了一些额外的注解:

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

花一些时间查看java.time API(特别是LocalTime.until)。检查FiniteDuration API以了解我使用的.millis后缀

相关问题