Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package zio

import java.time.temporal.ChronoUnit
import java.util.concurrent.TimeUnit

private[zio] trait ClockSyntaxPlatformSpecific {
final protected def toChronoUnit(unit: TimeUnit): ChronoUnit =
unit match {
case TimeUnit.NANOSECONDS => ChronoUnit.NANOS
case TimeUnit.MICROSECONDS => ChronoUnit.MICROS
case TimeUnit.MILLISECONDS => ChronoUnit.MILLIS
case TimeUnit.SECONDS => ChronoUnit.SECONDS
case TimeUnit.MINUTES => ChronoUnit.MINUTES
case TimeUnit.HOURS => ChronoUnit.HOURS
case TimeUnit.DAYS => ChronoUnit.DAYS
}
}
9 changes: 9 additions & 0 deletions core/jvm/src/main/scala/zio/ClockSyntaxPlatformSpecific.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package zio

import java.time.temporal.ChronoUnit
import java.util.concurrent.TimeUnit

private[zio] trait ClockSyntaxPlatformSpecific {
@inline final protected def toChronoUnit(unit: TimeUnit): ChronoUnit =
unit.toChronoUnit
}
30 changes: 5 additions & 25 deletions core/shared/src/main/scala/zio/Clock.scala
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ trait Clock extends Serializable { self =>
}
}

object Clock extends ClockPlatformSpecific with Serializable {
object Clock extends ClockPlatformSpecific with ClockSyntaxPlatformSpecific with Serializable {

val tag: Tag[Clock] = Tag[Clock]

Expand Down Expand Up @@ -107,17 +107,8 @@ object Clock extends ClockPlatformSpecific with Serializable {

override val unsafe: UnsafeAPI =
new UnsafeAPI {
override def currentTime(unit: TimeUnit)(implicit unsafe: Unsafe): Long = {
val inst = instant()
unit match {
case TimeUnit.NANOSECONDS =>
inst.getEpochSecond * 1000000000 + inst.getNano
case TimeUnit.MICROSECONDS =>
inst.getEpochSecond * 1000000 + inst.getNano / 1000
case TimeUnit.MILLISECONDS => inst.toEpochMilli
case _ => unit.convert(inst.toEpochMilli, TimeUnit.MILLISECONDS)
}
}
override def currentTime(unit: TimeUnit)(implicit unsafe: Unsafe): Long =
currentTime(toChronoUnit(unit))

override def currentTime(unit: ChronoUnit)(implicit unsafe: Unsafe): Long =
unit.between(Instant.EPOCH, instant())
Expand Down Expand Up @@ -186,19 +177,8 @@ object Clock extends ClockPlatformSpecific with Serializable {

override val unsafe: UnsafeAPI =
new UnsafeAPI {
override def currentTime(unit: TimeUnit)(implicit unsafe: Unsafe): Long = {
val inst = instant()
// A nicer solution without loss of precision or range would be
// unit.toChronoUnit.between(Instant.EPOCH, inst)
// However, ChronoUnit is not available on all platforms
unit match {
case TimeUnit.NANOSECONDS =>
inst.getEpochSecond() * 1000000000 + inst.getNano()
case TimeUnit.MICROSECONDS =>
inst.getEpochSecond() * 1000000 + inst.getNano() / 1000
case _ => unit.convert(inst.toEpochMilli(), TimeUnit.MILLISECONDS)
}
}
override def currentTime(unit: TimeUnit)(implicit unsafe: Unsafe): Long =
currentTime(toChronoUnit(unit))

override def currentTime(unit: ChronoUnit)(implicit unsafe: Unsafe): Long =
unit.between(Instant.EPOCH, instant())
Expand Down