Duration format

I notice that when formatting the new Duration there is an issue when you limit to sub-second units.

import Foundation

let exposure: Duration = .milliseconds(2000) + .milliseconds(12) + .microseconds(923)

exposure.formatted(
  .units(allowed: [.seconds, .milliseconds]) // "2 sec, 13 ms" ๐Ÿ˜€
)

exposure.formatted(
  .units(allowed: [.milliseconds]) // "13 ms" ๐Ÿคจ Expected: 2,013 ms
)

exposure.formatted(
  .units(allowed: [.milliseconds], valueLength: 4)  // "0,013 ms" ๐Ÿ™ƒ
)

I know that Duration is split into seconds and atto-seconds under the hood so have a guess why it is happening. Where should I report a bug about this? Duration is part of Swift and the formatters are part of Foundation?

1 Like

This looks like issues with formatting, not Duration itself so probably feedback assistant would be the right spot.

cc @Philippe_Hausler

1 Like

This part is definitely part of the localization/internationalization; my guess is either a) there is a feature in the .time format style missing that you need or b) you are really trying to express a Measurement<UnitDuration> instead.

Note: this area is not per-se my focus so other folks may have a more nuanced suggestion; never the less as @ktoso suggested submitting feedback would definitely be the route to take here.

1 Like

Thank you very much. I posted FB12010264 with these two examples:

```swift
Duration.microseconds(2042).formatted(
  .units(allowed: [.microseconds])
)  // produces expected output "2,042 ฮผs"

Duration.milliseconds(2042).formatted(
  .units(allowed: [.milliseconds])
) // produces unexpected output "42 ms"

Hope this helps. :grinning:

Worth trying this one as well:

Duration.microseconds(1002042).formatted(
  .units(allowed: [.microseconds])
)

it might also return "2,042 ฮผs" (I didn't try).

Have you tried the version of "formatted" call that takes "valueLengthLimits" parameter?

@Ray_Fix If you can try this on the current seeds, it appears this has been fixed.

3 Likes