rust/src/libstd/time.rs

85 lines
2.2 KiB
Rust
Raw Normal View History

#[abi = "cdecl"]
native mod rustrt {
2011-09-12 05:39:38 -05:00
fn get_time(&sec: u32, &usec: u32);
fn precise_time_ns(&ns: u64);
}
2012-03-07 20:17:30 -06:00
#[doc = "A record specifying a time value in seconds and microseconds."]
2011-07-27 07:19:39 -05:00
type timeval = {sec: u32, usec: u32};
2012-03-07 20:17:30 -06:00
#[doc = "
Returns the current time as a `timeval` containing the seconds and
microseconds since 1970-01-01T00:00:00Z.
2012-03-07 20:17:30 -06:00
"]
fn get_time() -> timeval {
let mut sec = 0u32;
let mut usec = 0u32;
2011-06-10 09:39:09 -05:00
rustrt::get_time(sec, usec);
2011-07-27 07:19:39 -05:00
ret {sec: sec, usec: usec};
}
2012-03-07 20:17:30 -06:00
#[doc = "
Returns the current value of a high-resolution performance counter
in nanoseconds since an unspecified epoch.
2012-03-07 20:17:30 -06:00
"]
fn precise_time_ns() -> u64 {
let mut ns = 0u64;
rustrt::precise_time_ns(ns);
ns
}
2012-03-07 20:17:30 -06:00
#[doc = "
Returns the current value of a high-resolution performance counter
in seconds since an unspecified epoch.
2012-03-07 20:17:30 -06:00
"]
fn precise_time_s() -> float {
ret (precise_time_ns() as float) / 1000000000.;
}
#[cfg(test)]
mod tests {
import task;
#[test]
fn test_get_time() {
const some_recent_date: u32 = 1325376000u32; // 2012-01-01T00:00:00Z
const some_future_date: u32 = 1577836800u32; // 2020-01-01T00:00:00Z
let tv1 = get_time();
log(debug, "tv1=" + uint::str(tv1.sec as uint) + " sec + "
+ uint::str(tv1.usec as uint) + " usec");
assert tv1.sec > some_recent_date;
assert tv1.usec < 1000000u32;
let tv2 = get_time();
log(debug, "tv2=" + uint::str(tv2.sec as uint) + " sec + "
+ uint::str(tv2.usec as uint) + " usec");
assert tv2.sec >= tv1.sec;
assert tv2.sec < some_future_date;
assert tv2.usec < 1000000u32;
if tv2.sec == tv1.sec {
assert tv2.usec >= tv1.usec;
}
}
#[test]
fn test_precise_time() {
let s0 = precise_time_s();
let ns1 = precise_time_ns();
log(debug, "s0=" + float::to_str(s0, 9u) + " sec");
assert s0 > 0.;
let ns0 = (s0 * 1000000000.) as u64;
log(debug, "ns0=" + u64::str(ns0) + " ns");
log(debug, "ns1=" + u64::str(ns1) + " ns");
assert ns1 >= ns0;
let ns2 = precise_time_ns();
log(debug, "ns2=" + u64::str(ns2) + " ns");
assert ns2 >= ns1;
}
}