-1

I have some mistakes, in my code in the 2 lines where a comment above them:

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

The test of the function ToStart, is false in these tow cases:

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

How can I change my code to find the best result ?

General Grievance
  • 4,555
  • 31
  • 31
  • 45
vero
  • 1,005
  • 6
  • 16
  • 29
  • `The test of the function ToStart, is false in these tow cases:` You posted 4 cases. What are the wrong cases? Please add expected output – Piro Mar 29 '18 at 12:08
  • in the 2 lines where a comment above them – vero Mar 29 '18 at 12:16

1 Answers1

1

Code should be similar to my answer to you here, but you need to understand what I did. You definitely need to check api calls I used, but I added some additional comments:

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

Spend some time and check java.time api (specifically LocalTime.until). Check FiniteDuration api to understand .millis suffix I used

Evgeny
  • 1,760
  • 10
  • 11
  • Really thank you very much, and especially for your advances. I'im starting from now checking the java.time API, it's very useful especially for my project. Thanks a lot @Evgeny – vero Mar 29 '18 at 13:04
  • I asked a question https://stackoverflow.com/questions/49574503/datetime-calcul-hours-in-days?noredirect=1#comment86157761_49574503 I'm using your function and tow another functions. I'm looking how resolve the function equals to call the other three functions (toEnd, ToStart and jourouvree) because I should apply it on my dataframe after and especially on 2 parameters in my dataframe (start_date, finish_date) Can you help me please. – vero Mar 30 '18 at 13:41