Is timeStyle.long and .full broken, or have I done something wrong?

47 views Asked by At

I am making an app in swift using Swift UI which uses several different date to string functions and they all seem to work ok for all the locales I've checked besides this one. This works for my home locale of "en_uk" but when I change the schema to be in Traditional Chinese or Brazilian Portuguese both

dateFormatter.timeStyle = .long

and

dateFormatter.timeStyle = .full

do not work. The reason I want these is to include the timeZone. I am aware I can include the timeZone this way.

dateFormatter.setLocalizedDateFormatFromTemplate("yyyyMMddHHmmsszzz")

However, this doesn't work in Traditional Chinese or Brazilian Portuguese (and I assume other locales). Below is the function that seems to be causing the problem.

func toStringIncSec(timeZone: String) -> String {
    let dateFormatter = DateFormatter()
    dateFormatter.dateStyle = .short
    dateFormatter.timeStyle = .long
    dateFormatter.timeZone = TimeZone(identifier: timeZone)
    dateFormatter.locale = Locale(identifier: String(describing: Locale.current))
    return dateFormatter.string(from: self)
}

I am saving the date and timezone like this to a coreData entity.

roll.rollcreationdate = Date.now
roll.rollcreationtimeZone = TimeZone.current.identifier

In my locale it works as expected and prints 25/07/2023, 23:03:38 BST

However when changing the schema to Traditional Chinese or Brazilian Portuguese it only prints the date and then crashes the app.

This is how I call the function.

roll.rollcreationdate!.toStringIncSec(timeZone: roll.rollcreationtimeZone!)

I understand that force unwrapping isn't great but don't believe its an issue here as it works in the UK but not in other locales.

If anyone knows anything please help!!

0

There are 0 answers