Skip to content

Commit

Permalink
Merge pull request #683 from OneBusAway/fix-681
Browse files Browse the repository at this point in the history
Attempts to fix wildly incorrect arrival-departure predictions
  • Loading branch information
aaronbrethorst authored Oct 21, 2023
2 parents 6eb79c3 + 2a3fbd6 commit 82e6043
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
11 changes: 6 additions & 5 deletions OBAKitCore/Models/Helpers/ModelHelpers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@
import Foundation

class ModelHelpers: NSObject {
/// Converts a date that represents the 1970 epoch date to `nil`.
///
/// Converts a date from before the specified `earlierDate` to `nil`.
///
/// - Parameter date: A date
/// - Returns: Nil if the date was represented by the value `0` and the date otherwise.
public static func nilifyEpochDate(_ date: Date) -> Date? {
if date == Date(timeIntervalSince1970: 0) {
/// - Parameter earlierDate: The lower bound for returning `date`.
/// - Returns: `date` if date >= `earlierDate`; otherwise `nil`.
public static func nilifyDate(_ date: Date, earlierThan earlierDate: Date) -> Date? {
if date < earlierDate {
return nil
}
else {
Expand Down
4 changes: 2 additions & 2 deletions OBAKitCore/Models/REST/ArrivalDeparture.swift
Original file line number Diff line number Diff line change
Expand Up @@ -145,13 +145,13 @@ public class ArrivalDeparture: NSObject, Identifiable, Decodable, HasReferences
predicted = try container.decode(Bool.self, forKey: .predicted)

if let predictedArrivalDate = try container.decodeIfPresent(Date.self, forKey: .predictedArrival) {
predictedArrival = ModelHelpers.nilifyEpochDate(predictedArrivalDate)
predictedArrival = ModelHelpers.nilifyDate(predictedArrivalDate, earlierThan: Date(timeIntervalSinceReferenceDate: 1.0))
} else {
predictedArrival = nil
}

if let predictedDepartureDate = try container.decodeIfPresent(Date.self, forKey: .predictedDeparture) {
predictedDeparture = ModelHelpers.nilifyEpochDate(predictedDepartureDate)
predictedDeparture = ModelHelpers.nilifyDate(predictedDepartureDate, earlierThan: Date(timeIntervalSinceReferenceDate: 1.0))
} else {
predictedDeparture = nil
}
Expand Down

0 comments on commit 82e6043

Please sign in to comment.