nyx-space/hifitime

Support formatting from the 1989 C standard (like strptime and strftime)

ChristopherRabotin opened this issue · 1 comments

This would allow formatting date times with the typical tokens like %Y-%m-%d. Chrono supports this, and its support should be used as a template for the Hifitime implementation: https://docs.rs/chrono/latest/chrono/format/index.html , https://docs.rs/chrono/latest/chrono/format/strftime/index.html .

Time-rs also has extensive support for formatting: https://time-rs.github.io/book/api/well-known-format-descriptions.html .

This formatting should heavily reuse the work in parser.rs as it already includes a number of tokens.

I think that the ending_char should be set by the user.

The formatting structure should also use fmt::Result in order to be compatible with no-std.

The format string could be internally represented as:

struct EpochFormat {
    items: [Option<EpochItem>; 16],
    num_items: usize
}

struct EpochItem {
    token: Token,
    sep_char: Option<char>,
    sep_char_alt: Option<char>,
}

Then, some formats could be defined at compile time. Date "only" would be (would be initialized at midnight):

let date_only = EpochFormat {
    items: [EpochItem { token: Token::Year, sep_char: Some('-'), sep_char_alt: None },
            EpochItem { token: Token::Month, sep_char: Some('-'), sep_char_alt: None },
            EpochItem { token: Token::Day, sep_char: Some('-'), sep_char_alt: None }],
    num_items: 3
}