time.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. /*++
  2. Copyright (c) 2013 Minoca Corp.
  3. This file is licensed under the terms of the GNU Lesser General Public
  4. License version 3. Alternative licensing terms are available. Contact
  5. info@minocacorp.com for details.
  6. Module Name:
  7. time.h
  8. Abstract:
  9. This header contains time related definitions, and the select function for
  10. whatever reason.
  11. Author:
  12. Evan Green 3-May-2013
  13. --*/
  14. #ifndef _SYS_TIME_H
  15. #define _SYS_TIME_H
  16. //
  17. // ------------------------------------------------------------------- Includes
  18. //
  19. #include <sys/select.h>
  20. //
  21. // --------------------------------------------------------------------- Macros
  22. //
  23. //
  24. // These convenience macros operate on struct timeval pointers.
  25. //
  26. #define timerisset(_TimeVal) \
  27. (((_TimeVal)->tv_sec != 0) || ((_TimeVal)->tv_usec != 0))
  28. #define timerclear(_TimeVal) \
  29. ((_TimeVal)->tv_sec = (_TimeVal)->tv_usec = 0)
  30. //
  31. // This macro compares two timeval structures. Operator is an actual C
  32. // operator, like <, >, or ==. The operators <= and >= do not work properly
  33. // with this macro.
  34. //
  35. #define timercmp(_TimeVal1, _TimeVal2, _Operator) \
  36. (((_TimeVal1)->tv_sec == (_TimeVal2)->tv_sec) ? \
  37. ((_TimeVal1)->tv_usec _Operator (_TimeVal2)->tv_usec) : \
  38. ((_TimeVal1)->tv_sec _Operator (_TimeVal2)->tv_sec))
  39. #define timeradd(_TimeVal1, _TimeVal2, _Result) \
  40. do { \
  41. (_Result)->tv_sec = (_TimeVal1)->tv_sec + (_TimeVal2)->tv_sec; \
  42. (_Result)->tv_usec = (_TimeVal1)->tv_usec + (_TimeVal2)->tv_usec; \
  43. while ((_Result)->tv_usec >= 1000000) { \
  44. (_Result)->tv_sec += 1; \
  45. (_Result)->tv_usec -= 1000000; \
  46. } \
  47. \
  48. } while (0)
  49. #define timersub(_TimeVal1, _TimeVal2, _Result) \
  50. do { \
  51. (_Result)->tv_sec = (_TimeVal1)->tv_sec - (_TimeVal2)->tv_sec; \
  52. (_Result)->tv_usec = (_TimeVal1)->tv_usec - (_TimeVal2)->tv_usec; \
  53. while ((_Result)->tv_usec < 0) { \
  54. (_Result)->tv_sec -= 1; \
  55. (_Result)->tv_usec += 1000000; \
  56. } \
  57. \
  58. } while (0)
  59. //
  60. // ---------------------------------------------------------------- Definitions
  61. //
  62. #ifdef __cplusplus
  63. extern "C" {
  64. #endif
  65. //
  66. // This interval timer type decrements in wall clock time.
  67. //
  68. #define ITIMER_REAL 0
  69. //
  70. // This interval timer type decrements in proportion to the CPU time the
  71. // process gets.
  72. //
  73. #define ITIMER_VIRTUAL 1
  74. //
  75. // This interval timer type decrements in proportion to the user and kernel
  76. // CPU time the process gets.
  77. //
  78. #define ITIMER_PROF 2
  79. //
  80. // ------------------------------------------------------ Data Type Definitions
  81. //
  82. /*++
  83. Structure Description:
  84. This structure defines the type for an interval timer.
  85. Members:
  86. it_value - Stores the relative due time from now of the timer.
  87. it_interval - Stores the periodic interval of the timer.
  88. --*/
  89. struct itimerval {
  90. struct timeval it_value;
  91. struct timeval it_interval;
  92. };
  93. //
  94. // -------------------------------------------------------------------- Globals
  95. //
  96. //
  97. // -------------------------------------------------------- Function Prototypes
  98. //
  99. LIBC_API
  100. int
  101. gettimeofday (
  102. struct timeval *Time,
  103. void *UnusedParameter
  104. );
  105. /*++
  106. Routine Description:
  107. This routine returns the current time in terms of seconds from the Epoch,
  108. midnight on January 1, 1970 GMT. The timezone is always GMT.
  109. Arguments:
  110. Time - Supplies a pointer where the result will be returned.
  111. UnusedParameter - Supplies an unused parameter provided for legacy reasons.
  112. It used to store the current time zone.
  113. Return Value:
  114. 0 on success.
  115. -1 on failure, and errno will be set to contain more information.
  116. --*/
  117. LIBC_API
  118. int
  119. settimeofday (
  120. const struct timeval *NewTime,
  121. void *UnusedParameter
  122. );
  123. /*++
  124. Routine Description:
  125. This routine sets the current time in terms of seconds from the Epoch,
  126. midnight on January 1, 1970 GMT. The timezone is always GMT. The caller
  127. must have appropriate privileges to set the system time.
  128. Arguments:
  129. NewTime - Supplies a pointer where the result will be returned.
  130. UnusedParameter - Supplies an unused parameter provided for legacy reasons.
  131. It used to provide the current time zone.
  132. Return Value:
  133. 0 on success.
  134. -1 on failure, and errno will be set to contain more information.
  135. --*/
  136. LIBC_API
  137. int
  138. getitimer (
  139. int Type,
  140. struct itimerval *CurrentValue
  141. );
  142. /*++
  143. Routine Description:
  144. This routine gets the current value of one of the interval timers.
  145. Arguments:
  146. Type - Supplies the timer type to get information for. See ITIMER_*
  147. definitions for details.
  148. CurrentValue - Supplies a pointer where the current due time and period of
  149. the timer will be returned, in relative seconds from now. Zero will
  150. be returned in the value portion if the timer is not currently armed
  151. or has already expired.
  152. Return Value:
  153. 0 on success.
  154. -1 on failure, and errno will be set to contain more information.
  155. --*/
  156. LIBC_API
  157. int
  158. setitimer (
  159. int Type,
  160. const struct itimerval *NewValue,
  161. struct itimerval *OldValue
  162. );
  163. /*++
  164. Routine Description:
  165. This routine sets the current value of one of the interval timers.
  166. Arguments:
  167. Type - Supplies the timer type to get information for. See ITIMER_*
  168. definitions for details.
  169. NewValue - Supplies a pointer to the new relative value and period to set
  170. in the timer.
  171. OldValue - Supplies an optional pointer where the remaining time left
  172. on the timer and the period before the set operation will be returned.
  173. Return Value:
  174. 0 on success.
  175. -1 on failure, and errno will be set to contain more information.
  176. --*/
  177. LIBC_API
  178. int
  179. utimes (
  180. const char *Path,
  181. const struct timeval Times[2]
  182. );
  183. /*++
  184. Routine Description:
  185. This routine sets the access and modification times of the given file. The
  186. effective user ID of the process must match the owner of the file, or the
  187. process must have appropriate privileges.
  188. Arguments:
  189. Path - Supplies a pointer to the path of the file to change times for.
  190. Times - Supplies an optional array of time value structures containing the
  191. access and modification times to set.
  192. Return Value:
  193. 0 on success.
  194. -1 on failure, and errno will be set to contain more information.
  195. --*/
  196. LIBC_API
  197. int
  198. lutimes (
  199. const char *Path,
  200. const struct timeval Times[2]
  201. );
  202. /*++
  203. Routine Description:
  204. This routine sets the access and modification times of the given file. The
  205. effective user ID of the process must match the owner of the file, or the
  206. process must have appropriate privileges. The only difference between this
  207. function and utimes is that if the path references a symbolic link, the
  208. times of the link itself will be changed rather than the file to which
  209. it refers.
  210. Arguments:
  211. Path - Supplies a pointer to the path of the file to change times for.
  212. Times - Supplies an optional array of time value structures containing the
  213. access and modification times to set.
  214. Return Value:
  215. 0 on success.
  216. -1 on failure, and errno will be set to contain more information.
  217. --*/
  218. LIBC_API
  219. int
  220. futimes (
  221. int File,
  222. const struct timeval Times[2]
  223. );
  224. /*++
  225. Routine Description:
  226. This routine sets the access and modification times of the given file. The
  227. effective user ID of the process must match the owner of the file, or the
  228. process must have appropriate privileges.
  229. Arguments:
  230. File - Supplies the open file descriptor of the file to change the access
  231. and modification times for.
  232. Times - Supplies an optional array of time value structures containing the
  233. access and modification times to set.
  234. Return Value:
  235. 0 on success.
  236. -1 on failure, and errno will be set to contain more information.
  237. --*/
  238. #ifdef __cplusplus
  239. }
  240. #endif
  241. #endif