Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
ChristopherRabotin opened this issue Dec 2, 2022 · 1 comment

Comments

@ChristopherRabotin
Copy link
Member

ChristopherRabotin commented Dec 2, 2022

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 .

@ChristopherRabotin
Copy link
Member Author

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
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant