summaryrefslogtreecommitdiffhomepage
path: root/src/Plugins/DateZone.hs
blob: f6c4f7a1eb92def94cf3462afe78f6aa81aef75e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
-----------------------------------------------------------------------------
-- |
-- Module      :  Plugins.DateZone
-- Copyright   :  (c) Martin Perner
-- License     :  BSD-style (see LICENSE)
--
-- Maintainer  :  Martin Perner <martin@perner.cc>
-- Stability   :  unstable
-- Portability :  unportable
--
-- A date plugin with location support for Xmobar
--
-- Based on Plugins.Date
--
-- Usage example: in template put
--
-- > Run DateZone "%H:%M:%S" "utcDate" "UTC" 10
--
-----------------------------------------------------------------------------

module Plugins.DateZone (DateZone(..)) where

import Plugins

import System.Locale

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
    deriving (Read, Show)

instance Exec DateZone where
    alias (DateZone _ a _ _) = a
    run   (DateZone f _ z _) = date f z
    rate  (DateZone _ _ _ r) = r

date :: String -> String -> IO String
date format zone = do
  timeZone <- getTimeZoneSeriesFromOlsonFile ("/usr/share/zoneinfo/" ++ zone)
  zonedTime <- getZonedTime
  return $ formatTime defaultTimeLocale format $ utcToLocalTime' timeZone $ zonedTimeToUTC zonedTime