time.odin 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. #+build linux, darwin, netbsd, openbsd, freebsd, haiku
  2. package posix
  3. import "core:c"
  4. import "core:c/libc"
  5. when ODIN_OS == .Darwin {
  6. foreign import lib "system:System"
  7. } else {
  8. foreign import lib "system:c"
  9. }
  10. // time.h - time types
  11. foreign lib {
  12. /*
  13. Convert the broken down time in the structure to a string form: Sun Sep 16 01:03:52 1973.
  14. [[ More; https://pubs.opengroup.org/onlinepubs/9699919799/functions/asctime_r.html ]]
  15. */
  16. asctime_r :: proc(tm: ^tm, buf: [^]c.char) -> cstring ---
  17. /*
  18. Convert a time value to a date and time string in the same format as asctime().
  19. [[ More; https://pubs.opengroup.org/onlinepubs/9699919799/functions/ctime_r.html ]]
  20. */
  21. @(link_name=LCTIMER)
  22. ctime_r :: proc(clock: ^time_t, buf: [^]c.char) -> cstring ---
  23. /*
  24. Converts the time in seconds since epoch to a broken-down tm struct.
  25. Returns: nil (setting errno) on failure, the result pointer on success.
  26. [[ More; https://pubs.opengroup.org/onlinepubs/9699919799/functions/gmtime_r.html ]]
  27. */
  28. @(link_name=LGMTIMER)
  29. gmtime_r :: proc(timer: ^time_t, result: ^tm) -> ^tm ---
  30. /*
  31. Convert the time in seconds since epoch to a broken-down tm struct in local time.
  32. Returns: nil (setting errno) on failure, the result pointer on success.
  33. [[ More; https://pubs.opengroup.org/onlinepubs/9699919799/functions/localtime_r.html ]]
  34. */
  35. @(link_name=LLOCALTIMER)
  36. localtime_r :: proc(timer: ^time_t, result: ^tm) -> ^tm ---
  37. /*
  38. Returns the resolution of any clock.
  39. [[ More; https://pubs.opengroup.org/onlinepubs/9699919799/functions/clock_getres.html ]]
  40. */
  41. @(link_name=LCLOCKGETRES)
  42. clock_getres :: proc(clock_id: Clock, res: ^timespec) -> result ---
  43. /*
  44. Returns the current value tp for the specified clock, clock_id.
  45. [[ More; https://pubs.opengroup.org/onlinepubs/9699919799/functions/clock_getres.html ]]
  46. */
  47. @(link_name=LCLOCKGETTIME)
  48. clock_gettime :: proc(clock_id: Clock, tp: ^timespec) -> result ---
  49. /*
  50. Sets the specified clock's time.
  51. [[ More; https://pubs.opengroup.org/onlinepubs/9699919799/functions/clock_getres.html ]]
  52. */
  53. @(link_name=LCLOCKSETTIME)
  54. clock_settime :: proc(clock_id: Clock, tp: ^timespec) -> result ---
  55. /*
  56. Converts a string representation of a date or time into a broken-down time.
  57. Returns: nil (setting getdate_err) on failure, the broken-down time otherwise
  58. [[ More; https://pubs.opengroup.org/onlinepubs/9699919799/functions/getdate.html ]]
  59. */
  60. getdate :: proc(string: cstring) -> ^tm ---
  61. /*
  62. Causes the current thread to be suspended from execution until either the time interval
  63. specified by rqtp has elapsed or a signal is delivered.
  64. Returns: -1 on failure (setting errno), if it was due to a signal, rmtp will be filled with the
  65. remaining time, 0 if all time has been slept
  66. [[ More; https://pubs.opengroup.org/onlinepubs/9699919799/functions/nanosleep.html ]]
  67. */
  68. @(link_name=LNANOSLEEP)
  69. nanosleep :: proc(rqtp: ^timespec, rmtp: ^timespec) -> result ---
  70. /*
  71. Converts the character string to values which are stored in tm, using the specified format.
  72. [[ More; https://pubs.opengroup.org/onlinepubs/9699919799/functions/strptime.html ]]
  73. */
  74. strptime :: proc(buf: [^]c.char, format: cstring, tm: ^tm) -> cstring ---
  75. /*
  76. Uses the value of the environment variable TZ (or default) to set time conversion info.
  77. `daylight` is set to whether daylight saving time conversion should be done.
  78. `timezone` is set to the difference, in seconds, between UTC and local standard time.
  79. `tzname` is set by `tzname[0] = "std"` and `tzname[1] = "dst"`
  80. Example:
  81. posix.tzset()
  82. fmt.println(posix.tzname)
  83. fmt.println(posix.daylight)
  84. fmt.println(posix.timezone)
  85. Possible Output:
  86. ["CET", "CEST"]
  87. true
  88. -3600
  89. [[ More; https://pubs.opengroup.org/onlinepubs/9699919799/functions/tzset.html ]]
  90. */
  91. tzset :: proc() ---
  92. // Whether daylight saving conversion should be done.
  93. daylight: b32
  94. // The time in seconds between UTC and local standard time.
  95. @(link_name=LTIMEZONE)
  96. timezone: c.long
  97. tzname: [2]cstring
  98. }
  99. time_t :: libc.time_t
  100. clock_t :: libc.clock_t
  101. tm :: libc.tm
  102. timespec :: libc.timespec
  103. CLOCKS_PER_SEC :: libc.CLOCKS_PER_SEC
  104. asctime :: libc.asctime
  105. clock :: libc.clock
  106. ctime :: libc.ctime
  107. difftime :: libc.difftime
  108. gmtime :: libc.gmtime
  109. localtime :: libc.localtime
  110. mktime :: libc.mktime
  111. strftime :: libc.strftime
  112. time :: libc.time
  113. Clock :: enum clockid_t {
  114. // system-wide monotonic clock, defined as clock measuring real time,
  115. // can be set with clock_settime() and cannot have negative clock jumps.
  116. MONOTONIC = CLOCK_MONOTONIC,
  117. // CPU-time clock associated with the process making a clock() function call.
  118. PROCESS_CPUTIME_ID = CLOCK_PROCESS_CPUTIME_ID,
  119. // system-wide clock measuring real time.
  120. REALTIME = CLOCK_REALTIME,
  121. // CPU-time clock associated with the thread making a clock() function call.
  122. THREAD_CPUTIME_ID = CLOCK_THREAD_CPUTIME_ID,
  123. }
  124. when ODIN_OS == .NetBSD {
  125. @(private) LCTIMER :: "__ctime_r50"
  126. @(private) LGMTIMER :: "__gmtime_r50"
  127. @(private) LLOCALTIMER :: "__localtime_r50"
  128. @(private) LCLOCKGETRES :: "__clock_getres50"
  129. @(private) LCLOCKGETTIME :: "__clock_gettime50"
  130. @(private) LCLOCKSETTIME :: "__clock_settime50"
  131. @(private) LNANOSLEEP :: "__nanosleep50"
  132. @(private) LTIMEZONE :: "__timezone13"
  133. } else {
  134. @(private) LCTIMER :: "ctime_r"
  135. @(private) LGMTIMER :: "gmtime_r"
  136. @(private) LLOCALTIMER :: "localtime_r"
  137. @(private) LCLOCKGETRES :: "clock_getres"
  138. @(private) LCLOCKGETTIME :: "clock_gettime"
  139. @(private) LCLOCKSETTIME :: "clock_settime"
  140. @(private) LNANOSLEEP :: "nanosleep"
  141. @(private) LTIMEZONE :: "timezone"
  142. }
  143. when ODIN_OS == .Darwin {
  144. clockid_t :: distinct c.int
  145. CLOCK_MONOTONIC :: 6
  146. CLOCK_PROCESS_CPUTIME_ID :: 12
  147. CLOCK_REALTIME :: 0
  148. CLOCK_THREAD_CPUTIME_ID :: 16
  149. foreign lib {
  150. getdate_err: Errno
  151. }
  152. } else when ODIN_OS == .FreeBSD {
  153. clockid_t :: distinct c.int
  154. CLOCK_MONOTONIC :: 4
  155. CLOCK_PROCESS_CPUTIME_ID :: 15
  156. CLOCK_REALTIME :: 0
  157. CLOCK_THREAD_CPUTIME_ID :: 14
  158. foreign lib {
  159. getdate_err: Errno
  160. }
  161. } else when ODIN_OS == .NetBSD {
  162. clockid_t :: distinct c.uint
  163. CLOCK_MONOTONIC :: 3
  164. CLOCK_PROCESS_CPUTIME_ID :: 0x40000000
  165. CLOCK_REALTIME :: 0
  166. CLOCK_THREAD_CPUTIME_ID :: 0x20000000
  167. foreign lib {
  168. getdate_err: Errno
  169. }
  170. } else when ODIN_OS == .OpenBSD {
  171. clockid_t :: distinct c.uint
  172. CLOCK_MONOTONIC :: 3
  173. CLOCK_PROCESS_CPUTIME_ID :: 2
  174. CLOCK_REALTIME :: 0
  175. CLOCK_THREAD_CPUTIME_ID :: 4
  176. getdate_err: Errno = .ENOSYS // NOTE: looks like it's not a thing on OpenBSD.
  177. } else when ODIN_OS == .Haiku {
  178. clockid_t :: distinct c.int32_t
  179. CLOCK_MONOTONIC :: 0
  180. CLOCK_PROCESS_CPUTIME_ID :: -2
  181. CLOCK_REALTIME :: -1
  182. CLOCK_THREAD_CPUTIME_ID :: -3
  183. getdate_err: Errno = .ENOSYS // NOTE: looks like it's not a thing on Haiku.
  184. } else when ODIN_OS == .Linux {
  185. clockid_t :: distinct c.int
  186. CLOCK_MONOTONIC :: 1
  187. CLOCK_PROCESS_CPUTIME_ID :: 2
  188. CLOCK_REALTIME :: 0
  189. CLOCK_THREAD_CPUTIME_ID :: 3
  190. foreign lib {
  191. getdate_err: Errno
  192. }
  193. }