I want to show timezone as "IST", "EST", etc. That seems to be simple but there is an issue.When I use following code:
let timeZone = TimeZone.current.abbreviation()
For India I get "GMT+5:30" but for Toronto, I get "EDT". Please help, or let me know some other way to show such abbreviations.
EDIT 1
I came up with a workaround but still, sometimes I get some weird timezones:
func getTimeZoneAbbrevation() -> String {
var returnTimeZone = ""
let timezone = TimeZone.current.identifier
let dict = TimeZone.abbreviationDictionary
for (key,value) in dict {
if value == timezone {
returnTimeZone = key
break
}
}
if returnTimeZone == "" {
if let timezone = NSTimeZone.default.abbreviation() {
returnTimeZone = timezone
}
}
return returnTimeZone
}