Skip to main content

xwrust/
xwtm.rs

1//! XWOS RUST:系统时间
2//! ========
3//!
4//! XWOS的系统时间库。
5//!
6
7use crate::types::*;
8
9extern "C" {
10    fn xwrustffi_xwtm_now() -> XwTm;
11    fn xwrustffi_xwtm_ft(time: XwTm) -> XwTm;
12    fn xwrustffi_xwtm_nowts() -> XwTm;
13    fn xwrustffi_xwtm_fts(time: XwTm) -> XwTm;
14    fn xwrustffi_xwtm_nowtc() -> u64;
15}
16
17/// 1微秒
18pub const MICROSECOND: XwTm = 1000;
19/// 1毫秒
20pub const MILLISECOND: XwTm = 1000000;
21/// 1秒
22pub const SECOND: XwTm = 1000000000;
23/// 1分
24pub const MINUTE: XwTm = 60000000000;
25/// 1小时
26pub const HOUR: XwTm = 3600000000000;
27/// 1天
28pub const DAY: XwTm = 86400000000000;
29
30
31/// 获取当前CPU的 **系统时间** 点
32///
33/// # 示例
34///
35/// ```rust
36/// use use xwrust::xwtm;
37///
38/// let now = xwtm::now();
39/// ```
40pub fn now() -> XwTm {
41    unsafe { xwrustffi_xwtm_now() }
42}
43
44
45/// 获取当前CPU的未来 **系统时间** 点
46///
47/// # 示例
48///
49/// ```rust
50/// use use xwrust::xwtm;
51///
52/// let ftp = xwtm::ft(xwtm::s(1));
53/// ```
54pub fn ft(time: XwTm) -> XwTm {
55    unsafe { xwrustffi_xwtm_ft(time) }
56}
57
58
59/// 获取当前CPU的 **系统时间戳**
60///
61/// # 示例
62///
63/// ```rust
64/// use use xwrust::xwtm;
65///
66/// let ts = xwtm::nowts();
67/// ```
68pub fn nowts() -> XwTm {
69    unsafe { xwrustffi_xwtm_nowts() }
70}
71
72
73/// 获取当前CPU的未来 **系统时间戳**
74///
75/// # 示例
76///
77/// ```rust
78/// use use xwrust::xwtm;
79///
80/// let ftp = xwtm::fts(xwtm::s(1));
81/// ```
82pub fn fts(time: XwTm) -> XwTm {
83    unsafe { xwrustffi_xwtm_fts(time) }
84}
85
86
87/// 获取当前CPU的系统滴答计数
88///
89/// # 示例
90///
91/// ```rust
92/// use use xwrust::xwtm;
93///
94/// let cnt = xwtm::nowtc();
95/// ```
96pub fn nowtc() -> u64 {
97    unsafe { xwrustffi_xwtm_nowtc() }
98}
99
100
101/// 返回以微秒为单位的系统时间
102///
103/// # 示例
104///
105/// ```rust
106/// use use xwrust::xwtm;
107///
108/// let tp = xwtm::us(8); // 8微妙
109/// ```
110pub const fn us(us: XwTm) -> XwTm {
111    us * MICROSECOND
112}
113
114
115/// 返回以毫秒为单位的系统时间
116///
117/// # 示例
118///
119/// ```rust
120/// use use xwrust::xwtm;
121///
122/// let tp = xwtm::ms(8); // 8毫妙
123/// ```
124pub const fn ms(ms: XwTm) -> XwTm {
125    ms * MILLISECOND
126}
127
128
129/// 返回以秒为单位的系统时间
130///
131/// # 示例
132///
133/// ```rust
134/// use use xwrust::xwtm;
135///
136/// let tp = xwtm::s(8); // 8妙
137/// ```
138pub const fn s(s: XwTm) -> XwTm {
139    s * SECOND
140}
141
142
143/// 返回以分为单位的系统时间
144///
145/// # 示例
146///
147/// ```rust
148/// use use xwrust::xwtm;
149///
150/// let tp = xwtm::m(8); // 8分
151/// ```
152pub const fn m(m: XwTm) -> XwTm {
153    m * MINUTE
154}
155
156
157/// 返回以小时为单位的系统时间
158///
159/// # 示例
160///
161/// ```rust
162/// use use xwrust::xwtm;
163///
164/// let tp = xwtm::h(8); // 8小时
165/// ```
166pub const fn h(h: XwTm) -> XwTm {
167    h * HOUR
168}
169
170
171/// 返回以天为单位的系统时间
172///
173/// # 示例
174///
175/// ```rust
176/// use use xwrust::xwtm;
177///
178/// let tp = xwtm::d(8); // 8天
179/// ```
180pub const fn d(d: XwTm) -> XwTm {
181    d * DAY
182}