Reputation: 8118
For a client of mine I'm working with really old dates like for example:
When I convert the string to a date it has a weird timezone. Example:
extension String {
func yearMonthDayDate() -> Date? {
return DateFormatter.yearMonthDayFormatter.date(from: self)
}
}
extension DateFormatter {
static let yearMonthDayFormatter: DateFormatter = {
let dateFormatter = DateFormatter()
dateFormatter.timeZone = .current
dateFormatter.dateFormat = "yyyy-MM-dd"
dateFormatter.locale = .current
return dateFormatter
}()
}
extension Date {
func zonedDate() -> String {
let dateFormatter = DateFormatter()
dateFormatter.timeZone = .current
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssXXX"
dateFormatter.locale = .current
return dateFormatter.string(from: self)
}
}
print("1000-06-17".yearMonthDayDate()!.zonedDate())
1000-06-17T00:00:00+00:17
Like you can see the timezone is +00:17. Why is this weird timezone showing up?
Upvotes: 1
Views: 142
Reputation: 271175
In the past, each local area kept their own local mean time by observing the times of sun sets and sun rises. This meant that the local time at each town/city not be a nice whole number offset from Greenwich, like +17 minutes for example. This wasn't that big of a problem until people invented trains, which had schedules. At around the 19th century, countries around the world standardised their local times, and that is why we have nice whole number offsets like +1 hour (most of the time).
So in the year 1000, your timezone, Europe/Brussels
was really 17 minutes ahead of Greenwich as far as the history we know. This bit of history got recorded in the IANA tz
database, which is what TimeZone
queries. That is why you got +00:17
.
Interestingly, when I ask the TimeZone
how many seconds it is from GMT at around the 1000s, it says 0:
let date = Date(timeIntervalSinceNow: 86400 * 365 * -1000)
// this is 0
TimeZone(identifier: "Europe/Brussels")!.secondsFromGMT(for: date)
It might have rounded the actual number of seconds down. Java can tell you the actual answer though:
// prints +00:17:30
System.out.println(ZoneId.of("Europe/Brussels")
.getRules().getOffset(LocalDate.of(1000, 1, 1).atStartOfDay()));
Upvotes: 4