diff --git a/core/js/src/main/scala/cats/effect/SyncIOCompanionPlatform.scala b/core/js/src/main/scala/cats/effect/SyncIOCompanionPlatform.scala index 164ce56862..f46e854a51 100644 --- a/core/js/src/main/scala/cats/effect/SyncIOCompanionPlatform.scala +++ b/core/js/src/main/scala/cats/effect/SyncIOCompanionPlatform.scala @@ -19,5 +19,5 @@ package cats.effect import scala.scalajs.js private[effect] trait SyncIOCompanionPlatform { this: SyncIO.type => - final def realTimeDate: SyncIO[js.Date] = realTime.map(d => new js.Date(d.toMillis.toDouble)) + final def realTimeDate: SyncIO[js.Date] = syncForSyncIO.realTimeDate } diff --git a/core/jvm-native/src/main/scala/cats/effect/SyncIOCompanionPlatform.scala b/core/jvm-native/src/main/scala/cats/effect/SyncIOCompanionPlatform.scala index 82411ae1b5..5f7c4dd878 100644 --- a/core/jvm-native/src/main/scala/cats/effect/SyncIOCompanionPlatform.scala +++ b/core/jvm-native/src/main/scala/cats/effect/SyncIOCompanionPlatform.scala @@ -16,9 +16,11 @@ package cats.effect -import java.time.Instant +import java.time.{Instant, ZonedDateTime} private[effect] trait SyncIOCompanionPlatform { this: SyncIO.type => - final def realTimeInstant: SyncIO[Instant] = - realTime.map(d => Instant.ofEpochMilli(d.toMillis)) + + final def realTimeInstant: SyncIO[Instant] = syncForSyncIO.realTimeInstant + + final def realTimeZonedDateTime: SyncIO[ZonedDateTime] = syncForSyncIO.realTimeZonedDateTime } diff --git a/tests/jvm-native/src/test/scala/cats/effect/SyncIOPlatformSuite.scala b/tests/jvm-native/src/test/scala/cats/effect/SyncIOPlatformSuite.scala index e01da69943..6aefda5718 100644 --- a/tests/jvm-native/src/test/scala/cats/effect/SyncIOPlatformSuite.scala +++ b/tests/jvm-native/src/test/scala/cats/effect/SyncIOPlatformSuite.scala @@ -30,6 +30,22 @@ trait SyncIOPlatformSuite { self: BaseSuite => assertCompleteAsSync(op, true) } + testUnit( + "realTimeZonedDateTime should return a ZonedDateTime constructed from realTime with UTC offset") { + + // Unfortunately since SyncIO doesn't use on a controllable + // clock source, so a diff best we can do + val op = for { + realTime <- SyncIO.realTime + now <- SyncIO.realTimeZonedDateTime + } yield ( + (now.toInstant.toEpochMilli - realTime.toMillis) <= 10000, + now.getOffset.getTotalSeconds + ) + + assertCompleteAsSync(op, (true, 0)) + } + } }