{-# LANGUAGE CPP #-}
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
readListPrec :: ReadPrec [Date]
$creadListPrec :: ReadPrec [Date]
readPrec :: ReadPrec Date
$creadPrec :: ReadPrec Date
readList :: ReadS [Date]
$creadList :: ReadS [Date]
readsPrec :: Int -> ReadS Date
$creadsPrec :: Int -> ReadS 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
showList :: [Date] -> ShowS
$cshowList :: [Date] -> ShowS
show :: Date -> String
$cshow :: Date -> String
showsPrec :: Int -> Date -> ShowS
$cshowsPrec :: Int -> 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 =
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 (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
TimeZone
zone <- IORef TimeZone -> IO TimeZone
forall a. IORef a -> IO a
readIORef IORef TimeZone
zoneRef
(UTCTime -> String) -> IO UTCTime -> IO String
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (TimeLocale -> String -> ZonedTime -> String
forall t. FormatTime t => TimeLocale -> String -> t -> String
formatTime TimeLocale
defaultTimeLocale String
format (ZonedTime -> String)
-> (UTCTime -> ZonedTime) -> UTCTime -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TimeZone -> UTCTime -> ZonedTime
utcToZonedTime TimeZone
zone) IO UTCTime
getCurrentTime
withRefreshingZone :: Int -> (IORef TimeZone -> IO ()) -> IO ()
withRefreshingZone :: Int -> (IORef TimeZone -> IO ()) -> IO ()
withRefreshingZone Int
r IORef TimeZone -> IO ()
action = do
IORef TimeZone
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 :: IO ()
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
IO () -> IO () -> IO ()
forall a b. IO a -> IO b -> IO ()
concurrently_ (Int -> IO () -> IO ()
doEveryTenthSeconds Int
r IO ()
refresh) (IORef TimeZone -> IO ()
action IORef TimeZone
zone)