Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
n_time.c
Go to the documentation of this file.
1/*
2 * Nilorea Library
3 * Copyright (C) 2005-2026 Castagnier Mickael
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17 */
18
26#include "nilorea/n_time.h"
27
28#include <stdio.h>
29#include <stdlib.h>
30#include <unistd.h>
31
32#if defined(__windows__)
37void u_sleep(__int64 usec) {
38 HANDLE timer;
39 LARGE_INTEGER ft;
40
41 ft.QuadPart = -(10 * usec); // Convert to 100 nanosecond interval, negative value indicates relative time
42
43 timer = CreateWaitableTimer(NULL, TRUE, NULL);
44 SetWaitableTimer(timer, &ft, 0, NULL, NULL, 0);
45 WaitForSingleObject(timer, INFINITE);
46 CloseHandle(timer);
47}
48#else
53void u_sleep(unsigned int usec) {
54 usleep(usec);
55}
56#endif
57
61void PAUSE(void) {
62 char k[2] = "";
63
64 printf("Press enter to continue...");
65
66 int n = scanf("%c", k);
67 int error = errno;
68 if (n == EOF) {
69 n_log(LOG_DEBUG, "Enter key was pressed !");
70 } else if (error != 0) {
71 n_log(LOG_ERR, "error %s when waiting for a key !", strerror(error));
72 } else {
73 n_log(LOG_DEBUG, "No matching characters");
74 }
75
76 printf("\n");
77} /* PAUSE(...) */
78
84int start_HiTimer(N_TIME* timer) {
85 __n_assert(timer, return FALSE);
86 timer->delta = 0;
87
88#if defined(__windows__)
89 if (QueryPerformanceFrequency((LARGE_INTEGER*)&timer->freq) == 0)
90 return FALSE;
91 if (QueryPerformanceCounter(&timer->startTime) == 0)
92 return FALSE;
93#else
94 if (gettimeofday(&timer->startTime, 0) != 0)
95 return FALSE;
96#endif
97
98 return TRUE;
99} /* init_HiTimer(...) */
100
106time_t get_usec(N_TIME* timer) {
107 __n_assert(timer, return 0);
108#ifdef __windows__
109 QueryPerformanceCounter((LARGE_INTEGER*)&timer->currentTime);
110 timer->delta = 1000000 * (timer->currentTime.QuadPart - timer->startTime.QuadPart) / timer->freq.QuadPart;
111 timer->startTime = timer->currentTime;
112#else
113 gettimeofday(&timer->currentTime, 0);
114 timer->delta = (timer->currentTime.tv_sec - timer->startTime.tv_sec) * 1000000 + (timer->currentTime.tv_usec - timer->startTime.tv_usec);
115 timer->startTime.tv_sec = timer->currentTime.tv_sec;
116 timer->startTime.tv_usec = timer->currentTime.tv_usec;
117#endif
118
119 return timer->delta;
120} /* get_usec( ... ) */
121
127time_t get_msec(N_TIME* timer) {
128 __n_assert(timer, return 0);
129#ifdef __windows__
130 QueryPerformanceCounter((LARGE_INTEGER*)&timer->currentTime);
131 timer->delta = 1000 * (timer->currentTime.QuadPart - timer->startTime.QuadPart) / timer->freq.QuadPart;
132 timer->startTime = timer->currentTime;
133#else
134 gettimeofday(&timer->currentTime, 0);
135 timer->delta = (timer->currentTime.tv_sec - timer->startTime.tv_sec) * 1000 + (timer->currentTime.tv_usec - timer->startTime.tv_usec) / 1000;
136 timer->startTime.tv_sec = timer->currentTime.tv_sec;
137 timer->startTime.tv_usec = timer->currentTime.tv_usec;
138#endif
139
140 return timer->delta;
141} /* get_msec(...) */
142
148time_t get_sec(N_TIME* timer) {
149 __n_assert(timer, return 0);
150#ifdef __windows__
151 QueryPerformanceCounter((LARGE_INTEGER*)&timer->currentTime);
152 timer->delta = (timer->currentTime.QuadPart - timer->startTime.QuadPart) / timer->freq.QuadPart;
153 timer->startTime = timer->currentTime;
154#else
155 gettimeofday(&timer->currentTime, 0);
156 timer->delta = (timer->currentTime.tv_sec - timer->startTime.tv_sec) + (timer->currentTime.tv_usec - timer->startTime.tv_usec) / 1000000;
157 timer->startTime.tv_sec = timer->currentTime.tv_sec;
158 timer->startTime.tv_usec = timer->currentTime.tv_usec;
159#endif
160 return timer->delta;
161} /* get_sec(...) */
#define __n_assert(__ptr, __ret)
macro to assert things
Definition n_common.h:278
#define n_log(__LEVEL__,...)
Logging function wrapper to get line and func.
Definition n_log.h:88
#define LOG_DEBUG
debug-level messages
Definition n_log.h:83
#define LOG_ERR
error conditions
Definition n_log.h:75
struct timeval startTime
start time
Definition n_time.h:60
time_t delta
time since last poll
Definition n_time.h:50
struct timeval currentTime
current time
Definition n_time.h:62
void u_sleep(unsigned int usec)
wrapper around usleep for API consistency
Definition n_time.c:53
int start_HiTimer(N_TIME *timer)
Initialize or restart from zero any N_TIME HiTimer.
Definition n_time.c:84
time_t get_usec(N_TIME *timer)
Poll any N_TIME HiTimer, returning usec, and moving currentTime to startTime.
Definition n_time.c:106
time_t get_msec(N_TIME *timer)
Poll any N_TIME HiTimer, returning msec, and moving currentTime to startTime.
Definition n_time.c:127
time_t get_sec(N_TIME *timer)
Poll any N_TIME HiTimer, returning sec, and moving currentTime to startTime.
Definition n_time.c:148
void PAUSE(void)
make a pause in a terminal
Definition n_time.c:61
Timing Structure.
Definition n_time.h:48
Timing utilities.