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

Fix large-dates ambiguity by creating a repr:four modifier #709

Closed
wants to merge 0 commits into from

Conversation

dennisorlando
Copy link

@dennisorlando dennisorlando commented Sep 25, 2024

This is linked to #683

This PR creates a new [year] modifier, i.e. repr:four, which represents a full year which is forced to be 4 digits long. This is different from the default repr:full, which has a variable amount of digits depending if the large-dates feature is enabled, thus causing parsing ambiguities that might produce errors in crates who depend oh time.rs.

Tests have still to be written for this PR, thus it's not ready to be merged, but I opened this pull request anyways so that someone else might write them.
Namings and comments might also need to be discussed.

(I'll make a rebase of the commits when this PR is ready to be merged)

@dennisorlando
Copy link
Author

dennisorlando commented Sep 25, 2024

...what's up with the failing checks?

Copy link

codecov bot commented Sep 25, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 97.7%. Comparing base (4a74924) to head (4d99768).
Report is 51 commits behind head on main.

Current head 4d99768 differs from pull request most recent head 19120ac

Please upload reports for the commit 19120ac to get more accurate results.

Additional details and impacted files
@@           Coverage Diff           @@
##            main    #709     +/-   ##
=======================================
- Coverage   97.8%   97.7%   -0.1%     
=======================================
  Files         81      83      +2     
  Lines       9378    8980    -398     
=======================================
- Hits        9169    8773    -396     
+ Misses       209     207      -2     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link

@morganava morganava left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For reviewability, you may want to do the cargo fmt changes first in their own separate commit, avoid polluting the Created tests by... commit with non-test changes.

@morganava
Copy link

And there should probably be an accompanying merge request on https://github.com/time-rs/book to clarify how to resolve the large-dates ambiguity, update the [year] component diagaram, etc in https://time-rs.github.io/book/api/format-description.html#components

@dennisorlando
Copy link
Author

dennisorlando commented Sep 27, 2024

Not sure about the formatting check; it appears that the cargo fmt in the Github Action is only complaining about a different order of some imports. I tried running cargo fmt --all on my repo but sadly it doesn't appear to fix it; I guess there's a version mismatch between my cargo fmt version and the one used in Github Actions

@morganava
Copy link

@dennisorlando it looks like the CI is using rust nightly (at least for that step):

  fmt:
    name: Formatting
    runs-on: ubuntu-latest
    if: (github.event_name == 'pull_request' && github.event.pull_request.head.repo.fork) || github.event_name == 'push'

    steps:
      - name: Checkout sources
        uses: actions/checkout@v4

      - name: Install toolchain
        uses: dtolnay/rust-toolchain@nightly
        with:
          components: rustfmt

      - name: Check formatting
        run: cargo fmt --all -- --check
        env:
          RUSTFLAGS: --cfg bench

@dennisorlando
Copy link
Author

dennisorlando commented Sep 28, 2024

@dennisorlando it looks like the CI is using rust nightly (at least for that step)

Thanks! I rerun cargo fmt with an updated nightly installation and it worked.
Should a "cargo fmt --version" be added to the formatting workflow or is it unnecessary? e.g.

      - name: Install toolchain
        <...>

      - name: Print version
        run: cargo fmt --version

      - name: Check formatting
        run: cargo fmt --all -- --check
        env:
          RUSTFLAGS: --cfg bench

Copy link
Member

@jhpratt jhpratt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall, nice work! I have a few nits. More generally, I would prefer this be named repr:narrow with the corresponding YearRepr::Narrow variant. I feel this better captures the intent when combined with the large-dates feature.

Finally, can you remove the formatting commit and rebase instead? It was something I hadn't kept up to date with on nightly, hence the issue.

tests/parsing.rs Outdated Show resolved Hide resolved
@@ -392,6 +392,7 @@ fn format_date() -> time::Result<()> {
(fd!("[year sign:mandatory]"), "+2019"),
(fd!("[year base:iso_week sign:mandatory]"), "+2020"),
(fd!("[year repr:last_two]"), "19"),
(fd!("[year repr:four]"), "2019"),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given that formatting can fail, it would be a good idea to add a test for that situation.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a specific section I can put such test in? I can't figure out where to put it

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can create a new method if one doesn't seem obvious. I want to say RFC 3339 is the only case where formatting can fail currently, so it's quite likely this is the case.

@dennisorlando
Copy link
Author

Overall, nice work! I have a few nits. More generally, I would prefer this be named repr:narrow with the corresponding YearRepr::Narrow variant. I feel this better captures the intent when combined with the large-dates feature.

Finally, can you remove the formatting commit and rebase instead? It was something I hadn't kept up to date with on nightly, hence the issue.

On it;
I just wanted to double your thoughts about the "Narrow" naming as I feel like it might be unintuitive, especially when googling it with a query like "time.rs force year to be four digits long".
Perhaps something like "Standard" would be a better fit? When large-dates is disabled, people reading Narrow might associate it to something which is narrower then the standard 4 digit format, and thus skipping to read it's documentation

@jhpratt
Copy link
Member

jhpratt commented Oct 9, 2024

"standard" works for me.

@jhpratt
Copy link
Member

jhpratt commented Oct 15, 2024

After some more recent work, namely adding repr:century, I think it would be best if this were actually its own modifier. If that were the case then it could apply to either repr:century or repr:full.

I don't believe it's documented how to add a new modifier, so let me know if you need any guidance. If you prefer, I can also tackle it myself.

@jhpratt jhpratt added A-parsing Area: parsing C-enhancement Category: an enhancement with existing code A-format-description Area: format description labels Oct 15, 2024
@dennisorlando
Copy link
Author

dennisorlando commented Oct 15, 2024

After some more recent work, namely adding repr:century, I think it would be best if this were actually its own modifier. If that were the case then it could apply to either repr:century or repr:full.

I don't believe it's documented how to add a new modifier, so let me know if you need any guidance. If you prefer, I can also tackle it myself.

Hmm I have a hard time decoupling it from repr: It is, after all, a representation of the year.
How do you think it should be called? Something along the lines of [year force-4-digits:true] ?

Maybe a digit modifier? It would create a lot of conflicts with the existing code tho

@jhpratt
Copy link
Member

jhpratt commented Oct 15, 2024

With the addition or repr:century, I'm viewing it as a way to control the range of supported values rather than a dedicated repr of its own volition. Otherwise we'd need to have a repr:full_standard and repr:century_standard, likewise for any further unforeseen variants.

My thought was range:extended and range:standard, with the former being default for backwards compatibility. That would be changed in the next breaking release, bringing behavior in line with most people's expectations.

@dennisorlando
Copy link
Author

With the addition or repr:century, I'm viewing it as a way to control the range of supported values rather than a dedicated repr of its own volition. Otherwise we'd need to have a repr:full_standard and repr:century_standard, likewise for any further unforeseen variants.

My thought was range:extended and range:standard, with the former being default for backwards compatibility. That would be changed in the next breaking release, bringing behavior in line with most people's expectations.

how should range:standard affect the different representations (e.g. when used with repr:century or repr:last_two)?
(P.S: I should be able to work on this this weekend)

@jhpratt
Copy link
Member

jhpratt commented Oct 22, 2024

For repr:century, it should output two digits if and only if the year is in 0..=9_999, failing otherwise. It would have no effect on repr:last_two.

P.S: I should be able to work on this this weekend

Awesome! 🙂

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-parsing Area: parsing C-enhancement Category: an enhancement with existing code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants