|
3 | 3 |
|
4 | 4 | extern crate libc; |
5 | 5 |
|
6 | | -use std::fmt; |
7 | | - |
| 6 | +pub mod rand; |
| 7 | +pub mod time; |
8 | 8 | pub mod validators; |
9 | | - |
10 | | -/// Constant to convert seconds to nanoseconds. |
11 | | -pub const NANOS_PER_SECOND: u64 = 1_000_000_000; |
12 | | - |
13 | | -/// Wrapper over `libc::clockid_t` to specify Linux Kernel clock source. |
14 | | -pub enum ClockType { |
15 | | - /// Equivalent to `libc::CLOCK_MONOTONIC`. |
16 | | - Monotonic, |
17 | | - /// Equivalent to `libc::CLOCK_REALTIME`. |
18 | | - Real, |
19 | | - /// Equivalent to `libc::CLOCK_PROCESS_CPUTIME_ID`. |
20 | | - ProcessCpu, |
21 | | - /// Equivalent to `libc::CLOCK_THREAD_CPUTIME_ID`. |
22 | | - ThreadCpu, |
23 | | -} |
24 | | - |
25 | | -impl Into<libc::clockid_t> for ClockType { |
26 | | - fn into(self) -> libc::clockid_t { |
27 | | - match self { |
28 | | - ClockType::Monotonic => libc::CLOCK_MONOTONIC, |
29 | | - ClockType::Real => libc::CLOCK_REALTIME, |
30 | | - ClockType::ProcessCpu => libc::CLOCK_PROCESS_CPUTIME_ID, |
31 | | - ClockType::ThreadCpu => libc::CLOCK_THREAD_CPUTIME_ID, |
32 | | - } |
33 | | - } |
34 | | -} |
35 | | - |
36 | | -/// Structure representing the date in local time with nanosecond precision. |
37 | | -pub struct LocalTime { |
38 | | - /// Seconds in current minute. |
39 | | - sec: i32, |
40 | | - /// Minutes in current hour. |
41 | | - min: i32, |
42 | | - /// Hours in current day, 24H format. |
43 | | - hour: i32, |
44 | | - /// Days in current month. |
45 | | - mday: i32, |
46 | | - /// Months in current year. |
47 | | - mon: i32, |
48 | | - /// Years passed since 1900 BC. |
49 | | - year: i32, |
50 | | - /// Nanoseconds in current second. |
51 | | - nsec: i64, |
52 | | -} |
53 | | - |
54 | | -impl LocalTime { |
55 | | - /// Returns the [LocalTime](struct.LocalTime.html) structure for the calling moment. |
56 | | - pub fn now() -> LocalTime { |
57 | | - let mut timespec = libc::timespec { |
58 | | - tv_sec: 0, |
59 | | - tv_nsec: 0, |
60 | | - }; |
61 | | - let mut tm: libc::tm = libc::tm { |
62 | | - tm_sec: 0, |
63 | | - tm_min: 0, |
64 | | - tm_hour: 0, |
65 | | - tm_mday: 0, |
66 | | - tm_mon: 0, |
67 | | - tm_year: 0, |
68 | | - tm_wday: 0, |
69 | | - tm_yday: 0, |
70 | | - tm_isdst: 0, |
71 | | - tm_gmtoff: 0, |
72 | | - tm_zone: std::ptr::null(), |
73 | | - }; |
74 | | - |
75 | | - // Safe because the parameters are valid. |
76 | | - unsafe { |
77 | | - libc::clock_gettime(libc::CLOCK_REALTIME, &mut timespec); |
78 | | - libc::localtime_r(×pec.tv_sec, &mut tm); |
79 | | - } |
80 | | - |
81 | | - LocalTime { |
82 | | - sec: tm.tm_sec, |
83 | | - min: tm.tm_min, |
84 | | - hour: tm.tm_hour, |
85 | | - mday: tm.tm_mday, |
86 | | - mon: tm.tm_mon, |
87 | | - year: tm.tm_year, |
88 | | - nsec: timespec.tv_nsec, |
89 | | - } |
90 | | - } |
91 | | -} |
92 | | - |
93 | | -impl fmt::Display for LocalTime { |
94 | | - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
95 | | - write!( |
96 | | - f, |
97 | | - "{}-{:02}-{:02}T{:02}:{:02}:{:02}.{:09}", |
98 | | - self.year + 1900, |
99 | | - self.mon + 1, |
100 | | - self.mday, |
101 | | - self.hour, |
102 | | - self.min, |
103 | | - self.sec, |
104 | | - self.nsec |
105 | | - ) |
106 | | - } |
107 | | -} |
108 | | - |
109 | | -/// Returns a timestamp in nanoseconds from a monotonic clock. |
110 | | -/// |
111 | | -/// Uses `_rdstc` on `x86_64` and [`get_time`](fn.get_time.html) on other architectures. |
112 | | -pub fn timestamp_cycles() -> u64 { |
113 | | - #[cfg(target_arch = "x86_64")] |
114 | | - // Safe because there's nothing that can go wrong with this call. |
115 | | - unsafe { |
116 | | - std::arch::x86_64::_rdtsc() as u64 |
117 | | - } |
118 | | - #[cfg(not(target_arch = "x86_64"))] |
119 | | - { |
120 | | - get_time(ClockType::Monotonic) |
121 | | - } |
122 | | -} |
123 | | - |
124 | | -/// Returns a timestamp in nanoseconds based on the provided clock type. |
125 | | -/// |
126 | | -/// # Arguments |
127 | | -/// |
128 | | -/// * `clock_type` - Identifier of the Linux Kernel clock on which to act. |
129 | | -pub fn get_time(clock_type: ClockType) -> u64 { |
130 | | - let mut time_struct = libc::timespec { |
131 | | - tv_sec: 0, |
132 | | - tv_nsec: 0, |
133 | | - }; |
134 | | - // Safe because the parameters are valid. |
135 | | - unsafe { libc::clock_gettime(clock_type.into(), &mut time_struct) }; |
136 | | - seconds_to_nanoseconds(time_struct.tv_sec).unwrap() as u64 + (time_struct.tv_nsec as u64) |
137 | | -} |
138 | | - |
139 | | -/// Converts a timestamp in seconds to an equivalent one in nanoseconds. |
140 | | -/// Returns `None` if the conversion overflows. |
141 | | -/// |
142 | | -/// # Arguments |
143 | | -/// |
144 | | -/// * `value` - Timestamp in seconds. |
145 | | -pub fn seconds_to_nanoseconds(value: i64) -> Option<i64> { |
146 | | - value.checked_mul(NANOS_PER_SECOND as i64) |
147 | | -} |
148 | | - |
149 | | -#[cfg(test)] |
150 | | -mod tests { |
151 | | - use super::*; |
152 | | - |
153 | | - #[test] |
154 | | - fn test_get_time() { |
155 | | - for _ in 0..1000 { |
156 | | - assert!(get_time(ClockType::Monotonic) <= get_time(ClockType::Monotonic)); |
157 | | - } |
158 | | - |
159 | | - for _ in 0..1000 { |
160 | | - assert!(get_time(ClockType::ProcessCpu) <= get_time(ClockType::ProcessCpu)); |
161 | | - } |
162 | | - |
163 | | - for _ in 0..1000 { |
164 | | - assert!(get_time(ClockType::ThreadCpu) <= get_time(ClockType::ThreadCpu)); |
165 | | - } |
166 | | - |
167 | | - assert_ne!(get_time(ClockType::Real), 0); |
168 | | - } |
169 | | - |
170 | | - #[test] |
171 | | - fn test_local_time_display() { |
172 | | - let local_time = LocalTime { |
173 | | - sec: 30, |
174 | | - min: 15, |
175 | | - hour: 10, |
176 | | - mday: 4, |
177 | | - mon: 6, |
178 | | - year: 119, |
179 | | - nsec: 123_456_789, |
180 | | - }; |
181 | | - assert_eq!( |
182 | | - String::from("2019-07-04T10:15:30.123456789"), |
183 | | - local_time.to_string() |
184 | | - ); |
185 | | - |
186 | | - let local_time = LocalTime { |
187 | | - sec: 5, |
188 | | - min: 5, |
189 | | - hour: 5, |
190 | | - mday: 23, |
191 | | - mon: 7, |
192 | | - year: 44, |
193 | | - nsec: 123, |
194 | | - }; |
195 | | - assert_eq!( |
196 | | - String::from("1944-08-23T05:05:05.000000123"), |
197 | | - local_time.to_string() |
198 | | - ); |
199 | | - |
200 | | - let local_time = LocalTime::now(); |
201 | | - assert!(local_time.mon >= 0 && local_time.mon <= 11); |
202 | | - } |
203 | | - |
204 | | - #[test] |
205 | | - fn test_seconds_to_nanoseconds() { |
206 | | - assert_eq!( |
207 | | - seconds_to_nanoseconds(100).unwrap() as u64, |
208 | | - 100 * NANOS_PER_SECOND |
209 | | - ); |
210 | | - |
211 | | - assert!(seconds_to_nanoseconds(9_223_372_037).is_none()); |
212 | | - } |
213 | | -} |
0 commit comments