1 | /* ncurses-minesweeper Copyright (c) 2021 Joshua 'joshuas3' Stockin |
2 | * <https://joshstock.in> |
3 | * <https://git.joshstock.in/ncurses-minesweeper> |
4 | * |
5 | * This software is licensed and distributed under the terms of the MIT License. |
6 | * See the MIT License in the LICENSE file of this project's root folder. |
7 | * |
8 | * This comment block and its contents, including this disclaimer, MUST be |
9 | * preserved in all copies or distributions of this software's source. |
10 | */ |
11 |
|
12 | #include <stdint.h> |
13 | #include <stdlib.h> |
14 |
|
15 | #if defined(__unix__) || defined(unix) || defined(__unix) || defined(__CYGWIN__) |
16 | # include <sys/time.h> |
17 | uint64_t time_us(void) { |
18 | uint64_t us; |
19 | struct timeval lnm_current_time; |
20 | gettimeofday(&lnm_current_time, NULL); |
21 | us = (lnm_current_time.tv_sec * 1000000ULL + lnm_current_time.tv_usec); |
22 | return us; |
23 | } |
24 | #elif defined(_WIN32) || defined(__WINDOWS__) |
25 | # include <sysinfoapi.h> |
26 | # include <windows.h> |
27 | uint64_t time_us(void) { |
28 | uint64_t us; |
29 | // get system time in ticks |
30 | FILETIME lnm_win32_filetime; |
31 | GetSystemTimeAsFileTime(&lnm_win32_filetime); |
32 | // load time from two 32-bit words into one 64-bit integer |
33 | us = lnm_win32_filetime.dwHighDateTime; |
34 | us = us << 32; |
35 | us |= lnm_win32_filetime.dwLowDateTime; |
36 | // convert to microseconds |
37 | us /= 10ULL; // is there a better way to do this? |
38 | // convert from time since Windows NT epoch to time since Unix epoch |
39 | us -= 11644473600000000ULL; |
40 | return us; |
41 | } |
42 | #endif |
43 |
|