Browse Source

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.
Nicolas Goaziou 5 years ago
parent
commit
1a0f8b5c8b
2 changed files with 65 additions and 0 deletions
  1. 35 0
      contrib/lisp/org-export.el
  2. 30 0
      testing/lisp/test-org-export.el

+ 35 - 0
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

+ 30 - 0
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 ()