From 6ee12efdb878b4b8db53cbea3633a4ef14c09245 Mon Sep 17 00:00:00 2001 From: Garth Vander Houwen Date: Sat, 31 Dec 2022 00:26:59 -0800 Subject: [PATCH] Localized dates --- Meshtastic/Export/WriteCsvFile.swift | 10 +++++++--- Meshtastic/Helpers/MeshLogger.swift | 6 ++++-- .../Views/Messages/ChannelMessageList.swift | 9 +++++---- Meshtastic/Views/Messages/Contacts.swift | 10 ++++++---- Meshtastic/Views/Messages/UserMessageList.swift | 7 +++++-- Meshtastic/Views/Nodes/DeviceMetricsLog.swift | 17 ++++++++++------- .../Views/Nodes/EnvironmentMetricsLog.swift | 15 ++++++++------- Meshtastic/Views/Nodes/PositionLog.swift | 6 ++++-- .../Views/Settings/AdminMessageList.swift | 5 +++-- 9 files changed, 52 insertions(+), 33 deletions(-) diff --git a/Meshtastic/Export/WriteCsvFile.swift b/Meshtastic/Export/WriteCsvFile.swift index c7003bae..0dbc4d69 100644 --- a/Meshtastic/Export/WriteCsvFile.swift +++ b/Meshtastic/Export/WriteCsvFile.swift @@ -9,6 +9,8 @@ import SwiftUI func TelemetryToCsvFile(telemetry: [TelemetryEntity], metricsType: Int) -> String { var csvString: String = "" + let localeDateFormat = DateFormatter.dateFormat(fromTemplate: "yyMMddjmma", options: 0, locale: Locale.current) + let dateFormatString = (localeDateFormat ?? "MM/dd/YY j:mma") if metricsType == 0 { // Create Device Metrics Header csvString = "Battery Level, Voltage, Channel Utilization, Airtime, Timestamp" @@ -23,7 +25,7 @@ func TelemetryToCsvFile(telemetry: [TelemetryEntity], metricsType: Int) -> Strin csvString += ", " csvString += String(dm.airUtilTx) csvString += ", " - csvString += dm.time?.formattedDate(format: "yyyy-MM-dd j:mm:ss") ?? NSLocalizedString("unknown.age", comment: "") + csvString += dm.time?.formattedDate(format: dateFormatString) ?? NSLocalizedString("unknown.age", comment: "") } } } else if metricsType == 1 { @@ -44,7 +46,7 @@ func TelemetryToCsvFile(telemetry: [TelemetryEntity], metricsType: Int) -> Strin csvString += ", " csvString += String(dm.current) csvString += ", " - csvString += dm.time?.formattedDate(format: "yyyy-MM-dd j:mm:ss") ?? NSLocalizedString("unknown.age", comment: "") + csvString += dm.time?.formattedDate(format: dateFormatString) ?? NSLocalizedString("unknown.age", comment: "") } } } @@ -53,6 +55,8 @@ func TelemetryToCsvFile(telemetry: [TelemetryEntity], metricsType: Int) -> Strin func PositionToCsvFile(positions: [PositionEntity]) -> String { var csvString: String = "" + let localeDateFormat = DateFormatter.dateFormat(fromTemplate: "yyMMddjmma", options: 0, locale: Locale.current) + let dateFormatString = (localeDateFormat ?? "MM/dd/YY j:mma") // Create Position Header csvString = "SeqNo, Latitude, Longitude, Alt, Sats, Speed, Heading, SNR, Timestamp" for pos in positions { @@ -73,7 +77,7 @@ func PositionToCsvFile(positions: [PositionEntity]) -> String { csvString += ", " csvString += String(pos.snr) csvString += ", " - csvString += pos.time?.formattedDate(format: "yyyy-MM-dd j:mm:ss") ?? NSLocalizedString("unknown.age", comment: "") + csvString += pos.time?.formattedDate(format: dateFormatString) ?? NSLocalizedString("unknown.age", comment: "") } return csvString } diff --git a/Meshtastic/Helpers/MeshLogger.swift b/Meshtastic/Helpers/MeshLogger.swift index 8de54f48..51d2a144 100644 --- a/Meshtastic/Helpers/MeshLogger.swift +++ b/Meshtastic/Helpers/MeshLogger.swift @@ -12,9 +12,11 @@ class MeshLogger { guard let logFile = logFile else { return } - + let localeDateFormat = DateFormatter.dateFormat(fromTemplate: "yyMMddjmmssSSa", options: 0, locale: Locale.current) + let dateFormatString = (localeDateFormat ?? "MM/dd/YY j:mm:ss.SS a") + let formatter = DateFormatter() - formatter.dateFormat = "M/d/yy h:mm:ss.SSSS" + formatter.dateFormat = dateFormatString let timestamp = formatter.string(from: Date()) guard let data = (message + " - " + timestamp + "\n").data(using: String.Encoding.utf8) else { return } print(message) diff --git a/Meshtastic/Views/Messages/ChannelMessageList.swift b/Meshtastic/Views/Messages/ChannelMessageList.swift index 707abee1..bab02dca 100644 --- a/Meshtastic/Views/Messages/ChannelMessageList.swift +++ b/Meshtastic/Views/Messages/ChannelMessageList.swift @@ -32,6 +32,8 @@ struct ChannelMessageList: View { var body: some View { NavigationStack { + let localeDateFormat = DateFormatter.dateFormat(fromTemplate: "yyMMddjmmssa", options: 0, locale: Locale.current) + let dateFormatString = (localeDateFormat ?? "MM/dd/YY j:mm:ss a") ScrollViewReader { scrollView in ScrollView { LazyVStack { @@ -104,7 +106,7 @@ struct ChannelMessageList: View { Menu("message.details") { VStack { let messageDate = Date(timeIntervalSince1970: TimeInterval(message.messageTimestamp)) - Text("Date \(messageDate, style: .date) \(messageDate.formattedDate(format: "h:mm:ss a"))").font(.caption2).foregroundColor(.gray) + Text(" \(messageDate.formattedDate(format: dateFormatString))").foregroundColor(.gray) } if !currentUser { VStack { @@ -127,16 +129,15 @@ struct ChannelMessageList: View { let ackDate = Date(timeIntervalSince1970: TimeInterval(message.ackTimestamp)) let sixMonthsAgo = Calendar.current.date(byAdding: .month, value: -6, to: Date()) if ackDate >= sixMonthsAgo! { - Text((ackDate.formattedDate(format: "h:mm:ss a"))).font(.caption2).foregroundColor(.gray) + Text("Ack Time: \(ackDate.formattedDate(format: "h:mm:ss a"))").foregroundColor(.gray) } else { - Text("unknown.age").font(.caption2).foregroundColor(.gray) + Text("unknown.age").foregroundColor(.gray) } } } if message.ackSNR != 0 { VStack { Text("Ack SNR\(String(format: "%.2f", message.ackSNR)) dB") - .font(.caption2) .foregroundColor(.gray) } } diff --git a/Meshtastic/Views/Messages/Contacts.swift b/Meshtastic/Views/Messages/Contacts.swift index ec412bd9..06abcafa 100644 --- a/Meshtastic/Views/Messages/Contacts.swift +++ b/Meshtastic/Views/Messages/Contacts.swift @@ -28,6 +28,8 @@ struct Contacts: View { var body: some View { NavigationSplitView { + let localeDateFormat = DateFormatter.dateFormat(fromTemplate: "yyMMdd", options: 0, locale: Locale.current) + let dateFormatString = (localeDateFormat ?? "MM/dd/YY") List { Section(header: Text("channels")) { // Display Contacts for the rest of the non admin channels @@ -65,10 +67,10 @@ struct Contacts: View { Text("Yesterday") .font(.subheadline) } else if lastMessageDay < (currentDay - 1) && lastMessageDay > (currentDay - 5) { - Text(lastMessageTime.formattedDate(format: "MM/dd/yy")) + Text(lastMessageTime.formattedDate(format: dateFormatString)) .font(.subheadline) } else if lastMessageDay < (currentDay - 1800) { - Text(lastMessageTime.formattedDate(format: "MM/dd/yy")) + Text(lastMessageTime.formattedDate(format: dateFormatString)) .font(.subheadline) } } @@ -162,10 +164,10 @@ struct Contacts: View { Text("Yesterday") .font(.subheadline) } else if lastMessageDay < (currentDay - 1) && lastMessageDay > (currentDay - 5) { - Text(lastMessageTime.formattedDate(format: "MM/dd/yy")) + Text(lastMessageTime.formattedDate(format: dateFormatString)) .font(.subheadline) } else if lastMessageDay < (currentDay - 1800) { - Text(lastMessageTime.formattedDate(format: "MM/dd/yy")) + Text(lastMessageTime.formattedDate(format: dateFormatString)) .font(.subheadline) } } diff --git a/Meshtastic/Views/Messages/UserMessageList.swift b/Meshtastic/Views/Messages/UserMessageList.swift index 027971cc..086682a2 100644 --- a/Meshtastic/Views/Messages/UserMessageList.swift +++ b/Meshtastic/Views/Messages/UserMessageList.swift @@ -31,6 +31,8 @@ struct UserMessageList: View { var body: some View { NavigationStack { + let localeDateFormat = DateFormatter.dateFormat(fromTemplate: "yyMMddjmmss", options: 0, locale: Locale.current) + let dateFormatString = (localeDateFormat ?? "MM/dd/YY j:mm:ss:a") ScrollViewReader { scrollView in ScrollView { LazyVStack { @@ -105,8 +107,9 @@ struct UserMessageList: View { } Menu("message.details") { VStack { + let messageDate = Date(timeIntervalSince1970: TimeInterval(message.messageTimestamp)) - Text("Date \(messageDate, style: .date) \(messageDate.formattedDate(format: "h:mm:ss a"))").font(.caption2).foregroundColor(.gray) + Text("\(messageDate.formattedDate(format: dateFormatString))").foregroundColor(.gray) } if !currentUser { VStack { @@ -130,7 +133,7 @@ struct UserMessageList: View { let ackDate = Date(timeIntervalSince1970: TimeInterval(message.ackTimestamp)) let sixMonthsAgo = Calendar.current.date(byAdding: .month, value: -6, to: Date()) if ackDate >= sixMonthsAgo! { - Text((ackDate.formattedDate(format: "h:mm:ss a"))).font(.caption2).foregroundColor(.gray) + Text("Ack Time: \(ackDate.formattedDate(format: "h:mm:ss a"))").foregroundColor(.gray) } else { Text("unknown.age").font(.caption2).foregroundColor(.gray) } diff --git a/Meshtastic/Views/Nodes/DeviceMetricsLog.swift b/Meshtastic/Views/Nodes/DeviceMetricsLog.swift index 59b9dcc6..301d2418 100644 --- a/Meshtastic/Views/Nodes/DeviceMetricsLog.swift +++ b/Meshtastic/Views/Nodes/DeviceMetricsLog.swift @@ -38,6 +38,8 @@ struct DeviceMetricsLog: View { .frame(height: 150) } } + let localeDateFormat = DateFormatter.dateFormat(fromTemplate: "yyMMddjmma", options: 0, locale: Locale.current) + let dateFormatString = (localeDateFormat ?? "MM/dd/YY j:mma") if UIDevice.current.userInterfaceIdiom == .pad || UIDevice.current.userInterfaceIdiom == .mac { //Add a table for mac and ipad Table(node.telemetries!.reversed() as! [TelemetryEntity]) { @@ -66,9 +68,9 @@ struct DeviceMetricsLog: View { Text("\(String(format: "%.2f", dm.airUtilTx))%") } } - TableColumn("Time Stamp") { dm in + TableColumn("Date & Time") { dm in if dm.metricsType == 0 { - Text(dm.time?.formattedDate(format: "MM/dd/yy j:mm") ?? "Unknown time") + Text(dm.time?.formattedDate(format: dateFormatString) ?? NSLocalizedString("unknown.age", comment: "")) } } } @@ -81,14 +83,14 @@ struct DeviceMetricsLog: View { GridItem(), GridItem(), GridItem(), - GridItem(.fixed(120)) + GridItem(.fixed(135)) ] LazyVGrid(columns: columns, alignment: .leading, spacing: 1) { GridRow { Text("Batt") .font(.caption) .fontWeight(.bold) - Text("Voltage") + Text("Volt") .font(.caption) .fontWeight(.bold) Text("ChUtil") @@ -97,7 +99,7 @@ struct DeviceMetricsLog: View { Text("AirTm") .font(.caption) .fontWeight(.bold) - Text("Timestamp") + Text("Date & Time") .font(.caption) .fontWeight(.bold) } @@ -117,8 +119,9 @@ struct DeviceMetricsLog: View { .font(.caption) Text("\(String(format: "%.2f", dm.airUtilTx))%") .font(.caption) - Text(dm.time?.formattedDate(format: "MM/dd/yy j:mm") ?? "Unknown time") - .font(.caption) + + Text(dm.time?.formattedDate(format: dateFormatString) ?? NSLocalizedString("unknown.age", comment: "")) + .font(.caption2) } } } diff --git a/Meshtastic/Views/Nodes/EnvironmentMetricsLog.swift b/Meshtastic/Views/Nodes/EnvironmentMetricsLog.swift index 1f4a3b24..f1a2e5de 100644 --- a/Meshtastic/Views/Nodes/EnvironmentMetricsLog.swift +++ b/Meshtastic/Views/Nodes/EnvironmentMetricsLog.swift @@ -21,7 +21,8 @@ struct EnvironmentMetricsLog: View { var body: some View { NavigationStack { - + let localeDateFormat = DateFormatter.dateFormat(fromTemplate: "yyMMddjmma", options: 0, locale: Locale.current) + let dateFormatString = (localeDateFormat ?? "MM/dd/YY j:mma") if UIDevice.current.userInterfaceIdiom == .pad || UIDevice.current.userInterfaceIdiom == .mac { //Add a table for mac and ipad Table(node.telemetries!.reversed() as! [TelemetryEntity]) { @@ -55,9 +56,9 @@ struct EnvironmentMetricsLog: View { Text("\(String(format: "%.2f", em.voltage))") } } - TableColumn("Time Stamp") { em in + TableColumn("Date & Time") { em in if em.metricsType == 1 { - Text(em.time?.formattedDate(format: "MM/dd/yy j:mm") ?? "Unknown time") + Text(em.time?.formattedDate(format: dateFormatString) ?? NSLocalizedString("unknown.age", comment: "")) } } } @@ -68,7 +69,7 @@ struct EnvironmentMetricsLog: View { GridItem(), GridItem(), GridItem(), - GridItem(.fixed(115)) + GridItem(.fixed(125)) ] LazyVGrid(columns: columns, alignment: .leading, spacing: 1) { @@ -86,7 +87,7 @@ struct EnvironmentMetricsLog: View { Text("Gas") .font(.caption) .fontWeight(.bold) - Text("Timestamp") + Text("Date & Time") .font(.caption) .fontWeight(.bold) } @@ -104,8 +105,8 @@ struct EnvironmentMetricsLog: View { .font(.caption) Text("\(String(format: "%.2f", em.gasResistance))") .font(.caption) - Text(em.time?.formattedDate(format: "MM/dd/yy j:mm") ?? "Unknown time") - .font(.caption) + Text(em.time?.formattedDate(format: dateFormatString) ?? NSLocalizedString("unknown.age", comment: "")) + .font(.caption2) } } } diff --git a/Meshtastic/Views/Nodes/PositionLog.swift b/Meshtastic/Views/Nodes/PositionLog.swift index 8fe6e582..6a084909 100644 --- a/Meshtastic/Views/Nodes/PositionLog.swift +++ b/Meshtastic/Views/Nodes/PositionLog.swift @@ -21,6 +21,8 @@ struct PositionLog: View { var body: some View { NavigationStack { + let localeDateFormat = DateFormatter.dateFormat(fromTemplate: "yyMMddjmma", options: 0, locale: Locale.current) + let dateFormatString = (localeDateFormat ?? "MM/dd/YY j:mma") if UIDevice.current.userInterfaceIdiom == .pad || UIDevice.current.userInterfaceIdiom == .mac { //Add a table for mac and ipad @@ -50,7 +52,7 @@ struct PositionLog: View { Text("\(String(format: "%.2f", position.snr)) dB") } TableColumn("Time Stamp") { position in - Text(position.time?.formattedDate(format: "MM/dd/yy j:mm") ?? "Unknown time") + Text(position.time?.formattedDate(format: dateFormatString) ?? NSLocalizedString("unknown.age", comment: "")) } } @@ -95,7 +97,7 @@ struct PositionLog: View { .font(.caption2) Text(String(mappin.altitude)) .font(.caption2) - Text(mappin.time?.formattedDate(format: "MM/dd/yy j:mm") ?? "Unknown time") + Text(mappin.time?.formattedDate(format: dateFormatString) ?? "Unknown time") .font(.caption2) } } diff --git a/Meshtastic/Views/Settings/AdminMessageList.swift b/Meshtastic/Views/Settings/AdminMessageList.swift index ac98d635..b2c40f8a 100644 --- a/Meshtastic/Views/Settings/AdminMessageList.swift +++ b/Meshtastic/Views/Settings/AdminMessageList.swift @@ -21,7 +21,8 @@ struct AdminMessageList: View { var user: UserEntity? var body: some View { - + let localeDateFormat = DateFormatter.dateFormat(fromTemplate: "yyMMddjmmssa", options: 0, locale: Locale.current) + let dateFormatString = (localeDateFormat ?? "MM/dd/YY j:mm:ss a") List { if user != nil { @@ -29,7 +30,7 @@ struct AdminMessageList: View { HStack { - Text("\(am.adminDescription ?? NSLocalizedString("unknown", comment: "Unknown")) - \(Date(timeIntervalSince1970: TimeInterval(am.messageTimestamp)), style: .date) \(Date(timeIntervalSince1970: TimeInterval(am.messageTimestamp)).formattedDate(format: "h:mm:ss a"))") + Text("\(am.adminDescription ?? NSLocalizedString("unknown", comment: "Unknown")) - \(Date(timeIntervalSince1970: TimeInterval(am.messageTimestamp)).formattedDate(format: dateFormatString))") .font(.caption) if am.receivedACK {