Coder Social home page Coder Social logo

Comments (10)

protesilaos avatar protesilaos commented on September 2, 2024

But this function does not accept only a date.

You mean it does not accept input like 2020-01-15? It seems to work on my end, but maybe I am doing something different.

I am moving old notes from years ago into denote and like to keep the original date, but I have not record of the original creation time.

Depending on what you are doing exactly, you might find better value in the commands denote-dired-rename-marked-files and denote-dired-rename-marked-files-and-add-front-matters. Both are in the main branch, though they will only be available for the regular package in the next tagged release (maybe at end of this week or beginning of the next one)). Their doc strings explain what they do.

Could this function be written so that it uses current time, when only a date is added?

Wouldn't this be the same as the denote command then? Or am I misunderstanding what you mean by "current time"? Maybe you want the input 2020-01-15 to be interpreted as, say, 2020-01-15 14:48 (current local hour and minute)?

Just asking to be sure I get this right.

from denote.

protesilaos avatar protesilaos commented on September 2, 2024

Assuming I understood your request earlier, this diff automatically adds an HOUR:MINUTE component to a YEAR-MONTH-DAY input. The helper function's doc string notes as much:

 denote.el | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)

diff --git a/denote.el b/denote.el
index 8a17fbe..0cbf13c 100644
--- a/denote.el
+++ b/denote.el
@@ -827,9 +827,20 @@ (defun denote--date-prompt ()
    "DATE and TIME for note (e.g. 2022-06-16 14:30): "
    nil 'denote--date-history))
 
+(defun denote--date-add-current-time (date)
+  "Add current time to date, if necessary.
+The idea is to turn 2020-01-15 into 2020-01-15 16:19 so that the
+hour and minute component is not left to 00:00.  This reduces the
+burden on the user who would otherwise need to input that value
+in order to avoid the error of duplicate identifiers."
+  (if (<= (length date) 10)
+      (format "%s %s" date (format-time-string "%H:%M:%S" (current-time)))
+    date))
+
 (defun denote--valid-date (date)
   "Return DATE if parsed by `date-to-time', else signal error."
-  (date-to-time date))
+  (let ((d (denote--date-add-current-time date)))
+    (date-to-time d)))
 
 (defun denote--buffer-file-names ()
   "Return file names of active buffers."

from denote.

pprevos avatar pprevos commented on September 2, 2024

I'll explain it a bit better:

  1. I evoke the denote-date function
  2. I enter a date only, e.g. 2013-12-22
  3. I enter filename and tags
  4. Emacs says "Invalid date: 2013-12-22

I am using Denote 0.3.1.

The manual either needs to specify that a time has to be provided, or the function should assume current time or 00:00:00 when no time is provided.

from denote.

protesilaos avatar protesilaos commented on September 2, 2024

That's strange. It works on my end.

Does this evaluate for you? (this is basically what we do):

(date-to-time "2013-12-22")

On my end, it returns (21174 3936). So this works as well:

(format-time-string "%Y%m%dT%H%M%S" (date-to-time "2013-12-22"))

It returns "20131222T000000" which is the expected value.

from denote.

pprevos avatar pprevos commented on September 2, 2024

Hi Prot,

I used a clean Emacs 28.1 with only Denote (GNU Emacs 28.1 (build 1, x86_64-pc-linux-gnu, GTK+ Version 3.24.33, cairo version 1.17.6) of 2022-04-28).

When I run (date-to-time "2013-12-22"), I get the same error: date-to-time: Invalid date: 2013-12-22.

This version works: (date-to-time "2013-12-22 12:13:14").

I excavated the date-time function in my setup. I am not much of an elisp wiz. Is this the same function you use?

(defun date-to-time (date)
  "Parse a string DATE that represents a date-time and return a time value.
DATE should be in one of the forms recognized by `parse-time-string'.
If DATE lacks timezone information, GMT is assumed."
  (condition-case err
      (encode-time (parse-time-string date))
    (error
     (let ((overflow-error '(error "Specified time is not representable")))
       (if (equal err overflow-error)
       (signal (car err) (cdr err))
     (condition-case err
         (encode-time (parse-time-string
		       (timezone-make-date-arpa-standard date)))
   (error
    (if (equal err overflow-error)
	(signal (car err) (cdr err))
      (error "Invalid date: %s" date)))))))))

from denote.

protesilaos avatar protesilaos commented on September 2, 2024

Hi Peter,

I just reproduced your problem. It is a difference between Emacs 28 and Emacs 29. I will update the code accordingly. The desired behaviour is to accept input like 2013-12-22.

from denote.

protesilaos avatar protesilaos commented on September 2, 2024

Just pushed commit f6a1276. It should fix your problem.

To recap, this would return an error on Emacs 28 but not on Emacs 29:

(denote--valid-date "2013-12-22")

Changing the code to the following makes (denote--valid-date "2013-12-22") work:

(defun denote--date-add-current-time (date)
  "Add current time to date, if necessary.
The idea is to turn 2020-01-15 into 2020-01-15 16:19 so that the
hour and minute component is not left to 00:00.

This reduces the burden on the user who would otherwise need to
input that value in order to avoid the error of duplicate
identifiers.

It also addresses a difference between Emacs 28 and Emacs 29
where the former does not read dates without a time component."
  (if (<= (length date) 10)
      (format "%s %s" date (format-time-string "%H:%M:%S" (current-time)))
    date))

(defun denote--valid-date (date)
  "Return DATE if parsed by `date-to-time', else signal error."
  (let ((datetime (denote--date-add-current-time date)))
    (date-to-time datetime)))

EDIT: clarify sentence.

from denote.

protesilaos avatar protesilaos commented on September 2, 2024

As for how date-to-time looks on Emacs 29:

;;;###autoload
;; `parse-time-string' isn't sufficiently general or robust.  It fails
;; to grok some of the formats that timezone does (e.g. dodgy
;; post-2000 stuff from some Elms) and either fails or returns bogus
;; values.  timezone-make-date-arpa-standard should help.
(defun date-to-time (date)
  "Parse a string DATE that represents a date-time and return a time value.
DATE should be in one of the forms recognized by `parse-time-string'.
If DATE lacks timezone information, GMT is assumed."
  (condition-case err
      (let ((parsed (parse-time-string date)))
	(when (decoded-time-year parsed)
	  (decoded-time-set-defaults parsed))
	(encode-time parsed))
    (error
     (let ((overflow-error '(error "Specified time is not representable")))
       (if (equal err overflow-error)
	   (signal (car err) (cdr err))
	 (condition-case err
	     (encode-time (parse-time-string
			   (timezone-make-date-arpa-standard date)))
	   (error
	    (if (equal err overflow-error)
		(signal (car err) (cdr err))
	      (error "Invalid date: %s" date)))))))))

from denote.

pprevos avatar pprevos commented on September 2, 2024

Excellent. I moved to the development version and now it works a treat. Thanks for researching this minor issue.

from denote.

protesilaos avatar protesilaos commented on September 2, 2024

You're welcome!

from denote.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.