-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathtimespec.c
110 lines (91 loc) · 2.72 KB
/
timespec.c
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include <edidentifier.h>
__CIDENT_RCSID(gr_timespec_c,"$Id: timespec.c,v 1.11 2024/04/17 15:57:14 cvsuser Exp $")
/* -*- mode: c; indent-width: 4; -*- */
/* $Id: timespec.c,v 1.11 2024/04/17 15:57:14 cvsuser Exp $
* timespec util functions.
*
*
* Copyright (c) 1998 - 2024, Adam Young.
* All rights reserved.
*
* This file is part of the GRIEF Editor.
*
* The GRIEF Editor is free software: you can redistribute it
* and/or modify it under the terms of the GRIEF Editor License.
*
* Redistributions of source code must retain the above copyright
* notice, and must be distributed with the license document above.
*
* Redistributions in binary form must reproduce the above copyright
* notice, and must include the license document above in
* the documentation and/or other materials provided with the
* distribution.
*
* The GRIEF Editor is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* License for more details.
* ==end==
*/
#include <edthreads.h>
#include <libtime.h>
struct timespec
timespec_diff(const struct timespec end, const struct timespec start)
{
struct timespec diff;
if ((end.tv_nsec - start.tv_nsec) < 0) {
diff.tv_sec = end.tv_sec - start.tv_sec - 1;
diff.tv_nsec = 1000000000 +end.tv_nsec - start.tv_nsec;
} else {
diff.tv_sec = end.tv_sec - start.tv_sec;
diff.tv_nsec = end.tv_nsec -start.tv_nsec;
}
return diff;
}
struct timespec
timespec_sub(struct timespec ts1, struct timespec ts2)
{
struct timespec rtn_val;
int xsec;
int sign = 1;
if (ts2.tv_nsec > ts1.tv_nsec ) {
xsec = (int)((ts2.tv_nsec - ts1.tv_nsec) / (1E9 + 1));
ts2.tv_nsec -= (long int)(1E9 * xsec);
ts2.tv_sec += xsec;
}
if ((ts1.tv_nsec - ts2.tv_nsec) > 1E9 ) {
xsec = (int)((ts1.tv_nsec - ts2.tv_nsec) / 1E9);
ts2.tv_nsec += (long int)(1E9 * xsec);
ts2.tv_sec -= xsec;
}
rtn_val.tv_sec = ts1.tv_sec - ts2.tv_sec;
rtn_val.tv_nsec = ts1.tv_nsec - ts2.tv_nsec;
if (ts1.tv_sec < ts2.tv_sec) {
sign = -1;
}
rtn_val.tv_sec = rtn_val.tv_sec * sign;
return rtn_val;
}
int
timespec_greater(struct timespec ts1, struct timespec ts2)
{
if (ts1.tv_sec < ts2.tv_sec) {
return 0;
} else if (ts1.tv_sec == ts2.tv_sec) {
if (ts1.tv_nsec < ts2.tv_nsec) {
return 0;
}
}
return 1;
}
double
timespec_seconds(struct timespec ts1, struct timespec ts2)
{
struct timespec diff;
double ret;
diff = timespec_sub(ts1, ts2);
ret = (double)diff.tv_sec;
ret += (double)diff.tv_nsec/(double)1E9;
return ret;
}
/*end*/