mirror of
https://github.com/tonytins/amtkstat.git
synced 2026-10-24 05:32:54 -04:00
Formatted code
Brought over and applied Swift Argument Parser's code formatting script
This commit is contained in:
parent
dc464de1c4
commit
c699007cbf
9 changed files with 593 additions and 563 deletions
|
|
@ -1,93 +1,99 @@
|
|||
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)
|
||||
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
|
||||
}
|
||||
|
||||
func time(from isoString: String?, in zone: TimeZone?, comparedTo originZone: TimeZone? = nil) -> String? {
|
||||
guard let isoString, !isoString.isEmpty, let date = isoFormatter.date(
|
||||
from: isoString) else {
|
||||
return nil
|
||||
}
|
||||
|
||||
timeFormatter.timeZone = zone
|
||||
|
||||
let formatted = timeFormatter.string(from: date)
|
||||
|
||||
guard
|
||||
let zone,
|
||||
let originZone,
|
||||
zone.identifier != originZone.identifier,
|
||||
let abbreviation = zone.abbreviation(for: date)
|
||||
else {
|
||||
return formatted
|
||||
}
|
||||
|
||||
return "\(formatted) \(abbreviation)"
|
||||
|
||||
return isoFormatter.date(from: isoString)
|
||||
}
|
||||
|
||||
func time(from isoString: String?, in zone: TimeZone?, comparedTo originZone: TimeZone? = nil)
|
||||
-> String?
|
||||
{
|
||||
guard let isoString, !isoString.isEmpty,
|
||||
let date = isoFormatter.date(
|
||||
from: isoString)
|
||||
else {
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
func isOnTime(actual: String?, scheduled: String?) -> LateTrain {
|
||||
|
||||
guard
|
||||
let actualDate = parsedDate(from: actual),
|
||||
let scheduledDate = parsedDate(from: scheduled)
|
||||
else {
|
||||
return .unknown
|
||||
}
|
||||
|
||||
let differenceInMinutes = Int(actualDate.timeIntervalSince(scheduledDate) / 60)
|
||||
|
||||
switch differenceInMinutes {
|
||||
case -1...1:
|
||||
return .onTime
|
||||
case ..<0:
|
||||
return .early(minutes: -differenceInMinutes)
|
||||
default:
|
||||
return .late(minutes: differenceInMinutes)
|
||||
}
|
||||
|
||||
timeFormatter.timeZone = zone
|
||||
|
||||
let formatted = timeFormatter.string(from: date)
|
||||
|
||||
guard
|
||||
let zone,
|
||||
let originZone,
|
||||
zone.identifier != originZone.identifier,
|
||||
let abbreviation = zone.abbreviation(for: date)
|
||||
else {
|
||||
return formatted
|
||||
}
|
||||
|
||||
|
||||
func localTime(timeZone: TimeZone?) -> String {
|
||||
let now = Date()
|
||||
timeFormatter.timeZone = timeZone
|
||||
return timeFormatter.string(from: now)
|
||||
|
||||
return "\(formatted) \(abbreviation)"
|
||||
}
|
||||
|
||||
func isOnTime(actual: String?, scheduled: String?) -> LateTrain {
|
||||
|
||||
guard
|
||||
let actualDate = parsedDate(from: actual),
|
||||
let scheduledDate = parsedDate(from: scheduled)
|
||||
else {
|
||||
return .unknown
|
||||
}
|
||||
|
||||
func updateTime(timeZone: TimeZone?) {
|
||||
let now = Date()
|
||||
timeFormatter.timeZone = timeZone
|
||||
|
||||
let differenceInMinutes = Int(actualDate.timeIntervalSince(scheduledDate) / 60)
|
||||
|
||||
switch differenceInMinutes {
|
||||
case -1...1:
|
||||
return .onTime
|
||||
case ..<0:
|
||||
return .early(minutes: -differenceInMinutes)
|
||||
default:
|
||||
return .late(minutes: differenceInMinutes)
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
func localTime(timeZone: TimeZone?) -> String {
|
||||
let now = Date()
|
||||
timeFormatter.timeZone = timeZone
|
||||
return timeFormatter.string(from: now)
|
||||
}
|
||||
|
||||
func updateTime(timeZone: TimeZone?) {
|
||||
let now = Date()
|
||||
timeFormatter.timeZone = timeZone
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue