]> git.lizzy.rs Git - rust.git/blob - src/libstd/time/mod.rs
d6c94f27a8baf9462dea4a451861ad329956d37b
[rust.git] / src / libstd / time / mod.rs
1 // Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 //! Temporal quantification.
12
13 use libc;
14
15 pub use self::duration::Duration;
16
17 pub mod duration;
18
19 /// Returns the current value of a high-resolution performance counter
20 /// in nanoseconds since an unspecified epoch.
21 // NB: this is intentionally not public, this is not ready to stabilize its api.
22 fn precise_time_ns() -> u64 {
23     return os_precise_time_ns();
24
25     #[cfg(windows)]
26     fn os_precise_time_ns() -> u64 {
27         let mut ticks_per_s = 0;
28         assert_eq!(unsafe {
29             libc::QueryPerformanceFrequency(&mut ticks_per_s)
30         }, 1);
31         let ticks_per_s = if ticks_per_s == 0 {1} else {ticks_per_s};
32         let mut ticks = 0;
33         assert_eq!(unsafe {
34             libc::QueryPerformanceCounter(&mut ticks)
35         }, 1);
36
37         return (ticks as u64 * 1000000000) / (ticks_per_s as u64);
38     }
39
40     #[cfg(any(target_os = "macos", target_os = "ios"))]
41     fn os_precise_time_ns() -> u64 {
42         use sync;
43
44         static mut TIMEBASE: libc::mach_timebase_info = libc::mach_timebase_info { numer: 0,
45                                                                                    denom: 0 };
46         static ONCE: sync::Once = sync::ONCE_INIT;
47         unsafe {
48             ONCE.call_once(|| {
49                 imp::mach_timebase_info(&mut TIMEBASE);
50             });
51             let time = imp::mach_absolute_time();
52             time * TIMEBASE.numer as u64 / TIMEBASE.denom as u64
53         }
54     }
55
56     #[cfg(not(any(windows, target_os = "macos", target_os = "ios")))]
57     fn os_precise_time_ns() -> u64 {
58         let mut ts = libc::timespec { tv_sec: 0, tv_nsec: 0 };
59         unsafe {
60             imp::clock_gettime(libc::CLOCK_MONOTONIC, &mut ts);
61         }
62         return (ts.tv_sec as u64) * 1000000000 + (ts.tv_nsec as u64)
63     }
64 }
65
66 #[cfg(all(unix, not(target_os = "macos"), not(target_os = "ios")))]
67 mod imp {
68     use libc::{c_int, timespec};
69
70     // Apparently android provides this in some other library?
71     #[cfg(not(target_os = "android"))]
72     #[link(name = "rt")]
73     extern {}
74
75     extern {
76         pub fn clock_gettime(clk_id: c_int, tp: *mut timespec) -> c_int;
77     }
78
79 }
80 #[cfg(any(target_os = "macos", target_os = "ios"))]
81 mod imp {
82     use libc::{c_int, mach_timebase_info};
83
84     extern {
85         pub fn mach_absolute_time() -> u64;
86         pub fn mach_timebase_info(info: *mut mach_timebase_info) -> c_int;
87     }
88 }