summaryrefslogtreecommitdiff
path: root/source/kit/time.h
blob: c14f8ff63ec11d3f3f96d0f1f67d8daad8c80b18 (plain)
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
48
49
50
51
52
53
54
#ifndef KIT_TIME_H
#define KIT_TIME_H

#ifndef _GNU_SOURCE
#  define _GNU_SOURCE
#endif

#include <time.h>

#ifdef __cplusplus
extern "C" {
#endif

#ifndef TIME_UTC
#  define TIME_UTC 1
#endif

#ifdef KIT_REQUIRE_TIMESPEC_GET
#  ifndef WIN32_LEAN_AND_MEAN
#    define WIN32_LEAN_AND_MEAN 1
#  endif
#  ifndef NOMINMAX
#    define NOMINMAX 1
#  endif
#  include <windows.h>

#  define KIT_TIMESPEC_IMPL_UNIX_EPOCH_IN_TICKS 116444736000000000ull
#  define KIT_TIMESPEC_IMPL_TICKS_PER_SECONDS 10000000ull

static int timespec_get(struct timespec *ts, int base) {
  if (ts == NULL || base != TIME_UTC)
    return 0;

  FILETIME       ft;
  ULARGE_INTEGER date;
  LONGLONG       ticks;

  GetSystemTimeAsFileTime(&ft);
  date.HighPart = ft.dwHighDateTime;
  date.LowPart  = ft.dwLowDateTime;
  ticks         = (LONGLONG) (date.QuadPart -
                      KIT_TIMESPEC_IMPL_UNIX_EPOCH_IN_TICKS);
  ts->tv_sec    = ticks / KIT_TIMESPEC_IMPL_TICKS_PER_SECONDS;
  ts->tv_nsec   = (ticks % KIT_TIMESPEC_IMPL_TICKS_PER_SECONDS) * 100;

  return base;
}
#endif

#ifdef __cplusplus
}
#endif

#endif