time.odin 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978
  1. package time
  2. import "base:intrinsics"
  3. import dt "core:time/datetime"
  4. /*
  5. Type representing duration, with nanosecond precision.
  6. This is the regular Unix timestamp, scaled to nanosecond precision.
  7. */
  8. Duration :: distinct i64
  9. /*
  10. The duration equal to one nanosecond (1e-9 seconds).
  11. */
  12. Nanosecond :: Duration(1)
  13. /*
  14. The duration equal to one microsecond (1e-6 seconds).
  15. */
  16. Microsecond :: 1000 * Nanosecond
  17. /*
  18. The duration equal to one millisecond (1e-3 seconds).
  19. */
  20. Millisecond :: 1000 * Microsecond
  21. /*
  22. The duration equal to one second.
  23. */
  24. Second :: 1000 * Millisecond
  25. /*
  26. The duration equal to one minute (60 seconds).
  27. */
  28. Minute :: 60 * Second
  29. /*
  30. The duration equal to one hour (3600 seconds).
  31. */
  32. Hour :: 60 * Minute
  33. /*
  34. Minimum representable duration.
  35. */
  36. MIN_DURATION :: Duration(-1 << 63)
  37. /*
  38. Maximum representable duration.
  39. */
  40. MAX_DURATION :: Duration(1<<63 - 1)
  41. /*
  42. Value specifying whether the time procedures are supported by the current
  43. platform.
  44. */
  45. IS_SUPPORTED :: _IS_SUPPORTED
  46. /*
  47. Specifies time since the UNIX epoch, with nanosecond precision.
  48. Capable of representing any time within the following range:
  49. - `min: 1677-09-21 00:12:44.145224192 +0000 UTC`
  50. - `max: 2262-04-11 23:47:16.854775807 +0000 UTC`
  51. */
  52. Time :: struct {
  53. _nsec: i64, // Measured in UNIX nanonseconds
  54. }
  55. /*
  56. Type representing a month.
  57. */
  58. Month :: enum int {
  59. January = 1,
  60. February,
  61. March,
  62. April,
  63. May,
  64. June,
  65. July,
  66. August,
  67. September,
  68. October,
  69. November,
  70. December,
  71. }
  72. /*
  73. Type representing a weekday.
  74. */
  75. Weekday :: enum int {
  76. Sunday = 0,
  77. Monday,
  78. Tuesday,
  79. Wednesday,
  80. Thursday,
  81. Friday,
  82. Saturday,
  83. }
  84. /*
  85. Type representing a stopwatch.
  86. The stopwatch is used for measuring the total time in multiple "runs". When the
  87. stopwatch is started, it starts counting time. When the stopwatch is stopped,
  88. the difference in time between the last start and the stop is added to the
  89. total. When the stopwatch resets, the total is reset.
  90. */
  91. Stopwatch :: struct {
  92. running: bool,
  93. _start_time: Tick,
  94. _accumulation: Duration,
  95. }
  96. /*
  97. Obtain the current time.
  98. */
  99. now :: proc "contextless" () -> Time {
  100. return _now()
  101. }
  102. /*
  103. Sleep for the specified duration.
  104. */
  105. sleep :: proc "contextless" (d: Duration) {
  106. _sleep(d)
  107. }
  108. /*
  109. Start the stopwatch.
  110. */
  111. stopwatch_start :: proc "contextless" (stopwatch: ^Stopwatch) {
  112. if !stopwatch.running {
  113. stopwatch._start_time = tick_now()
  114. stopwatch.running = true
  115. }
  116. }
  117. /*
  118. Stop the stopwatch.
  119. */
  120. stopwatch_stop :: proc "contextless" (stopwatch: ^Stopwatch) {
  121. if stopwatch.running {
  122. stopwatch._accumulation += tick_diff(stopwatch._start_time, tick_now())
  123. stopwatch.running = false
  124. }
  125. }
  126. /*
  127. Reset the stopwatch.
  128. */
  129. stopwatch_reset :: proc "contextless" (stopwatch: ^Stopwatch) {
  130. stopwatch._accumulation = {}
  131. stopwatch.running = false
  132. }
  133. /*
  134. Obtain the total time, counted by the stopwatch.
  135. This procedure obtains the total time, counted by the stopwatch. If the stopwatch
  136. isn't stopped at the time of calling this procedure, the time between the last
  137. start and the current time is also accounted for.
  138. */
  139. stopwatch_duration :: proc "contextless" (stopwatch: Stopwatch) -> Duration {
  140. if !stopwatch.running {
  141. return stopwatch._accumulation
  142. }
  143. return stopwatch._accumulation + tick_diff(stopwatch._start_time, tick_now())
  144. }
  145. /*
  146. Calculate the duration elapsed between two times.
  147. */
  148. diff :: proc "contextless" (start, end: Time) -> Duration {
  149. d := end._nsec - start._nsec
  150. return Duration(d)
  151. }
  152. /*
  153. Calculate the duration elapsed since a specific time.
  154. */
  155. since :: proc "contextless" (start: Time) -> Duration {
  156. return diff(start, now())
  157. }
  158. /*
  159. Obtain the number of nanoseconds in a duration.
  160. */
  161. duration_nanoseconds :: proc "contextless" (d: Duration) -> i64 {
  162. return i64(d)
  163. }
  164. /*
  165. Obtain the number of microseconds in a duration.
  166. */
  167. duration_microseconds :: proc "contextless" (d: Duration) -> f64 {
  168. return duration_seconds(d) * 1e6
  169. }
  170. /*
  171. Obtain the number of milliseconds in a duration.
  172. */
  173. duration_milliseconds :: proc "contextless" (d: Duration) -> f64 {
  174. return duration_seconds(d) * 1e3
  175. }
  176. /*
  177. Obtain the number of seconds in a duration.
  178. */
  179. duration_seconds :: proc "contextless" (d: Duration) -> f64 {
  180. sec := d / Second
  181. nsec := d % Second
  182. return f64(sec) + f64(nsec)/1e9
  183. }
  184. /*
  185. Obtain the number of minutes in a duration.
  186. */
  187. duration_minutes :: proc "contextless" (d: Duration) -> f64 {
  188. min := d / Minute
  189. nsec := d % Minute
  190. return f64(min) + f64(nsec)/(60*1e9)
  191. }
  192. /*
  193. Obtain the number of hours in a duration.
  194. */
  195. duration_hours :: proc "contextless" (d: Duration) -> f64 {
  196. hour := d / Hour
  197. nsec := d % Hour
  198. return f64(hour) + f64(nsec)/(60*60*1e9)
  199. }
  200. /*
  201. Round a duration to a specific unit
  202. This procedure rounds the duration to a specific unit
  203. **Note**: Any duration can be supplied as a unit.
  204. Inputs:
  205. - d: The duration to round
  206. - m: The unit to round to
  207. Returns:
  208. - The duration `d`, rounded to the unit specified by `m`
  209. Example:
  210. time.duration_round(my_duration, time.Second)
  211. */
  212. duration_round :: proc "contextless" (d, m: Duration) -> Duration {
  213. _less_than_half :: #force_inline proc "contextless" (x, y: Duration) -> bool {
  214. return u64(x)+u64(x) < u64(y)
  215. }
  216. if m <= 0 {
  217. return d
  218. }
  219. r := d % m
  220. if d < 0 {
  221. r = -r
  222. if _less_than_half(r, m) {
  223. return d + r
  224. }
  225. if d1 := d-m+r; d1 < d {
  226. return d1
  227. }
  228. return MIN_DURATION
  229. }
  230. if _less_than_half(r, m) {
  231. return d - r
  232. }
  233. if d1 := d+m-r; d1 > d {
  234. return d1
  235. }
  236. return MAX_DURATION
  237. }
  238. /*
  239. Truncate the duration to the specified unit.
  240. This procedure truncates the duration `d` to the unit specified by `m`.
  241. **Note**: Any duration can be supplied as a unit.
  242. Inputs:
  243. - d: The duration to truncate.
  244. - m: The unit to truncate to.
  245. Returns:
  246. - The duration `d`, truncated to the unit specified by `m`.
  247. Example:
  248. time.duration_round(my_duration, time.Second)
  249. */
  250. duration_truncate :: proc "contextless" (d, m: Duration) -> Duration {
  251. return d if m <= 0 else d - d%m
  252. }
  253. /*
  254. Parse time into date components.
  255. */
  256. date :: proc "contextless" (t: Time) -> (year: int, month: Month, day: int) {
  257. year, month, day, _ = _abs_date(_time_abs(t), true)
  258. return
  259. }
  260. /*
  261. Obtain the year of the date specified by time.
  262. */
  263. year :: proc "contextless" (t: Time) -> (year: int) {
  264. year, _, _, _ = _date(t, true)
  265. return
  266. }
  267. /*
  268. Obtain the month of the date specified by time.
  269. */
  270. month :: proc "contextless" (t: Time) -> (month: Month) {
  271. _, month, _, _ = _date(t, true)
  272. return
  273. }
  274. /*
  275. Obtain the day of the date specified by time.
  276. */
  277. day :: proc "contextless" (t: Time) -> (day: int) {
  278. _, _, day, _ = _date(t, true)
  279. return
  280. }
  281. /*
  282. Obtain the week day of the date specified by time.
  283. */
  284. weekday :: proc "contextless" (t: Time) -> (weekday: Weekday) {
  285. abs := _time_abs(t)
  286. sec := (abs + u64(Weekday.Monday) * SECONDS_PER_DAY) % SECONDS_PER_WEEK
  287. return Weekday(int(sec) / SECONDS_PER_DAY)
  288. }
  289. /*
  290. Obtain the time components from a time, a duration or a stopwatch's total.
  291. */
  292. clock :: proc { clock_from_time, clock_from_duration, clock_from_stopwatch }
  293. /*
  294. Obtain the time components from a time.
  295. */
  296. clock_from_time :: proc "contextless" (t: Time) -> (hour, min, sec: int) {
  297. return clock_from_seconds(_time_abs(t))
  298. }
  299. /*
  300. Obtain the time components from a duration.
  301. */
  302. clock_from_duration :: proc "contextless" (d: Duration) -> (hour, min, sec: int) {
  303. return clock_from_seconds(u64(d/1e9))
  304. }
  305. /*
  306. Obtain the time components from a stopwatch's total.
  307. */
  308. clock_from_stopwatch :: proc "contextless" (s: Stopwatch) -> (hour, min, sec: int) {
  309. return clock_from_duration(stopwatch_duration(s))
  310. }
  311. /*
  312. Obtain the time components from the number of seconds.
  313. */
  314. clock_from_seconds :: proc "contextless" (nsec: u64) -> (hour, min, sec: int) {
  315. sec = int(nsec % SECONDS_PER_DAY)
  316. hour = sec / SECONDS_PER_HOUR
  317. sec -= hour * SECONDS_PER_HOUR
  318. min = sec / SECONDS_PER_MINUTE
  319. sec -= min * SECONDS_PER_MINUTE
  320. return
  321. }
  322. MIN_HMS_LEN :: 8
  323. MIN_HMS_12_LEN :: 11
  324. MIN_YYYY_DATE_LEN :: 10
  325. MIN_YY_DATE_LEN :: 8
  326. /*
  327. Formats a `Time` as a 24-hour `hh:mm:ss` string.
  328. **Does not allocate**
  329. Inputs:
  330. - t: The Time to format.
  331. - buf: The backing buffer to use.
  332. Returns:
  333. - res: The formatted string, backed by buf
  334. Example:
  335. buf: [MIN_HMS_LEN]u8
  336. now := time.now()
  337. fmt.println(time.to_string_hms(now, buf[:]))
  338. */
  339. time_to_string_hms :: proc(t: Time, buf: []u8) -> (res: string) #no_bounds_check {
  340. assert(len(buf) >= MIN_HMS_LEN)
  341. h, m, s := clock(t)
  342. buf[7] = '0' + u8(s % 10); s /= 10
  343. buf[6] = '0' + u8(s)
  344. buf[5] = ':'
  345. buf[4] = '0' + u8(m % 10); m /= 10
  346. buf[3] = '0' + u8(m)
  347. buf[2] = ':'
  348. buf[1] = '0' + u8(h % 10); h /= 10
  349. buf[0] = '0' + u8(h)
  350. return string(buf[:MIN_HMS_LEN])
  351. }
  352. /*
  353. Formats a `Duration` as a 24-hour `hh:mm:ss` string.
  354. **Does not allocate**
  355. Inputs:
  356. - d: The Duration to format.
  357. - buf: The backing buffer to use.
  358. Returns:
  359. - res: The formatted string, backed by buf
  360. Example:
  361. buf: [MIN_HMS_LEN]u8
  362. d := time.since(earlier)
  363. fmt.println(time.to_string_hms(now, buf[:]))
  364. */
  365. duration_to_string_hms :: proc(d: Duration, buf: []u8) -> (res: string) #no_bounds_check {
  366. return time_to_string_hms(Time{_nsec=i64(d)}, buf)
  367. }
  368. to_string_hms :: proc{time_to_string_hms, duration_to_string_hms}
  369. /*
  370. Formats a `Time` as a 12-hour `hh:mm:ss pm` string
  371. **Does not allocate**
  372. Inputs:
  373. - t: The Time to format
  374. - buf: The backing buffer to use
  375. - ampm: An optional pair of am/pm strings to use in place of the default
  376. Returns:
  377. - res: The formatted string, backed by buf
  378. Example:
  379. buf: [64]u8
  380. now := time.now()
  381. fmt.println(time.to_string_hms_12(now, buf[:]))
  382. fmt.println(time.to_string_hms_12(now, buf[:], {"㏂", "㏘"}))
  383. */
  384. to_string_hms_12 :: proc(t: Time, buf: []u8, ampm: [2]string = {" am", " pm"}) -> (res: string) #no_bounds_check {
  385. assert(len(buf) >= MIN_HMS_LEN + max(len(ampm[0]), len(ampm[1])))
  386. h, m, s := clock(t)
  387. _h := h % 12
  388. buf[7] = '0' + u8(s % 10); s /= 10
  389. buf[6] = '0' + u8(s)
  390. buf[5] = ':'
  391. buf[4] = '0' + u8(m % 10); m /= 10
  392. buf[3] = '0' + u8(m)
  393. buf[2] = ':'
  394. buf[1] = '0' + u8(_h% 10); _h /= 10
  395. buf[0] = '0' + u8(_h)
  396. if h < 13 {
  397. copy(buf[8:], ampm[0])
  398. return string(buf[:MIN_HMS_LEN+len(ampm[0])])
  399. } else {
  400. copy(buf[8:], ampm[1])
  401. return string(buf[:MIN_HMS_LEN+len(ampm[1])])
  402. }
  403. }
  404. /*
  405. Formats a Time as a yyyy-mm-dd date string.
  406. Inputs:
  407. - t: The Time to format.
  408. - buf: The backing buffer to use.
  409. Returns:
  410. - res: The formatted string, backed by `buf`.
  411. Example:
  412. buf: [MIN_YYYY_DATE_LEN]u8
  413. now := time.now()
  414. fmt.println(time.to_string_yyyy_mm_dd(now, buf[:]))
  415. */
  416. to_string_yyyy_mm_dd :: proc(t: Time, buf: []u8) -> (res: string) #no_bounds_check {
  417. assert(len(buf) >= MIN_YYYY_DATE_LEN)
  418. y, _m, d := date(t)
  419. m := u8(_m)
  420. buf[9] = '0' + u8(d % 10); d /= 10
  421. buf[8] = '0' + u8(d % 10)
  422. buf[7] = '-'
  423. buf[6] = '0' + u8(m % 10); m /= 10
  424. buf[5] = '0' + u8(m % 10)
  425. buf[4] = '-'
  426. buf[3] = '0' + u8(y % 10); y /= 10
  427. buf[2] = '0' + u8(y % 10); y /= 10
  428. buf[1] = '0' + u8(y % 10); y /= 10
  429. buf[0] = '0' + u8(y)
  430. return string(buf[:MIN_YYYY_DATE_LEN])
  431. }
  432. /*
  433. Formats a Time as a yy-mm-dd date string.
  434. Inputs:
  435. - t: The Time to format.
  436. - buf: The backing buffer to use.
  437. Returns:
  438. - res: The formatted string, backed by `buf`.
  439. Example:
  440. buf: [MIN_YY_DATE_LEN]u8
  441. now := time.now()
  442. fmt.println(time.to_string_yy_mm_dd(now, buf[:]))
  443. */
  444. to_string_yy_mm_dd :: proc(t: Time, buf: []u8) -> (res: string) #no_bounds_check {
  445. assert(len(buf) >= MIN_YY_DATE_LEN)
  446. y, _m, d := date(t)
  447. y %= 100; m := u8(_m)
  448. buf[7] = '0' + u8(d % 10); d /= 10
  449. buf[6] = '0' + u8(d % 10)
  450. buf[5] = '-'
  451. buf[4] = '0' + u8(m % 10); m /= 10
  452. buf[3] = '0' + u8(m % 10)
  453. buf[2] = '-'
  454. buf[1] = '0' + u8(y % 10); y /= 10
  455. buf[0] = '0' + u8(y)
  456. return string(buf[:MIN_YY_DATE_LEN])
  457. }
  458. /*
  459. Formats a Time as a dd-mm-yyyy date string.
  460. Inputs:
  461. - t: The Time to format.
  462. - buf: The backing buffer to use.
  463. Returns:
  464. - res: The formatted string, backed by `buf`.
  465. Example:
  466. buf: [MIN_YYYY_DATE_LEN]u8
  467. now := time.now()
  468. fmt.println(time.to_string_dd_mm_yyyy(now, buf[:]))
  469. */
  470. to_string_dd_mm_yyyy :: proc(t: Time, buf: []u8) -> (res: string) #no_bounds_check {
  471. assert(len(buf) >= MIN_YYYY_DATE_LEN)
  472. y, _m, d := date(t)
  473. m := u8(_m)
  474. buf[9] = '0' + u8(y % 10); y /= 10
  475. buf[8] = '0' + u8(y % 10); y /= 10
  476. buf[7] = '0' + u8(y % 10); y /= 10
  477. buf[6] = '0' + u8(y)
  478. buf[5] = '-'
  479. buf[4] = '0' + u8(m % 10); m /= 10
  480. buf[3] = '0' + u8(m % 10)
  481. buf[2] = '-'
  482. buf[1] = '0' + u8(d % 10); d /= 10
  483. buf[0] = '0' + u8(d % 10)
  484. return string(buf[:MIN_YYYY_DATE_LEN])
  485. }
  486. /*
  487. Formats a Time as a dd-mm-yy date string.
  488. Inputs:
  489. - t: The Time to format.
  490. - buf: The backing buffer to use.
  491. Returns:
  492. - res: The formatted string, backed by `buf`.
  493. Example:
  494. buf: [MIN_YY_DATE_LEN]u8
  495. now := time.now()
  496. fmt.println(time.to_string_dd_mm_yy(now, buf[:]))
  497. */
  498. to_string_dd_mm_yy :: proc(t: Time, buf: []u8) -> (res: string) #no_bounds_check {
  499. assert(len(buf) >= MIN_YY_DATE_LEN)
  500. y, _m, d := date(t)
  501. y %= 100; m := u8(_m)
  502. buf[7] = '0' + u8(y % 10); y /= 10
  503. buf[6] = '0' + u8(y)
  504. buf[5] = '-'
  505. buf[4] = '0' + u8(m % 10); m /= 10
  506. buf[3] = '0' + u8(m % 10)
  507. buf[2] = '-'
  508. buf[1] = '0' + u8(d % 10); d /= 10
  509. buf[0] = '0' + u8(d % 10)
  510. return string(buf[:MIN_YY_DATE_LEN])
  511. }
  512. /*
  513. Formats a Time as a mm-dd-yyyy date string.
  514. Inputs:
  515. - t: The Time to format.
  516. - buf: The backing buffer to use.
  517. Returns:
  518. - res: The formatted string, backed by `buf`.
  519. Example:
  520. buf: [MIN_YYYY_DATE_LEN]u8
  521. now := time.now()
  522. fmt.println(time.to_string_mm_dd_yyyy(now, buf[:]))
  523. */
  524. to_string_mm_dd_yyyy :: proc(t: Time, buf: []u8) -> (res: string) #no_bounds_check {
  525. assert(len(buf) >= MIN_YYYY_DATE_LEN)
  526. y, _m, d := date(t)
  527. m := u8(_m)
  528. buf[9] = '0' + u8(y % 10); y /= 10
  529. buf[8] = '0' + u8(y % 10); y /= 10
  530. buf[7] = '0' + u8(y % 10); y /= 10
  531. buf[6] = '0' + u8(y)
  532. buf[5] = '-'
  533. buf[4] = '0' + u8(d % 10); d /= 10
  534. buf[3] = '0' + u8(d % 10)
  535. buf[2] = '-'
  536. buf[1] = '0' + u8(m % 10); m /= 10
  537. buf[0] = '0' + u8(m % 10)
  538. return string(buf[:MIN_YYYY_DATE_LEN])
  539. }
  540. /*
  541. Formats a Time as a mm-dd-yy date string.
  542. Inputs:
  543. - t: The Time to format.
  544. - buf: The backing buffer to use.
  545. Returns:
  546. - res: The formatted string, backed by `buf`.
  547. Example:
  548. buf: [MIN_YY_DATE_LEN]u8
  549. now := time.now()
  550. fmt.println(time.to_string_mm_dd_yy(now, buf[:]))
  551. */
  552. to_string_mm_dd_yy :: proc(t: Time, buf: []u8) -> (res: string) #no_bounds_check {
  553. assert(len(buf) >= MIN_YY_DATE_LEN)
  554. y, _m, d := date(t)
  555. y %= 100; m := u8(_m)
  556. buf[7] = '0' + u8(y % 10); y /= 10
  557. buf[6] = '0' + u8(y)
  558. buf[5] = '-'
  559. buf[4] = '0' + u8(d % 10); d /= 10
  560. buf[3] = '0' + u8(d % 10)
  561. buf[2] = '-'
  562. buf[1] = '0' + u8(m % 10); m /= 10
  563. buf[0] = '0' + u8(m % 10)
  564. return string(buf[:MIN_YY_DATE_LEN])
  565. }
  566. /*
  567. Read the timestamp counter of the CPU.
  568. */
  569. read_cycle_counter :: proc "contextless" () -> u64 {
  570. return u64(intrinsics.read_cycle_counter())
  571. }
  572. /*
  573. Obtain time from unix seconds and unix nanoseconds.
  574. */
  575. unix :: proc "contextless" (sec: i64, nsec: i64) -> Time {
  576. sec, nsec := sec, nsec
  577. if nsec < 0 || nsec >= 1e9 {
  578. n := nsec / 1e9
  579. sec += n
  580. nsec -= n * 1e9
  581. if nsec < 0 {
  582. nsec += 1e9
  583. sec -= 1
  584. }
  585. }
  586. return Time{(sec*1e9 + nsec)}
  587. }
  588. /*
  589. Obtain time from unix nanoseconds.
  590. */
  591. from_nanoseconds :: #force_inline proc "contextless" (nsec: i64) -> Time {
  592. return Time{nsec}
  593. }
  594. /*
  595. Alias for `time_to_unix`.
  596. */
  597. to_unix_seconds :: time_to_unix
  598. /*
  599. Obtain the Unix timestamp in seconds from a Time.
  600. */
  601. time_to_unix :: proc "contextless" (t: Time) -> i64 {
  602. return t._nsec/1e9
  603. }
  604. /*
  605. Alias for `time_to_unix_nano`.
  606. */
  607. to_unix_nanoseconds :: time_to_unix_nano
  608. /*
  609. Obtain the Unix timestamp in nanoseconds from a Time.
  610. */
  611. time_to_unix_nano :: proc "contextless" (t: Time) -> i64 {
  612. return t._nsec
  613. }
  614. /*
  615. Add duration to a time.
  616. */
  617. time_add :: proc "contextless" (t: Time, d: Duration) -> Time {
  618. return Time{t._nsec + i64(d)}
  619. }
  620. /*
  621. Accurate sleep
  622. This procedure sleeps for the duration specified by `d`, very accurately.
  623. **Note**: Implementation borrowed from: [this source](https://blat-blatnik.github.io/computerBear/making-accurate-sleep-function/)
  624. **Note(linux)**: The accuracy is within around 4µs (microseconds), in the worst case.
  625. **Note(windows)**: The accuracy depends but is comparable with regular sleep in
  626. the worst case. To get the same kind of accuracy as on Linux, have your program
  627. call `windows.timeBeginPeriod(1)` to tell Windows to use a more accurate timer
  628. for your process. Additionally your program should call `windows.timeEndPeriod(1)`
  629. once you're done with `accurate_sleep`.
  630. */
  631. accurate_sleep :: proc "contextless" (d: Duration) {
  632. to_sleep, estimate, mean, m2, count: Duration
  633. to_sleep = d
  634. estimate = 5 * Millisecond
  635. mean = 5 * Millisecond
  636. count = 1
  637. for to_sleep > estimate {
  638. start := tick_now()
  639. sleep(1 * Millisecond)
  640. observed := tick_since(start)
  641. to_sleep -= observed
  642. count += 1
  643. delta := observed - mean
  644. mean += delta / count
  645. m2 += delta * (observed - mean)
  646. stddev := intrinsics.sqrt(f64(m2) / f64(count - 1))
  647. estimate = mean + Duration(stddev)
  648. }
  649. start := tick_now()
  650. for to_sleep > tick_since(start) {
  651. // prevent the spinlock from taking the thread hostage, still accurate enough
  652. _yield()
  653. // NOTE: it might be possible that it yields for too long, in that case it should spinlock freely for a while
  654. // TODO: needs actual testing done to check if that's the case
  655. }
  656. }
  657. ABSOLUTE_ZERO_YEAR :: i64(-292277022399) // Day is chosen so that 2001-01-01 is Monday in the calculations
  658. ABSOLUTE_TO_INTERNAL :: i64(-9223371966579724800) // i64((ABSOLUTE_ZERO_YEAR - 1) * 365.2425 * SECONDS_PER_DAY);
  659. INTERNAL_TO_ABSOLUTE :: -ABSOLUTE_TO_INTERNAL
  660. UNIX_TO_INTERNAL :: i64((1969*365 + 1969/4 - 1969/100 + 1969/400) * SECONDS_PER_DAY)
  661. INTERNAL_TO_UNIX :: -UNIX_TO_INTERNAL
  662. WALL_TO_INTERNAL :: i64((1884*365 + 1884/4 - 1884/100 + 1884/400) * SECONDS_PER_DAY)
  663. INTERNAL_TO_WALL :: -WALL_TO_INTERNAL
  664. UNIX_TO_ABSOLUTE :: UNIX_TO_INTERNAL + INTERNAL_TO_ABSOLUTE
  665. ABSOLUTE_TO_UNIX :: -UNIX_TO_ABSOLUTE
  666. @(private)
  667. _date :: proc "contextless" (t: Time, full: bool) -> (year: int, month: Month, day: int, yday: int) {
  668. year, month, day, yday = _abs_date(_time_abs(t), full)
  669. return
  670. }
  671. @(private)
  672. _time_abs :: proc "contextless" (t: Time) -> u64 {
  673. return u64(t._nsec/1e9 + UNIX_TO_ABSOLUTE)
  674. }
  675. @(private)
  676. _abs_date :: proc "contextless" (abs: u64, full: bool) -> (year: int, month: Month, day: int, yday: int) {
  677. d := abs / SECONDS_PER_DAY
  678. // 400 year cycles
  679. n := d / DAYS_PER_400_YEARS
  680. y := 400 * n
  681. d -= DAYS_PER_400_YEARS * n
  682. // Cut-off 100 year cycles
  683. n = d / DAYS_PER_100_YEARS
  684. n -= n >> 2
  685. y += 100 * n
  686. d -= DAYS_PER_100_YEARS * n
  687. // Cut-off 4 year cycles
  688. n = d / DAYS_PER_4_YEARS
  689. y += 4 * n
  690. d -= DAYS_PER_4_YEARS * n
  691. n = d / 365
  692. n -= n >> 2
  693. y += n
  694. d -= 365 * n
  695. year = int(i64(y) + ABSOLUTE_ZERO_YEAR)
  696. yday = int(d)
  697. if !full {
  698. return
  699. }
  700. day = yday
  701. if is_leap_year(year) {
  702. switch {
  703. case day > 31+29-1:
  704. day -= 1
  705. case day == 31+29-1:
  706. month = .February
  707. day = 29
  708. return
  709. }
  710. }
  711. month = Month(day / 31)
  712. end := int(days_before[int(month)+1])
  713. begin: int
  714. if day >= end {
  715. (^int)(&month)^ += 1
  716. begin = end
  717. } else {
  718. begin = int(days_before[month])
  719. }
  720. (^int)(&month)^ += 1 // January is 1
  721. day = day - begin + 1
  722. return
  723. }
  724. /*
  725. Convert datetime components into time.
  726. This procedure calculates the time from datetime components supplied in the
  727. arguments to this procedure. If the datetime components don't represent a valid
  728. datetime, the function returns `false` in the second argument.
  729. */
  730. components_to_time :: proc "contextless" (#any_int year, #any_int month, #any_int day, #any_int hour, #any_int minute, #any_int second: i64, #any_int nsec := i64(0)) -> (t: Time, ok: bool) {
  731. this_date, err := dt.components_to_datetime(year, month, day, hour, minute, second, nsec)
  732. if err != .None {
  733. return
  734. }
  735. return compound_to_time(this_date)
  736. }
  737. /*
  738. Convert datetime into time.
  739. If the datetime represents a time outside of a valid range, `false` is returned
  740. as the second return value. See `Time` for the representable range.
  741. */
  742. compound_to_time :: proc "contextless" (datetime: dt.DateTime) -> (t: Time, ok: bool) {
  743. unix_epoch := dt.DateTime{{1970, 1, 1}, {0, 0, 0, 0}}
  744. delta, err := dt.sub(datetime, unix_epoch)
  745. ok = err == .None
  746. seconds := delta.days * 86_400 + delta.seconds
  747. nanoseconds := i128(seconds) * 1e9 + i128(delta.nanos)
  748. // Can this moment be represented in i64 worth of nanoseconds?
  749. // min(Time): 1677-09-21 00:12:44.145224192 +0000 UTC
  750. // max(Time): 2262-04-11 23:47:16.854775807 +0000 UTC
  751. if nanoseconds < i128(min(i64)) || nanoseconds > i128(max(i64)) {
  752. return {}, false
  753. }
  754. return Time{_nsec=i64(nanoseconds)}, true
  755. }
  756. /*
  757. Convert datetime components into time.
  758. */
  759. datetime_to_time :: proc{components_to_time, compound_to_time}
  760. /*
  761. Check if a year is a leap year.
  762. */
  763. is_leap_year :: proc "contextless" (year: int) -> (leap: bool) {
  764. return year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)
  765. }
  766. /*
  767. Days before each month in a year, not counting the leap day on february 29th.
  768. */
  769. @(rodata)
  770. days_before := [?]i32{
  771. 0,
  772. 31,
  773. 31 + 28,
  774. 31 + 28 + 31,
  775. 31 + 28 + 31 + 30,
  776. 31 + 28 + 31 + 30 + 31,
  777. 31 + 28 + 31 + 30 + 31 + 30,
  778. 31 + 28 + 31 + 30 + 31 + 30 + 31,
  779. 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31,
  780. 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30,
  781. 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31,
  782. 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30,
  783. 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + 31,
  784. }
  785. /*
  786. Number of seconds in a minute (without leap seconds).
  787. */
  788. SECONDS_PER_MINUTE :: 60
  789. /*
  790. Number of seconds in an hour (without leap seconds).
  791. */
  792. SECONDS_PER_HOUR :: 60 * SECONDS_PER_MINUTE
  793. /*
  794. Number of seconds in a day (without leap seconds).
  795. */
  796. SECONDS_PER_DAY :: 24 * SECONDS_PER_HOUR
  797. /*
  798. Number of seconds in a week (without leap seconds).
  799. */
  800. SECONDS_PER_WEEK :: 7 * SECONDS_PER_DAY
  801. /*
  802. Days in 400 years, with leap days.
  803. */
  804. DAYS_PER_400_YEARS :: 365*400 + 97
  805. /*
  806. Days in 100 years, with leap days.
  807. */
  808. DAYS_PER_100_YEARS :: 365*100 + 24
  809. /*
  810. Days in 4 years, with leap days.
  811. */
  812. DAYS_PER_4_YEARS :: 365*4 + 1