summaryrefslogtreecommitdiffhomepage
path: root/src/Plugins
diff options
context:
space:
mode:
authorMartin Perner <martin@perner.cc>2011-10-27 20:51:19 +0200
committerMartin Perner <martin@perner.cc>2011-10-27 22:46:28 +0200
commitf6bb8eb4c4350522367f4ab2dacbfeb0a99cd06d (patch)
tree304e4ca8230f78cb659dff9a4aeadd1f65242e85 /src/Plugins
parent9718dabe6c6d5979e3f6837ef04a39d3ad8c786c (diff)
downloadxmobar-f6bb8eb4c4350522367f4ab2dacbfeb0a99cd06d.tar.gz
xmobar-f6bb8eb4c4350522367f4ab2dacbfeb0a99cd06d.tar.bz2
DateZone, Date*L Plugin merges; DateZone API-Change!
To support multiple locales, an lock was introduced. Although through this, supporting DateL and DateZoneL would be cumbersome. To simplify the usage, DateZone was replaced with DateZoneL. Additionally the position of the Alias parameter was changed. Using "" as Zone parameter for DateZoneL simulates DateL. Providing also "" for locale simulates Date.
Diffstat (limited to 'src/Plugins')
-rw-r--r--src/Plugins/DateL.hs35
-rw-r--r--src/Plugins/DateZone.hs50
-rw-r--r--src/Plugins/DateZoneL.hs42
3 files changed, 39 insertions, 88 deletions
diff --git a/src/Plugins/DateL.hs b/src/Plugins/DateL.hs
deleted file mode 100644
index d8859ed..0000000
--- a/src/Plugins/DateL.hs
+++ /dev/null
@@ -1,35 +0,0 @@
------------------------------------------------------------------------------
--- |
--- Module : Plugins.DateL
--- Copyright : (c) Andrea Rossato
--- License : BSD-style (see LICENSE)
---
--- Maintainer : Martin Perner <martin@perner.cc>
--- Stability : unstable
--- Portability : unportable
---
--- A date plugin with localization for Xmobar
---
------------------------------------------------------------------------------
-
-module Plugins.DateL (DateL(..)) where
-
-import Plugins
-import Localize
-
-import System.Time
-
-data DateL = DateL String String String Int
- deriving (Read, Show)
-
-instance Exec DateL where
- alias (DateL _ _ a _) = a
- start (DateL f l _ r) cb = do
- setupTimeLocale l
- go
- where go = date f >>= cb >> tenthSeconds r >> go
-
-date :: String -> IO String
-date format = do
- t <- toCalendarTime =<< getClockTime
- return $ formatCalendarTime getTimeLocale format t
diff --git a/src/Plugins/DateZone.hs b/src/Plugins/DateZone.hs
index f6c4f7a..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 location 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
diff --git a/src/Plugins/DateZoneL.hs b/src/Plugins/DateZoneL.hs
deleted file mode 100644
index 2b7c467..0000000
--- a/src/Plugins/DateZoneL.hs
+++ /dev/null
@@ -1,42 +0,0 @@
------------------------------------------------------------------------------
--- |
--- Module : Plugins.DateZoneL
--- Copyright : (c) Martin Perner
--- License : BSD-style (see LICENSE)
---
--- Maintainer : Martin Perner <martin@perner.cc>
--- Stability : unstable
--- Portability : unportable
---
--- A date plugin with localization and location support for Xmobar
---
--- Based on Plugins.DateZone
---
------------------------------------------------------------------------------
-
-module Plugins.DateZoneL (DateZoneL(..)) where
-
-import Plugins
-
-import Localize
-
-import Data.Time.LocalTime
-import Data.Time.Format
-import Data.Time.LocalTime.TimeZone.Olson
-import Data.Time.LocalTime.TimeZone.Series
-
-data DateZoneL = DateZoneL String String String String Int
- deriving (Read, Show)
-
-instance Exec DateZoneL where
- alias (DateZoneL _ _ a _ _) = a
- start (DateZoneL f l _ z r) cb = do
- setupTimeLocale l
- go
- where go = date f z >>= cb >> tenthSeconds r >> go
-
-date :: String -> String -> IO String
-date format zone = do
- timeZone <- getTimeZoneSeriesFromOlsonFile ("/usr/share/zoneinfo/" ++ zone)
- zonedTime <- getZonedTime
- return $ formatTime getTimeLocale format $ utcToLocalTime' timeZone $ zonedTimeToUTC zonedTime