Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add methods to get finite duration from duration #8832

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions core-tests/shared/src/test/scala/zio/DurationSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import zio.test.assert
import java.time.temporal.ChronoUnit
import java.time.{Duration => JavaDuration}
import java.util.concurrent.TimeUnit
import scala.concurrent.duration.{Duration => ScalaDuration}
import scala.concurrent.duration.{Duration => ScalaDuration, FiniteDuration => ScalaFiniteDuration}

object DurationSpec extends ZIOBaseSpec {

Expand Down Expand Up @@ -154,7 +154,7 @@ object DurationSpec extends ZIOBaseSpec {
assert(Duration.Infinity.isZero)(equalTo(false))
},
test("It converts into the infinite s.c.d.Duration") {
assert(Duration.Infinity.asScala)(equalTo(ScalaDuration.Inf: ScalaDuration))
assert(Duration.Infinity.asScala)(equalTo(ScalaFiniteDuration(Long.MaxValue, TimeUnit.NANOSECONDS)))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will note this is likely not preferable if users are using APIs which special case for Duration.Inf...

},
test("It converts into a Long.MaxValue second-long JDK Duration") {
assert(Duration.Infinity)(equalTo(JavaDuration.ofNanos(Long.MaxValue)))
Expand Down
7 changes: 3 additions & 4 deletions core/shared/src/main/scala/zio/Duration.scala
Original file line number Diff line number Diff line change
Expand Up @@ -173,10 +173,9 @@ final class DurationOps(private val duration: Duration) extends AnyVal {
}
}

def asScala: ScalaDuration = duration match {
case Duration.Infinity => ScalaDuration.Inf
case Duration.Zero => ScalaDuration.Zero
case _ => ScalaFiniteDuration(duration.toNanos, TimeUnit.NANOSECONDS)
def asScala: ScalaFiniteDuration = duration match {
case Duration.Zero => ScalaDuration.Zero
case _ => ScalaFiniteDuration(duration.toNanos, TimeUnit.NANOSECONDS)
}

def asJava: JavaDuration = duration
Expand Down
Loading