Skip to content
Snippets Groups Projects
Unverified Commit 1ecd5f8f authored by Moses Miller's avatar Moses Miller
Browse files

Implement clock() and add CLOCK_* constants

parent 018f9508
No related branches found
No related tags found
No related merge requests found
......@@ -42,3 +42,8 @@ pub(crate) const DAY_NAMES: [&str; 7] = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri
pub(crate) const MON_NAMES: [&str; 12] = [
"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
];
pub(crate) const CLOCK_REALTIME: clockid_t = 0;
pub(crate) const CLOCK_MONOTONIC: clockid_t = 1;
pub(crate) const CLOCK_PROCESS_CPUTIME_ID: clockid_t = 2;
pub(crate) const CLOCK_THREAD_CPUTIME_ID: clockid_t = 3;
......@@ -12,7 +12,6 @@ mod helpers;
use platform::types::*;
use constants::*;
use helpers::*;
use core::fmt::write;
use core::mem::transmute;
use errno::EIO;
......@@ -99,7 +98,23 @@ pub extern "C" fn asctime_r(tm: *const tm, buf: *mut c_char) -> *mut c_char {
#[no_mangle]
pub extern "C" fn clock() -> clock_t {
unimplemented!();
let mut ts: timespec;
unsafe {
ts = core::mem::uninitialized();
}
if clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &mut ts) != 0 {
return -1;
}
if ts.tv_sec > time_t::max_value() / 1000000
|| ts.tv_nsec / 1000 > time_t::max_value() - 1000000 * ts.tv_sec
{
return -1;
}
return ts.tv_sec * 1_000_000 + ts.tv_nsec / 1000;
}
#[no_mangle]
......
......@@ -7,5 +7,7 @@ int main(int argc, char** argv) {
perror("clock_gettime");
time(NULL);
perror("time");
clock_t c = clock();
perror("clock");
return 0;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment