-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtime_utils.c
More file actions
48 lines (40 loc) · 1.69 KB
/
Copy pathtime_utils.c
File metadata and controls
48 lines (40 loc) · 1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* time_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dporhomo <dporhomo@student.42prague.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/06/07 19:38:52 by dporhomo #+# #+# */
/* Updated: 2026/06/14 01:37:25 by dporhomo ### ########.fr */
/* */
/* ************************************************************************** */
/* Time management utils */
#include "codexion.h"
/* timeeval - time in sec (tv_sec) and ms (tv_usec)*/
/* tv_sec: seconds since Epoch, tv_usec: microseconds in last second*/
long long get_current_time(void)
{
struct timeval tv;
if (gettimeofday(&tv, NULL) == -1)
return (-1);
return ((tv.tv_sec * 1000) + (tv.tv_usec / 1000));
}
/* sleeps for time_in_ms in 500 microsec intervals*/
void ft_usleep(long long time_in_ms)
{
long long start;
start = get_current_time();
while ((get_current_time() - start) < time_in_ms)
usleep(500);
}
/* Converts an absolute time in ms to sc */
/* ms to sec */
/* micros % to 0, remaining ml to nanosec*/
struct timespec ms_to_timespec(long long target_time_ms)
{
struct timespec ts;
ts.tv_sec = target_time_ms / 1000;
ts.tv_nsec = (target_time_ms % 1000) * 1000000;
return (ts);
}