rust/src/libstd/time.rs

95 lines
2.3 KiB
Rust
Raw Normal View History

2011-10-27 16:54:18 -05:00
/*
Module: time
*/
#[abi = "cdecl"]
native mod rustrt {
2011-09-12 05:39:38 -05:00
fn get_time(&sec: u32, &usec: u32);
fn nano_time(&ns: u64);
}
/*
Type: timeval
A record specifying a time value in seconds and microseconds.
*/
2011-07-27 07:19:39 -05:00
type timeval = {sec: u32, usec: u32};
/*
Function: get_time
Returns the current time as a `timeval` containing the seconds and
microseconds since 1970-01-01T00:00:00Z.
*/
fn get_time() -> timeval {
2011-07-27 07:19:39 -05:00
let sec = 0u32;
let 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};
}
/*
Function: precise_time_ns
Returns the current value of a high-resolution performance counter
in nanoseconds since an unspecified epoch.
*/
2011-07-27 07:19:39 -05:00
fn precise_time_ns() -> u64 { let ns = 0u64; rustrt::nano_time(ns); ret ns; }
/*
Function: precise_time_s
Returns the current value of a high-resolution performance counter
in seconds since an unspecified epoch.
*/
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;
}
}