From: Philipp Stephani <p.stephani2@gmail.com>
To: emacs-devel@gnu.org
Cc: Philipp Stephani <phst@google.com>
Subject: [PATCH] Add a new function to convert a time value to the canonical format.
Date: Tue, 23 Apr 2019 14:46:13 +0200 [thread overview]
Message-ID: <20190423124613.36743-1-phst@google.com> (raw)
Such a function is useful for users that want to do their own time
calculations and conversions.
* src/timefns.c (Ftime_canonicalize): New defun.
* test/src/timefns-tests.el (time-canonicalize): New unit test.
---
src/timefns.c | 12 ++++++++++++
test/src/timefns-tests.el | 10 ++++++++++
2 files changed, 22 insertions(+)
diff --git a/src/timefns.c b/src/timefns.c
index cb953d1b4c..632d6c6d64 100644
--- a/src/timefns.c
+++ b/src/timefns.c
@@ -958,6 +958,17 @@ lisp_seconds_argument (Lisp_Object specified_time)
return t.tv_sec;
}
+DEFUN ("time-canonicalize", Ftime_canonicalize, Stime_canonicalize, 1, 1, NULL,
+ doc: /* Return canonical form of TIME as pair (TICKS . HZ).
+TIME can be any time value. See Info node `(elisp)Time of Day'. The
+return value is a pair (TICKS . HZ). TICKS / HZ is the time value in
+seconds. */)
+ (Lisp_Object time)
+{
+ struct lisp_time lt = lisp_time_struct (time, NULL);
+ return Fcons (lt.ticks, lt.hz);
+}
+
/* Given Lisp operands A and B, add their values, and return the
result as a Lisp timestamp that is in (TICKS . HZ) form if either A
or B are in that form, (HI LO US PS) form otherwise. Subtract
@@ -1764,6 +1775,7 @@ syms_of_timefns (void)
DEFSYM (Qencode_time, "encode-time");
defsubr (&Scurrent_time);
+ defsubr (&Stime_canonicalize);
defsubr (&Stime_add);
defsubr (&Stime_subtract);
defsubr (&Stime_less_p);
diff --git a/test/src/timefns-tests.el b/test/src/timefns-tests.el
index 5c858ef3bd..e681b17a3c 100644
--- a/test/src/timefns-tests.el
+++ b/test/src/timefns-tests.el
@@ -17,6 +17,7 @@
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <https://www.gnu.org/licenses/>.
+(require 'cl-lib)
(require 'ert)
;;; Check format-time-string and decode-time with various TZ settings.
@@ -142,3 +143,12 @@ timefns-tests--have-leap-seconds
(< 0.99 (/ x y) 1.01)
(< 0.99 (/ (- (float-time a)) (float-time b))
1.01))))))))
+
+(ert-deftest time-canonicalize ()
+ (dolist (time '(123.45 (123 . 1000000) (1 2) (1 2 3) (1 2 3 4)))
+ (ert-info ((prin1-to-string `(time-canonicalize ,time)))
+ (let ((result (time-canonicalize time)))
+ (should (consp result))
+ (should (integerp (car result)))
+ (should (integerp (cdr result)))
+ (should (cl-plusp (cdr result)))))))
--
2.20.1 (Apple Git-117)
next reply other threads:[~2019-04-23 12:46 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-04-23 12:46 Philipp Stephani [this message]
2019-04-23 13:11 ` [PATCH] Add a new function to convert a time value to the canonical format Eli Zaretskii
2019-04-23 13:13 ` Philipp Stephani
2019-04-23 13:21 ` Eli Zaretskii
2019-04-23 16:20 ` Paul Eggert
2019-04-23 17:11 ` Eli Zaretskii
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
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20190423124613.36743-1-phst@google.com \
--to=p.stephani2@gmail.com \
--cc=emacs-devel@gnu.org \
--cc=phst@google.com \
/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 external index
https://git.savannah.gnu.org/cgit/emacs.git
https://git.savannah.gnu.org/cgit/emacs/org-mode.git
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.