blob: b28cba8708e9f1db84d6e0f420af92b09884fb3f (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
import Foundation
struct SolsticeEvent: Identifiable, Codable {
let id: UUID
let name: String
let date: Date
let season: Season
init(name: String, date: Date, season: Season) {
self.id = UUID()
self.name = name
self.date = date
self.season = season
}
func localDateTime() -> Date {
let utcCalendar = Calendar(identifier: .gregorian)
let utcComponents = utcCalendar.dateComponents([.year, .month, .day, .hour, .minute, .second], from: date)
let timeZone = TimeZone.current
let offset = timeZone.secondsFromGMT(for: date)
var localCalendar = Calendar.current
localCalendar.timeZone = timeZone
var localComponents = utcComponents
localComponents.second = (localComponents.second ?? 0) + offset
return localCalendar.date(from: localComponents) ?? date
}
func daysUntil(from now: Date = Date()) -> Int {
let today = Calendar.current.startOfDay(for: now)
let eventDay = Calendar.current.startOfDay(for: date)
let components = Calendar.current.dateComponents([.day], from: today, to: eventDay)
return max(0, components.day ?? 0)
}
}
|