From 1477a61cf46c5a4418b45fd6ff9b8637a56355c5 Mon Sep 17 00:00:00 2001 From: Arif Shaikh Date: Sun, 23 Jan 2022 12:30:37 +0530 Subject: [PATCH] replace tzc-favourite-time-zones by tzc-favourite-time-zones-alist #2 --- README.org | 10 ++++++++-- tzc.el | 54 ++++++++++++++++++++++++++++++++---------------------- 2 files changed, 40 insertions(+), 24 deletions(-) diff --git a/README.org b/README.org index d428631..8246944 100644 --- a/README.org +++ b/README.org @@ -28,9 +28,15 @@ :ensure t) #+END_SRC * Customization -You can customize ~tzc-favourite-time-zones~ to set your favourite ~time-zones~ +You can customize ~tzc-favourite-time-zones-alist~ to set your favourite ~time-zones~ and ~labels~ #+BEGIN_SRC emacs-lisp - (setq tzc-favourite-time-zones '("Asia/Kolkata" "America/New_York")) + (setq tzc-favourite-time-zones-alist '(("UTC+0000" "UTC") + ("Asia/Kolkata" "Kolkata") + ("America/New_York" "New York") + ("UK/London" "London") + ("Europe/Berlin" "Berlin") + ("Asia/Shanghai" "Shanghai") + ("Asia/Tokyo" "Tokyo"))) #+END_SRC * How to use it ** Convert a time between time zones diff --git a/tzc.el b/tzc.el index 821ecb5..060c3e3 100644 --- a/tzc.el +++ b/tzc.el @@ -30,22 +30,33 @@ ;; time-zone to a list of favourite time-zones. ;; ;; A list of favourite time zones could be set using like following -;; (setq tzc-favourite-time-zones '("Asia/Kolkata" "America/New_York" "Europe/Berlin")) +;; (setq tzc-favourite-time-zones-alist '(("Asia/Kolkata" "Kolkata") ("America/New_York" "New York") ("Europe/Berlin" "Berlin"))) ;;; Code: (require 'timezone) -(defcustom tzc-favourite-time-zones '("UTC+0000" - "Asia/Kolkata" - "America/New_York" - "UK/London" - "Europe/Berlin" - "Asia/Shanghai" - "Asia/Tokyo") - "List of favourite time zones." - :type 'list + +(defcustom tzc-favourite-time-zones-alist '(("UTC+0000" "UTC") + ("Asia/Kolkata" "Kolkata") + ("America/New_York" "New York") + ("UK/London" "London") + ("Europe/Berlin" "Berlin") + ("Asia/Shanghai" "Shanghai") + ("Asia/Tokyo" "Tokyo")) + "Alist for favourite time zones containing timezone and label." + :type '(repeat (list string string)) :group 'tzc) +(defun tzc--favourite-time-zones () + "Get the list of favourite time zones." + (mapcar #'car tzc-favourite-time-zones-alist)) + +(defun tzc--get-time-zone-label (time-zone) + "Get the label for the TIME-ZONE." + (if (member time-zone (tzc--favourite-time-zones)) + (nth 1 (assoc time-zone tzc-favourite-time-zones-alist)) + time-zone)) + (defcustom tzc-main-dir (cond ((string-equal system-type "darwin") "/usr/share/zoneinfo.default/") ((string-equal system-type "gnu/linux") "/usr/share/zoneinfo/")) "Main directory to look for the zoneinfo data on your system" @@ -64,7 +75,7 @@ (setq zones (append zones (mapcar (lambda (zone) (concat area "/" zone)) (directory-files (concat tzc-main-dir area) nil directory-files-no-dot-files-regexp))))) zones)) -(defcustom tzc-time-zones (delete-dups (append tzc-favourite-time-zones (tzc--get-time-zones))) +(defcustom tzc-time-zones (delete-dups (append (tzc--favourite-time-zones) (tzc--get-time-zones))) "List of time zones." :type 'list :group 'tzc) @@ -145,7 +156,7 @@ erroneous calculation. Please use correct format for time!")) (to-day-string)) (unless (string-equal day "+0d") (setq to-day-string (format " %s" day))) - (concat to-time-string to-day-string " " to-zone))) + (concat to-time-string to-day-string))) (defun tzc--time-list (time-zone) "A list of times to display for completion based on TIME-ZONE." @@ -164,36 +175,35 @@ erroneous calculation. Please use correct format for time!")) (to-zone (completing-read (format "Convert time from %s to: " from-zone) tzc-time-zones)) (time-string (completing-read (format "Enter time to covert from %s to %s: " from-zone to-zone) (tzc--time-list from-zone)))) (list time-string from-zone to-zone))) - (message (concat time-string " " from-zone " = " (tzc--get-converted-time-string time-string from-zone to-zone)))) + (message (concat time-string " " (tzc--get-time-zone-label from-zone) " = " (tzc--get-converted-time-string time-string from-zone to-zone) " " (tzc--get-time-zone-label to-zone)))) (defun tzc-convert-current-time (to-zone) "Convert current local time to TO-ZONE." (interactive (list (completing-read "Enter To Zone: " tzc-time-zones))) (let ((time-now (format-time-string "%H:%M"))) - (message (concat "Local Time " time-now " = " (tzc--get-converted-time-string time-now nil to-zone))))) + (message (concat "Local Time " time-now " = " (tzc--get-converted-time-string time-now nil to-zone) " " (tzc--get-time-zone-label to-zone))))) (defun tzc-convert-time-to-favourite-time-zones (time-string from-zone) - "Convert time in TIME-STRING from FROM-ZONE to `tzc-favourite-time-zones`." + "Convert time in TIME-STRING from FROM-ZONE to `(tzc--favourite-time-zones)`." (interactive (let* ((from-zone (completing-read "Enter From Zone: " tzc-time-zones)) (time-string (completing-read "Enter time to covert: " (tzc--time-list from-zone)))) (list time-string from-zone))) (with-current-buffer (generate-new-buffer "*tzc-times*") - (insert time-string " " from-zone) - (dolist (to-zone tzc-favourite-time-zones) + (insert time-string " " (tzc--get-time-zone-label from-zone)) + (dolist (to-zone (tzc--favourite-time-zones)) (unless (string-equal to-zone from-zone) - (insert " = " (tzc--get-converted-time-string time-string from-zone to-zone) "\n"))) + (insert " = " (tzc--get-converted-time-string time-string from-zone to-zone) " " (tzc--get-time-zone-label to-zone) "\n"))) (align-regexp (point-min) (point-max) "\\(\\s-*\\)=") (switch-to-buffer-other-window "*tzc-times*"))) (defun tzc-convert-current-time-to-favourite-time-zones () - "Convert current local time to `tzc-favourite-time-zones`." + "Convert current local time to `(tzc--favourite-time-zones)`." (interactive) (with-current-buffer (generate-new-buffer "*tzc-times*") - (insert "Local Time " (format-time-string "%H:%M")) - (dolist (to-zone tzc-favourite-time-zones) + (dolist (to-zone (tzc--favourite-time-zones)) (unless (string-equal to-zone nil) - (insert " = " (tzc--get-converted-time-string (format-time-string "%H:%M") nil to-zone) "\n"))) + (insert (tzc--get-converted-time-string (format-time-string "%H:%M") nil to-zone) " " (tzc--get-time-zone-label to-zone) "\n"))) (align-regexp (point-min) (point-max) "\\(\\s-*\\)=") (switch-to-buffer-other-window "*tzc-times*")))