0

I have a file with the date of birthday of my friends, I use this to remember me the date if birthday...But I have a problem: the date are set in the US way (month-days-year). I would like to change it to the Italian way (day-month-year) because on the init file I've set to use that date format.

EDIT: the file looks like this:

 * compleanni
%%(diary-anniversary 01 08 1965) compleanno Fabio (%d years old con diary32) 
%%(diary-anniversary 01 12 1977) compleanno Barbara (%d anni compiuti) 
%%(diary-anniversary 01 16 1963) compleanno Carla Ricci (%d anni compiuti) 
%%(diary-anniversary 01 27 1957) compleanno Luca (%d anni compiuti) 
...

with about 25 lines like these.

Is there a way to do this automatically, or do I have to change it one for one?

3
  • 1
    Please show us the input file. Your comment seems to indicate that you are using diary-anniversary, but there is no mention of that above.
    – NickD
    Commented Aug 17, 2023 at 19:09
  • @HighPerformanceMark edited to put the blame where it belongs :-) See the sordid details in Date format by country
    – NickD
    Commented Aug 18, 2023 at 20:19
  • BTW, Org mode uses ISO 8601 (YMD) exclusively, no matter what format you have set in your systems or your init file.
    – NickD
    Commented Aug 18, 2023 at 20:23

2 Answers 2

1

You did not tell us the exact format of the dates and the file, but you could try loading the following command (the following command has been updated to work for the date format you mention in your comment)

(defun swap-day-month ()
  (interactive)
  (goto-char (point-min))
  (let ((sep " ")
        (num "\\([0-9]+\\)"))
    (while (re-search-forward (concat num sep num sep num) nil t)
      (replace-match (concat "\\2" sep "\\1" sep "\\3")))))

and after loading it do M-x swap-day-month within your file(buffer).

Beware that it swaps the first two sub-patterns in any pattern that matches the regexp.

8
  • "You did not tell us the exact format of the date". exact! I was thinking that few minute ago. this is the format: %%(diary-anniversary 01 08 1965) that mean: 8 of January 1965 where do I have to insert this commans? On init.el?
    – RenatoP
    Commented Aug 17, 2023 at 17:23
  • 1
    If you are using diary-anniversary as you seem to be saying in the comment, you can cannot change the order without breaking things: the function expects the month first, then the day, then the year - do C-h f diary-anniversary to check its signature. Please specify (in your question!) what the input file looks like. I misunderstood the question the same way that @dalanicolai did, which indicates that the question needs to be clarified.
    – NickD
    Commented Aug 17, 2023 at 19:06
  • @dalanicolai:I've inserted it on my init.el, then opened the file and made M-sw (I renamed the function) but nothing happen. What I made wrong?
    – RenatoP
    Commented Aug 18, 2023 at 10:18
  • @RenatoP Have you read the comment by @NickD? Also, what is M-sw? Probably nothing happened because the function (before I updated the answer) expected a different date format. Although you could try again with the function of my updated answer, you better read and understand @NickD's comment first (i.e. probably you don't want to use the above function). B.t.w. you should add the information of your first comment to your answer. Thanks! Commented Aug 18, 2023 at 10:33
  • 1
    PLEASE show us the input file! WIthout that, we are left guessing what you started with. @dalanicolai made a reasonable guess but it is probably wrong so the answer has no hope of working. BUT we won't know until you clarify the question.
    – NickD
    Commented Aug 18, 2023 at 12:29
1

So you write your own function that takes day, month, and (optional) year arguments and calls the original function with the arguments in the order that IT expects, and add it to your init file:

(defun diario-anniversario (day month &optional year mark)
   (diary-anniversary month day year mark))

It just calls the original function with the arguments in the correct order for that function. You can then change your anniversary file to call this function with day, month and year arguments in that order:

 * compleanni
%%(diario-anniversario 08 01 1965) compleanno Fabio (%d years old con diary32) 
%%(diario-anniversario 12 01 1977) compleanno Barbara (%d anni compiuti) 
%%(diario-anniversario 16 01 1963) 0compleanno Carla Ricci (%d anni compiuti) 
%%(diario-anniversario 27 01 1957) compleanno Luca (%d anni compiuti) 

You will have to change the name of the function and the order of the arguments by hand.

Or you can use M-x replace-string RET diary-anniversary RET diario-anniversario RET to do the name change globally. You can use M-x replace-regexp RET \([0-9]\{2\}\) \([0-9]\{2\}\) RET \2 \1 RET to change the order of the month and day fields. The regexp matches a sequence of 2 numeric digits, followed by a space, followed by another sequence of 2 numeric digits; the replacement consists of the second match followed by the first match. See Regular expressions in the Emacs manual.

It might be simpler for you to change the order of the arguments manually, by positioning the cursor in-between the month and date fields and then typing M-t to exchange the two fields. Then position the cursor in-between the month and day fields on the next line and do M-t again. Repeat as necessary.

If you get tired of doing that, you can use a keyboard macro to automate the sequence.

3
  • thank you for your help. finally, I've used the M-t command. I could have used also the "manual way (there are about 30 lines...nothing scary...) but I would see like regular expression solve this prob. thank you to all that have helped me Renato
    – RenatoP
    Commented Aug 19, 2023 at 13:50
  • I would recommend that you make a copy of the file and then experiment with the regular expression I provided using the copy so that if something gets messed up, you can always get another copy and try again. But for a dozen or two dozen lines, doing M-t manually is indeed quite possible. When a task is small and will only be done once, it is probably not worth automating. But you can always use it as practice for the next task which might be bigger (e.g, the file is 1000 lines long, or you have to do something to 1000 files - or both): in such cases, automation becomes necessary.
    – NickD
    Commented Aug 19, 2023 at 14:21
  • ... or you can use the function that @dalanicolai provides in his answer to do the swapping of the first and second numbers.
    – NickD
    Commented Aug 19, 2023 at 14:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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