module Data.Timeframe (
  Timeframe,
  module Data.Interval,
  localTimeframeAt,
  localTimeframe,
  pureLocalTimeframe,
  duration,
) where

import Control.Monad.IO.Class (MonadIO (..))
import Data.Function (on)
import Data.Functor ((<&>))
import Data.Interval
import Data.Time.Compat
import GHC.IO (unsafePerformIO)

-- | > type Timeframe = Interval UTCTime
type Timeframe = Interval UTCTime

localTimeframeAt :: TimeZone -> LocalTime -> LocalTime -> Timeframe
localTimeframeAt :: TimeZone -> LocalTime -> LocalTime -> Timeframe
localTimeframeAt = forall b c a. (b -> b -> c) -> (a -> b) -> a -> a -> c
on forall x. Ord x => x -> x -> Interval x
(:||:) forall b c a. (b -> c) -> (a -> b) -> a -> c
. TimeZone -> LocalTime -> UTCTime
localTimeToUTC

localTimeframe :: (MonadIO io) => LocalTime -> LocalTime -> io Timeframe
localTimeframe :: forall (io :: * -> *).
MonadIO io =>
LocalTime -> LocalTime -> io Timeframe
localTimeframe LocalTime
t1 LocalTime
t2 =
  forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO IO TimeZone
getCurrentTimeZone forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> \TimeZone
tz -> TimeZone -> LocalTime -> LocalTime -> Timeframe
localTimeframeAt TimeZone
tz LocalTime
t1 LocalTime
t2

pureLocalTimeframe :: LocalTime -> LocalTime -> Timeframe
pureLocalTimeframe :: LocalTime -> LocalTime -> Timeframe
pureLocalTimeframe LocalTime
t1 LocalTime
t2 =
  let tz :: TimeZone
tz = forall a. IO a -> a
unsafePerformIO IO TimeZone
getCurrentTimeZone
   in TimeZone -> LocalTime -> LocalTime -> Timeframe
localTimeframeAt TimeZone
tz LocalTime
t1 LocalTime
t2

duration :: Timeframe -> Maybe NominalDiffTime
duration :: Timeframe -> Maybe NominalDiffTime
duration = forall y x.
(Ord x, Num y) =>
(x -> x -> y) -> Interval x -> Maybe y
measuring UTCTime -> UTCTime -> NominalDiffTime
diffUTCTime