Load light or dark theme based on time of the day

I have to work in a dark room sometimes during the night, so I have the screen brightness set to nearly zero. And it's a PITA to see what on the screen with a dark color scheme such as tomorrow-night. Time like this, the light scheme is way better, I find that solarized-light is one of the best.

So I wrote a script to automatically select the right color scheme when I open Emacs. If it's between 20:00 in the evening and 6:00 in the morning, it is the light theme, otherwise, use the dark one:

(defun set-light-theme ()
  "Set the light theme with some customization if needed."
  (load-theme 'doom-solarized-light t))

(defun set-dark-theme ()
  "Set the dark theme with some customization if needed."
  (load-theme 'doom-tomorrow-night t))

(let ((current-hour (string-to-number (format-time-string "%H"))))
  (if (or (< current-hour 6) (> current-hour 20)) (set-light-theme) (set-dark-theme)))

If you want your Emacs automatically switch between the color schemes when the time come, you can use run-with-timer function to do the check every hour:

(defun theme-switcher ()
  (let ((current-hour (string-to-number (format-time-string "%H"))))
    (if (or (< current-hour 6) (> current-hour 20)) (set-light-theme) (set-dark-theme))))

;; Run at every 3600 seconds, after 0s delay
(run-with-timer 0 3600 'theme-switcher)

Date: 2019-03-29 Fri 00:00

Author: Huy Tran

Created: 2019-03-30 Sat 00:00