emacs-orgmode
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: custom time format extension


From: Orm Finnendahl
Subject: Re: custom time format extension
Date: Thu, 30 Jul 2020 09:25:09 +0200

Hi,

 for the record, here's the code if anyone ever comes across this:

(defun same-day-p (timestamp)
  "check if start and end of timestamp are on the same day."
  (equal
   (mapcar (lambda (prop) (or (org-element-property prop timestamp) 0))
           '(:day-start :month-start :year-start))
   (mapcar (lambda (prop) (or (org-element-property prop timestamp) 0))
           '(:day-end :month-end :year-end))))

(defun org-timestamp-translate (timestamp &optional boundary)
  "Translate TIMESTAMP object to custom format.

Format string is defined in `org-time-stamp-custom-formats',
which see.

When optional argument BOUNDARY is non-nil, it is either the
symbol `start' or `end'.  In this case, only translate the
starting or ending part of TIMESTAMP if it is a date or time
range.  Otherwise, translate both parts.

Return timestamp as-is if `org-display-custom-times' is nil or if
it has a `diary' type."
  (let ((type (org-element-property :type timestamp)))
    (if (or (not org-display-custom-times) (eq type 'diary))
        (org-element-interpret-data timestamp)
      (let ((same-day (same-day-p timestamp))
            (fmt (funcall (if (org-timestamp-has-time-p timestamp) #'cdr #'car)
                          org-time-stamp-custom-formats)))
        (if (and (not boundary) (memq type '(active-range inactive-range)))
            (concat (org-timestamp-format timestamp (if (consp fmt)
                                                        (if same-day (cadr fmt) 
(car fmt))
                                                      fmt))
                    "--"
                    (org-timestamp-format timestamp (if (consp fmt)
                                                        (if same-day (caddr 
fmt) (car fmt))
                                                      fmt)
                                          t))
          (org-timestamp-format timestamp fmt (eq boundary 'end)))))))

As mentioned before it should be fully backwards compatible.

--
Orm

Am Mittwoch, den 29. Juli 2020 um 22:16:34 Uhr (+0200) schrieb Orm Finnendahl:
> Hi,
> 
>  when exporting timestamps it always bothered me that timestamps with
> start and end time on the same day got exported with a full date for
> the start and end times separated by a dash.
> 
> After thinking about this for some time I implemented a method today,
> which combines the advantage of being utmost flexible while
> maintaining full backwards compatibility.
> 
> This is how it works: The cdr of org-time-stamp-custom-formats
> normally contains a format string for formatting timestamps with
> start/end time. When supplying a list of three strings instead of the
> single format string to the cdr of org-time-stamp-custom-formats, the
> first, second and third elements of that list are interpreted as
> follows:
> 
> The first element is the full format in case start and end of the
> timestamp are not on the same day. The second element of the list is
> the format string for the start time of the timestamp. The third
> element of the list is a format string for the end time of the
> timestamp. (BTW: Supplying a list as second element of a dotted list
> is equivalent to supplying a list with four strings to
> org-time-stamp-custom-formats)
> 
> Below the mail is a short example for german date strings.
> 
> In case somebody is interested, I can provide the code off list. If
> any of the maintainers is reading on this list and thinks it should
> get revised and maybe adopted for orgmode, please let me know how to
> issue pull requests for the code. I tried to be minimally invasive ;-)
> 
> --
> Orm
> 
> -----------
> Example:
> 
> (org-time-stamp-custom-formats '("%a %d.%m.%Y" . ("%a %d.%m.%Y %H:%M Uhr"
>                                                   "%a %d.%m.%Y %H:%M"
>                                                   "%H:%M Uhr")))
> 
> Formatting
> 
> <2020-11-21 Sa>  -> Sa 21.11.2020
> 
> <2020-11-21 Sa 10:00-18:00>  -> Sa 21.11.2020 10:00--18:00 Uhr
> 
> 
> <2020-11-21 Sa 10:00>--<2020-11-22 So 18:00> -> Sa 21.11.2020 10:00 Uhr--So 
> 22.11.2020 18:00 Uhr
> 



reply via email to

[Prev in Thread] Current Thread [Next in Thread]