help-gnu-emacs
[Top][All Lists]
Advanced

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

The wild and weird world of Emacs Lisp date/time arithmetic


From: Skip Montanaro
Subject: The wild and weird world of Emacs Lisp date/time arithmetic
Date: Thu, 17 Dec 2020 09:57:18 -0600

I have a very simple date calculation. I have a CSV file whose first field
is an ISO-8601 date, for example:

2020-12-03,738050,2262878,0.33

I want to write a function to add a day and append a new (empty) record.
The end result should look like this:

2020-12-03,738050,2262878,0.33
2020-12-04

Easy peasy, right? I have no problem plucking the time out of the buffer or
decoding the string. I'm having trouble converting back to string form
after parsing and doing the arithmetic. It seems to me that there are a
myriad number of datetime representations. I would have thought (based on
their names) that parse-time-string and format-time-string were
complementary functions, but they are not. This raises an exception:

(format-time-string "%Y-%m-%d" (parse-time-string "2020-05-17"))

So, given a timestamp as returned from (parse-time-string), how does one
format it for display? More concretely, I would like the return value from
this function:

(defun dt-convert ()
  (setq this-date (parse-time-string "2020-05-17"))
  (setq one-day (make-decoded-time :day 1))
  (setq next-date (decoded-time-add this-date one-day))
  (format-time-string "%Y-%m-%d" next-date)
  )

to be "2020-05-18".

Completely perplexed...

Skip Montanaro


reply via email to

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