emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Orm Finnendahl <orm.finnendahl@selma.hfmdk-frankfurt.de>
To: emacs-orgmode@gnu.org
Subject: Re: custom time format extension
Date: Thu, 30 Jul 2020 09:25:09 +0200	[thread overview]
Message-ID: <20200730072509.GA818435@t480s-orm.localdomain> (raw)
In-Reply-To: <20200729201634.GA813784@t480s-orm.localdomain>

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	other threads:[~2020-07-30  7:25 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-07-29 20:16 custom time format extension Orm Finnendahl
2020-07-30  7:25 ` Orm Finnendahl [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200730072509.GA818435@t480s-orm.localdomain \
    --to=orm.finnendahl@selma.hfmdk-frankfurt.de \
    --cc=emacs-orgmode@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).