Skip to content

Add realTimeZonedDateTime to Clock JVM extensions #4316

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

Merged
merged 3 commits into from
Mar 27, 2025
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 @@ -19,7 +19,7 @@ package cats.effect
import cats.effect.std.Console
import cats.effect.tracing.Tracing

import java.time.Instant
import java.time.{Instant, ZonedDateTime}
import java.util.concurrent.{CompletableFuture, CompletionStage}

private[effect] abstract class IOCompanionPlatform { this: IO.type =>
Expand Down Expand Up @@ -124,6 +124,8 @@ private[effect] abstract class IOCompanionPlatform { this: IO.type =>

def realTimeInstant: IO[Instant] = asyncForIO.realTimeInstant

def realTimeZonedDateTime: IO[ZonedDateTime] = asyncForIO.realTimeZonedDateTime

/**
* Reads a line as a string from the standard input using the platform's default charset, as
* per `java.nio.charset.Charset.defaultCharset()`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,13 @@

package cats.effect.kernel

import java.time.Instant
import java.time.{Instant, ZoneOffset, ZonedDateTime}

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

def realTimeZonedDateTime: F[ZonedDateTime] =
self.applicative.map(realTimeInstant)(d => ZonedDateTime.ofInstant(d, ZoneOffset.UTC))
}
Loading