branch: elpa/datetime
commit f448813852629d75c2ce2acb69ab9d918e956468
Author: Paul Pogonyshev <pogonys...@gmail.com>
Commit: Paul Pogonyshev <pogonys...@gmail.com>

    Add some hints about what to do if system locale and/or timezone cannot be 
guessed; improve customization a bit.
---
 README.md   | 19 +++++++++++++++++++
 datetime.el | 47 ++++++++++++++++++++++++++++++++---------------
 2 files changed, 51 insertions(+), 15 deletions(-)

diff --git a/README.md b/README.md
index ef597f958f..bfcfa85ceb 100644
--- a/README.md
+++ b/README.md
@@ -11,6 +11,25 @@ timestamps and date-time format strings.  Not all of the 
planned
 functionality is implemented yet.
 
 
+## System locale and timezone
+
+The library will try to guess your system locale and timezone, but
+this is frustratingly difficult.  In particular, on MS Windows it will
+not be able to determine the timezone (not sure about locale).
+Patches to improve this are welcome.
+
+In any case, when it fails completely or guesses incorrectly, you can
+always override the heuristics results by setting variables
+`datetime-locale` and/or `datetime-timezone` manually.  Both are also
+available through Emacs customization interface, group `datetime`.
+
+To find the list of all supported locales and timezones, evaluate the
+following forms:
+
+    (prin1-to-string (sort (datetime-list-locales t) #'string<))
+    (prin1-to-string (sort (datetime-list-timezones) #'string<))
+
+
 ## Pattern types
 
 There exist several different ways to specify date and/or time format.
diff --git a/datetime.el b/datetime.el
index 19fb8e9524..0cbb522506 100644
--- a/datetime.el
+++ b/datetime.el
@@ -160,19 +160,31 @@
   "Date-time handling library."
   :group 'i18n)
 
+;; Unfortunately I see no way to provide completion from a non-fixed
+;; set of options.
 (defcustom datetime-locale nil
   "Default locale for date-time formatting and parsing.
 Leave unset to let the library auto-determine it from your OS
-when necessary."
+when necessary.
+
+You can see the list of locales supported by the library by
+evaluating this form:
+
+    (prin1-to-string (sort (datetime-list-locales t) #\\='string<))"
   :group 'datetime
-  :type  'symbol)
+  :type  '(restricted-sexp :match-alternatives ((lambda (value) (or (null 
value) (extmap-contains-key datetime--locale-extmap value))))))
 
 (defcustom datetime-timezone nil
   "Default timezone for date-time formatting and parsing.
 Leave unset to let the library auto-determine it from your OS
-when necessary."
+when necessary.
+
+You can see the list of supported timezones by evaluating this
+form:
+
+    (prin1-to-string (sort (datetime-list-timezones) #\\='string<))"
   :group 'datetime
-  :type  'symbol)
+  :type  '(restricted-sexp :match-alternatives ((lambda (value) (or (null 
value) (extmap-contains-key datetime--timezone-extmap value))))))
 
 
 (defun datetime--get-locale (options)
@@ -213,17 +225,22 @@ when necessary."
     (let ((system-timezone (intern (or (pcase system-type
                                          ((or `gnu `gnu/linux `gnu/kfreebsd)
                                           (or ;; For Debian-based distros.
-                                             (when (file-exists-p 
"/etc/timezone")
-                                               (condition-case nil
-                                                   (with-temp-buffer
-                                                     
(insert-file-contents-literally "/etc/timezone")
-                                                     (when (looking-at "\\S-+")
-                                                       
(match-string-no-properties 0)))
-                                                 (error)))
-                                             ;; Freedesktop standard (?).
-                                             (let ((locatime (file-symlink-p 
"/etc/localtime")))
-                                               (when (and locatime 
(string-match "/usr/share/zoneinfo/\\(.+\\)" locatime))
-                                                 (match-string-no-properties 1 
locatime))))))
+                                              (when (file-exists-p 
"/etc/timezone")
+                                                (condition-case nil
+                                                    (with-temp-buffer
+                                                      
(insert-file-contents-literally "/etc/timezone")
+                                                      (when (looking-at 
"\\S-+")
+                                                        
(match-string-no-properties 0)))
+                                                  (error)))
+                                              ;; Freedesktop standard (?).
+                                              (let ((localtime (file-symlink-p 
"/etc/localtime")))
+                                                (when (and localtime 
(string-match "/usr/share/zoneinfo/\\(.+\\)" localtime))
+                                                  (match-string-no-properties 
1 localtime)))))
+                                         ;; FIXME: On Windows we could 
(probably) use "tzutil /g" command to get
+                                         ;;        timezone identifier, but 
then it still needs to be mapped to what we
+                                         ;;        have in 
`timezone-data.extmap' (i.e. Java format)...  So, currently
+                                         ;;        Windows users have to set 
`datetime-timezone' manually.
+                                         )
                                        (cadr (current-time-zone))
                                        "?"))))
       (if (extmap-contains-key datetime--timezone-extmap system-timezone)

Reply via email to