Skip to content

Commit

Permalink
Merge pull request #410 from fthomas/topic/ZonedDateTimeScheduler
Browse files Browse the repository at this point in the history
Add `ZonedDateTimeScheduler`
  • Loading branch information
fthomas authored Mar 7, 2023
2 parents 914c688 + 1925bff commit 7404ea3
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,10 @@
package eu.timepit.fs2cron.calev

import cats.effect.{Sync, Temporal}
import cats.syntax.all._
import eu.timepit.fs2cron.Scheduler
import com.github.eikek.calev.CalEvent
import eu.timepit.fs2cron.{Scheduler, ZonedDateTimeScheduler}

import java.time.temporal.ChronoUnit
import java.time.{ZoneId, ZoneOffset, ZonedDateTime}
import java.util.concurrent.TimeUnit
import scala.concurrent.duration.FiniteDuration

object CalevScheduler {
def systemDefault[F[_]](implicit temporal: Temporal[F], F: Sync[F]): Scheduler[F, CalEvent] =
Expand All @@ -34,23 +30,14 @@ object CalevScheduler {
from(F.pure(ZoneOffset.UTC))

def from[F[_]](zoneId: F[ZoneId])(implicit F: Temporal[F]): Scheduler[F, CalEvent] =
new Scheduler[F, CalEvent] {
override def fromNowUntilNext(schedule: CalEvent): F[FiniteDuration] =
now.flatMap { from =>
schedule.nextElapse(from) match {
case Some(next) =>
val durationInMillis = from.until(next, ChronoUnit.MILLIS)
F.pure(FiniteDuration(durationInMillis, TimeUnit.MILLISECONDS))
case None =>
val msg = s"Could not calculate the next date-time from $from " +
s"given the calendar event expression '${schedule.asString}'. This should never happen."
F.raiseError(new Throwable(msg))
}
new ZonedDateTimeScheduler[F, CalEvent](zoneId) {
override def next(from: ZonedDateTime, schedule: CalEvent): F[ZonedDateTime] =
schedule.nextElapse(from) match {
case Some(next) => F.pure(next)
case None =>
val msg = s"Could not calculate the next date-time from $from " +
s"given the calendar event expression '${schedule.asString}'. This should never happen."
F.raiseError(new Throwable(msg))
}

override def temporal: Temporal[F] = F

private val now: F[ZonedDateTime] =
(F.realTimeInstant, zoneId).mapN(_.atZone(_))
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright 2018-2021 fs2-cron contributors
*
* 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 eu.timepit.fs2cron

import cats.effect.Temporal
import cats.syntax.all._

import java.time.temporal.ChronoUnit
import java.time.{ZoneId, ZonedDateTime}
import java.util.concurrent.TimeUnit
import scala.concurrent.duration.FiniteDuration

abstract class ZonedDateTimeScheduler[F[_], Schedule](zoneId: F[ZoneId])(implicit
override val temporal: Temporal[F]
) extends Scheduler[F, Schedule] {
def next(from: ZonedDateTime, schedule: Schedule): F[ZonedDateTime]

override def fromNowUntilNext(schedule: Schedule): F[FiniteDuration] =
now.flatMap { from =>
next(from, schedule).map { to =>
val durationInMillis = from.until(to, ChronoUnit.MILLIS)
FiniteDuration(durationInMillis, TimeUnit.MILLISECONDS)
}
}

private val now: F[ZonedDateTime] =
(temporal.realTimeInstant, zoneId).mapN(_.atZone(_))
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,12 @@
package eu.timepit.fs2cron.cron4s

import cats.effect.{Sync, Temporal}
import cats.syntax.all._
import cron4s.expr.CronExpr
import cron4s.lib.javatime._
import cron4s.syntax.cron._
import eu.timepit.fs2cron.Scheduler
import eu.timepit.fs2cron.{Scheduler, ZonedDateTimeScheduler}

import java.time.temporal.ChronoUnit
import java.time.{ZoneId, ZoneOffset, ZonedDateTime}
import java.util.concurrent.TimeUnit
import scala.concurrent.duration.FiniteDuration

object Cron4sScheduler {
def systemDefault[F[_]](implicit temporal: Temporal[F], F: Sync[F]): Scheduler[F, CronExpr] =
Expand All @@ -36,23 +32,14 @@ object Cron4sScheduler {
from(F.pure(ZoneOffset.UTC))

def from[F[_]](zoneId: F[ZoneId])(implicit F: Temporal[F]): Scheduler[F, CronExpr] =
new Scheduler[F, CronExpr] {
override def fromNowUntilNext(schedule: CronExpr): F[FiniteDuration] =
now.flatMap { from =>
schedule.next(from) match {
case Some(next) =>
val durationInMillis = from.until(next, ChronoUnit.MILLIS)
F.pure(FiniteDuration(durationInMillis, TimeUnit.MILLISECONDS))
case None =>
val msg = s"Could not calculate the next date-time from $from " +
s"given the cron expression '$schedule'. This should never happen."
F.raiseError(new Throwable(msg))
}
new ZonedDateTimeScheduler[F, CronExpr](zoneId) {
override def next(from: ZonedDateTime, schedule: CronExpr): F[ZonedDateTime] =
schedule.next(from) match {
case Some(next) => F.pure(next)
case None =>
val msg = s"Could not calculate the next date-time from $from " +
s"given the cron expression '$schedule'. This should never happen."
F.raiseError(new Throwable(msg))
}

override def temporal: Temporal[F] = F

private val now: F[ZonedDateTime] =
(F.realTimeInstant, zoneId).mapN(_.atZone(_))
}
}

0 comments on commit 7404ea3

Please sign in to comment.