1 #![allow(dead_code)] 2 3 use core::intrinsics::unlikely; 4 5 use system_error::SystemError; 6 7 use crate::driver::rtc::interface::rtc_read_time_default; 8 9 use super::{PosixTimeSpec, NSEC_PER_SEC}; 10 11 // 参考:https://code.dragonos.org.cn/xref/linux-3.4.99/include/linux/time.h#110 12 const KTIME_MAX: i64 = !(1u64 << 63) as i64; 13 const KTIME_SEC_MAX: i64 = KTIME_MAX / NSEC_PER_SEC as i64; 14 15 #[allow(non_camel_case_types)] 16 pub type ktime_t = i64; 17 18 // @brief 将ktime_t类型转换为纳秒类型 19 #[inline] ktime_to_ns(kt: ktime_t) -> i6420fn ktime_to_ns(kt: ktime_t) -> i64 { 21 return kt; 22 } 23 24 /// @brief 从RTC获取当前时间,然后计算时间戳。 25 /// 时间戳为从UTC+0 1970-01-01 00:00到当前UTC+0时间,所经过的纳秒数。 26 /// 注意,由于当前未引入时区,因此本函数默认时区为UTC+8来计算 ktime_get_real() -> Result<ktime_t, SystemError>27fn ktime_get_real() -> Result<ktime_t, SystemError> { 28 let rtc_time = rtc_read_time_default()?; 29 let time_spec: PosixTimeSpec = rtc_time.into(); 30 let r = time_spec.tv_sec * 1_000_000_000 + time_spec.tv_nsec; 31 return Ok(r); 32 } 33 34 /// @brief 暴露给外部使用的接口,返回一个时间戳 35 #[inline] ktime_get_real_ns() -> i6436pub fn ktime_get_real_ns() -> i64 { 37 let kt: ktime_t = ktime_get_real().unwrap_or(0); 38 return ktime_to_ns(kt); 39 } 40 41 // # 用于将两个ktime_t类型的变量相加 42 // #[inline(always)] 43 // pub(super) fn ktime_add(add1: ktime_t, add2: ktime_t) -> ktime_t { 44 // let res = add1 + add2; 45 // } 46 47 /// # 通过sec和nsec构造一个ktime_t 48 #[inline(always)] ktime_set(secs: i64, nsecs: u64) -> ktime_t49fn ktime_set(secs: i64, nsecs: u64) -> ktime_t { 50 if unlikely(secs >= KTIME_SEC_MAX) { 51 return KTIME_MAX; 52 } 53 54 return secs * NSEC_PER_SEC as i64 + nsecs as i64; 55 } 56 57 /// # 将PosixTimeSpec转换成ktime_t 58 #[inline(always)] timespec_to_ktime(ts: PosixTimeSpec) -> ktime_t59pub fn timespec_to_ktime(ts: PosixTimeSpec) -> ktime_t { 60 return ktime_set(ts.tv_sec, ts.tv_nsec as u64); 61 } 62