Skip to content

Commit 41d8219

Browse files
authored
Merge pull request #330 from loopandlearn/units_and_timezone
Unit and timezone handling
2 parents f9cfc00 + 30367eb commit 41d8219

File tree

2 files changed

+41
-9
lines changed

2 files changed

+41
-9
lines changed

LoopFollow/Controllers/Nightscout/NSProfile.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ struct NSProfile: Decodable {
4242
let target_high: [TargetEntry]?
4343
let target_low: [TargetEntry]?
4444
let timezone: String
45+
46+
let units: String
4547
}
4648

4749
let store: [String: Store]

LoopFollow/Controllers/Nightscout/ProfileManager.swift

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ struct ProfileManager {
1414
var targetHighSchedule: [TimeValue<HKQuantity>]
1515
var overrides: [Override]
1616
var units: HKUnit
17-
var timezone: String
17+
var timezone: TimeZone
1818
var defaultProfile: String
1919

2020
struct TimeValue<T> {
@@ -38,19 +38,20 @@ struct ProfileManager {
3838
self.targetHighSchedule = []
3939
self.overrides = []
4040
self.units = .millimolesPerLiter
41-
self.timezone = "UTC"
41+
self.timezone = TimeZone.current
4242
self.defaultProfile = ""
4343
}
4444

4545
mutating func loadProfile(from profileData: NSProfile) {
46-
guard let store = profileData.store["default"] ?? profileData.store["Default"] else {
46+
guard let store = profileData.store[profileData.defaultProfile] else {
4747
return
4848
}
4949

50-
self.units = profileData.units.lowercased() == "mg/dl" ? .milligramsPerDeciliter : .millimolesPerLiter
51-
self.timezone = store.timezone
50+
self.units = store.units.lowercased() == "mg/dl" ? .milligramsPerDeciliter : .millimolesPerLiter
5251
self.defaultProfile = profileData.defaultProfile
5352

53+
self.timezone = getTimeZone(from: store.timezone)
54+
5455
self.isfSchedule = store.sens.map { TimeValue(timeAsSeconds: Int($0.timeAsSeconds), value: HKQuantity(unit: self.units, doubleValue: $0.value)) }
5556
self.basalSchedule = store.basal.map { TimeValue(timeAsSeconds: Int($0.timeAsSeconds), value: $0.value) }
5657
self.carbRatioSchedule = store.carbratio.map { TimeValue(timeAsSeconds: Int($0.timeAsSeconds), value: $0.value) }
@@ -66,7 +67,7 @@ struct ProfileManager {
6667
func currentISF() -> HKQuantity? {
6768
return getCurrentValue(from: isfSchedule)
6869
}
69-
70+
7071
func currentBasal() -> String? {
7172
if let basal = getCurrentValue(from: basalSchedule) {
7273
return Localizer.formatToLocalizedString(basal, maxFractionDigits: 2, minFractionDigits: 0)
@@ -90,7 +91,9 @@ struct ProfileManager {
9091
guard !schedule.isEmpty else { return nil }
9192

9293
let now = Date()
93-
let calendar = Calendar.current
94+
var calendar = Calendar.current
95+
calendar.timeZone = self.timezone
96+
9497
let currentTimeInSeconds = calendar.component(.hour, from: now) * 3600 +
9598
calendar.component(.minute, from: now) * 60 +
9699
calendar.component(.second, from: now)
@@ -106,15 +109,42 @@ struct ProfileManager {
106109
return lastValue
107110
}
108111

112+
private func getTimeZone(from identifier: String) -> TimeZone {
113+
if let timeZone = TimeZone(identifier: identifier) {
114+
return timeZone
115+
}
116+
117+
let adjustedIdentifier = identifier.replacingOccurrences(of: "ETC/", with: "Etc/")
118+
if let timeZone = TimeZone(identifier: adjustedIdentifier) {
119+
return timeZone
120+
}
121+
122+
if identifier.uppercased().contains("GMT") {
123+
let components = identifier.uppercased().components(separatedBy: "GMT")
124+
if components.count > 1 {
125+
let offsetString = components[1]
126+
if let offsetHours = Int(offsetString) {
127+
let correctedOffsetHours = -offsetHours
128+
let secondsFromGMT = correctedOffsetHours * 3600
129+
if let timeZone = TimeZone(secondsFromGMT: secondsFromGMT) {
130+
return timeZone
131+
}
132+
}
133+
}
134+
}
135+
136+
return TimeZone.current
137+
}
138+
109139
mutating func clear() {
110140
self.isfSchedule = []
111141
self.basalSchedule = []
112142
self.carbRatioSchedule = []
113143
self.targetLowSchedule = []
114144
self.targetHighSchedule = []
115145
self.overrides = []
116-
self.units = HKUnit.millimolesPerLiter
117-
self.timezone = "UTC"
146+
self.units = .millimolesPerLiter
147+
self.timezone = TimeZone.current
118148
self.defaultProfile = ""
119149
}
120150
}

0 commit comments

Comments
 (0)