mirror of
https://github.com/tonytins/amtkstat.git
synced 2026-10-24 13:42:51 -04:00
- 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.
46 lines
1.3 KiB
Swift
46 lines
1.3 KiB
Swift
import Foundation
|
|
|
|
struct AmtrakDateFormatting {
|
|
let isoFormatter = ISO8601DateFormatter()
|
|
|
|
let timeFormatter: DateFormatter = {
|
|
let formatter = DateFormatter()
|
|
formatter.dateStyle = .none
|
|
formatter.timeStyle = .short
|
|
return formatter
|
|
}()
|
|
|
|
let dateFormatter: DateFormatter = {
|
|
let formatter = DateFormatter()
|
|
formatter.dateStyle = .medium
|
|
formatter.timeStyle = .none
|
|
return formatter
|
|
}()
|
|
|
|
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
|
|
}
|
|
|
|
return timeFormatter.string(from: date)
|
|
}
|
|
|
|
func date(from isoString: String?) -> String? {
|
|
guard let isoString, !isoString.isEmpty, let date = isoFormatter.date(
|
|
from: isoString) else {
|
|
return nil
|
|
}
|
|
|
|
return dateFormatter.string(from: date)
|
|
}
|
|
}
|