OpcUaCanOpen
CANopen OPC-UA server
CanModuleUtils.cpp
Go to the documentation of this file.
1 
25 #include <CanModuleUtils.h>
26 #include <errno.h>
27 #include <string.h>
28 #include <chrono>
29 
30 namespace CanModule
31 {
33  {
34  const int max_len = 1000;
35  char buf[max_len];
36 #ifdef _WIN32
37  strerror_s(buf, max_len - 1, errno);
38  return std::string(buf);
39 #else
40  return std::string(strerror_r(errno, buf, max_len - 1));
41 #endif
42  }
43 
44  timeval convertTimepointToTimeval(const std::chrono::system_clock::time_point &t1)
45  {
46  timeval dest;
47  auto millisecs = std::chrono::duration_cast<std::chrono::milliseconds>(t1.time_since_epoch());
48  dest.tv_sec = millisecs.count() / 1000;
49  dest.tv_usec = (millisecs.count() % 1000) * 1000;
50  return dest;
51  }
52 
53  std::chrono::system_clock::time_point convertTimevalToTimepoint(const timeval &t1)
54  {
55  auto d = std::chrono::seconds(t1.tv_sec) + std::chrono::nanoseconds(t1.tv_usec);
56  std::chrono::system_clock::time_point tp(std::chrono::duration_cast<std::chrono::system_clock::duration>(d));
57  return tp;
58  }
59 
60  std::chrono::system_clock::time_point currentTimeTimeval()
61  {
62  // timeval ftTimeStamp;
63 
64  auto now = std::chrono::system_clock::now();
65  /*
66  auto nMicrosecs =
67  std::chrono::duration_cast<std::chrono::microseconds>(
68  now.time_since_epoch()
69  );
70  ftTimeStamp.tv_sec = nMicrosecs.count() / 1000000L;
71  ftTimeStamp.tv_usec = (nMicrosecs.count() % 1000000L);
72  */
73  return now;
74  }
75 
76  double CanModulesubtractTimeval(const std::chrono::system_clock::time_point &t1, const std::chrono::system_clock::time_point &t2)
77  {
78  std::chrono::duration<double> differ = t2 - t1;
79  return differ.count();
80  }
81 }