Skip to content

Commit 3abfc9f

Browse files
authored
Merge pull request #952 from LoopKit/non-english-chart-times
Show 12h or 24h time on chart x-axis depending on locale
2 parents 1b5705d + c48a5be commit 3abfc9f

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

LoopUI/Charts/ChartsManager.swift

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,16 @@ import os.log
1717
open class ChartsManager {
1818
private let log = OSLog(category: "ChartsManager")
1919

20+
private lazy var timeFormatter: DateFormatter = {
21+
let formatter = DateFormatter()
22+
let dateFormat = DateFormatter.dateFormat(fromTemplate: "j", options: 0, locale: Locale.current)!
23+
let isAmPmTimeFormat = dateFormat.index( of: "a") != nil
24+
formatter.dateFormat = isAmPmTimeFormat
25+
? "h a"
26+
: "H:mm"
27+
return formatter
28+
}()
29+
2030
public init(colors: ChartColorPalette, settings: ChartSettings, charts: [ChartProviding]) {
2131
self.colors = colors
2232
self.chartSettings = settings
@@ -105,7 +115,7 @@ open class ChartsManager {
105115
/// - Parameter date: The new candidate date
106116
public func updateEndDate(_ date: Date) {
107117
if date > endDate {
108-
var components = DateComponents(minute: 0)
118+
let components = DateComponents(minute: 0)
109119
endDate = min(
110120
maxEndDate,
111121
Calendar.current.nextDate(
@@ -156,14 +166,12 @@ open class ChartsManager {
156166

157167
// MARK: - Shared Axis
158168

169+
159170
private func generateXAxisValues() {
160171
if let endDate = charts.compactMap({ $0.endDate }).max() {
161172
updateEndDate(endDate)
162173
}
163174

164-
let timeFormatter = DateFormatter()
165-
timeFormatter.dateFormat = "h a"
166-
167175
let points = [
168176
ChartPoint(
169177
x: ChartAxisValueDate(date: startDate, formatter: timeFormatter),

0 commit comments

Comments
 (0)