3

I have an org document:

#+Title: Birthdays of The Beatles

John Lennon was born on <1940-10-09 Mon>.

Each member of the Beatles had a birthday.

| Beatle          | Birthday         |
|-----------------+------------------|
| John Lennon     | <1940-10-09 Mon> |
| Paul McCartney  | <1942-06-18 Thu> |
| George Harrison | <1943-02-25 Thu> |
| Ringo Starr     | <1940-07-07 Sun> |

which exports to ascii as

               __________________________

            BIRTHDAYS OF THE BEATLES

                   Me
               __________________________


John Lennon was born on <1940-10-09 Wed>.

Each member of the Beatles had a birthday.

 Beatle           Birthday         
-----------------------------------
 John Lennon      <1940-10-09 Wed> 
 Paul McCartney   <1942-06-18 Thu> 
 George Harrison  <1943-02-25 Thu> 
 Ringo Starr      <1940-07-07 Sun> 

I want to alter the formats of the dates so that the dates in the body and the dates in the table are different:

               __________________________

            BIRTHDAYS OF THE BEATLES

                   Me
               __________________________


John Lennon was born on <Wed Oct-09 1940>.

Each member of the Beatles had a birthday.

 Beatle           Birthday         
-----------------------------------
 John Lennon      <Oct-09>
 Paul McCartney   <Jun-18>
 George Harrison  <Feb-25>
 Ringo Starr      <Jul-07>

Is this possible?

2
+25

What happens to timestamps when you export to ASCII is determined by the function org-ascii-timestamp (likewise org-latex-timestamp for Latex export and so on). You just have to redefine that to make them export to whatever you like. Here we use org-read-date to convert the timestamp to an internal time and format-time-string to format the time in whatever way we like:

(defun org-ascii-timestamp (timestamp _contents info)
  (format-time-string
   "<%b-%d>"
   (org-read-date nil t (org-timestamp-translate timestamp))))

Unfortunately, this will alter the timestamps for /all/ ASCII exports, not just this document. To make it truly document local, you would have to redefine the export function to something like

(defun org-ascii-timestamp (timestamp contents info)
    (funcall org-ascii-timestamp-function (timestamp contents info))

and set org-ascii-timestamp-function to be a function with the current definition of org-ascii-timestamp. Then you could use #+BIND to change the definition locally. This is a bit of a hack and also affects the whole document.

What we really want is to set a property (say TIMESTAMP_FMT) and have it apply only to that subtree. It is somewhat difficult to lookup properties during export, but it can be done:

(defvar org-ascii-timestamp-format "<%Y-%m-%d %a>")

(defun org-ascii-timestamp (timestamp _contents info)
  (let ((org-ascii-timestamp-format
         (or (org-entry-get (plist-get (car (cdr timestamp)) :begin)
                            "TIMESTAMP_FMT")
             org-ascii-timestamp-format)))
    (format-time-string
     org-ascii-timestamp-format
     (org-read-date nil t (org-timestamp-translate timestamp)))))

This makes the timestamp format local to a tree (it can be inherited or not depending on how you set org-use-property-inheritance). I don't know a way to make a property local to a table.

  • Wouldn't that change the timestamp in the text as well as the ones in the table? – NickD May 23 '17 at 0:13
  • Also in "modern" org-mode (>=9.0) the org-timestamp-translate function uses the variable org-time-stamp-custom-formats, so you don't have to redefine the org-ascii-timestamp function; just the variable (I think, but I did not test). But it still would affect both the text and table timestamps. – NickD May 23 '17 at 0:21
  • Yes changing the function changes it everywhere. I've added in a function that changes it in just a subtree. – erikstokes May 23 '17 at 1:12
  • @Nick I hadn't been aware of org-time-stamp-custom-formats. It almost does the right thing, but I don't see a way of modifying it locally short of redefining the function as above. – erikstokes May 24 '17 at 0:03

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.