From 7e174058589ff8eea4a40ecf6fe3bcea253722a4 Mon Sep 17 00:00:00 2001 From: Hykilpikonna Date: Sun, 17 Jan 2021 14:25:35 -0500 Subject: [PATCH] [+] Create computed field nextActivate to calculate the next activation date for an alarm --- ProjectClock/Models.swift | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/ProjectClock/Models.swift b/ProjectClock/Models.swift index 1f4bf20..17b13e0 100644 --- a/ProjectClock/Models.swift +++ b/ProjectClock/Models.swift @@ -53,6 +53,22 @@ struct Alarm: Codable /// When is the last time that the alarm went off var lastActivate: Date? = nil + /// When should the alarm activate next + var nextActivate: Date? + { + // Get current date + let now = Date() + let (y, m, d) = now.getYMD() + let (nh, nm, _) = now.getHMS() + + // Create activation date + var date = Date.create(y, m, d, hour, minute) + + // If it will activate tomorrow + if nh > hour || (nh == hour && nm > minute) { date = date.added(.day, 1) } + + return date + } } class Alarms: Codable