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

Well-known format descriptions should support formatting Date. #616

Closed
wfraser opened this issue Aug 25, 2023 · 4 comments
Closed

Well-known format descriptions should support formatting Date. #616

wfraser opened this issue Aug 25, 2023 · 4 comments
Labels
A-formatting Area: formatting A-parsing Area: parsing C-feature-request Category: a new feature (not already implemented)

Comments

@wfraser
Copy link

wfraser commented Aug 25, 2023

None of the three well-known format descriptions provided here support formatting Date instances:

use time::format_description::well_known::*;

let today = time::OffsetDateTime::now_utc().date();
dbg!(today.format(&Iso8601::DEFAULT));
dbg!(today.format(&Rfc2822));
dbg!(today.format(&Rfc3339));
[test.rs:14] today.format(&Iso8601::DEFAULT) = Err(
    InsufficientTypeInformation,
)
[test.rs:15] today.format(&Rfc2822) = Err(
    InsufficientTypeInformation,
)
[test.rs:16] today.format(&Rfc3339) = Err(
    InsufficientTypeInformation,
)

Far as I can tell, the only way to format a date without using parse() to construct my own format is to do this very long-winded incantation:

const ISO8601_DATE: u128 = iso8601::Config::DEFAULT.set_formatted_components(FormattedComponents::Date).encode();
today.format(&Iso8601::<ISO8601_DATE>);

And this has to have the config declared as a const and can't be written as a one-liner. This is extremely verbose for such a basic operation.

Can I request either one of these changes:

  • Existing formats could support formatting a date by omitting the time parts when none of them are present. (preferred)
  • A config of Iso8601 could be provided out of the box (alongside Iso8601::DEFAULT) which does the same and doesn't require me to dig so deep into the weeds here?
@jhpratt
Copy link
Member

jhpratt commented Aug 25, 2023

For RFC 2822 and RFC 3339, the only supported formats are all-inclusive, as there is no indication in the specifications that anything else is permitted.

For ISO 8601, this can absolutely be done with the addition of an associated const.

const ISO8601_DATE: u128 = iso8601::Config::DEFAULT.set_formatted_components(FormattedComponents::Date).encode();
today.format(&Iso8601::<ISO8601_DATE>);

Completely unrelated, but do not rely on encode returning a u128. The return type is not guaranteed. You should use the EncodedConfig type alias.

And this has to have the config declared as a const and can't be written as a one-liner.

The config can be inlined; a separately declared const isn't strictly necessary.

  • Existing formats could support formatting a date by omitting the time parts when none of them are present. (preferred)

The implementation has no knowledge of what type is actually being formatted. The default configuration for ISO 8601 is documented as formatting the date, time, and UTC offset.

  • A config of Iso8601 could be provided out of the box (alongside Iso8601::DEFAULT) which does the same and doesn't require me to dig so deep into the weeds here?

This is almost certainly what will be done. It'll be quite easy to add further associated constants.

@jhpratt jhpratt added C-feature-request Category: a new feature (not already implemented) A-formatting Area: formatting A-parsing Area: parsing labels Aug 25, 2023
@wfraser
Copy link
Author

wfraser commented Aug 25, 2023

Agreed, for 2822 it probably doesn't make sense, you never see those with the time omitted either.

However I don't read RFC 3339 as requiring there be a time, and I do see date-only RFC-3339 in use. For example, GNU Coreutils' date lets you do date --rfc-3339=date in addition to date --rfc-3339=second which prints date and time.

The RFC's ABNF breaks it out separately as

full-date       = date-fullyear "-" date-month "-" date-mday

But it doesn't positively say the time is optional either, and the examples all have both date and time.

Ultimately, being a subset of ISO 8601, as long as that code can do it easily, it doesn't matter :)

An associated const on Iso8601 would be perfect and much appreciated.

The config can be inlined; a separately declared const isn't strictly necessary.

You're right, I was getting a syntax error when I tried earlier, but I took another look and figured out what I was doing wrong. The syntax for it is very unusual (I don't think I've seen it before in my ~7 years of doing rust 😄). You have to use braces around the const expression:

today.format(&Iso8601::<{iso8601::Config::DEFAULT.set_formatted_components(FormattedComponents::Date).encode()}>)

@jhpratt
Copy link
Member

jhpratt commented Aug 26, 2023

I have implemented more associated constants for Iso8601. You can view them here. As part of the change, I made it very easy to add additional configurations if there is the desire to do so in the future.

With regard to RFC 2822 and RFC 3339, I see no indication in either that there is intent to permit date-only formatting, among others. In neither RFC does the ABNF clearly show that each item is to be permitted on its own. Such an interpretation would be seem to also permit formatting only the hours, for example, which is presumably not the goal. I also checked the ABNF specification to see if there was anything implicit that I wasn't aware of, but did not find anything.

For the date command, that's its own program that can do whatever it wants. I'm following the specifications as strictly as possible, and I don't see anything that shows it was supposed to be allowed. On the contrary, the examples only include date-time-offset values.

Ultimately, being a subset of ISO 8601

RFC 3339 may have been a subset of ISO 8601 when it was written, but it's not a subset of the latest version of ISO 8601 (from 2019). There are minor differences here and there.

As I've implemented the associated constants for ISO 8601 and don't see where it's permitted in the two RFCs, I'm closing this as completed. Thank you for the request!

@jhpratt jhpratt closed this as completed Aug 26, 2023
@wfraser
Copy link
Author

wfraser commented Aug 30, 2023

This is great, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-formatting Area: formatting A-parsing Area: parsing C-feature-request Category: a new feature (not already implemented)
Projects
None yet
Development

No branches or pull requests

2 participants