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