1 /* @(#)gettimeofday.c	1.9 12/11/29 Copyright 2007-2012 J. Schilling */
2 #include <schily/mconfig.h>
3 #ifndef lint
4 static	UConst char sccsid[] =
5 	"@(#)gettimeofday.c	1.9 12/11/29 Copyright 2007-2012 J. Schilling";
6 #endif
7 /*
8  *	Emulate gettimeofday where it does not exist
9  *
10  *	Copyright (c) 2007-2012 J. Schilling
11  */
12 /*
13  * The contents of this file are subject to the terms of the
14  * Common Development and Distribution License, Version 1.0 only
15  * (the "License").  You may not use this file except in compliance
16  * with the License.
17  *
18  * See the file CDDL.Schily.txt in this distribution for details.
19  * A copy of the CDDL is also available via the Internet at
20  * http://www.opensource.org/licenses/cddl1.txt
21  *
22  * When distributing Covered Code, include this CDDL HEADER in each
23  * file and include the License file CDDL.Schily.txt from this distribution.
24  */
25 
26 #if	!defined(HAVE_GETTIMEOFDAY)
27 #if	(defined(_MSC_VER) || defined(__MINGW32__))
28 #include <schily/windows.h>
29 #include <schily/time.h>
30 #include <schily/utypes.h>
31 #include <schily/standard.h>
32 
33 #ifdef	_MSC_VER
34 const	__int64 MS_FTIME_ADD	= 0x2b6109100i64;
35 const	__int64 MS_FTIME_SECS	= 10000000i64;
36 #else
37 const	Int64_t MS_FTIME_ADD	= 0x2b6109100LL;
38 const	Int64_t MS_FTIME_SECS	= 10000000LL;
39 #endif
40 
41 EXPORT int
gettimeofday(tp,dummy)42 gettimeofday(tp, dummy)
43 	struct timeval	*tp;
44 	void		*dummy;		/* tzp is unspecified by POSIX */
45 {
46 	FILETIME	ft;
47 	Int64_t		T;
48 
49 	if (tp == 0)
50 		return (0);
51 
52 	GetSystemTimeAsFileTime(&ft);	/* 100ns time since 1601 */
53 	T   = ft.dwHighDateTime;
54 	T <<= 32;
55 	T  += ft.dwLowDateTime;
56 
57 	/*
58 	 * Cast to avoid a loss of data warning
59 	 * MSVC uses long instead of time_t for tv_sec
60 	 */
61 	tp->tv_sec  = (long) (T / MS_FTIME_SECS - MS_FTIME_ADD);
62 	tp->tv_usec = (long) (T % MS_FTIME_SECS) / 10;
63 
64 	return (0);
65 }
66 #else	/* (defined(_MSC_VER) || defined(__MINGW32__)) */
67 
68 #ifdef	HAVE_TIME
69 #include <schily/time.h>
70 #include <schily/standard.h>
71 
72 EXPORT int
gettimeofday(tp,dummy)73 gettimeofday(tp, dummy)
74 	struct timeval	*tp;
75 	void		*dummy;		/* tzp is unspecified by POSIX */
76 {
77 	time_t	t;
78 
79 	if (tp == 0)
80 		return (0);
81 
82 	(void) time(&t);
83 	tp->tv_sec  = t;
84 	tp->tv_usec = 0;
85 
86 	return (0);
87 }
88 #endif
89 
90 #endif	/* (defined(_MSC_VER) || defined(__MINGW32__)) */
91 #endif	/* !defined(HAVE_GETTIMEOFDAY) */
92