How it works...

To create our howLongUntilChristmas function, let's first get the currently set calendar and time zone as they will be needed for the date calculations to come:

let calendar = Calendar.currentlet timeZone = TimeZone.current

While retrieving the current time zone is self-explanatory, it is not immediately obvious what the Calendar type represents and why we need to retrieve it.

How dates are represented is not as universally agreed as you might believe. Certain time components are mostly universal, such as the length of years and days, as they are connected to astronomical events, such as the time it takes for the earth to perform one revolution of the sun, and for the earth to complete one revolution on its axis, respectively. ...

Get Swift 4 Programming Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.