Skip to content

Instantly share code, notes, and snippets.

@stephent
Created May 7, 2019 21:06
Show Gist options
  • Save stephent/5121334efd175f6772327fc352732530 to your computer and use it in GitHub Desktop.
Save stephent/5121334efd175f6772327fc352732530 to your computer and use it in GitHub Desktop.
Swift Playground showing unexpected output for historical time zone rules (using example of 8 July 1842 in Europe/Rome)
/**
Playground illustrating incorrect result for secondsFromGMT for 8 July 1842 in time zone Europe/Rome. A current date is included to illustrate that secondsFromGMT and the output of the date formatter are in agreement.
For 8 July 1842, expected output is:
Describing date: 1842-07-09 05:39:33 +0000
Seconds from GMT: 2996
Abbreviation: LMT
Formatted string: 6:29:29 AM Jul 09, 1842, GMT+00:49:56
The abbreviation LMT comes from line 1660 of the current IANA tz database, filename "europe"
*/
import UIKit
let iso8601df = ISO8601DateFormatter()
iso8601df.timeZone = TimeZone(identifier: "Etc/UTC")
let europeRome = TimeZone(identifier: "Europe/Rome")
let today = Date()
// Formatter to display time of day
let tf = DateFormatter()
tf.timeStyle = .medium
// Formatter to display date
let df = DateFormatter()
df.setLocalizedDateFormatFromTemplate("MMM dd, yyyy, ZZZZ")
func describe(date:Date, using timeZone:TimeZone) {
print("***")
print("Describing date: \(date)")
print("Seconds from GMT: \(String(describing: timeZone.secondsFromGMT(for: date)))")
print("Abbreviation: \(timeZone.abbreviation(for: date) ?? "?")")
print("Formatted string: \(tf.string(from: date)) \(df.string(from: date))")
print("***")
}
if let tz = europeRome, let longLongAgo = iso8601df.date(from: "1842-07-09T05:39:33+00:00") {
tf.timeZone = tz
df.timeZone = tz
describe(date: today, using: tz)
describe(date: longLongAgo, using: tz)
}
@stephent
Copy link
Author

stephent commented May 7, 2019

When run, the playground produces the following debug output:

`


Describing date: 2019-05-07 20:56:07 +0000
Seconds from GMT: 7200
Abbreviation: GMT+2
Formatted string: 10:56:07 PM May 07, 2019, GMT+02:00



Describing date: 1842-07-09 05:39:33 +0000
Seconds from GMT: 3600
Abbreviation: GMT+1
Formatted string: 6:29:29 AM Jul 09, 1842, GMT+00:49:56


`

See comments in Gist for expected output for the 1842 date.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment