diff options
| author | Jose Antonio Ortega Ruiz <jao@gnu.org> | 2011-11-16 18:12:09 +0100 | 
|---|---|---|
| committer | Jose Antonio Ortega Ruiz <jao@gnu.org> | 2011-11-16 18:12:09 +0100 | 
| commit | ea8c116ce91b37d923e258069f2b0c717a89d9a3 (patch) | |
| tree | 6de0fab2a45986a733d15bd26c4960c496f324bf /src/Plugins | |
| parent | 9232bea653070ae48be1a94924f43d55eb32cc8c (diff) | |
| parent | f6bb8eb4c4350522367f4ab2dacbfeb0a99cd06d (diff) | |
| download | xmobar-ea8c116ce91b37d923e258069f2b0c717a89d9a3.tar.gz xmobar-ea8c116ce91b37d923e258069f2b0c717a89d9a3.tar.bz2  | |
Merge remote-tracking branch 'skinner/localeDate' into localeDate
Diffstat (limited to 'src/Plugins')
| -rw-r--r-- | src/Plugins/DateZone.hs | 50 | 
1 files changed, 39 insertions, 11 deletions
diff --git a/src/Plugins/DateZone.hs b/src/Plugins/DateZone.hs index 4d5ce6a..86114bb 100644 --- a/src/Plugins/DateZone.hs +++ b/src/Plugins/DateZone.hs @@ -1,3 +1,4 @@ +{-# LANGUAGE DoAndIfThenElse #-}  -----------------------------------------------------------------------------  -- |  -- Module      :  Plugins.DateZone @@ -8,13 +9,13 @@  -- Stability   :  unstable  -- Portability :  unportable  -- --- A date plugin with localization support for Xmobar +-- A date plugin with localization and location support for Xmobar  --  -- Based on Plugins.Date  --  -- Usage example: in template put  -- --- > Run DateZone "%H:%M:%S" "utcDate" "UTC" 10 +-- > Run DateZone "%a %H:%M:%S" "de_DE.UTF-8" "UTC" "utcDate" 10  --  ----------------------------------------------------------------------------- @@ -22,23 +23,50 @@ module Plugins.DateZone (DateZone(..)) where  import Plugins -import System.Locale +import Localize + +import Control.Concurrent.STM  import Data.Time.LocalTime  import Data.Time.Format  import Data.Time.LocalTime.TimeZone.Olson  import Data.Time.LocalTime.TimeZone.Series -data DateZone = DateZone String String String Int +import System.IO.Unsafe +import System.Locale (TimeLocale) +import System.Time + + + +{-# NOINLINE localeLock #-} +-- ensures that only one plugin instance sets the locale +localeLock :: TMVar Bool +localeLock = unsafePerformIO (newTMVarIO False) + +data DateZone = DateZone String String String String Int      deriving (Read, Show)  instance Exec DateZone where -    alias (DateZone _ a _ _) = a -    run   (DateZone f _ z _) = date f z -    rate  (DateZone _ _ _ r) = r +    alias (DateZone _ _ _ a _) = a +    start (DateZone f l z _ r) cb = do +      lock <- atomically $ takeTMVar localeLock +      setupTimeLocale l +      locale <- getTimeLocale +      atomically $ putTMVar localeLock lock +      if z /= "" then do +        timeZone <- getTimeZoneSeriesFromOlsonFile ("/usr/share/zoneinfo/" ++ z) +        go (dateZone f locale timeZone) +       else +        go (date f locale) + +      where go func = func >>= cb >> tenthSeconds r >> go func + +date :: String -> TimeLocale -> IO String +date format loc = do +  t <- toCalendarTime =<< getClockTime +  return $ formatCalendarTime loc format t -date :: String -> String -> IO String -date format zone = do -  timeZone <- getTimeZoneSeriesFromOlsonFile ("/usr/share/zoneinfo/" ++ zone) +dateZone :: String -> TimeLocale -> TimeZoneSeries -> IO String +dateZone format loc timeZone = do    zonedTime <- getZonedTime -  return $ formatTime defaultTimeLocale format $ utcToLocalTime' timeZone $ zonedTimeToUTC zonedTime +  return $ formatTime loc format $ utcToLocalTime' timeZone $ zonedTimeToUTC zonedTime  | 
