bindings: cxx: fix event timestamp calculation for 32bit
PATCH 1/1 bindings: cxx: fix event timestamp calculation for 32bit
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.
---
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(-)
bindings/cxx/line.cpp +3 -2
@@ -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;
Signature · 1 line
2.28.0
Quoted 39 lines · Kent Gibson
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 <[email protected]>Signed-off-by Kent Gibson <[email protected]>---
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(-)bindings/cxx/line.cpp +3
-3@@ -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; --
Quoted 2 lines
2.28.0
Patch applied, thanks!
Bartosz