time.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * Copyright (C) 2013 Ole Reinhardt <ole.reinhardt@embedded-it.de>
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. Neither the name of the copyright holders nor the names of
  16. * contributors may be used to endorse or promote products derived
  17. * from this software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  22. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  23. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  24. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  25. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  26. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  27. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  28. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  29. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  30. * SUCH DAMAGE.
  31. *
  32. * For additional information see http://www.ethernut.de/
  33. *
  34. */
  35. #ifndef _SYS_TIME_H_
  36. #define _SYS_TIME_H_
  37. #include <cfg/os.h>
  38. #include <time.h>
  39. /*!
  40. * \addtogroup xgCrtTime
  41. * @{
  42. */
  43. /*!
  44. * \brief Timezone structure type.
  45. *
  46. * \note Obsolete and should not be used, but is needed for historic reasons
  47. *
  48. */
  49. struct timezone {
  50. int tz_minuteswest; /* Minutes west of GMT. */
  51. int tz_dsttime; /* Non zero in case of dailight saving time (summer time) */
  52. };
  53. /*!
  54. * \brief structure to store a timestamp with microseconds resolution
  55. */
  56. struct timeval {
  57. int32_t tv_sec; /* seconds */
  58. int32_t tv_usec; /* microseconds */
  59. };
  60. extern int gettimeofday (struct timeval *tv, struct timezone *tz);
  61. extern int settimeofday (struct timeval *tv, struct timezone *tz);
  62. /* Convenience macros to manipulate or check timevals */
  63. /*!
  64. * \brief Check if a timeval value is != 0
  65. *
  66. * \param tvp Pointer to timeval struct
  67. *
  68. * \return true is timeval is != 0, false if not
  69. *
  70. */
  71. #define timerisset(tvp) ((tvp)->tv_sec || (tvp)->tv_usec)
  72. /*!
  73. * \brief Clear a timeval (set both entries to 0)
  74. *
  75. * \param tvp Pointer to timeval struct
  76. *
  77. */
  78. #define timerclear(tvp) ((tvp)->tv_sec = (tvp)->tv_usec = 0)
  79. /*!
  80. * \brief Compare two timevals
  81. *
  82. * \note does not work for >= or <=
  83. *
  84. * \param a Pointer to first timeval struct
  85. * \param b Pointer to second timeval struct
  86. * \param cmp Operand to use for comparision
  87. *
  88. * \returns comparision result
  89. */
  90. #define timercmp(a, b, cmp) \
  91. (((a)->tv_sec == (b)->tv_sec) ? \
  92. ((a)->tv_usec cmp (b)->tv_usec) : \
  93. ((a)->tv_sec cmp (b)->tv_sec))
  94. /*!
  95. * \brief Add two timevals and return as res
  96. *
  97. * \param a Pointer to first timeval struct
  98. * \param b Pointer to second timeval struct
  99. * \param res Pointer to result timeval struct
  100. */
  101. #define timeradd(a, b, res) \
  102. { \
  103. (res)->tv_sec = (a)->tv_sec + (b)->tv_sec; \
  104. (res)->tv_usec = (a)->tv_usec + (b)->tv_usec; \
  105. if ((res)->tv_usec >= 1000000) { \
  106. (res)->tv_sec++; \
  107. (res)->tv_usec -= 1000000; \
  108. } \
  109. }
  110. /*!
  111. * \brief Substract two timevals and return as res
  112. *
  113. * \param a Pointer to first timeval struct
  114. * \param b Pointer to second timeval struct
  115. * \param res Pointer to result timeval struct
  116. */
  117. #define timersub(a, b, res) \
  118. { \
  119. (res)->tv_sec = (a)->tv_sec - (b)->tv_sec; \
  120. (res)->tv_usec = (a)->tv_usec - (b)->tv_usec; \
  121. if ((res)->tv_usec < 0) { \
  122. (res)->tv_sec--; \
  123. (res)->tv_usec += 1000000; \
  124. } \
  125. }
  126. /*@}*/
  127. #endif