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

Permit skipping [optional] values when formatting #708

Open
MaxKingPor opened this issue Sep 25, 2024 · 5 comments
Open

Permit skipping [optional] values when formatting #708

MaxKingPor opened this issue Sep 25, 2024 · 5 comments
Labels
A-format-description Area: format description A-formatting Area: formatting C-enhancement Category: an enhancement with existing code

Comments

@MaxKingPor
Copy link
Contributor

How to format optional as empty

@jhpratt
Copy link
Member

jhpratt commented Sep 25, 2024

Can you share some code? I'm not certain what you're referring to.

@jhpratt jhpratt added the C-question Category: seeking clarification on a topic label Sep 25, 2024
@MaxKingPor
Copy link
Contributor Author

MaxKingPor commented Sep 25, 2024

Can you share some code? I'm not certain what you're referring to.

    #[test]
    fn test_format() {
        let format =
            time::macros::format_description!(version = 2, "[year]-[month]-[optional [[day]]]");

        // let mut format = format_description!(version = 2, "[year]-[month]-[optional format:false [[day]]]");
        // format.format_optional(false)
        
        let date = time::Date::from_calendar_date(2024, time::Month::April, 1).unwrap();

        let str = date.format(format).unwrap(); // --> format to "2024-04-"
                                                // assert_eq!(str,"2024-04-");
        println!("{:?}", str)
    }

@jhpratt
Copy link
Member

jhpratt commented Sep 25, 2024

Why is this needed? Generally you should be providing as much information as possible when formatting, only inferring when parsing if absolutely necessary. The description in documentation matches this:

An item that may or may not be present while parsing. While formatting, the value is always present.

@jhpratt jhpratt added the C-needs-details Category: more details are needed to assess the situation label Sep 25, 2024
@MaxKingPor
Copy link
Contributor Author

MaxKingPor commented Sep 25, 2024

Why is this needed? Generally you should be providing as much information as possible when formatting, only inferring when parsing if absolutely necessary. The description in documentation matches this:

An item that may or may not be present while parsing. While formatting, the value is always present.

Format it into a simpler form when supporting more formats

pub const FORMAT: &[time::format_description::BorrowedFormatItem] = time::macros::format_description!(
    version = 2,
    "[year]-[month]-[day][first [ ] [T]][hour]:[minute]:[second][optional [[first [[end]] [.[subsecond digits:1+]]]]][optional [[first [[end]] [[offset_hour]:[offset_minute][optional [:[offset_second]]]]]]]"
);

Just like this, if I want to format the time zone without formatting milliseconds, I can't do it

@jhpratt jhpratt added A-formatting Area: formatting C-enhancement Category: an enhancement with existing code A-format-description Area: format description and removed C-question Category: seeking clarification on a topic C-needs-details Category: more details are needed to assess the situation labels Oct 9, 2024
@jhpratt
Copy link
Member

jhpratt commented Oct 21, 2024

Unfortunately the optional component does not permit modifiers in the current public API and said API is not marked #[non_exhaustive]. As such, it would be a breaking change to implement this. I'll add it to the list in #650.

@jhpratt jhpratt changed the title How to disable optional for formatting Permit skipping [optional] values when formatting Dec 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-format-description Area: format description A-formatting Area: formatting C-enhancement Category: an enhancement with existing code
Projects
None yet
Development

No branches or pull requests

2 participants