0

I m not able to understand this function "func abbreviation(for: Date) -> String?" available on TimeZone in swift. What it does. is there any tutorials that explain it in details with example?

 let time1 = TimeZone(identifier: "America/Chicago")
 print(time1?.abbreviation(for: Date()))

if I call .abbreviation() method, how device time/region/language affect the result. I m getting different result based on region and language.

If region is austrilia output is "GMT-6". If region is Canada output is "CST"

  • Surely you have read its [documentation](https://developer.apple.com/documentation/foundation/timezone/2293351-abbreviation)? What exactly didn't you understand? – Sweeper Jan 20 '21 at 08:49
  • I'm not sure what kind of answer that you expect here. "Yes, device region and language does affect the result"? It appears that you already know that. It's just a simple fact that different timezones are called differently in different parts of the world. – Sweeper Jan 20 '21 at 08:59
  • is time zone for identifier "America/Chicago" will be called differently in different parts of the world ? – Aashish Nagar Jan 20 '21 at 09:06

1 Answers1

3

If region is austrilia output is "GMT-6". If region is Canada output is "CST"

This is quite reasonable. If you search for "CST" in this list, you will find that it is an abbreviation for quite a lot of timezones.

Specifically, CST stands for both Central Standard Time in America and Central Standard Time in Australia (the latter can also be abbreviated as ACST).

If the system region is Australia, then abbreviation outputting "CST" for America/Chicago would be rather confusing. An Australian would see "CST" and go "Ah that's Australian Central Standard Time" (at least that's what iOS thinks). Clearly not what you mean. Therefore, it resorts to the more accurate but less meaningful "UTC-6".

Another example is that the same timezone is called differently depending on the language. If the phone's region is in Canada, America/Toronto, for example, would have different abbreviations depending on the phone's language. In English, it is EST at the time of writing - Eastern Standard Time. In French (Canada), it is Heure Normale de l'Est (HNE). See other timezone abbreviations and names in French.

The output of abbreviation(for:) is meant to be human readable, not so much machine-readable. It tries to produce an abbreviation that is understandable to people living in the system region, speaking the system language. For the most part, it is a black box. Don't think too much about it.

Sweeper
  • 213,210
  • 22
  • 193
  • 313
  • This is a great answer. I can't find in the docs where it says the output of `abbreviation` is localised so this answer is extra valuable. Thank you – lewis Aug 05 '22 at 14:50