Skip to content

Commit

Permalink
Merge pull request #1461 from sergeykolbasov/instant-clock
Browse files Browse the repository at this point in the history
Use platform-dependent implementation of Clock.realTime
  • Loading branch information
djspiewak authored Dec 12, 2020
2 parents d2c1eed + 71df136 commit 940f855
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 12 deletions.
26 changes: 26 additions & 0 deletions core/js/src/main/scala/cats/effect/internals/DefaultClock.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright (c) 2017-2019 The Typelevel Cats-effect Project Developers
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package cats.effect.internals

import cats.effect.{Clock, Sync}

import scala.concurrent.duration.{MILLISECONDS, TimeUnit}

abstract private[effect] class DefaultClock[F[_]](implicit F: Sync[F]) extends Clock[F] {
def realTime(unit: TimeUnit): F[Long] =
F.delay(unit.convert(System.currentTimeMillis(), MILLISECONDS))
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import java.time.Instant
import cats.Functor
import cats.effect.Clock

import scala.concurrent.duration.MILLISECONDS
import scala.concurrent.duration.NANOSECONDS

private[effect] trait ClockPlatform {
implicit class JvmClockOps[F[_]](val self: Clock[F]) {
Expand All @@ -31,6 +31,8 @@ private[effect] trait ClockPlatform {
* for any `F` that has `Functor` defined.
*/
def instantNow(implicit F: Functor[F]): F[Instant] =
F.map(self.realTime(MILLISECONDS))(Instant.ofEpochMilli)
F.map(self.realTime(NANOSECONDS)) { ns =>
Instant.EPOCH.plusNanos(ns)
}
}
}
30 changes: 30 additions & 0 deletions core/jvm/src/main/scala/cats/effect/internals/DefaultClock.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright (c) 2017-2019 The Typelevel Cats-effect Project Developers
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package cats.effect.internals

import java.time.Instant

import cats.effect.{Clock, Sync}

import scala.concurrent.duration.{NANOSECONDS, SECONDS, TimeUnit}

abstract private[effect] class DefaultClock[F[_]](implicit F: Sync[F]) extends Clock[F] {
def realTime(unit: TimeUnit): F[Long] = F.delay {
val now = Instant.now()
unit.convert(now.getEpochSecond, SECONDS) + unit.convert(now.getNano.toLong, NANOSECONDS)
}
}
16 changes: 6 additions & 10 deletions core/shared/src/main/scala/cats/effect/Clock.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@
package cats.effect

import cats.effect.internals.ClockPlatform

import cats.effect.internals.DefaultClock
import cats.{~>, Applicative, Functor, Monoid}
import cats.data._

import scala.annotation.implicitNotFound
import scala.concurrent.duration.{MILLISECONDS, NANOSECONDS, TimeUnit}
import scala.concurrent.duration.{NANOSECONDS, TimeUnit}

/**
* Clock provides the current time, as a pure alternative to:
Expand Down Expand Up @@ -132,14 +132,10 @@ object Clock extends LowPriorityImplicits with ClockPlatform {
/**
* Provides Clock instance for any `F` that has `Sync` defined
*/
def create[F[_]](implicit F: Sync[F]): Clock[F] =
new Clock[F] {
override def realTime(unit: TimeUnit): F[Long] =
F.delay(unit.convert(System.currentTimeMillis(), MILLISECONDS))

override def monotonic(unit: TimeUnit): F[Long] =
F.delay(unit.convert(System.nanoTime(), NANOSECONDS))
}
def create[F[_]](implicit F: Sync[F]): Clock[F] = new DefaultClock[F] {
def monotonic(unit: TimeUnit): F[Long] =
F.delay(unit.convert(System.nanoTime(), NANOSECONDS))
}

implicit class ClockOps[F[_]](val self: Clock[F]) extends AnyVal {

Expand Down

0 comments on commit 940f855

Please sign in to comment.