Time until next day starts in UTC (Swift)

Although users interact with NewsWave on their iOS and -soon- Mac, some features are provided by a server.

So, how can I let users know when the server triggers certain tasks? If they are triggered at regular intervals you can simply calculate on the device how long until it will be triggered again. This reduces calls to the server, which in turn reduces cost.

In this example, the function calculates how many hours until the next day -in UTC timezone- starts:

var calendarIso8601 = Calendar(identifier: .iso8601) // Gregorian calendar, which “serves as an international standard for civil use.

calendarIso8601.timeZone = TimeZone(identifier: “UTC”)!

let startOfDayTodayUTC = calendarIso8601.startOfDay(for: Date()) //Start day at UTC

let endOfDayTodayUTC = Calendar.current.date(byAdding: .day, value: 1, to: startOfDayTodayUTC)

let secondsToTomo = endOfDayTodayUTC?.timeIntervalSince(Date())

let hoursToTomo = secondsToTomo!/60/60

The app can then show this number to the user.