From: Nicolas Goaziou Date: Mon, 19 Nov 2012 20:38:16 +0000 (+0100) Subject: org-export: Add tools for timestamps X-Git-Tag: release_8.0-pre~805 X-Git-Url: https://repo.or.cz/w/org-mode.git/commitdiff_plain/1a0f8b5c8b49ba59d21fabebf962c47852a5a14b org-export: Add tools for timestamps * contrib/lisp/org-export.el (org-export-timestamp-has-time-p, org-export-format-timestamp): New functions. * testing/lisp/test-org-export.el: Add tests. --- diff --git a/contrib/lisp/org-export.el b/contrib/lisp/org-export.el index 59f3f60ae..1ae54728a 100644 --- a/contrib/lisp/org-export.el +++ b/contrib/lisp/org-export.el @@ -4278,6 +4278,41 @@ Return a list of src-block elements with a caption." (org-export-collect-elements 'src-block info)) +;;;; For Timestamps +;; +;; `org-export-timestamp-has-time-p' is a predicate to know if hours +;; and minutes are defined in a given timestamp. +;; +;; `org-export-format-timestamp' allows to format a timestamp object +;; with an arbitrary format string. + +(defun org-export-timestamp-has-time-p (timestamp) + "Non-nil when TIMESTAMP has a time specified." + (org-element-property :hour-start timestamp)) + +(defun org-export-format-timestamp (timestamp format &optional end utc) + "Format a TIMESTAMP element into a string. + +FORMAT is a format specifier to be passed to +`format-time-string'. + +When optional argument END is non-nil, use end of date-range or +time-range, if possible. + +When optional argument UTC is non-nil, time will be expressed as +Universal Time." + (format-time-string + format + (apply 'encode-time + (cons 0 + (mapcar + (lambda (prop) (or (org-element-property prop timestamp) 0)) + (if end '(:minute-end :hour-end :day-end :month-end :year-end) + '(:minute-start :hour-start :day-start :month-start + :year-start))))) + utc)) + + ;;;; Smart Quotes ;; ;; The main function for the smart quotes sub-system is diff --git a/testing/lisp/test-org-export.el b/testing/lisp/test-org-export.el index e049406c3..18b01039b 100644 --- a/testing/lisp/test-org-export.el +++ b/testing/lisp/test-org-export.el @@ -2021,6 +2021,36 @@ Another text. (ref:text) +;;; Timestamps + +(ert-deftest test-org-export/timestamp-has-time-p () + "Test `org-export-timestamp-has-time-p' specifications." + ;; With time. + (should + (org-test-with-temp-text "<2012-03-29 Thu 16:40>" + (org-export-timestamp-has-time-p (org-element-context)))) + ;; Without time. + (should-not + (org-test-with-temp-text "<2012-03-29 Thu>" + (org-export-timestamp-has-time-p (org-element-context))))) + +(ert-deftest test-org-export/format-timestamp () + "Test `org-export-format-timestamp' specifications." + ;; Regular test. + (should + (equal + "2012-03-29 16:40" + (org-test-with-temp-text "<2012-03-29 Thu 16:40>" + (org-export-format-timestamp (org-element-context) "%Y-%m-%d %R")))) + ;; Range end. + (should + (equal + "2012-03-29" + (org-test-with-temp-text "[2011-07-14 Thu]--[2012-03-29 Thu]" + (org-export-format-timestamp (org-element-context) "%Y-%m-%d" t))))) + + + ;;; Topology (ert-deftest test-org-export/get-next-element ()