DateComponentsFormatter UnitsStyle .abbreviated in MacOS

According to the documentation for DateComponentsFormatter UnitsStyle .abbreviated https://developer.apple.com/documentation/foundation/datecomponentsformatter/unitsstyle, this style (abbreviated) should create a string like “9h 41m 30s”. In some circumstances, eg MacOS target with import Foundation, the result is "9h 41min. 30s.", which is a sort-of mix between .brief and .abbreviated.

In my SwiftUI multi-platform app (MacOS & iOS) the incorrect (mixed) style is generated for both platforms. Using Playground, the mixed formatting occurs when the platform is set to MacOS, but not iOS with either import UIKit or import Foundation.

Is this a bug, or a "featured" difference between MacOS and iOS?

My multi-platform function is

import Foundation
public func strDuration(_ duration: TimeInterval, style: DateComponentsFormatter.UnitsStyle = .abbreviated) -> String {
        let formatter = DateComponentsFormatter()
        formatter.allowedUnits = [.hour, .minute, .second]
        formatter.unitsStyle = style
        formatter.maximumUnitCount = 3
        return formatter.string(from: duration) ?? "n/a"
    }

Regards, Michaela

Is this a bug, or a "featured" difference between MacOS and iOS?

Well, it’s certainly weird, and weird in a way that’s not helpful to you. Given that, I think it warrants a bug report even if it is a ‘feature’.

Please post your bug number, just for the record.

Having said that, I’m confused by your description of how to reproduce this. You wrote:

In my SwiftUI multi-platform app (macOS & iOS) the incorrect (mixed) style is generated for both platforms.

I tried this here in my office and I didn’t see the problem. Specifically:

  1. Using Xcode 13.0 on macOS 10.15.2 with the language set to English (UK) and the region set to UK, I created a new app from the iOS > App template.

  2. I changed the view to look like this:

    struct ContentView: View {
        var body: some View {
            Text(strDuration(9 * 3600 + 41 * 60 + 30, style: .abbreviated))
                .padding()
        }
    }
    
  3. I ran it in the iOS 15.0 simulator; it showed 9h 41m 30s.

  4. I enabled Mac support and ran it on my Mac; it showed the same thing.

It’s possible that this bug is specific to your main app. Please retry with a new app to see if that changes things. If it still reproduces with a new app, please post details of your language and region setup.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Thanks Quinn.

I'm using Xcode 13.0 Beta 5, MacOS 12.0 Beta 2, with language set to English (Aus) and region set to Aus. I've a vague recollection of the problem being around for a while, but only now needed to investigate.

On my system, a new Xcode created Mac project sees the same problem, but a new iOS project (also with the import Foundation - not UIKit) does not. As I said earlier, a Playground for iOS gives the "correct" formatting, but a Playground for the Mac platform (Playground Settings) gives the "mixed" formatting. Interestingly, in an Xcode generated multi-platform project (MacOS and iOS share the same code) the iOS app also shows the incorrect formatting, i.e. mixed.

I'll lodge a bug report in the morning.

Regards, Michaela

Bug report is FB9643489

Regards, Michaela

Bug report is FB9643489

Thanks.

As I said earlier, a Playground for iOS

Yeah, I ignored that part of it. Playgrounds are cool for… well… playing around, but they’re not what ships to users and so are irrelevant (unless you’re building developer tutorials :-).

I'm using Xcode 13.0 Beta 5, MacOS 12.0 Beta 2

Beta 2? That’s really old. We just release beta 7. Why are you stuck on this old beta?

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Accepted Answer

The reference to MacOS Beta 2 was a typo error

*phew*

So, this seems to be tied to the en_AU locale. I can reproduce it on both 11.5.2 and 12 beta if I switch my locale in System Preferences. And if I add this line to your strDuration(…) function:

formatter.calendar!.locale = Locale(identifier: "en_AU")

it reproduces without me having to do that.

I’ve no idea what the en_AU behaves this way. I think we’ll have to wait and see how your bug pans out.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Thanks Quinn,

I've added formatter.calendar!.locale = Locale(identifier: "en_UK") to my function (we sort of speak the same language here, in Oz, as in the UK). All works correctly with that change applied.

Regards, Michaela

DateComponentsFormatter UnitsStyle .abbreviated in MacOS
 
 
Q