diff mbox series

[libgpiod] bindings: cxx: fix event timestamp calculation for 32bit

Message ID 20200909014003.11487-1-warthog618@gmail.com
State New
Headers show
Series [libgpiod] bindings: cxx: fix event timestamp calculation for 32bit | expand

Commit Message

Kent Gibson Sept. 9, 2020, 1:40 a.m. UTC
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(-)

Comments

Bartosz Golaszewski Sept. 14, 2020, 7:43 a.m. UTC | #1
On Wed, Sep 9, 2020 at 3:40 AM Kent Gibson <warthog618@gmail.com> wrote:
>
> 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
>

Patch applied, thanks!

Bartosz
diff mbox series

Patch

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;