Fix up path generation to use OPENSSL_MODULES
[openssl.git] / ssl / time.c
1 /*
2  * Copyright 2022 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 #include <errno.h>
11 #include <openssl/err.h>
12 #include "internal/time.h"
13
14 OSSL_TIME ossl_time_now(void)
15 {
16 #if defined(_WIN32)
17     SYSTEMTIME st;
18     union {
19         unsigned __int64 ul;
20         FILETIME ft;
21     } now;
22
23     GetSystemTime(&st);
24     SystemTimeToFileTime(&st, &now.ft);
25     /* re-bias to 1/1/1970 */
26 # ifdef  __MINGW32__
27     now.ul -= 116444736000000000ULL;
28 # else
29     now.ul -= 116444736000000000UI64;
30 # endif
31     return ((uint64_t)now.ul) * (OSSL_TIME_SECOND / 10000000);
32 #else
33     struct timeval t;
34
35     if (gettimeofday(&t, NULL) < 0) {
36         ERR_raise_data(ERR_LIB_SYS, get_last_sys_error(),
37                        "calling gettimeofday()");
38         return 0;
39     }
40     if (t.tv_sec <= 0)
41         return t.tv_usec <= 0 ? 0 : t.tv_usec * (OSSL_TIME_SECOND / 1000000);
42     return ((uint64_t)t.tv_sec * 1000000 + t.tv_usec)
43            * (OSSL_TIME_SECOND / 1000000);
44 #endif
45 }
46