1use rstime::{DateTime, Date, Time, Duration, TimeDelta, TimeFormat, MonotonicClock};
2
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}