diff options
| author | zhang san <[email protected]> | 2025-12-08 18:34:25 +0800 |
|---|---|---|
| committer | GitHub <[email protected]> | 2025-12-08 18:34:25 +0800 |
| commit | 69980f8b9d29b38dbd4ddf6373b4b45f3a138409 (patch) | |
| tree | 21dd9a8ce80e3d30192e9ffa56ea39a199bae309 /components/rust/core/src/time.rs | |
| parent | cd1d47b87c5d47e131730e5506eb3ad77d357653 (diff) | |
Adding Rust Language Support for RT-Thread #10910
Diffstat (limited to 'components/rust/core/src/time.rs')
| -rw-r--r-- | components/rust/core/src/time.rs | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/components/rust/core/src/time.rs b/components/rust/core/src/time.rs new file mode 100644 index 0000000000..60dcf47d1d --- /dev/null +++ b/components/rust/core/src/time.rs @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2006-2025, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2025-10-10 foxglove RT-Thread Time implementation + */ +use crate::api::*; +use crate::bindings::libc; +use core::time::Duration; + +pub fn sleep(time: Duration) { + let mut time = time.as_millis(); + const MAX_DELAY: u128 = i32::MAX as u128; + const MAX_DELAY_P1: u128 = i32::MAX as u128 + 1; + loop { + match time { + 1..=MAX_DELAY => { + let _ = thread_m_delay(time as i32); + return; + } + 0 => return, + MAX_DELAY_P1..=u128::MAX => { + let _ = thread_m_delay(i32::MAX); + time -= i32::MAX as u128; + } + } + } +} + +pub fn get_time() -> Duration { + let mut tv = libc::timeval { + tv_sec: 0, + tv_usec: 0, + }; + unsafe { + libc::gettimeofday(&mut tv, core::ptr::null_mut()); + } + Duration::new(tv.tv_sec as u64, tv.tv_usec as u32 * 1000) +}
\ No newline at end of file |
