pub struct Duration {
pub secs: i64,
pub nanos: i32,
}Expand description
A positive duration with second and nanosecond precision
Supports arithmetic operations (+, -, +=, -=) and
conversion to/from standard library types.
Use try_new to safely create a Duration from values
that may be negative.
§Examples
use rstime::Duration;
let d = Duration::try_new(5, 500_000_000).unwrap();
assert_eq!(d.total_seconds(), 5.5);Fields§
§secs: i64Seconds component (must be >= 0 for valid Duration)
nanos: i32Nanoseconds component (0..1_000_000_000, must be >= 0)
Implementations§
Source§impl Duration
impl Duration
Sourcepub fn try_new(secs: i64, nanos: i32) -> Option<Duration>
pub fn try_new(secs: i64, nanos: i32) -> Option<Duration>
Try to create a duration from seconds and nanoseconds.
Returns None if the result would be negative (duration must be positive).
Normalizes nanoseconds into the 0..1_000_000_000 range.
§Examples
use rstime::Duration;
assert!(Duration::try_new(5, 500_000_000).is_some());
assert!(Duration::try_new(-1, 0).is_none());Sourcepub fn from_secs(secs: i64) -> Duration
pub fn from_secs(secs: i64) -> Duration
Create from seconds only
Examples found in repository?
3fn main() {
4 println!("=== rstime 时间库使用示例 ===\n");
5
6 println!("1. 获取当前时间");
7 let now = DateTime::now();
8 println!(" 现在: {}\n", now);
9
10 println!("2. 格式化输出");
11 let dt = DateTime::from_ymd_hms_milli(2026, 5, 10, 14, 5, 9, 37);
12 println!(" ISO 8601: {}", dt.format("{YYYY}-{MM}-{DD}T{HH}:{mm}:{ss}"));
13 println!(" 中文格式: {}", dt.format("{YYYY}年{MM}月{DD}日 {HH}:{mm}:{ss}"));
14 println!(" 12小时制: {}", dt.format("{hh}:{mm}:{ss} {AMPM}"));
15 println!(" 星期: {}", dt.format("{WW}"));
16 println!(" 毫秒: {}", dt.format("{SSS}"));
17 println!();
18
19 println!("3. 日期计算");
20 let d1 = Date::new(2026, 1, 1);
21 let d2 = d1 + TimeDelta::new(365 * 86400, 0);
22 let diff = d2 - d1;
23 println!(" {} + 365 天 = {}", d1, d2);
24 println!(" 相差 {} 天", diff.total_seconds() as i64 / 86400);
25 println!();
26
27 println!("4. 星期计算");
28 let dates = [Date::new(2026, 1, 1), Date::new(2026, 5, 10), Date::new(2026, 12, 25)];
29 for d in &dates {
30 println!(" {} = {}", d, d.weekday());
31 }
32 println!();
33
34 println!("5. 闰年判断");
35 for year in [2024, 2025, 2000, 1900] {
36 println!(" {}: {}", year, if Date::new(year, 1, 1).is_leap_year() { "闰年" } else { "平年" });
37 }
38 println!();
39
40 println!("6. Unix 时间戳");
41 let epoch = DateTime::from_unix(0);
42 println!(" epoch: {}", epoch);
43 let ts = now.unix_timestamp();
44 println!(" 当前时间戳: {}", ts);
45 let ts_ms = now.unix_timestamp_millis();
46 println!(" 毫秒时间戳: {}", ts_ms);
47 println!();
48
49 println!("7. 时间加法(跨天回绕)");
50 let t = Time::from_hms(23, 30, 0);
51 println!(" {} + 2h = {}", t, t + Duration::from_secs(7200));
52 println!(" {} - 1h = {}", t, t - Duration::HOUR);
53 println!();
54
55 println!("8. 时钟功能");
56 let today = Date::today();
57 let time_now = Time::now();
58 println!(" 今天: {}", today);
59 println!(" 此刻: {}", time_now);
60 println!();
61
62 println!("9. 单调时钟(性能计时)");
63 let timer = MonotonicClock::start();
64 let _sum: u64 = (0..10_000_000).sum();
65 let elapsed = timer.elapsed();
66 println!(" 累加 1000 万次耗时: {}ms", elapsed.total_millis());
67 println!();
68
69 println!("✅ 所有示例运行完成!");
70}Sourcepub fn from_millis(ms: i64) -> Duration
pub fn from_millis(ms: i64) -> Duration
Create from milliseconds
Sourcepub fn from_nanos(ns: i64) -> Duration
pub fn from_nanos(ns: i64) -> Duration
Create from nanoseconds
Sourcepub fn from_std(d: StdDuration) -> Duration
pub fn from_std(d: StdDuration) -> Duration
Convert from std::time::Duration
Sourcepub fn to_std(self) -> Option<StdDuration>
pub fn to_std(self) -> Option<StdDuration>
Convert to std::time::Duration, returns None if negative
Sourcepub fn to_time_delta(self) -> TimeDelta
pub fn to_time_delta(self) -> TimeDelta
Convert to a TimeDelta
Sourcepub fn total_seconds(&self) -> f64
pub fn total_seconds(&self) -> f64
Total duration in seconds as a floating-point value
Sourcepub fn total_millis(&self) -> i64
pub fn total_millis(&self) -> i64
Total duration in milliseconds
Examples found in repository?
3fn main() {
4 println!("=== rstime 时钟测试 ===\n");
5
6 println!("1. 系统时钟 - 当前时间");
7 let clock = SystemClock;
8 let now = clock.now();
9 println!(" DateTime: {}", now);
10 println!(" Date: {}", clock.today());
11 println!(" Time: {}", clock.time_now());
12 println!();
13
14 println!("2. 便捷方法(无需实例化 Clock)");
15 let today = rstime::Date::today();
16 let time_now = rstime::Time::now();
17 let datetime_now = rstime::DateTime::now();
18 println!(" Date::today() → {}", today);
19 println!(" Time::now() → {}", time_now);
20 println!(" DateTime::now() → {}", datetime_now);
21 println!();
22
23 println!("3. 单调时钟 - 性能计时");
24 let timer = MonotonicClock::start();
25 let mut _sum: u64 = 0;
26 for i in 0..1_000_000 {
27 _sum += i;
28 }
29 let elapsed = timer.elapsed();
30 println!(" 累加 100 万次: {}ms ({} 纳秒)", elapsed.total_millis(), elapsed.nanos);
31 println!();
32
33 println!("4. 单调时钟 - 重置");
34 let mut timer = MonotonicClock::start();
35 std::thread::sleep(std::time::Duration::from_millis(10));
36 println!(" 第一次: {}ms", timer.elapsed().total_millis());
37 timer.reset();
38 println!(" 重置后: {}ms", timer.elapsed().total_millis());
39 println!();
40
41 println!("5. Duration 常量");
42 println!(" ZERO: {}ms", Duration::ZERO.total_millis());
43 println!(" SECOND: {}ms", Duration::SECOND.total_millis());
44 println!(" MINUTE: {}ms", Duration::MINUTE.total_millis());
45 println!(" HOUR: {}ms", Duration::HOUR.total_millis());
46 println!(" DAY: {}ms", Duration::DAY.total_millis());
47 println!();
48
49 println!("✅ 时钟测试完成!");
50}More examples
3fn main() {
4 println!("=== rstime 时间库使用示例 ===\n");
5
6 println!("1. 获取当前时间");
7 let now = DateTime::now();
8 println!(" 现在: {}\n", now);
9
10 println!("2. 格式化输出");
11 let dt = DateTime::from_ymd_hms_milli(2026, 5, 10, 14, 5, 9, 37);
12 println!(" ISO 8601: {}", dt.format("{YYYY}-{MM}-{DD}T{HH}:{mm}:{ss}"));
13 println!(" 中文格式: {}", dt.format("{YYYY}年{MM}月{DD}日 {HH}:{mm}:{ss}"));
14 println!(" 12小时制: {}", dt.format("{hh}:{mm}:{ss} {AMPM}"));
15 println!(" 星期: {}", dt.format("{WW}"));
16 println!(" 毫秒: {}", dt.format("{SSS}"));
17 println!();
18
19 println!("3. 日期计算");
20 let d1 = Date::new(2026, 1, 1);
21 let d2 = d1 + TimeDelta::new(365 * 86400, 0);
22 let diff = d2 - d1;
23 println!(" {} + 365 天 = {}", d1, d2);
24 println!(" 相差 {} 天", diff.total_seconds() as i64 / 86400);
25 println!();
26
27 println!("4. 星期计算");
28 let dates = [Date::new(2026, 1, 1), Date::new(2026, 5, 10), Date::new(2026, 12, 25)];
29 for d in &dates {
30 println!(" {} = {}", d, d.weekday());
31 }
32 println!();
33
34 println!("5. 闰年判断");
35 for year in [2024, 2025, 2000, 1900] {
36 println!(" {}: {}", year, if Date::new(year, 1, 1).is_leap_year() { "闰年" } else { "平年" });
37 }
38 println!();
39
40 println!("6. Unix 时间戳");
41 let epoch = DateTime::from_unix(0);
42 println!(" epoch: {}", epoch);
43 let ts = now.unix_timestamp();
44 println!(" 当前时间戳: {}", ts);
45 let ts_ms = now.unix_timestamp_millis();
46 println!(" 毫秒时间戳: {}", ts_ms);
47 println!();
48
49 println!("7. 时间加法(跨天回绕)");
50 let t = Time::from_hms(23, 30, 0);
51 println!(" {} + 2h = {}", t, t + Duration::from_secs(7200));
52 println!(" {} - 1h = {}", t, t - Duration::HOUR);
53 println!();
54
55 println!("8. 时钟功能");
56 let today = Date::today();
57 let time_now = Time::now();
58 println!(" 今天: {}", today);
59 println!(" 此刻: {}", time_now);
60 println!();
61
62 println!("9. 单调时钟(性能计时)");
63 let timer = MonotonicClock::start();
64 let _sum: u64 = (0..10_000_000).sum();
65 let elapsed = timer.elapsed();
66 println!(" 累加 1000 万次耗时: {}ms", elapsed.total_millis());
67 println!();
68
69 println!("✅ 所有示例运行完成!");
70}Trait Implementations§
Source§impl AddAssign for Duration
impl AddAssign for Duration
Source§fn add_assign(&mut self, rhs: Duration)
fn add_assign(&mut self, rhs: Duration)
+= operation. Read moreSource§impl Ord for Duration
impl Ord for Duration
1.21.0 (const: unstable) · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
Source§impl PartialOrd for Duration
impl PartialOrd for Duration
Source§impl SubAssign for Duration
impl SubAssign for Duration
Source§fn sub_assign(&mut self, rhs: Duration)
fn sub_assign(&mut self, rhs: Duration)
-= operation. Read more