Skip to content

Commit

Permalink
core: support sub-second durations (Netflix#1632)
Browse files Browse the repository at this point in the history
Update `Strings.parseDuration` to support sub-second
durations.
  • Loading branch information
brharrington authored and manolama committed May 22, 2024
1 parent b6a9207 commit 9af0ed4
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ object Strings {
/**
* Period following conventions of unix `at` command.
*/
private val AtPeriod = """^(\d+)([a-z]+)$""".r
private val AtPeriod = """^(\d+)([a-]+)$""".r

/**
* Period following the ISO8601 conventions.
Expand Down Expand Up @@ -420,10 +420,13 @@ object Strings {
* Convert an `at` command time range into a joda period object.
*/
private def parseAtDuration(amount: String, unit: String): Duration = {
val v = amount.toInt
val v = amount.toLong

// format: off
unit match {
case "ns" => Duration.ofNanos(v)
case "us" | "μs" => Duration.ofNanos(v * 1000L)
case "ms" => Duration.ofMillis(v)
case "seconds" | "second" | "s" => Duration.ofSeconds(v)
case "minutes" | "minute" | "min" | "m" => Duration.ofMinutes(v)
case "hours" | "hour" | "h" => Duration.ofHours(v)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,19 @@ class StringsSuite extends FunSuite {
assertEquals(parseQueryString(query), expected)
}

test("parseDuration, at nanoseconds") {
assertEquals(parseDuration("42ns"), Duration.ofNanos(42L))
}

test("parseDuration, at microseconds") {
assertEquals(parseDuration("42us"), Duration.ofNanos(42_000L))
assertEquals(parseDuration("42μs"), Duration.ofNanos(42_000L))
}

test("parseDuration, at milliseconds") {
assertEquals(parseDuration("42ms"), Duration.ofMillis(42))
}

test("parseDuration, at seconds") {
assertEquals(parseDuration("42seconds"), Duration.ofSeconds(42))
assertEquals(parseDuration("42second"), Duration.ofSeconds(42))
Expand Down

0 comments on commit 9af0ed4

Please sign in to comment.