xref: /freebsd/crypto/krb5/src/util/support/gettimeofday.c (revision d0ff5773cefaf3fa41b1be3e44ca35bd9d5f68ee)
1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /* util/support/gettimeofday.c */
3 /*
4  * Copyright (C) 2011 by the Massachusetts Institute of Technology.
5  * All rights reserved.
6  *
7  * Export of this software from the United States of America may
8  *   require a specific license from the United States Government.
9  *   It is the responsibility of any person or organization contemplating
10  *   export to obtain such a license before exporting.
11  *
12  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
13  * distribute this software and its documentation for any purpose and
14  * without fee is hereby granted, provided that the above copyright
15  * notice appear in all copies and that both that copyright notice and
16  * this permission notice appear in supporting documentation, and that
17  * the name of M.I.T. not be used in advertising or publicity pertaining
18  * to distribution of the software without specific, written prior
19  * permission.  Furthermore if you modify this software you must label
20  * your software as modified software and not distribute it in such a
21  * fashion that it might be confused with the original M.I.T. software.
22  * M.I.T. makes no representations about the suitability of
23  * this software for any purpose.  It is provided "as is" without express
24  * or implied warranty.
25  */
26 /*
27  * Copyright (c) 1995, 1996, 1997 Kungliga Tekniska Högskolan
28  * (Royal Institute of Technology, Stockholm, Sweden).
29  * All rights reserved.
30  *
31  * Redistribution and use in source and binary forms, with or without
32  * modification, are permitted provided that the following conditions
33  * are met:
34  *
35  * 1. Redistributions of source code must retain the above copyright
36  *    notice, this list of conditions and the following disclaimer.
37  *
38  * 2. Redistributions in binary form must reproduce the above copyright
39  *    notice, this list of conditions and the following disclaimer in the
40  *    documentation and/or other materials provided with the distribution.
41  *
42  * 3. Neither the name of the Institute nor the names of its contributors
43  *    may be used to endorse or promote products derived from this software
44  *    without specific prior written permission.
45  *
46  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
47  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
48  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
49  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
50  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
51  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
52  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
53  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
54  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
55  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
56  * SUCH DAMAGE.
57  */
58 
59 #include "k5-platform.h"
60 
61 #ifdef _WIN32
62 
63 #include <winsock2.h>
64 
65 int
66 krb5int_gettimeofday (struct timeval *tp, void *ignore)
67 {
68     FILETIME ft;
69     ULARGE_INTEGER li;
70     ULONGLONG ull;
71 
72     GetSystemTimeAsFileTime(&ft);
73     li.LowPart = ft.dwLowDateTime;
74     li.HighPart = ft.dwHighDateTime;
75     ull = li.QuadPart;
76 
77     ull -= 116444736000000000i64;
78     ull /= 10i64;               /* ull is now in microseconds */
79 
80     tp->tv_usec = (long)(ull % 1000000i64);
81     tp->tv_sec  = (long)(ull / 1000000i64);
82 
83     return 0;
84 }
85 
86 #else
87 
88 /*
89  * Simple gettimeofday that only returns seconds.
90  */
91 int
92 krb5int_gettimeofday (struct timeval *tp, void *ignore)
93 {
94     time_t t;
95 
96     t = time(NULL);
97     tp->tv_sec  = (long) t;
98     tp->tv_usec = 0;
99     return 0;
100 }
101 
102 #endif  /* !_WIN32 */
103