summaryrefslogtreecommitdiffstats
path: root/web/server/h2o/libh2o/deps/picotls/picotlsvs/picotls/wintimeofday.c
blob: a293707ea2435ea2fe045ec96b9dd43d03ac37f0 (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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/*
 * Copyright (c) 2016 Christian Huitema <huitema@huitema.net>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#ifdef _WINDOWS
#include "wincompat.h"

 /*
  * This is a simple replacement for the "gettimeofday" finction,
  * which is not available on Windows
  */

int wintimeofday(struct timeval* tv, struct timezone* tz)
{
	FILETIME ft;
	uint64_t now = 0;

	/*
	 * The GetSystemTimeAsFileTime API returns  the number
	 * of 100-nanosecond intervals since January 1, 1601 (UTC),
	 * in FILETIME format.
	 */
	GetSystemTimeAsFileTime(&ft);

	/*
	 * Convert to plain 64 bit format, without making
	 * assumptions about the FILETIME structure alignment.
	 */
	now = ft.dwHighDateTime;
	now <<= 32;
	now |= ft.dwLowDateTime;
	/*
	 * Convert units from 100ns to 1us
	 */
	now /= 10;
	/*
	 * Account for microseconds elapsed between 1601 and 1970.
	 */
	now -= 11644473600000000ULL;

	if (tv != NULL)
	{
		uint64_t sec = now / 1000000;
		uint64_t usec = now % 1000000;

		tv->tv_sec = (long)sec;
		tv->tv_usec = (long)usec;
	}

	if (tz != NULL)
	{
	    /*
		 * TODO: implement a timezone retrieval function.
		 * Not urgent, since the GetDNS code always set this parameter to NULL.
		 */ 
		return -1;
	}

	return 0;
}
#endif /* WIN32 */