forked from facebook/watchman
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtime.c
65 lines (51 loc) · 1.74 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/* Copyright 2014-present Facebook, Inc.
* Licensed under the Apache License, Version 2.0 */
#include "watchman.h"
// 100's of nanoseconds since the FILETIME epoch
static const uint64_t EPOCH = ((uint64_t) 116444736000000000ULL);
// FILETIME is expressed in 100's of nanoseconds
void FILETIME_LARGE_INTEGER_to_timespec(LARGE_INTEGER ft, struct timespec *ts) {
static const uint32_t factor = WATCHMAN_NSEC_IN_SEC / 100;
ft.QuadPart -= EPOCH;
ts->tv_sec = ft.QuadPart / factor;
ft.QuadPart -= ts->tv_sec * factor;
ts->tv_nsec = ft.QuadPart * 100;
}
void FILETIME_to_timespec(const FILETIME *ft, struct timespec *ts) {
LARGE_INTEGER li;
li.HighPart = ft->dwHighDateTime;
li.LowPart = ft->dwLowDateTime;
FILETIME_LARGE_INTEGER_to_timespec(li, ts);
}
static void timespec_to_timeval(const struct timespec *ts, struct timeval *tv) {
tv->tv_sec = (long)ts->tv_sec;
tv->tv_usec = (long)(ts->tv_nsec / WATCHMAN_NSEC_IN_USEC);
}
int gettimeofday(struct timeval *tv, void *ignored) {
SYSTEMTIME system_time;
FILETIME file_time;
struct timespec ts;
unused_parameter(ignored);
GetSystemTime( &system_time );
SystemTimeToFileTime( &system_time, &file_time );
FILETIME_to_timespec(&file_time, &ts);
timespec_to_timeval(&ts, tv);
return 0;
}
void usleep(int64_t usec) {
HANDLE timer;
LARGE_INTEGER ft;
// Convert to 100 nanosecond interval, negative value indicates relative time
ft.QuadPart = -(10*usec);
timer = CreateWaitableTimer(NULL, TRUE, NULL);
if (!timer) {
return;
}
SetWaitableTimer(timer, &ft, 0, NULL, NULL, 0);
// Use an alertable wait to play well with our stream implementation
WaitForSingleObjectEx(timer, INFINITE, true);
CloseHandle(timer);
}
void sleep(int sec) {
SleepEx(sec * 1000, true);
}