Train status now properly gives minutes (or hours) a train is early or late

- Convert AmtrakDateFormatting API from static to instance properties/methods and added parsedDate(from:) to return Date.
- Update time(from:) and date(from:) to instance methods. Improve isOnTime logic to compute actual vs scheduled differences (minutes/hours) and report early/late/on-time.
- Updated ContentView to create an AmtrakDateFormatting instance and use the new API. Remove the Train.serviceDate extension.
This commit is contained in:
Tony Bark 2026-08-23 20:32:33 -04:00
parent 69fc10c6f2
commit 3533e5d4b8
2 changed files with 35 additions and 18 deletions

View file

@ -1,23 +1,32 @@
import Foundation
struct AmtrakDateFormatting {
static let isoFormatter = ISO8601DateFormatter()
let isoFormatter = ISO8601DateFormatter()
static let timeFormatter: DateFormatter = {
let timeFormatter: DateFormatter = {
let formatter = DateFormatter()
formatter.dateStyle = .none
formatter.timeStyle = .short
return formatter
}()
static let dateFormatter: DateFormatter = {
let dateFormatter: DateFormatter = {
let formatter = DateFormatter()
formatter.dateStyle = .medium
formatter.timeStyle = .none
return formatter
}()
static func time(from isoString: String?) -> String? {
func parsedDate(from isoString: String?) -> Date? {
guard let isoString, !isoString.isEmpty, let date = isoFormatter.date(
from: isoString) else {
return nil
}
return isoFormatter.date(from: isoString)
}
func time(from isoString: String?) -> String? {
guard let isoString, !isoString.isEmpty, let date = isoFormatter.date(
from: isoString) else {
return nil
@ -26,7 +35,7 @@ struct AmtrakDateFormatting {
return timeFormatter.string(from: date)
}
static func date(from isoString: String?) -> String? {
func date(from isoString: String?) -> String? {
guard let isoString, !isoString.isEmpty, let date = isoFormatter.date(
from: isoString) else {
return nil