Home > Enterprise >  Swift configuring timezone with identifier changes GMT value
Swift configuring timezone with identifier changes GMT value

Time:11-02

I have the following playground code:

let dateFormatter = ISO8601DateFormatter()
let zurichTimeZone = TimeZone(identifier: "Europe/Zurich")
dateFormatter.timeZone = zurichTimeZone
// dateFormatter.timeZone = .init(abbreviation: "GMT 1:00")


// prints timezone  1
print(dateFormatter.string(from: Date(timeIntervalSince1970: TimeInterval(100400))))

// prints timezone  2
print(dateFormatter.string(from: Date(timeIntervalSince1970: TimeInterval(1665957700))))

The first output is "1970-01-02T04:53:20 01:00" and the second output is "2022-10-17T00:01:40 02:00". Why does the GMT value change? If use "Europe/Kalingrad" as the identifier, I will get 3 from the first and 2 from the second print. The problem won't appear when I'm setting the timezone with "dateFormatter.timeZone = .init(abbreviation: "GMT 1:00")". I also can't find this behaviour when I'm using "Pacific/Guam".

CodePudding user response:

The named TimeZones take daylight saving time (DST) into consideration, while the ones created by a GMT offset do not.

Zurich is GMT 1 in winter time, but GMT 2 in summer time.

The 2 dates you create are on different sides of the clock change, so 1 uses summer time, while the other uses winter time. This is where the 1h time difference using the 2 different timezones is coming from.

  • Related