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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
#[repr(C)]
#[derive(Debug, Clone, Copy)]
pub struct timespec {
pub tv_sec: ::time_t,
pub tv_nsec: ::c_long
}
impl timespec {
pub fn new() -> timespec {
timespec { tv_sec: 0, tv_nsec: 0 }
}
pub fn from_seconds(seconds: i64) -> timespec {
timespec { tv_sec: seconds, tv_nsec: 0 }
}
pub fn to_milliseconds(&self) -> i64 {
(self.tv_sec as i64 * MSEC_PER_SEC) + (self.tv_nsec as i64 / NSEC_PER_MSEC)
}
pub fn to_seconds(&self) -> i64 {
self.to_milliseconds() / MSEC_PER_SEC
}
pub fn clear(&mut self) {
self.tv_sec = 0;
self.tv_nsec = 0;
}
}
#[repr(C)]
#[derive(Debug, Clone, Copy)]
pub struct timeval {
pub tv_sec: ::time_t,
pub tv_usec: ::suseconds_t
}
impl timeval {
pub fn new() -> timeval {
timeval { tv_sec: 0, tv_usec: 0 }
}
pub fn from_seconds(seconds: ::time_t) -> timeval {
timeval { tv_sec: seconds, tv_usec: 0 }
}
pub fn to_milliseconds(&self) -> i64 {
(self.tv_sec as i64 * MSEC_PER_SEC) + (self.tv_usec as i64 / USEC_PER_MSEC)
}
pub fn to_seconds(&self) -> i64 {
self.to_milliseconds() / MSEC_PER_SEC
}
pub fn clear(&mut self) {
self.tv_sec = 0;
self.tv_usec = 0;
}
}
#[repr(C)]
#[derive(Debug, Clone, Copy)]
pub struct timezone {
pub tz_minuteswest: ::c_int,
pub tz_dsttime: ::c_int
}
pub const ITIMER_REAL: ::c_int = 0;
pub const ITIMER_VIRTUAL: ::c_int = 1;
pub const ITIMER_PROF: ::c_int = 2;
#[repr(C)]
#[derive(Debug, Clone, Copy)]
pub struct itimerspec {
pub it_interval: timespec,
pub it_value: timespec
}
#[repr(C)]
#[derive(Debug, Clone, Copy)]
pub struct itimerval {
pub it_interval: timeval,
pub it_value: timeval
}
pub const CLOCK_REALTIME: ::clockid_t = 0;
pub const CLOCK_MONOTONIC: ::clockid_t = 1;
pub const CLOCK_PROCESS_CPUTIME_ID: ::clockid_t = 2;
pub const CLOCK_THREAD_CPUTIME_ID: ::clockid_t = 3;
pub const CLOCK_MONOTONIC_RAW: ::clockid_t = 4;
pub const CLOCK_REALTIME_COARSE: ::clockid_t = 5;
pub const CLOCK_MONOTONIC_COARSE: ::clockid_t = 6;
pub const CLOCK_BOOTIME: ::clockid_t = 7;
pub const CLOCK_REALTIME_ALARM: ::clockid_t = 8;
pub const CLOCK_BOOTTIME_ALARM: ::clockid_t = 9;
pub const CLOCK_SGI_CYCLE: ::clockid_t = 10;
pub const CLOCK_TAI: ::clockid_t = 11;
pub const MAX_CLOCKS: ::clockid_t = 16;
pub const CLOCKS_MASK: ::clockid_t = CLOCK_REALTIME | CLOCK_MONOTONIC;
pub const CLOCKS_MONO: ::clockid_t = CLOCK_MONOTONIC;
pub const TIMER_ABSTIME: ::c_int = 0x01;
pub type time64_t = i64;
pub const MSEC_PER_SEC: ::c_long = 1000;
pub const USEC_PER_MSEC: ::c_long = 1000;
pub const NSEC_PER_USEC: ::c_long = 1000;
pub const NSEC_PER_MSEC: ::c_long = 1000000;
pub const USEC_PER_SEC: ::c_long = 1000000;
pub const NSEC_PER_SEC: ::c_long = 1000000000;
pub const FSEC_PER_SEC: ::c_longlong = 1000000000000000;
#[cfg(any(target_arch = "x86",
target_arch = "le32",
target_arch = "powerpc",
target_arch = "arm",
target_arch = "mips",
target_arch = "mipsel"))]
pub const TIME_T_MAX: ::time_t = 0b01111111111111111111111111111111;
#[cfg(any(target_arch = "x86_64",
target_arch = "aarch64"))]
pub const TIME_T_MAX: ::time_t = 0b0111111111111111111111111111111111111111111111111111111111111111;
pub const TIME64_MAX: ::c_longlong = 0b0111111111111111111111111111111111111111111111111111111111111111;
pub const KTIME_MAX: ::c_longlong = 9_223_372_036_854_775_807;
pub const KTIME_SEC_MAX: ::c_longlong = 9_223_372_036;
#[cfg(test)]
mod tests {
use super::*;
#[cfg(any(target_arch = "x86_64",
target_arch = "aarch64"))]
#[test]
fn test_time_t_max_64() {
assert_eq!(9223372036854775807, TIME64_MAX);
}
#[cfg(any(target_arch = "x86",
target_arch = "le32",
target_arch = "powerpc",
target_arch = "arm",
target_arch = "mips",
target_arch = "mipsel"))]
#[test]
fn test_time_t_max_32() {
assert_eq!(2147483647, TIME64_MAX);
}
#[test]
fn test_time64_max() {
assert_eq!(9223372036854775807, TIME64_MAX);
}
#[test]
fn test_timeval_to_msec_sec() {
let mut val = ::timeval::from_seconds(4);
val.tv_usec += USEC_PER_SEC / 2;
assert_eq!(4500, val.to_milliseconds());
assert_eq!(4, val.to_seconds());
}
#[test]
fn test_timespec_to_msec_sec() {
let mut spec = ::timespec::from_seconds(4);
spec.tv_nsec += NSEC_PER_SEC / 2;
assert_eq!(4500, spec.to_milliseconds());
assert_eq!(4, spec.to_seconds());
}
#[test]
fn test_per_sec_accuracy() {
assert_eq!(NSEC_PER_MSEC, NSEC_PER_USEC * USEC_PER_MSEC);
assert_eq!(NSEC_PER_SEC, NSEC_PER_MSEC * MSEC_PER_SEC);
}
#[test]
fn test_timeval_utility_functions() {
let mut val: timeval = timeval::new();
assert_eq!(0, val.tv_sec);
val = timeval::from_seconds(100);
assert_eq!(100, val.tv_sec);
val.clear();
assert_eq!(0, val.tv_sec);
}
#[test]
fn test_timespec_utility_functions() {
let mut spec: timespec = timespec::new();
assert_eq!(0, spec.tv_sec);
spec = timespec::from_seconds(164);
assert_eq!(164, spec.tv_sec);
spec.clear();
assert_eq!(0, spec.tv_sec);
}
}