diff options
| author | Patrick Walton <[email protected]> | 2011-04-29 11:54:06 -0700 |
|---|---|---|
| committer | Patrick Walton <[email protected]> | 2011-04-29 11:54:06 -0700 |
| commit | 5d3e5531412952c556fabb541536bcae34b0ce09 (patch) | |
| tree | fabf066bf9227a0026be2e2b62f2012e9d32ee14 /src | |
| parent | rustc: Remove the wrong-compiler warning; we're self-hosting now. (diff) | |
| download | rust-5d3e5531412952c556fabb541536bcae34b0ce09.tar.xz rust-5d3e5531412952c556fabb541536bcae34b0ce09.zip | |
stdlib: Add a Time module to the standard library
Diffstat (limited to 'src')
| -rw-r--r-- | src/lib/Time.rs | 12 | ||||
| -rw-r--r-- | src/lib/std.rc | 1 | ||||
| -rw-r--r-- | src/rt/rust_builtin.cpp | 29 | ||||
| -rw-r--r-- | src/rt/rustrt.def.in | 1 |
4 files changed, 43 insertions, 0 deletions
diff --git a/src/lib/Time.rs b/src/lib/Time.rs new file mode 100644 index 00000000..70a12a6f --- /dev/null +++ b/src/lib/Time.rs @@ -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; +} + diff --git a/src/lib/std.rc b/src/lib/std.rc index 4efff14b..44a0689c 100644 --- a/src/lib/std.rc +++ b/src/lib/std.rc @@ -76,6 +76,7 @@ mod ExtFmt; mod Box; mod GetOpts; mod Term; +mod Time; // Local Variables: // mode: rust; diff --git a/src/rt/rust_builtin.cpp b/src/rt/rust_builtin.cpp index a2323898..297b9df7 100644 --- a/src/rt/rust_builtin.cpp +++ b/src/rt/rust_builtin.cpp @@ -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++ diff --git a/src/rt/rustrt.def.in b/src/rt/rustrt.def.in index d30c146e..b2d42463 100644 --- a/src/rt/rustrt.def.in +++ b/src/rt/rustrt.def.in @@ -8,6 +8,7 @@ debug_tag debug_trap debug_tydesc do_gc +get_time last_os_error rand_free rand_new |