1/*
2 * bcmseclib_win_timer.c -- windows platform dependent timer stuff
3 *
4 * Copyright (C) 2014, Broadcom Corporation
5 * All Rights Reserved.
6 *
7 * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Broadcom Corporation;
8 * the contents of this file may not be disclosed to third parties, copied
9 * or duplicated in any form, in whole or in part, without the prior
10 * written permission of Broadcom Corporation.
11 *
12 * $Id: bcmseclib_win_timer.c,v 1.1 2010-12-11 00:53:59 $
13 */
14
15#include <bcmseclib_timer.h>
16#include <bcmseclib_timer_os.h>
17#include <windows.h>
18
19
20int
21bcmseclib_os_get_time(bcmseclib_time_t *time)
22{
23	FILETIME	file_time;
24	ULARGE_INTEGER	ularge_int;
25
26	/* Retrieves the current system date and time in Coordinated Universal
27	 * Time (UTC) format. FILETIME contains a 64-bit value representing the
28	 * number of 100-nanosecond intervals since January 1, 1601 (UTC).
29	 */
30	GetSystemTimeAsFileTime(&file_time);
31
32	/* From the MSDN documentation: "It is not recommended that you add and
33	 * subtract values from the FILETIME structure to obtain relative times.
34	 * Instead, you should copy the low- and high-order parts of the file time
35	 * to a ULARGE_INTEGER structure, perform 64-bit arithmetic on the QuadPart
36	 * member, and copy the LowPart and HighPart members into the FILETIME structure."
37	 */
38	ularge_int.LowPart = file_time.dwLowDateTime;
39	ularge_int.HighPart = file_time.dwHighDateTime;
40
41	/* Convert to usec units. */
42	ularge_int.QuadPart /= 10;
43
44	/* Windows uses an epoch time of Jan 1, 1601, and Unix uses Jan 1, 1970.
45	 * Convert from Windows to Unix epoch for consistentcy and to avoid
46	 * overflow in the number of seconds. 11644473600000000 is the number of
47	 * usec between the Windows and Unix epoc times.
48	 */
49	ularge_int.QuadPart -= 11644473600000000;
50
51	/* Separate out sec and usec units. */
52	time->sec = (long)(ularge_int.QuadPart / 1000000);
53	time->usec = (long)(ularge_int.QuadPart % 1000000);
54
55	return (0);
56}
57