Localized dates

This commit is contained in:
Garth Vander Houwen 2022-12-31 00:26:59 -08:00
parent 82d7f55552
commit 6ee12efdb8
9 changed files with 52 additions and 33 deletions

View file

@ -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
}

View file

@ -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)

View file

@ -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)
}
}

View file

@ -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)
}
}

View file

@ -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)
}

View file

@ -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)
}
}
}

View file

@ -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)
}
}
}

View file

@ -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)
}
}

View file

@ -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 {