stdlib: Add a Time module to the standard library
This commit is contained in:
parent
c52fb52fbc
commit
5d3e553141
12
src/lib/Time.rs
Normal file
12
src/lib/Time.rs
Normal file
@ -0,0 +1,12 @@
|
||||
native "rust" mod rustrt {
|
||||
fn get_time(&mutable u32 sec, &mutable u32 usec);
|
||||
}
|
||||
|
||||
type timeval = rec(u32 sec, u32 usec);
|
||||
|
||||
fn get_time() -> timeval {
|
||||
let timeval res = rec(sec=0u32, usec=0u32);
|
||||
rustrt.get_time(res.sec, res.usec);
|
||||
ret res;
|
||||
}
|
||||
|
@ -76,6 +76,7 @@ mod ExtFmt;
|
||||
mod Box;
|
||||
mod GetOpts;
|
||||
mod Term;
|
||||
mod Time;
|
||||
|
||||
// Local Variables:
|
||||
// mode: rust;
|
||||
|
@ -1,6 +1,10 @@
|
||||
|
||||
#include "rust_internal.h"
|
||||
|
||||
#if !defined(__WIN32__)
|
||||
#include <sys/time.h>
|
||||
#endif
|
||||
|
||||
/* Native builtins. */
|
||||
|
||||
extern "C" CDECL rust_str*
|
||||
@ -508,6 +512,31 @@ rust_ptr_eq(rust_task *task, type_desc *t, rust_box *a, rust_box *b) {
|
||||
return a == b;
|
||||
}
|
||||
|
||||
#if defined(__WIN32__)
|
||||
extern "C" CDECL void
|
||||
get_time(rust_task *task, uint32_t *sec, uint32_t *usec) {
|
||||
SYSTEMTIME systemTime;
|
||||
FILETIME fileTime;
|
||||
GetSystemTime(&systemTime);
|
||||
if (!SystemTimeToFileTime(&systemTime, &fileTime)) {
|
||||
task->fail(1);
|
||||
return;
|
||||
}
|
||||
|
||||
// FIXME: This is probably completely wrong.
|
||||
*sec = fileTime.dwHighDateTime;
|
||||
*usec = fileTime.dwLowDateTime;
|
||||
}
|
||||
#else
|
||||
extern "C" CDECL void
|
||||
get_time(rust_task *task, uint32_t *sec, uint32_t *usec) {
|
||||
struct timeval tv;
|
||||
gettimeofday(&tv, NULL);
|
||||
*sec = tv.tv_sec;
|
||||
*usec = tv.tv_usec;
|
||||
}
|
||||
#endif
|
||||
|
||||
//
|
||||
// Local Variables:
|
||||
// mode: C++
|
||||
|
@ -8,6 +8,7 @@ debug_tag
|
||||
debug_trap
|
||||
debug_tydesc
|
||||
do_gc
|
||||
get_time
|
||||
last_os_error
|
||||
rand_free
|
||||
rand_new
|
||||
|
Loading…
x
Reference in New Issue
Block a user