-
Notifications
You must be signed in to change notification settings - Fork 0
/
Time.c
47 lines (39 loc) · 1.01 KB
/
Time.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/** vim: set noet ci pi sts=0 sw=4 ts=4
* @file Time.c
* @brief Functions for getting the current time.
*/
#include "Time.h"
#ifdef WINDOWS
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#elif defined POSIX
#include <sys/time.h>
#endif
/**
* @returns the current time in ms since Jan 1, 1970.
*
* @warning Does *NOT* take into account leap seconds, but does take into
* account leap days.
*/
int64 SAL_Time_Now(void) {
#ifdef WINDOWS
FILETIME time;
uint64 result;
GetSystemTimeAsFileTime(&time);
result = time.dwHighDateTime;
result <<= 32;
result += time.dwLowDateTime;
result /= 10000; // to shift from 100ns intervals to 1ms intervals
result -= 11644473600000; // to shift from epoch of 1/1/1601 to 1/1/1970
return (int64)result;
#elif defined POSIX
int64 result;
struct timeval tv;
gettimeofday(&tv, null);
result = tv.tv_sec;
result *= 1000000; // convert seconds to microseconds
result += tv.tv_usec; // add microseconds
result /= 1000; // convert to milliseconds
return result;
#endif
}