time.rs 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. /*
  2. * Copyright (c) 2006-2025, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2025-10-10 foxglove RT-Thread Time implementation
  9. */
  10. use crate::api::*;
  11. use crate::bindings::libc;
  12. use core::time::Duration;
  13. pub fn sleep(time: Duration) {
  14. let mut time = time.as_millis();
  15. const MAX_DELAY: u128 = i32::MAX as u128;
  16. const MAX_DELAY_P1: u128 = i32::MAX as u128 + 1;
  17. loop {
  18. match time {
  19. 1..=MAX_DELAY => {
  20. let _ = thread_m_delay(time as i32);
  21. return;
  22. }
  23. 0 => return,
  24. MAX_DELAY_P1..=u128::MAX => {
  25. let _ = thread_m_delay(i32::MAX);
  26. time -= i32::MAX as u128;
  27. }
  28. }
  29. }
  30. }
  31. pub fn get_time() -> Duration {
  32. let mut tv = libc::timeval {
  33. tv_sec: 0,
  34. tv_usec: 0,
  35. };
  36. unsafe {
  37. libc::gettimeofday(&mut tv, core::ptr::null_mut());
  38. }
  39. Duration::new(tv.tv_sec as u64, tv.tv_usec as u32 * 1000)
  40. }