time64.h 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /*!
  2. * Copyright (C) Fraunhofer-Institut for Photonic Microsystems (IPMS)
  3. * Maria-Reiche-Str. 2
  4. * 01109 Dresden
  5. *
  6. * Unauthorized copying of this file, via any medium is strictly prohibited
  7. * Proprietary and confidential
  8. *
  9. * \file time64.h
  10. * \author zimmerli
  11. * \date 2020-01-15
  12. * \brief Time64
  13. *
  14. */
  15. #ifndef TIME64_H_
  16. #define TIME64_H_
  17. #include <stdint.h>
  18. /**
  19. * \brief Time format
  20. */
  21. struct timespec64 {
  22. int64_t tv_sec; //!< seconds, signed
  23. uint32_t tv_nsec; //!< nanoseconds, 0 .. 10^9-1
  24. };
  25. #define MSEC_PER_SEC 1000L
  26. #define USEC_PER_SEC 1000000L
  27. #define NSEC_PER_SEC 1000000000LL
  28. extern void set_normalized_timespec64(struct timespec64 *ts, int64_t sec, int64_t nsec);
  29. static inline struct timespec64 timespec64_add(struct timespec64 lhs, struct timespec64 rhs)
  30. {
  31. struct timespec64 ts_delta;
  32. set_normalized_timespec64(&ts_delta, lhs.tv_sec + rhs.tv_sec, lhs.tv_nsec + rhs.tv_nsec);
  33. return ts_delta;
  34. }
  35. /**
  36. * \brief subtract timespec, normalize result
  37. *
  38. * @param lhs left hand side of operation
  39. * @param rhs right hand side of operation
  40. * @return normalized timespec
  41. */
  42. static inline struct timespec64 timespec64_sub(struct timespec64 lhs, struct timespec64 rhs)
  43. {
  44. struct timespec64 ts_delta;
  45. set_normalized_timespec64(&ts_delta, lhs.tv_sec - rhs.tv_sec, (int64_t)lhs.tv_nsec - (int64_t)rhs.tv_nsec);
  46. return ts_delta;
  47. }
  48. /**
  49. * \brief: Convert timespec to nanoseconds
  50. *
  51. * @param ts pointer to timespec
  52. * @return scalar nanoseconds
  53. */
  54. static inline int64_t timespec64_to_ns(const struct timespec64 *ts)
  55. {
  56. return ((int64_t)ts->tv_sec * NSEC_PER_SEC) + ts->tv_nsec;
  57. }
  58. #endif /* TIME64_H_ */