test_format/
test_format.rs1use rstime::{DateTime, Date, Time, TimeFormat};
2
3fn main() {
4 println!("=== rstime 格式化测试 ===\n");
5
6 let dt = DateTime::from_ymd_hms_milli(2026, 5, 10, 14, 5, 9, 37);
7 let d = Date::new(2026, 5, 10);
8 let t = Time::new(14, 5, 9, 37);
9
10 println!("DateTime 格式化:");
11 println!(" YYYY-MM-DD HH:mm:ss → {}", dt.format("{YYYY}-{MM}-{DD} {HH}:{mm}:{ss}"));
12 println!(" YYYY/MM/DD → {}", dt.format("{YYYY}/{MM}/{DD}"));
13 println!(" YY-M-D → {}", dt.format("{YY}-{M}-{D}"));
14 println!(" 12h → {}", dt.format("{hh}:{mm} {AMPM}"));
15 println!(" 12h lowercase → {}", dt.format("{h}:{m} {ampm}"));
16 println!(" With millis → {}", dt.format("{HH}:{mm}:{ss}.{SSS}"));
17 println!(" Weekday short → {}", dt.format("{W}"));
18 println!(" Weekday full → {}", dt.format("{WW}"));
19 println!(" Literal text → {}", dt.format("Today is {YYYY}-{MM}-{DD}!"));
20 println!();
21
22 println!("Date 格式化:");
23 println!(" YYYY-MM-DD → {}", d.format("{YYYY}-{MM}-{DD}"));
24 println!(" Chinese date → {}", d.format("{YYYY}年{MM}月{DD}日"));
25 println!(" Year only → {}", d.format("{YYYY}"));
26 println!();
27
28 println!("Time 格式化:");
29 println!(" HH:mm:ss → {}", t.format("{HH}:{mm}:{ss}"));
30 println!(" 12h with AM/PM → {}", t.format("{hh}:{mm} {AMPM}"));
31 println!(" With millis → {}", t.format("{HH}:{mm}:{ss}.{SSS}"));
32 println!();
33
34 println!("预置格式:");
35 println!(" ISO8601: {}", dt.format("{YYYY}-{MM}-{DD}T{HH}:{mm}:{ss}"));
36 println!(" RFC2822-ish: {}", dt.format("{W}, {DD} {MM} {YYYY} {HH}:{mm}:{ss}"));
37 println!();
38
39 println!("边界值测试:");
40 let midnight = Time::MIDNIGHT;
41 let noon = Time::NOON;
42 let max = Time::MAX;
43 println!(" Midnight → {}", midnight.format("{HH}:{mm}:{ss} {AMPM}"));
44 println!(" Noon → {}", noon.format("{HH}:{mm}:{ss} {AMPM}"));
45 println!(" Max → {}", max.format("{HH}:{mm}:{ss}.{SSS}"));
46 println!();
47
48 println!("✅ 格式化测试完成!");
49}