chrono icon indicating copy to clipboard operation
chrono copied to clipboard

Feat: Make `write_rfc3339` public or add `Fixed::RFC3339Secs(SecondsFormat)`

Open tgrushka opened this issue 3 months ago • 2 comments

Only similar issue I found is closed #1339.

The RFC3339 does not require microsecond precision!

  • Why not make write_rfc3339 public so we can reuse it?
  • Or, add a variant: Fixed::RFC3339Secs(SecondsFormat) that passes SecondsFormat to write_rfc3339 internally (that would actually be really easy to do)?

I want to offer easier seconds formatting for better_tracing to make configuring time easier. The only option from the upstream tracing_subscriber is to use a string to format, much like #1339 suggests.

It's kind of infuriating to have to re-invent the wheel and look up the correct strftime format for the whole thing just to change the number of digits on seconds. In development, who needs microseconds precision 90% of the time?

I'm making public and adding a variant to ChronoFmtType:

#[derive(Debug, Clone, Eq, PartialEq, Default)]
pub enum ChronoFmtType {
    /// Format according to the RFC 3339 convention.
    #[default]
    Rfc3339,
    /// Format with [`SecondsFormat`] seconds format according to the RFC 3339 convention.
    Rfc3339Secs(SecondsFormat),
    /// Format according to multiple `chrono` format [Items][`Item`].
    Items(Vec<Item<'static>>),
    /// Format according to a custom format string.
    Custom(String),
}

and noticed that rfc3339 is just:

            ChronoFmtType::Rfc3339 => {
                write!(
                    w,
                    "{}",
                    t.format_with_items(core::iter::once(Item::Fixed(Fixed::RFC3339)))
                )
            }

But then Fixed::RFC3339 uses write_rfc3339 internally.

So why not Fixed::RFC3339Secs(SecondsFormat) or allow us access to write_rfc3339? Because the feature is already there, it's just not public.

tgrushka avatar Sep 18 '25 02:09 tgrushka

Oh wait, I found the solution:

            ChronoFmtType::Rfc3339Secs(secs_fmt) => {
                write!(
                    w,
                    "{}",
                    t.to_rfc3339_opts(*secs_fmt, true)
                )
            }

But this wasn't obvious. Had to "discover" it. May be helpful to make that method public anyway, or add docs to suggest how to achieve the same result.

tgrushka avatar Sep 18 '25 02:09 tgrushka

Open to reviewing a PR for this.

djc avatar Sep 19 '25 04:09 djc