I have a LaTeX package declared with a date:
\usepackage{foo}[=2021-04-01]
and I need to check if this date is before or after "2021-03-01".
Is there a way to compare dates, like (if (< date1 date0) ... )
?
Raw solution
Thanks to @Drew's answer I have found this solution:
(defun myfunc ()
(interactive)
(goto-char (point-min))
(when (re-search-forward "\\\\usepackage{foo}\\[=\\([0-9]+\\)-\\([0-9]+\\)-\\([0-9]+\\)\\]" nil t)
(let* ((year (match-string 1))
(month-num (match-string 2))
(day (match-string 3))
(month)
(t0)
(t1))
(if (string-equal month-num "01")
(setq month "jan")
(if (string-equal month-num "02")
(setq month "feb")
(if (string-equal month-num "03")
(setq month "mar")
(if (string-equal month-num "04")
(setq month "apr")
(if (string-equal month-num "05")
(setq month "may")
(if (string-equal month-num "06")
(setq month "jun")
(if (string-equal month-num "07")
(setq month "jul")
(if (string-equal month-num "08")
(setq month "aug")
(if (string-equal month-num "09")
(setq month "sep")
(if (string-equal month-num "10")
(setq month "oct")
(if (string-equal month-num "11")
(setq month "nov")
(if (string-equal month-num "12")
(setq month "dec")))))))))))))
(setq t0 (date-to-time "01 mar 2021 00.00.00"))
(setq t1 (date-to-time (concat day
" "
month
" "
year
" 00.00.00")))
(if (time-less-p t1 t0)
(insert "foo")
(if (time-less-p t0 t1)
(insert "bar")))
)))
I can't figure out how to manipulate date-to-time
argument: how can I simply use "2021-01-01" as date format?
I have read about (format-time-string "%Y-%m-%d")
, but I don't understand how to use it.
Raw Solution Update
For the sake of completeness:
(cond
((string= month-num "01")
(setq month "jan"))
((string= month-num "02")
(setq month "feb"))
((string= month-num "03")
(setq month "mar"))
((string= month-num "04")
(setq month "apr"))
((string= month-num "05")
(setq month "may"))
((string= month-num "06")
(setq month "jun"))
((string= month-num "07")
(setq month "jul"))
((string= month-num "08")
(setq month "aug"))
((string= month-num "09")
(setq month "sep"))
((string= month-num "10")
(setq month "oct"))
((string= month-num "11")
(setq month "nov"))
((string= month-num "12")
(setq month "dec")))
(I don't know about pcase
.)
Final Solution
Putting together all your suggestions, I got the following solution:
(defun myfunc ()
(interactive)
(goto-char (point-min))
(when (re-search-forward "\\\\usepackage{foo}\\[=\\([0-9]+\\)-\\([0-9]+\\)-\\([0-9]+\\)\\]" nil t)
(let* ((release (concat (match-string 1)
"-"
(match-string 2)
"-"
(match-string 3)
" 00:00:00"))
(t0)
(t1))
(setq t0 (date-to-time "2021-03-01 00:00:00"))
(setq t1 (date-to-time release))
(if (time-less-p t1 t0)
(insert "foo")
(if (time-less-p t0 t1)
(insert "bar")))
)))
if
expressions with a single flatcond
orpcase
.(parse-time-string (format "%s 00:00:00 Z" "2021-04-16"))
- I used a string in the example to demonstrate but you could just store your whole regex match in a variable and use that. However, see my comment on the answer below about a potential issue withtime-less-p
.