xref: /freebsd/crypto/libecc/src/external_deps/time.c (revision f0865ec9906d5a18fa2a3b61381f22ce16e606ad)
1*f0865ec9SKyle Evans /*
2*f0865ec9SKyle Evans  *  Copyright (C) 2017 - This file is part of libecc project
3*f0865ec9SKyle Evans  *
4*f0865ec9SKyle Evans  *  Authors:
5*f0865ec9SKyle Evans  *      Ryad BENADJILA <ryadbenadjila@gmail.com>
6*f0865ec9SKyle Evans  *      Arnaud EBALARD <arnaud.ebalard@ssi.gouv.fr>
7*f0865ec9SKyle Evans  *      Jean-Pierre FLORI <jean-pierre.flori@ssi.gouv.fr>
8*f0865ec9SKyle Evans  *
9*f0865ec9SKyle Evans  *  Contributors:
10*f0865ec9SKyle Evans  *      Nicolas VIVET <nicolas.vivet@ssi.gouv.fr>
11*f0865ec9SKyle Evans  *      Karim KHALFALLAH <karim.khalfallah@ssi.gouv.fr>
12*f0865ec9SKyle Evans  *
13*f0865ec9SKyle Evans  *  This software is licensed under a dual BSD and GPL v2 license.
14*f0865ec9SKyle Evans  *  See LICENSE file at the root folder of the project.
15*f0865ec9SKyle Evans  */
16*f0865ec9SKyle Evans 
17*f0865ec9SKyle Evans #include <libecc/external_deps/time.h>
18*f0865ec9SKyle Evans 
19*f0865ec9SKyle Evans /* Unix and compatible case (including macOS) */
20*f0865ec9SKyle Evans #if defined(WITH_STDLIB) && (defined(__unix__) || defined(__APPLE__))
21*f0865ec9SKyle Evans #include <stddef.h>
22*f0865ec9SKyle Evans #include <sys/time.h>
23*f0865ec9SKyle Evans 
get_ms_time(u64 * time)24*f0865ec9SKyle Evans int get_ms_time(u64 *time)
25*f0865ec9SKyle Evans {
26*f0865ec9SKyle Evans 	struct timeval tv;
27*f0865ec9SKyle Evans 	int ret;
28*f0865ec9SKyle Evans 
29*f0865ec9SKyle Evans 	if (time == NULL) {
30*f0865ec9SKyle Evans 		ret = -1;
31*f0865ec9SKyle Evans 		goto err;
32*f0865ec9SKyle Evans 	}
33*f0865ec9SKyle Evans 
34*f0865ec9SKyle Evans 	ret = gettimeofday(&tv, NULL);
35*f0865ec9SKyle Evans 	if (ret < 0) {
36*f0865ec9SKyle Evans 		ret = -1;
37*f0865ec9SKyle Evans 		goto err;
38*f0865ec9SKyle Evans 	}
39*f0865ec9SKyle Evans 
40*f0865ec9SKyle Evans 	(*time) = (u64)(((tv.tv_sec) * 1000) + ((tv.tv_usec) / 1000));
41*f0865ec9SKyle Evans 	ret = 0;
42*f0865ec9SKyle Evans 
43*f0865ec9SKyle Evans err:
44*f0865ec9SKyle Evans 	return ret;
45*f0865ec9SKyle Evans }
46*f0865ec9SKyle Evans 
47*f0865ec9SKyle Evans /* Windows case */
48*f0865ec9SKyle Evans #elif defined(WITH_STDLIB) && defined(__WIN32__)
49*f0865ec9SKyle Evans #include <stddef.h>
50*f0865ec9SKyle Evans #include <windows.h>
get_ms_time(u64 * time)51*f0865ec9SKyle Evans int get_ms_time(u64 *time)
52*f0865ec9SKyle Evans {
53*f0865ec9SKyle Evans 	int ret;
54*f0865ec9SKyle Evans 	SYSTEMTIME st;
55*f0865ec9SKyle Evans 
56*f0865ec9SKyle Evans 	if (time == NULL) {
57*f0865ec9SKyle Evans 		ret = -1;
58*f0865ec9SKyle Evans 		goto err;
59*f0865ec9SKyle Evans 	}
60*f0865ec9SKyle Evans 
61*f0865ec9SKyle Evans 	GetSystemTime(&st);
62*f0865ec9SKyle Evans 	(*time) = (u64)((((st.wMinute * 60) + st.wSecond) * 1000) + st.wMilliseconds);
63*f0865ec9SKyle Evans 	ret = 0;
64*f0865ec9SKyle Evans 
65*f0865ec9SKyle Evans err:
66*f0865ec9SKyle Evans 	return ret;
67*f0865ec9SKyle Evans }
68*f0865ec9SKyle Evans 
69*f0865ec9SKyle Evans /* No platform detected, the used must provide an implementation! */
70*f0865ec9SKyle Evans #else
71*f0865ec9SKyle Evans #error "time.c: you have to implement get_ms_time()"
72*f0865ec9SKyle Evans #endif
73