From mboxrd@z Thu Jan 1 00:00:00 1970
Path: news.gmane.org!.POSTED.blaine.gmane.org!not-for-mail
From: Philipp Stephani
Newsgroups: gmane.emacs.devel
Subject: [PATCH] Add a new function to convert a time value to the canonical
format.
Date: Tue, 23 Apr 2019 14:46:13 +0200
Message-ID: <20190423124613.36743-1-phst@google.com>
Mime-Version: 1.0
Content-Transfer-Encoding: 8bit
Injection-Info: blaine.gmane.org; posting-host="blaine.gmane.org:195.159.176.226";
logging-data="138654"; mail-complaints-to="usenet@blaine.gmane.org"
Cc: Philipp Stephani
To: emacs-devel@gnu.org
Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Tue Apr 23 14:50:58 2019
Return-path:
Envelope-to: ged-emacs-devel@m.gmane.org
Original-Received: from lists.gnu.org ([209.51.188.17])
by blaine.gmane.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:256)
(Exim 4.89)
(envelope-from )
id 1hIusz-000Zta-86
for ged-emacs-devel@m.gmane.org; Tue, 23 Apr 2019 14:50:57 +0200
Original-Received: from localhost ([127.0.0.1]:53317 helo=lists.gnu.org)
by lists.gnu.org with esmtp (Exim 4.71)
(envelope-from )
id 1hIusy-0000iC-0e
for ged-emacs-devel@m.gmane.org; Tue, 23 Apr 2019 08:50:56 -0400
Original-Received: from eggs.gnu.org ([209.51.188.92]:53426)
by lists.gnu.org with esmtp (Exim 4.71)
(envelope-from ) id 1hIusr-0000UP-Gy
for emacs-devel@gnu.org; Tue, 23 Apr 2019 08:50:50 -0400
Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71)
(envelope-from ) id 1hIuoc-0006Gq-1c
for emacs-devel@gnu.org; Tue, 23 Apr 2019 08:46:27 -0400
Original-Received: from mail-wr1-x435.google.com ([2a00:1450:4864:20::435]:44327)
by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16)
(Exim 4.71) (envelope-from )
id 1hIuob-0006GO-Ro
for emacs-devel@gnu.org; Tue, 23 Apr 2019 08:46:25 -0400
Original-Received: by mail-wr1-x435.google.com with SMTP id c5so11183851wrs.11
for ; Tue, 23 Apr 2019 05:46:25 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025;
h=from:to:cc:subject:date:message-id:mime-version
:content-transfer-encoding;
bh=dmITZupSuPHXomO4RMWxbkdddmU28zVt3xMQQ4C534Q=;
b=L00e31lQshhFLBWmJAK0eBTmxJJ03uhD+iBopqHexWsLRYtxMY07JckbSF9MnRhcHP
pAU6ByHjwtCAMndv2BoeBUOz1IutcA7iH8/cl5HHYVdMG8NOnulPJX3icOUJuVlDZyP0
h5WjFCxxEdVst/MC+V64AJ0H7kcW9FRoWUBlhmJ0qauaO5i0h6yJTY8U/0UXpQrcOp1I
fnF4x2wTVB6QkDPKWZdCuq6xOvT4oyOGbckxTHNkXHSpmJxYM05cDqiIvnByMSfEijCH
jgX4y6Hlcn8c0iZJZLgRfGk6g3Ml/GP2Sgqng6RcGdSc8/x+bYCnWRG0RlabRHnK541P
GZbQ==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=1e100.net; s=20161025;
h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version
:content-transfer-encoding;
bh=dmITZupSuPHXomO4RMWxbkdddmU28zVt3xMQQ4C534Q=;
b=Xzs7MB5Ml3zDbIb27pMqofp13skr3hDyAin7yNxCZ5k0VvYRUG7p6CS3VZ6A0+M7BU
2dxCqApsRpw5hmxq3cKazal/niIjm/ebUkeWUJRP147+l+zyUQDK4Bfl/P+pg8ml54nr
kkOrwkAhvmdHfTUMVo0jNuFi0SPeMXVVm/7lP5hK7uQVZBh8qK54YSFG4wrWG47DRJvW
Zah/3ddloTmU7w0l0c4/q3jZlOqlLempmXxEU4UhnKfDdQ9HTVDFH5iRtxkNfH9yhvMC
w8AG1nVm8dJPq3EOvOr6VkAgJTCfBjSuZoes2iMGy6X/jIB2hduynzrn0xaT2kunXN9g
fVtA==
X-Gm-Message-State: APjAAAXhGdxnOyvObLrebkgBSw2HD6Nuld/sHo0FMwzwvrmnrON8f0JZ
3pn1A4d2BY6O43VR/Ty9q3dAT6Qr
X-Google-Smtp-Source: APXvYqyivzezCFAeNHRq4zeGa9k8b5F/goRAbZzXgx9bU39uzOzl6T9A6vA/MjbbQPIaE8SklI9Zcg==
X-Received: by 2002:a5d:4087:: with SMTP id o7mr17923586wrp.9.1556023583706;
Tue, 23 Apr 2019 05:46:23 -0700 (PDT)
Original-Received: from p.fritz.box (p5B13F5EA.dip0.t-ipconnect.de. [91.19.245.234])
by smtp.gmail.com with ESMTPSA id
r9sm14236175wmh.38.2019.04.23.05.46.22
(version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128);
Tue, 23 Apr 2019 05:46:22 -0700 (PDT)
X-Google-Original-From: Philipp Stephani
X-Mailer: git-send-email 2.20.1 (Apple Git-117)
X-detected-operating-system: by eggs.gnu.org: Genre and OS details not
recognized.
X-Received-From: 2a00:1450:4864:20::435
X-BeenThere: emacs-devel@gnu.org
X-Mailman-Version: 2.1.21
Precedence: list
List-Id: "Emacs development discussions."
List-Unsubscribe: ,
List-Archive:
List-Post:
List-Help:
List-Subscribe: ,
Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org
Original-Sender: "Emacs-devel"
Xref: news.gmane.org gmane.emacs.devel:235814
Archived-At:
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 .
+(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)