linux-gpio.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Kent Gibson <warthog618@gmail.com>
To: linux-gpio@vger.kernel.org, bgolaszewski@baylibre.com,
	florian-evers@gmx.de
Cc: Kent Gibson <warthog618@gmail.com>
Subject: [libgpiod][PATCH] bindings: cxx: fix event timestamp calculation for 32bit
Date: Wed,  9 Sep 2020 09:40:03 +0800	[thread overview]
Message-ID: <20200909014003.11487-1-warthog618@gmail.com> (raw)

Use appropriate C++ chrono library functions to convert the event
timestamp from a struct timespec to ::std::chrono::nanoseconds to
ensure correct conversion independent of platform.

Reported-by: Florian Evers <florian-evers@gmx.de>
Signed-off-by: Kent Gibson <warthog618@gmail.com>
---

Florian suggests a C cast before the multiply, but using the C++ library
to do all the work, which removes the 1000000000 as well, seems the more
correct way to go to me.

I don't have a 32bit setup handy to test this, so perhaps Florian could
check if it works for him?

Cheers,
Kent.

 bindings/cxx/line.cpp | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/bindings/cxx/line.cpp b/bindings/cxx/line.cpp
index 11deae6..b71b6db 100644
--- a/bindings/cxx/line.cpp
+++ b/bindings/cxx/line.cpp
@@ -215,8 +215,9 @@ line_event line::make_line_event(const ::gpiod_line_event& event) const noexcept
 	else if (event.event_type == GPIOD_LINE_EVENT_FALLING_EDGE)
 		ret.event_type = line_event::FALLING_EDGE;
 
-	ret.timestamp = ::std::chrono::nanoseconds(
-				event.ts.tv_nsec + (event.ts.tv_sec * 1000000000));
+	ret.timestamp = ::std::chrono::duration_cast<::std::chrono::nanoseconds>(
+					::std::chrono::seconds(event.ts.tv_sec))
+				+ ::std::chrono::nanoseconds(event.ts.tv_nsec);
 
 	ret.source = *this;
 
-- 
2.28.0


             reply	other threads:[~2020-09-09  1:40 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-09-09  1:40 Kent Gibson [this message]
2020-09-14  7:43 ` [libgpiod][PATCH] bindings: cxx: fix event timestamp calculation for 32bit Bartosz Golaszewski

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=20200909014003.11487-1-warthog618@gmail.com \
    --to=warthog618@gmail.com \
    --cc=bgolaszewski@baylibre.com \
    --cc=florian-evers@gmx.de \
    --cc=linux-gpio@vger.kernel.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.
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 NNTP newsgroup(s).