module System.Cron.Internal.Schedule (findNextMinuteDelay') where

import           Data.Time

findNextMinuteDelay' :: UTCTime -> (UTCTime, Int)
findNextMinuteDelay' :: UTCTime -> (UTCTime, Int)
findNextMinuteDelay' UTCTime
now = (UTCTime
next, Int
delay)
  where
    oneMinuteLater :: UTCTime
oneMinuteLater = NominalDiffTime -> UTCTime -> UTCTime
addUTCTime NominalDiffTime
oneMinute UTCTime
now
    plainMinute :: DiffTime
plainMinute = DiffTime -> DiffTime
truncateToPlainMinute (DiffTime -> DiffTime) -> DiffTime -> DiffTime
forall a b. (a -> b) -> a -> b
$ UTCTime -> DiffTime
utctDayTime UTCTime
oneMinuteLater
    next :: UTCTime
next = UTCTime
oneMinuteLater { utctDayTime = plainMinute }
    diff :: NominalDiffTime
diff = UTCTime -> UTCTime -> NominalDiffTime
diffUTCTime UTCTime
next UTCTime
now
    delay :: Int
delay = Double -> Int
forall b. Integral b => Double -> b
forall a b. (RealFrac a, Integral b) => a -> b
round (NominalDiffTime -> Double
forall a b. (Real a, Fractional b) => a -> b
realToFrac (NominalDiffTime
diff NominalDiffTime -> NominalDiffTime -> NominalDiffTime
forall a. Num a => a -> a -> a
* NominalDiffTime
1000000) :: Double) :: Int

oneMinute :: NominalDiffTime
oneMinute :: NominalDiffTime
oneMinute = NominalDiffTime
60

truncateToPlainMinute :: DiffTime -> DiffTime
truncateToPlainMinute :: DiffTime -> DiffTime
truncateToPlainMinute = Integer -> DiffTime
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Integer -> DiffTime)
-> (DiffTime -> Integer) -> DiffTime -> DiffTime
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
* Integer
60) (Integer -> Integer)
-> (DiffTime -> Integer) -> DiffTime -> Integer
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Integer -> Integer -> Integer
forall a. Integral a => a -> a -> a
`quot` Integer
60) (Integer -> Integer)
-> (DiffTime -> Integer) -> DiffTime -> Integer
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (DiffTime -> Integer
forall b. Integral b => DiffTime -> b
forall a b. (RealFrac a, Integral b) => a -> b
truncate :: DiffTime -> Integer)