diff options
-rw-r--r-- | changelog.md | 3 | ||||
-rw-r--r-- | src/Xmobar/Plugins/Date.hs | 14 |
2 files changed, 14 insertions, 3 deletions
diff --git a/changelog.md b/changelog.md index e77af85..5a02fc4 100644 --- a/changelog.md +++ b/changelog.md @@ -5,6 +5,9 @@ _New features_ - New plugin `HandleReader` for reading data from a Haskell `Handle`. This is useful if you are running xmobar from within a Haskell program. - Build with ghc 8.10 allowed. + - Optimize date plugin by avoiding calling getTimeZone for each of + the time the date has to be updated. Instead, it's computed once + at the start and re-used for each invocation. ## Version 0.33 (February, 2020) diff --git a/src/Xmobar/Plugins/Date.hs b/src/Xmobar/Plugins/Date.hs index 1cb0596..83ca1c9 100644 --- a/src/Xmobar/Plugins/Date.hs +++ b/src/Xmobar/Plugins/Date.hs @@ -31,8 +31,16 @@ data Date = Date String String Int instance Exec Date where alias (Date _ a _) = a - run (Date f _ _) = date f rate (Date _ _ r) = r + start (Date f _ r) cb = do + t <- getCurrentTime + zone <- getTimeZone t + go zone + where + go zone = doEveryTenthSeconds r $ date zone f >>= cb -date :: String -> IO String -date format = fmap (formatTime defaultTimeLocale format) getZonedTime +date :: TimeZone -> String -> IO String +date timezone format = do + time <- getCurrentTime + let zonedTime = utcToZonedTime timezone time + pure $ formatTime defaultTimeLocale format zonedTime |