SuperXtra / githubApi

0 stars 0 forks source link

Side note about Clock #9

Open patrykcelinski opened 4 years ago

patrykcelinski commented 4 years ago

https://github.com/SuperXtra/githubApi/blob/9f55aed6b5491cfd8849cc0dfd6a36333d144756/app/Module.scala#L18

Although java Clock is perfectly fine. I can also recommend you to use cats.Clock[F] typeclass. https://typelevel.org/cats-effect/datatypes/clock.html

In this project you don't need it - they want futures. But if you will go into F[ _ ] this clock might be useful.

class SomeService[F[_]: Sync: Clock](

) {

  private def today: F[LocalDate] = {
    Clock[F].realTime(MILLISECONDS).map(new LocalDate(_))
  }

  private def secondsToMidnight: F[Int] = {
    import com.github.nscala_time.time.Implicits._
    for {
      timeNow  <- Clock[F].realTime(MILLISECONDS)
      now      = new DateTime(timeNow)
      midnight = now + (1 day) withTimeAtStartOfDay ()
    } yield Seconds.secondsBetween(now, midnight).getSeconds
  }
patrykcelinski commented 4 years ago

Apart from clock joda time is helpful.

Its just side info. Don't waste time on this issue.