time.h 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /*
  2. *
  3. * Embedded Linux library
  4. *
  5. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. */
  22. #ifndef __ELL_TIME_H
  23. #define __ELL_TIME_H
  24. #ifdef __cplusplus
  25. extern "C" {
  26. #endif
  27. #include <stdint.h>
  28. #include <stdbool.h>
  29. #define L_USEC_PER_SEC 1000000ULL
  30. #define L_MSEC_PER_SEC 1000ULL
  31. #define L_USEC_PER_MSEC 1000ULL
  32. #define L_NSEC_PER_SEC 1000000000ULL
  33. #define L_NSEC_PER_MSEC 1000000ULL
  34. #define L_NSEC_PER_USEC 1000ULL
  35. #define L_TIME_INVALID ((uint64_t) -1)
  36. uint64_t l_time_now(void);
  37. static inline bool l_time_after(uint64_t a, uint64_t b)
  38. {
  39. return a > b;
  40. }
  41. static inline bool l_time_before(uint64_t a, uint64_t b)
  42. {
  43. return l_time_after(b, a);
  44. }
  45. static inline uint64_t l_time_offset(uint64_t time, uint64_t offset)
  46. {
  47. /* check overflow */
  48. if (offset > UINT64_MAX - time)
  49. return UINT64_MAX;
  50. return time + offset;
  51. }
  52. static inline uint64_t l_time_diff(uint64_t a, uint64_t b)
  53. {
  54. return (a < b) ? b - a : a - b;
  55. }
  56. static inline uint64_t l_time_to_secs(uint64_t time)
  57. {
  58. return time / L_USEC_PER_SEC;
  59. }
  60. static inline uint64_t l_time_to_msecs(uint64_t time)
  61. {
  62. return time / L_USEC_PER_MSEC;
  63. }
  64. #ifdef __cplusplus
  65. }
  66. #endif
  67. #endif /* __ELL_TIME_H */