2

I want to convert a lot of french dates in date to time

(date-to-time "june 7 2021 12:00:00" )
=>
(24765 60960)

(date-to-time "7 juin 2021 12:00:00" )
=>
Debugger entered--Lisp error: (error "Invalid date: 7 juin 2021 12:00:00")

Should I translate the french date in english ?

2

You can override the global variables parse-time-months and parse-time-weekdays, which are alists from month/weekday names to their respective numbers. These variables are undocumented, but they've been around for over two decades.

(defvar parse-time-months-french
              '(("jan" . 1) ("fév" . 2) ("mar" . 3)
                ("avr" . 4) ("mai" . 5) ("jun" . 6)
                ("jul" . 7) ("aoû" . 8) ("sep" . 9)
                ("oct" . 10) ("nov" . 11) ("déc" . 12)
                ("janvier" . 1) ("février" . 2)
                ("mars" . 3) ("avril" . 4) ("juin" . 6)
                ("juillet" . 7) ("août" . 8)
                ("septembre" . 9) ("octobre" . 10)
                ("novembre" . 11) ("décembre" . 12)))
(defvar parse-time-weekdays-french
                '(("dim" . 0) ("lun" . 1) ("mar" . 2)
                  ("mer" . 3) ("jeu" . 4) ("ven" . 5)
                  ("sam" . 6) ("dimanche" . 0) ("lundi" . 1)
                  ("mardi" . 2) ("mercredi" . 3)
                  ("jeudi" . 4) ("vendredi" . 5)
                  ("samedi" . 6)))

(defun date-to-time-french (date)
  "Like `date-to-time', but require French names."
  (let ((parse-time-months parse-time-months-french)
        (parse-time-weekdays parse-time-weekdays-french))
    (date-to-time date)))

(defun date-to-time-french-english (date)
  "Like `date-to-time', but recognize French names as well as English names."
  (let ((parse-time-months (append parse-time-months-french parse-time-months))
        (parse-time-weekdays (append parse-time-weekdays-french parse-time-weekdays)))
    (date-to-time date)))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.