{-# LANGUAGE CPP #-}
-----------------------------------------------------------------------------
-- |
-- Module      :  Plugins.Date
-- Copyright   :  (c) Andrea Rossato
-- License     :  BSD-style (see LICENSE)
--
-- Maintainer  :  Jose A. Ortega Ruiz <jao@gnu.org>
-- Stability   :  unstable
-- Portability :  unportable
--
-- A date plugin for Xmobar
--
-- Usage example: in template put
--
-- > Run Date "%a %b %_d %Y <fc=#ee9a00> %H:%M:%S</fc>" "Mydate" 10
--
-----------------------------------------------------------------------------

module Xmobar.Plugins.Date (Date(..), date) where

import Xmobar.Run.Exec

#if ! MIN_VERSION_time(1,5,0)
import System.Locale
#endif
import Data.IORef
import Data.Time
import Control.Concurrent.Async (concurrently_)

data Date = Date String String Int
    deriving (ReadPrec [Date]
ReadPrec Date
Int -> ReadS Date
ReadS [Date]
(Int -> ReadS Date)
-> ReadS [Date] -> ReadPrec Date -> ReadPrec [Date] -> Read Date
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
$creadsPrec :: Int -> ReadS Date
readsPrec :: Int -> ReadS Date
$creadList :: ReadS [Date]
readList :: ReadS [Date]
$creadPrec :: ReadPrec Date
readPrec :: ReadPrec Date
$creadListPrec :: ReadPrec [Date]
readListPrec :: ReadPrec [Date]
Read, Int -> Date -> ShowS
[Date] -> ShowS
Date -> String
(Int -> Date -> ShowS)
-> (Date -> String) -> ([Date] -> ShowS) -> Show Date
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Date -> ShowS
showsPrec :: Int -> Date -> ShowS
$cshow :: Date -> String
show :: Date -> String
$cshowList :: [Date] -> ShowS
showList :: [Date] -> ShowS
Show)

instance Exec Date where
    alias :: Date -> String
alias (Date String
_ String
a Int
_) = String
a
    rate :: Date -> Int
rate  (Date String
_ String
_ Int
r) = Int
r
    start :: Date -> (String -> IO ()) -> IO ()
start (Date String
f String
_ Int
r) String -> IO ()
cb =
        -- refresh time zone once a minute to avoid wasting CPU cycles
        Int -> (IORef TimeZone -> IO ()) -> IO ()
withRefreshingZone Int
600 ((IORef TimeZone -> IO ()) -> IO ())
-> (IORef TimeZone -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \IORef TimeZone
zone ->
            Int -> IO () -> IO ()
doEveryTenthSeconds Int
r (IO () -> IO ()) -> IO () -> IO ()
forall a b. (a -> b) -> a -> b
$ IORef TimeZone -> String -> IO String
date IORef TimeZone
zone String
f IO String -> (String -> IO ()) -> IO ()
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= String -> IO ()
cb

date :: IORef TimeZone -> String -> IO String
date :: IORef TimeZone -> String -> IO String
date IORef TimeZone
zoneRef String
format = do
    zone <- IORef TimeZone -> IO TimeZone
forall a. IORef a -> IO a
readIORef IORef TimeZone
zoneRef
    fmap (formatTime defaultTimeLocale format . utcToZonedTime zone) getCurrentTime

withRefreshingZone :: Int -> (IORef TimeZone -> IO ()) -> IO ()
withRefreshingZone :: Int -> (IORef TimeZone -> IO ()) -> IO ()
withRefreshingZone Int
r IORef TimeZone -> IO ()
action = do
    zone <- TimeZone -> IO (IORef TimeZone)
forall a. a -> IO (IORef a)
newIORef (TimeZone -> IO (IORef TimeZone))
-> IO TimeZone -> IO (IORef TimeZone)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< IO TimeZone
getCurrentTimeZone
    let refresh = IORef TimeZone -> TimeZone -> IO ()
forall a. IORef a -> a -> IO ()
atomicWriteIORef IORef TimeZone
zone (TimeZone -> IO ()) -> IO TimeZone -> IO ()
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< IO TimeZone
getCurrentTimeZone
    concurrently_ (doEveryTenthSeconds r refresh) (action zone)