{- This file is part of irc-fun-client.
 -
 - Written in 2015 by fr33domlover <fr33domlover@rel4tion.org>.
 -
 - ♡ Copying is an act of love. Please copy, reuse and share.
 -
 - The author(s) have dedicated all copyright and related and neighboring
 - rights to this software to the public domain worldwide. This software is
 - distributed without any warranty.
 -
 - You should have received a copy of the CC0 Public Domain Dedication along
 - with this software. If not, see
 - <http://creativecommons.org/publicdomain/zero/1.0/>.
 -}

-- | Get the current UTC time and formatted time strings, using auto-update to
-- run the actual action periodically and by demand. In other words, these
-- functions can safely be used at high frequency.
--
-- Intended use: Create a single getter in the main thread and pass as needed
-- to other threads for their own use.
module Network.IRC.Fun.Client.Time
    ( currentTimeGetter
    )
where

import Control.AutoUpdate
import Data.Time.Clock (UTCTime, getCurrentTime)
import Data.Time.Format (defaultTimeLocale, formatTime)
import Data.Time.Units (Second, toMicroseconds)

format = formatTime defaultTimeLocale "%F %T"

action = do
    t <- getCurrentTime
    return (t, format t)

settings = defaultUpdateSettings
    { updateFreq   = fromInteger $ toMicroseconds (1 :: Second)
    , updateAction = action
    }

-- | Make a getter which returns the current time, and a formatted time string
-- for use in IRC logs.
currentTimeGetter :: IO (IO (UTCTime, String))
currentTimeGetter = mkAutoUpdate settings