time.odin 6.5 KB

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