Can anyone help me to find out difference between :
TimeZone.init(identifier: "GMT")
TimeZone(abbreviation: "GMT")
TimeZone.init(identifier: "UTC")
TimeZone(abbreviation: "UTC")
TL;DR:
identifier:
means the resulting TimeZone instance continues to use the identifier you provide when referencing the timezone.abbreviation:
, instead, uses the default name for the timezone. Otherwise, they are effectively the same for calculations.
You don’t need to call Class.init()
. Class()
is the standard form.
Since Apple’s documentation is somewhat lacking in the matter of TimeZone initialization, and we don’t have access to their source code, here’s what I’ve been able to determine through testing variations on these:
let tz1 = TimeZone(identifier: "PST")
let tz2 = TimeZone(identifier: "America/Los_Angeles")
let tz3 = TimeZone(abbreviation: "PST")
let tz4 = TimeZone(abbreviation: "America/Los_Angeles")
print(tz1!)
print(tz2!)
print(tz3!)
print(tz4 == nil ? "nil" : tz4!)
produces:
PST (fixed)
America/Los_Angeles (fixed)
America/Los_Angeles (fixed)
nil
From which, we can conclude that the identifier:
mode means the supplied identifier will be used when referencing the name/description (aka: identifier
) of the timezone, and the abbreviation:
mode means the abbreviation will be used to lookup the timezone, but subsequent references to the name/description of the timezone will use the full name/description.
Also note that, while identifier:
will accept both the abbreviation and long names for a timezone, abbreviation:
will only accept abbreviations.