fmt_trait/
fmt-trait.rs

1// This example shows how to add a new method to the Timestamp struct.
2
3use uts2ts::uts2ts;
4
5pub trait MyFormat {
6    fn my_format(&self) -> String;
7}
8
9impl MyFormat for uts2ts::Timestamp {
10    fn my_format(&self) -> String {
11        if self.year.is_negative() {
12            panic!("The method my_format() is only implemented to work for years >=0");
13        }
14        let weekday_name: [&str; 7] = [
15            "Sunday",
16            "Monday",
17            "Tuesday",
18            "Wednesday",
19            "Thursday",
20            "Friday",
21            "Saturday",
22        ];
23        format!(
24            "{year:0>4}{month:0>2}{day:0>2}-{hour:0>2}{minute:0>2}{second:0>2} ({weekday})",
25            year = self.year,
26            month = self.month,
27            day = self.day,
28            hour = self.hour,
29            minute = self.minute,
30            second = self.second,
31            weekday = weekday_name[self.weekday as usize]
32        )
33    }
34}
35
36fn main() {
37    println!("{}", uts2ts(204158100).as_string());
38    println!("{}", uts2ts(204158100).my_format());
39}