time64.c 876 B

1234567891011121314151617181920212223242526272829303132333435363738
  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.c
  10. * \author zimmerli
  11. * \date 2020-01-15
  12. * \brief Time64
  13. *
  14. */
  15. #include "time64.h"
  16. /**
  17. * \brief normalize time, resulting nanoseconds in range 0 .. 10^9-1
  18. *
  19. * @param ts pointer to timespec64, destination of normalized time
  20. * @param sec input seconds
  21. * @param nsec input nanoseconds, can be out of range 0 .. 10^9-1
  22. */
  23. void set_normalized_timespec64(struct timespec64 *ts, int64_t sec, int64_t nsec)
  24. {
  25. while (nsec >= NSEC_PER_SEC) {
  26. nsec -= NSEC_PER_SEC;
  27. ++sec;
  28. }
  29. while (nsec < 0) {
  30. nsec += NSEC_PER_SEC;
  31. --sec;
  32. }
  33. ts->tv_sec = sec;
  34. ts->tv_nsec = nsec;
  35. }