summaryrefslogtreecommitdiffstatshomepage
path: root/3rdparty/genie/src/host/os_ticks.c
blob: 1d0588c8cbe748fecba47c2556703f6cbd6bdf88 (plain) (blame)
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
/**
 * \file   os_ticks.c
 * \brief  Return the system tick counter (in microsecond units).
 */

#include <stdlib.h>
#include "premake.h"

// Epochs used by Windows and Unix time APIs.  These adjustments,
// when added to the time value returned by the OS, will yield
// the number of microsecond intervals since Jan 1, year 1.
#define TICK_EPOCH_WINDOWS ((lua_Integer)0x0701ce1722770000)
#define TICK_EPOCH_UNIX    ((lua_Integer)0x00dcbffeff2bc000)

#define TICKS_PER_SECOND   ((lua_Integer)1000000)

int os_ticks(lua_State* L)
{
    lua_Integer ticks = 0;

#if PLATFORM_WINDOWS
    FILETIME fileTimeUtc;
    GetSystemTimeAsFileTime(&fileTimeUtc);
    ticks =
        TICK_EPOCH_WINDOWS
        + ((lua_Integer)fileTimeUtc.dwHighDateTime << 32
            | (lua_Integer)fileTimeUtc.dwLowDateTime);
    ticks /= (lua_Integer)10;
#else
    struct timeval tp;
    if (gettimeofday(&tp, NULL) == 0) {
        ticks = 
            TICK_EPOCH_UNIX
            + (lua_Integer)tp.tv_sec * TICKS_PER_SECOND
            + (lua_Integer)tp.tv_usec;
    }
#endif

    lua_pushinteger(L, ticks);
    return 1;
}