BurntSushi / jiff

A date-time library for Rust that encourages you to jump into the pit of success.
The Unlicense
1.74k stars 33 forks source link

Allow truncation in `%.f` format specifier #73

Closed sgoll closed 3 months ago

sgoll commented 3 months ago

Description

Currently, using %.f format specifiers in strftime() allows setting the minimum amount of digits to use, e.g. %.3f, as stated in the docs:

… print a fractional second component to at least 3 decimal places.

This is fine and indeed the useful approach for most use-cases. However, it could be nice to be able to also limit the maximum number of digits. Right now, this can only be achieved by rounding the value before printing it, e.g. round(jiff::Unit::Millisecond).

BurntSushi commented 3 months ago

I think this is a bug. My intent was that %.3f would also limit to 3 decimal places. But indeed, that's not the current behavior. This new test:

        insta::assert_snapshot!(f("%.3f", mk(123_456_000)), @".123");

Fails:

Expression: f("%.3f", mk(123_456_000))
─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
-old snapshot
+new results
────────────┬────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
    0       │-.123
          0 │+.123456
────────────┴────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
BurntSushi commented 3 months ago

I fixed this in #74. I'll do a release this weekend at some point.

Note that this will only do truncation. If you need non-truncation rounding, you'll have to keep doing what you're doing. Formatting can't really feasibly do anything other than truncation because rounding can increase other units. The cases are probably pathological, but for example, doing %Y %.0f with 2024-12-31T23:59:59.999999999Z would need to increment the year when using the HalfExpand rounding mode.

My bet though is that truncating at the sub-second level is probably good enough for most use cases. You might consider doing that in your GraphQL integration since rounding can be a bit heavy-weight. (Rounding requires normalizing the date/time representation, doing the rounding and then un-normalizing back.)

sgoll commented 2 months ago

I fixed this in #74. I'll do a release this weekend at some point.

When will you make the next release? I think, the Jiff integration in Juniper can be merged once I make the changes that will be made possible by the upcoming Jiff release, cf. https://github.com/graphql-rust/juniper/pull/1271#discussion_r1703399879.

BurntSushi commented 2 months ago

Right, yeah, it slipped. I'll do one today.

BurntSushi commented 2 months ago

Okay, jiff 0.1.5 is now out!