emacs-elpa-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[elpa] externals/denote 0a5c142287 1/6: Refactor denote--date-convert


From: ELPA Syncer
Subject: [elpa] externals/denote 0a5c142287 1/6: Refactor denote--date-convert
Date: Sat, 14 Dec 2024 03:57:57 -0500 (EST)

branch: externals/denote
commit 0a5c1422876ec5f3e05d7ec6dedf290a81e93210
Author: Jean-Philippe Gagné Guay <jeanphilippe150@gmail.com>
Commit: Jean-Philippe Gagné Guay <jeanphilippe150@gmail.com>

    Refactor denote--date-convert
---
 README.org           |  9 ++++----
 denote.el            | 60 +++++++++++++++++++++++-----------------------------
 tests/denote-test.el | 12 +++++------
 3 files changed, 35 insertions(+), 46 deletions(-)

diff --git a/README.org b/README.org
index d295150799..4aa0893a2d 100644
--- a/README.org
+++ b/README.org
@@ -5949,11 +5949,10 @@ might change them without further notice.
   ([[#h:e7ef08d6-af1b-4ab3-bb00-494a653e6d63][The 
denote-date-prompt-use-org-read-date option]]). With optional
   =INITIAL-DATE= use it as the initial minibuffer text. With optional
   =PROMPT-TEXT= use it in the minibuffer instead of the default
-  prompt. When ~denote-date-prompt-use-org-read-date~ is non-nil, the
-  value of =INITIAL-DATE= is of the format understood by
-  ~org-read-date~. Otherwise, it is a string that can be processed by
-  ~denote-valid-date-p~. [ The =INITIAL-DATE= and =PROMPT-TEXT= are
-  part of {{{development-version}}}. ]
+  prompt. =INITIAL-DATE= is a string that can be processed by
+  ~denote-valid-date-p~, a value that can be parsed by ~decode-time~
+  or nil. [ The =INITIAL-DATE= and =PROMPT-TEXT= are part of
+  {{{development-version}}}. ]
 
 #+findex: denote-command-prompt
 + Function ~denote-command-prompt~ ::  Prompt for command among
diff --git a/denote.el b/denote.el
index 40f776405f..1ec6b117c1 100644
--- a/denote.el
+++ b/denote.el
@@ -2737,20 +2737,12 @@ here for clarity."
 
 (defun denote--date-convert (date prefer-type)
   "Determine how to convert DATE to PREFER-TYPE `:list' or `:string'."
-  (let ((parsed-date (denote-valid-date-p date)))
-    (unless (memq prefer-type '(:list :string))
-      (error "The PREFER-TYPE must be either `:list' or `:string'"))
-    (cond
-     ((listp date)
-      (if (eq prefer-type :list)
-          parsed-date
-        (format-time-string "%F %T" date)))
-     ((stringp date)
-      (if (eq prefer-type :string)
-          date
-        parsed-date))
-     (t
-      (error "The `%s' is neither a list nor a string" date)))))
+  (unless (memq prefer-type '(:list :string))
+    (error "The PREFER-TYPE must be either `:list' or `:string'"))
+  (cond ((eq prefer-type :list)
+         date)
+        ((eq prefer-type :string)
+         (if date (format-time-string "%F %T" date) ""))))
 
 (defun denote-date-prompt (&optional initial-date prompt-text)
   "Prompt for date, expecting YYYY-MM-DD or that plus HH:MM.
@@ -2761,25 +2753,25 @@ With optional INITIAL-DATE use it as the initial 
minibuffer
 text.  With optional PROMPT-TEXT use it in the minibuffer instead
 of the default prompt.
 
-When `denote-date-prompt-use-org-read-date' is non-nil, the value of
-INITIAL-DATE is of the format understood by `org-read-date'.  Otherwise,
-it is a string that can be processed by `denote-valid-date-p'."
-  (if (and denote-date-prompt-use-org-read-date
-           (require 'org nil :no-error))
-      (let* ((time (org-read-date nil t nil prompt-text (denote--date-convert 
initial-date :list)))
-             (org-time-seconds (format-time-string "%S" time))
-             (cur-time-seconds (format-time-string "%S" (current-time))))
-        ;; When the user does not input a time, org-read-date defaults to 00 
for seconds.
-        ;; When the seconds are 00, we add the current seconds to avoid 
identifier collisions.
-        (when (string-equal "00" org-time-seconds)
-          (setq time (time-add time (string-to-number cur-time-seconds))))
-        (format-time-string "%Y-%m-%d %H:%M:%S" time))
-    (read-string
-     (or
-      "DATE and TIME for note (e.g. 2022-06-16 14:30): "
-      prompt-text)
-     (denote--date-convert initial-date :string)
-     'denote-date-history)))
+INITIAL-DATE is a string that can be processed by `denote-valid-date-p',
+a value that can be parsed by `decode-time' or nil."
+  (let ((initial-date (denote-valid-date-p initial-date)))
+    (if (and denote-date-prompt-use-org-read-date
+             (require 'org nil :no-error))
+        (let* ((time (org-read-date nil t nil prompt-text 
(denote--date-convert initial-date :list)))
+               (org-time-seconds (format-time-string "%S" time))
+               (cur-time-seconds (format-time-string "%S" (current-time))))
+          ;; When the user does not input a time, org-read-date defaults to 00 
for seconds.
+          ;; When the seconds are 00, we add the current seconds to avoid 
identifier collisions.
+          (when (string-equal "00" org-time-seconds)
+            (setq time (time-add time (string-to-number cur-time-seconds))))
+          (format-time-string "%Y-%m-%d %H:%M:%S" time))
+      (read-string
+       (or
+        "DATE and TIME for note (e.g. 2022-06-16 14:30): "
+        prompt-text)
+       (denote--date-convert initial-date :string)
+       'denote-date-history))))
 
 (defun denote-prompt-for-date-return-id (&optional initial-date prompt-text)
   "Use `denote-date-prompt' and return it as `denote-id-format'.
@@ -2787,7 +2779,7 @@ Optional INITIAL-DATE and PROMPT-TEXT have the same 
meaning as
 `denote-date-prompt'."
   (denote-get-identifier
    (denote-valid-date-p
-    (denote-date-prompt initial-date prompt-text))))
+    (denote-date-prompt (denote-valid-date-p initial-date) prompt-text))))
 
 (defvar denote-subdirectory-history nil
   "Minibuffer history of `denote-subdirectory-prompt'.")
diff --git a/tests/denote-test.el b/tests/denote-test.el
index 8dd2ba92b0..9371c0e905 100644
--- a/tests/denote-test.el
+++ b/tests/denote-test.el
@@ -507,15 +507,13 @@ does not involve the time zone."
            (equal (denote--date-convert '(26454 45206 461174 657000) :string)
                   "2024-12-09 10:55:50")
 
-           (equal (denote--date-convert "2024-12-09 10:55:50" :list)
-                  '(26454 45206))
+           (equal (denote--date-convert nil :string)
+                  "")
 
-           (equal (denote--date-convert "2024-12-09 10:55:50" :string)
-                  "2024-12-09 10:55:50")))
+           (equal (denote--date-convert nil :list)
+                  nil)))
   (should-error (denote--date-convert '(26454 45206 461174 657000) 
:not-valid-type))
-  (should-error (denote--date-convert "2024-12-09 10:55:50" :not-valid-type))
-  (should-error (denote--date-convert "Not right date" :list))
-  (should-error (denote--date-convert "Not right date" :string)))
+  (should-error (denote--date-convert nil :not-valid-type)))
 
 ;;;; denote-journal-extras.el
 



reply via email to

[Prev in Thread] Current Thread [Next in Thread]