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
Expand Up @@ -38,6 +38,7 @@ private[unsafe] abstract class SchedulerCompanionPlatform { this: Scheduler.type

def nowMillis() = System.currentTimeMillis()
def monotonicNanos() = System.nanoTime()
override def nowMicros(): Long = nowMillis() * 1000
},
() => ())

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ package cats.effect.unsafe

import scala.concurrent.duration.FiniteDuration

import java.time.Instant
import java.time.temporal.ChronoField
import java.util.concurrent.{Executors, ScheduledExecutorService}

private[unsafe] abstract class SchedulerCompanionPlatform { this: Scheduler.type =>
Expand Down Expand Up @@ -45,6 +47,11 @@ private[unsafe] abstract class SchedulerCompanionPlatform { this: Scheduler.type

def nowMillis() = System.currentTimeMillis()

override def nowMicros(): Long = {
val now = Instant.now()
now.getEpochSecond * 1000000 + now.getLong(ChronoField.MICRO_OF_SECOND)
Comment thread
djspiewak marked this conversation as resolved.
}

def monotonicNanos() = System.nanoTime()
}
}
8 changes: 4 additions & 4 deletions core/shared/src/main/scala/cats/effect/IOFiber.scala
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ private final class IOFiber[A](
/* RealTime */
case 3 =>
runLoop(
succeeded(runtime.scheduler.nowMillis().millis, 0),
succeeded(runtime.scheduler.nowMicros().micros, 0),
Comment thread
djspiewak marked this conversation as resolved.
nextCancelation,
nextAutoCede)

Expand Down Expand Up @@ -346,7 +346,7 @@ private final class IOFiber[A](
runLoop(nextIO, nextCancelation - 1, nextAutoCede)

case 3 =>
val realTime = runtime.scheduler.nowMillis().millis
val realTime = runtime.scheduler.nowMicros().micros
runLoop(next(realTime), nextCancelation - 1, nextAutoCede)

case 4 =>
Expand Down Expand Up @@ -411,7 +411,7 @@ private final class IOFiber[A](
runLoop(result, nextCancelation - 1, nextAutoCede)

case 3 =>
val realTime = runtime.scheduler.nowMillis().millis
val realTime = runtime.scheduler.nowMicros().micros
runLoop(next(realTime), nextCancelation - 1, nextAutoCede)

case 4 =>
Expand Down Expand Up @@ -472,7 +472,7 @@ private final class IOFiber[A](
runLoop(next, nextCancelation - 1, nextAutoCede)

case 3 =>
val realTime = runtime.scheduler.nowMillis().millis
val realTime = runtime.scheduler.nowMicros().micros
runLoop(succeeded(Right(realTime), 0), nextCancelation - 1, nextAutoCede)

case 4 =>
Expand Down
3 changes: 3 additions & 0 deletions core/shared/src/main/scala/cats/effect/unsafe/Scheduler.scala
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ trait Scheduler {

def nowMillis(): Long

def nowMicros(): Long =
nowMillis() * 1000

def monotonicNanos(): Long
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ import java.time.Instant

private[effect] trait ClockPlatform[F[_]] extends Serializable { self: Clock[F] =>
def realTimeInstant: F[Instant] = {
self.applicative.map(self.realTime)(d => Instant.ofEpochMilli(d.toMillis))
self.applicative.map(self.realTime)(d => Instant.EPOCH.plusNanos(d.toNanos))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Nanos since the epoch, seems like a tall ask, but I'm not really familiar with time APIs, so I might be wrong.

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,9 @@ object TestControl {
def nowMillis() =
ctx.now().toMillis

override def nowMicros(): Long =
ctx.now().toMicros

def monotonicNanos() =
ctx.now().toNanos
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@ trait TestInstances extends ParallelFGenerators with OutcomeGenerators with Sync
}

def nowMillis() = ctx.now().toMillis
override def nowMicros(): Long = ctx.now().toMicros
def monotonicNanos() = ctx.now().toNanos
}
}
Expand Down