Skip to content

Commit

Permalink
TimeUnitsFormat
Browse files Browse the repository at this point in the history
  • Loading branch information
rpiaggio committed Jul 17, 2024
1 parent 2b3a756 commit 2f74eff
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 18 deletions.
20 changes: 14 additions & 6 deletions modules/tests/src/test/scala/lucuma/ui/syntax/SyntaxSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package lucuma.ui.syntax

import lucuma.core.util.TimeSpan
import lucuma.ui.TimeUnitsFormat
import lucuma.ui.syntax.time.*

import java.time.Duration
Expand All @@ -12,16 +13,23 @@ class SyntaxSuite extends munit.FunSuite:

test("toHoursMinutes should format a TimeSpan"):
assertEquals(TimeSpan.Zero.toHoursMinutes, "0mins")
assertEquals(TimeSpan.Zero.toHoursMinutes(true), "0m")
assertEquals(TimeSpan.unsafeFromDuration(Duration.ofHours(1)).toHoursMinutes(false), "1hrs")
assertEquals(TimeSpan.unsafeFromDuration(Duration.ofHours(1)).toHoursMinutes(true), "1h")
assertEquals(TimeSpan.Zero.toHoursMinutes(TimeUnitsFormat.Letter), "0m")
assertEquals(TimeSpan.unsafeFromDuration(Duration.ofHours(1)).toHoursMinutes, "1hrs")
assertEquals(
TimeSpan.unsafeFromDuration(Duration.ofHours(1)).toHoursMinutes(TimeUnitsFormat.Letter),
"1h"
)
assertEquals(TimeSpan.unsafeFromDuration(Duration.ofMinutes(30)).toHoursMinutes, "30mins")
assertEquals(
TimeSpan.unsafeFromDuration(Duration.ofHours(1).plusMinutes(30)).toHoursMinutes,
TimeSpan
.unsafeFromDuration(Duration.ofHours(1).plusMinutes(30))
.toHoursMinutes(TimeUnitsFormat.Abbreviation),
"1hrs 30mins"
)
assertEquals(
TimeSpan.unsafeFromDuration(Duration.ofHours(1).plusMinutes(30)).toHoursMinutes(true),
TimeSpan
.unsafeFromDuration(Duration.ofHours(1).plusMinutes(30))
.toHoursMinutes(TimeUnitsFormat.Letter),
"1h 30m"
)

Expand All @@ -43,6 +51,6 @@ class SyntaxSuite extends munit.FunSuite:
assertEquals(
TimeSpan
.unsafeFromDuration(Duration.ofHours(1).plusMinutes(30).plusSeconds(30))
.toHoursMinutes(true),
.toHoursMinutes(TimeUnitsFormat.Letter),
"1h 31m"
)
8 changes: 8 additions & 0 deletions modules/ui/src/main/scala/lucuma/ui/TimeUnitsFormat.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Copyright (c) 2016-2023 Association of Universities for Research in Astronomy, Inc. (AURA)
// For license information see LICENSE or https://opensource.org/licenses/BSD-3-Clause

package lucuma.ui

enum TimeUnitsFormat(val hours: String, val minutes: String):
case Letter extends TimeUnitsFormat("h", "m")
case Abbreviation extends TimeUnitsFormat("hrs", "mins")
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import japgolly.scalajs.react.*
import japgolly.scalajs.react.vdom.html_<^.*
import lucuma.core.util.TimeSpan
import lucuma.react.common.ReactFnProps
import lucuma.ui.TimeUnitsFormat
import lucuma.ui.syntax.time.*

/**
Expand All @@ -15,9 +16,9 @@ import lucuma.ui.syntax.time.*
* Hovering over the view will show the full (unrounded) TimeSpan in hours, minutes, and seconds.
*/
case class TimeSpanView(
timespan: TimeSpan,
shortUnits: Boolean = false,
modifiers: Seq[TagMod] = Seq.empty
timespan: TimeSpan,
unitsFormat: TimeUnitsFormat = TimeUnitsFormat.Abbreviation,
modifiers: Seq[TagMod] = Seq.empty
) extends ReactFnProps(TimeSpanView.component):
def addModifiers(modifiers: Seq[TagMod]) = copy(modifiers = this.modifiers ++ modifiers)
def withMods(mods: TagMod*) = addModifiers(mods)
Expand All @@ -32,5 +33,5 @@ object TimeSpanView:
props.modifiers.toTagMod,
^.title := s"${ts.toHoursPart} hours, ${ts.toMinutesPart} minutes, ${ts.toSecondsPart} seconds"
)(
ts.toHoursMinutes(props.shortUnits)
ts.toHoursMinutes(props.unitsFormat)
)
15 changes: 7 additions & 8 deletions modules/ui/src/main/scala/lucuma/ui/syntax/time.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,25 @@ package lucuma.ui.syntax

import cats.syntax.eq.*
import lucuma.core.util.TimeSpan
import lucuma.ui.TimeUnitsFormat

trait time:
extension (timespan: TimeSpan)
/**
* Format a timespan in the format `${hh}hrs ${mm}mins`
*/
def toHoursMinutes: String =
toHoursMinutes(shortUnits = false)
toHoursMinutes(TimeUnitsFormat.Abbreviation)

/**
* Format a timespan in the format `${hh}hrs ${mm}mins` or `${hh}h ${mm}m`
*/
def toHoursMinutes(shortUnits: Boolean = false): String =
val hours = timespan.toHoursPart
val hourUnits = if (shortUnits) "h" else "hrs"
def hourStr = s"$hours$hourUnits"
def toHoursMinutes(unitsFormat: TimeUnitsFormat): String =
val hours = timespan.toHoursPart
def hourStr = s"$hours${unitsFormat.hours}"
// Remaining minutes, rounded to the nearest minute
val minutes = timespan.toMinutes.setScale(0, BigDecimal.RoundingMode.HALF_UP) % 60
val minuteUnits = if (shortUnits) "m" else "mins"
def minuteStr = s"$minutes$minuteUnits"
val minutes = timespan.toMinutes.setScale(0, BigDecimal.RoundingMode.HALF_UP) % 60
def minuteStr = s"$minutes${unitsFormat.minutes}"

if hours === 0 then minuteStr
else if minutes === 0 then hourStr
Expand Down

0 comments on commit 2f74eff

Please sign in to comment.