clock_gettime.2 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. .\" $OpenBSD: clock_gettime.2,v 1.4 1997/05/08 20:21:16 kstailey Exp $
  2. .\"
  3. .\" Copyright (c) 1980, 1991, 1993
  4. .\" The Regents of the University of California. 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. .\" 1. Redistributions of source code must retain the above copyright
  10. .\" notice, this list of conditions and the following disclaimer.
  11. .\" 2. Redistributions in binary form must reproduce the above copyright
  12. .\" notice, this list of conditions and the following disclaimer in the
  13. .\" documentation and/or other materials provided with the distribution.
  14. .\" 3. Neither the name of the University nor the names of its contributors
  15. .\" may be used to endorse or promote products derived from this software
  16. .\" without specific prior written permission.
  17. .\"
  18. .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  19. .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  20. .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  21. .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  22. .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  23. .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  24. .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  25. .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  26. .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  27. .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  28. .\" SUCH DAMAGE.
  29. .\"
  30. .Dd June 28, 2024
  31. .Dt CLOCK_GETTIME 2
  32. .Os
  33. .Sh NAME
  34. .Nm clock_gettime ,
  35. .Nm clock_settime ,
  36. .Nm clock_getres
  37. .Nd get/set/calibrate date and time
  38. .Sh LIBRARY
  39. .Lb libc
  40. .Sh SYNOPSIS
  41. .In time.h
  42. .Ft int
  43. .Fn clock_gettime "clockid_t clock_id" "struct timespec *tp"
  44. .Ft int
  45. .Fn clock_settime "clockid_t clock_id" "const struct timespec *tp"
  46. .Ft int
  47. .Fn clock_getres "clockid_t clock_id" "struct timespec *tp"
  48. .Sh DESCRIPTION
  49. The
  50. .Fn clock_gettime
  51. and
  52. .Fn clock_settime
  53. system calls allow the calling process to retrieve or set the value
  54. used by a clock which is specified by
  55. .Fa clock_id .
  56. .Pp
  57. The
  58. .Fa clock_id
  59. argument can be a value obtained from
  60. .Xr clock_getcpuclockid 3
  61. or
  62. .Xr pthread_getcpuclockid 3
  63. as well as the following values:
  64. .Pp
  65. .Bl -tag -width indent -compact
  66. .It Dv CLOCK_REALTIME
  67. .It Dv CLOCK_REALTIME_PRECISE
  68. .It Dv CLOCK_REALTIME_FAST
  69. .It Dv CLOCK_REALTIME_COARSE
  70. Increments in SI seconds like a wall clock.
  71. It uses a 1970 epoch and implements the UTC timescale.
  72. The count of physical SI seconds since 1970, adjusted by subtracting the number
  73. of positive leap seconds and adding the number of negative leap seconds.
  74. Behavior during a leap second is not defined by and POSIX standard.
  75. .It Dv CLOCK_MONOTONIC
  76. .It Dv CLOCK_MONOTONIC_PRECISE
  77. .It Dv CLOCK_MONOTONIC_FAST
  78. .It Dv CLOCK_MONOTONIC_COARSE
  79. .It Dv CLOCK_BOOTTIME
  80. Increments in SI seconds, even while the system is suspended.
  81. Its epoch is unspecified.
  82. The count is not adjusted by leap seconds.
  83. .Fx implements
  84. .It Dv CLOCK_UPTIME
  85. .It Dv CLOCK_UPTIME_PRECISE
  86. .It Dv CLOCK_UPTIME_FAST
  87. Increments monotonically in SI seconds while the machine is running.
  88. The count is not adjusted by leap seconds.
  89. The epoch is unspecified.
  90. .It Dv CLOCK_VIRTUAL
  91. Increments only when
  92. the CPU is running in user mode on behalf of the calling process.
  93. .It Dv CLOCK_PROF
  94. Increments when the CPU is running in user or kernel mode.
  95. .It Dv CLOCK_SECOND
  96. Returns the current second without performing a full time counter
  97. query, using an in-kernel cached value of the current second.
  98. .It Dv CLOCK_PROCESS_CPUTIME_ID
  99. Returns the execution time of the calling process.
  100. .It Dv CLOCK_THREAD_CPUTIME_ID
  101. Returns the execution time of the calling thread.
  102. .El
  103. .Pp
  104. The clock IDs
  105. .Dv CLOCK_BOOTTIME ,
  106. .Dv CLOCK_REALTIME ,
  107. .Dv CLOCK_MONOTONIC ,
  108. and
  109. .Dv CLOCK_UPTIME
  110. perform a full time counter query.
  111. The clock IDs with the _FAST suffix, i.e.,
  112. .Dv CLOCK_REALTIME_FAST ,
  113. .Dv CLOCK_MONOTONIC_FAST ,
  114. and
  115. .Dv CLOCK_UPTIME_FAST ,
  116. do not perform
  117. a full time counter query, so their accuracy is one timer tick.
  118. Similarly,
  119. .Dv CLOCK_REALTIME_PRECISE ,
  120. .Dv CLOCK_MONOTONIC_PRECISE ,
  121. and
  122. .Dv CLOCK_UPTIME_PRECISE
  123. are used to get the most exact value as possible, at the expense of
  124. execution time.
  125. The clock IDs
  126. .Dv CLOCK_REALTIME_COARSE
  127. and
  128. .Dv CLOCK_MONOTONIC_COARSE
  129. are aliases of corresponding IDs with _FAST suffix for compatibility with other
  130. systems.
  131. Finally,
  132. .Dv CLOCK_BOOTTIME
  133. is an alias for
  134. .Dv CLOCK_MONOTONIC
  135. for compatibility with other systems and is unrelated to the
  136. .Fa kern.boottime
  137. .Xr sysctl 8 .
  138. .Pp
  139. The structure pointed to by
  140. .Fa tp
  141. is defined in
  142. .In sys/timespec.h
  143. as:
  144. .Bd -literal
  145. struct timespec {
  146. time_t tv_sec; /* seconds */
  147. long tv_nsec; /* and nanoseconds */
  148. };
  149. .Ed
  150. .Pp
  151. Only the super-user may set the time of day, using only
  152. .Dv CLOCK_REALTIME .
  153. If the system
  154. .Xr securelevel 7
  155. is greater than 1 (see
  156. .Xr init 8 ) ,
  157. the time may only be advanced.
  158. This limitation is imposed to prevent a malicious super-user
  159. from setting arbitrary time stamps on files.
  160. The system time can still be adjusted backwards using the
  161. .Xr adjtime 2
  162. system call even when the system is secure.
  163. .Pp
  164. The resolution (granularity) of a clock is returned by the
  165. .Fn clock_getres
  166. system call.
  167. This value is placed in a (non-NULL)
  168. .Fa *tp .
  169. .Sh RETURN VALUES
  170. .Rv -std
  171. .Sh ERRORS
  172. The following error codes may be set in
  173. .Va errno :
  174. .Bl -tag -width Er
  175. .It Bq Er EINVAL
  176. The
  177. .Fa clock_id
  178. or
  179. .Fa timespec
  180. argument
  181. was not a valid value.
  182. .It Bq Er EPERM
  183. A user other than the super-user attempted to set the time.
  184. .El
  185. .Sh SEE ALSO
  186. .Xr date 1 ,
  187. .Xr adjtime 2 ,
  188. .Xr clock_getcpuclockid 3 ,
  189. .Xr ctime 3 ,
  190. .Xr pthread_getcpuclockid 3
  191. .Sh STANDARDS
  192. The
  193. .Fn clock_gettime ,
  194. .Fn clock_settime ,
  195. and
  196. .Fn clock_getres
  197. system calls conform to
  198. .St -p1003.1-2008 .
  199. The clock IDs
  200. .Dv CLOCK_BOOTTIME ,
  201. .Dv CLOCK_MONOTONIC_FAST ,
  202. .Dv CLOCK_MONOTONIC_PRECISE ,
  203. .Dv CLOCK_REALTIME_FAST ,
  204. .Dv CLOCK_REALTIME_PRECISE ,
  205. .Dv CLOCK_SECOND
  206. .Dv CLOCK_UPTIME ,
  207. .Dv CLOCK_UPTIME_FAST ,
  208. and
  209. .Dv CLOCK_UPTIME_PRECISE
  210. are
  211. .Fx
  212. extensions to the POSIX interface.
  213. .Pp
  214. UTC is defined by ITU-R TF.460-6, Standard-frequency and time-signal emissions.
  215. However, the
  216. .Vt time_t
  217. type is a simple count that does not provide a unique encoding for leap seconds,
  218. nor a specification for what values should be used to encode a leap second.
  219. .Pp
  220. .Sh HISTORY
  221. The
  222. .Fn clock_gettime ,
  223. .Fn clock_settime ,
  224. and
  225. .Fn clock_getres
  226. system calls first appeared in
  227. .Fx 3.0 .