1

I need to get three/four letter abbreviation for the country called 'Africa/Nairobi', I have tried using

print(TimeZone(abbreviation: "SAST")?.identifier) from the NSTimeZone.abbreviationDictionary, results nil.

How to get the three/four letter abbreviations for this country 'Africa/Nairobi'. Thanks in Advance

1 Answers1

3

You can get a full standard localized name of timezone by the following method.

let timezone:TimeZone = TimeZone.init(identifier: "Africa/Nairobi") ?? TimeZone.current
print(timezone.localizedName(for: .generic, locale: .autoupdatingCurrent))
print(timezone.localizedName(for: .standard, locale: .autoupdatingCurrent))

Will give you following output

Optional("East Africa Time")

Optional("East Africa Time")

I think now you get easily get EAT form East Africa Time by spliting and combining string

let fullName = timezone.localizedName(for: .standard, locale: .autoupdatingCurrent) ?? ""
var result = ""
fullName.enumerateSubstrings(in: fullName.startIndex..<fullName.endIndex, options: .byWords) { (substring, _, _, _) in
    if let substring = substring { result += substring.prefix(1) }
}
print(result)

Will give you out put

EAT

Jaydeep Vyas
  • 4,411
  • 1
  • 18
  • 44