From 9718dabe6c6d5979e3f6837ef04a39d3ad8c786c Mon Sep 17 00:00:00 2001 From: Martin Perner Date: Wed, 26 Oct 2011 01:00:32 +0200 Subject: Adding locale support to Date plugins This commits adds support for localized datetime outputs like date(1). --- src/Plugins/DateL.hs | 35 +++++++++++++++++++++++++++++++++++ src/Plugins/DateZone.hs | 2 +- src/Plugins/DateZoneL.hs | 42 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 78 insertions(+), 1 deletion(-) create mode 100644 src/Plugins/DateL.hs create mode 100644 src/Plugins/DateZoneL.hs (limited to 'src/Plugins') diff --git a/src/Plugins/DateL.hs b/src/Plugins/DateL.hs new file mode 100644 index 0000000..d8859ed --- /dev/null +++ b/src/Plugins/DateL.hs @@ -0,0 +1,35 @@ +----------------------------------------------------------------------------- +-- | +-- Module : Plugins.DateL +-- Copyright : (c) Andrea Rossato +-- License : BSD-style (see LICENSE) +-- +-- Maintainer : Martin Perner +-- 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 4d5ce6a..f6c4f7a 100644 --- a/src/Plugins/DateZone.hs +++ b/src/Plugins/DateZone.hs @@ -8,7 +8,7 @@ -- Stability : unstable -- Portability : unportable -- --- A date plugin with localization support for Xmobar +-- A date plugin with location support for Xmobar -- -- Based on Plugins.Date -- diff --git a/src/Plugins/DateZoneL.hs b/src/Plugins/DateZoneL.hs new file mode 100644 index 0000000..2b7c467 --- /dev/null +++ b/src/Plugins/DateZoneL.hs @@ -0,0 +1,42 @@ +----------------------------------------------------------------------------- +-- | +-- Module : Plugins.DateZoneL +-- Copyright : (c) Martin Perner +-- License : BSD-style (see LICENSE) +-- +-- Maintainer : Martin Perner +-- 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 -- cgit v1.2.3