Created
October 16, 2025 15:14
-
-
Save sogaiu/d47537e1d513b0f87e896e9d84fe0323 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;;; | |
(defvar cycle-date-month-alist | |
'(("jan" . "01") ("feb" . "02") ("mar" . "03") | |
("apr" . "04") ("may" . "05") ("jun" . "06") | |
("jul" . "07") ("aug" . "08") ("sep" . "09") | |
("oct" . "10") ("nov" . "11") ("dec" . "12"))) | |
(ignore | |
(cdr (assoc-string "jan" cycle-date-month-alist)) | |
;; => | |
"01" | |
(cdr (assoc-string "dec" cycle-date-month-alist)) | |
;; => | |
"12" | |
) | |
(defun cycle-date-month-name-to-number (name) | |
"" | |
(cdr (assoc-string (downcase name) | |
cycle-date-month-alist))) | |
(ignore | |
(cycle-date-month-name-to-number "Jan") | |
;; => | |
"01" | |
(cycle-date-month-name-to-number "dec") | |
;; => | |
"12" | |
) | |
(defvar cycle-date-form-date-regex | |
(rx (group (= 2 digit)) | |
(not digit) | |
(group (or "Jan" "Feb" "Mar" "Apr" "May" "Jun" | |
"Jul" "Aug" "Sep" "Oct" "Nov" "Dec")) | |
(not digit) | |
(group (= 4 digit)))) | |
(ignore | |
(setq sample-str "01 Mar 2025") | |
(list (string-match cycle-date-form-date-regex sample-str) | |
(match-string 1 sample-str) | |
(match-string 2 sample-str) | |
(match-string 3 sample-str)) | |
;; => | |
'(0 "01" "Mar" "2025") | |
) | |
(defun cycle-date-swap-next () | |
"Search forward for strings of the form DD MMM YYYY and replace. | |
DD is expected to be 00 through 31. | |
MMM is one of the month name abbreviations (e.g. Jan, Jun, etc). | |
YYYY is expected to be a four-digit year. | |
The replacement date string is of the form YYYY-MM-DD, where | |
MM ranges from 01 through 12." | |
(interactive) | |
(let ((day nil) | |
(month nil) | |
(year nil)) | |
(when (search-forward-regexp cycle-date-form-date-regex) | |
;; move back to where month name string ends | |
(backward-char (+ 1 4)) ; space year | |
;; find and save year string | |
(save-excursion | |
;; space year | |
(when (looking-at (rx (not digit) (group (= 4 digit)))) | |
(setq year (match-string 1)))) | |
;; find month name and save month number string | |
(save-excursion | |
;; month name | |
(backward-char 3) | |
(let ((month-name (buffer-substring (point) (+ 3 (point))))) | |
(setq month (cycle-date-month-name-to-number month-name)))) | |
;; find day string | |
(save-excursion | |
;; month name | |
(backward-char 3) | |
;; day space (5 is just something greater than length of day + space) | |
(when (looking-back (rx (group (= 2 digit)) (not digit)) 5) | |
(setq day (match-string 1)))) | |
;; if pieces found, replace | |
(when (and day year) | |
;; day space month | |
(backward-char (+ 2 1 3)) | |
;; day space month space year | |
(delete-char (+ 2 1 3 1 4)) | |
(insert year "-" month "-" day))))) | |
;; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment