Commit f2156b33 by Demid Merzlyakov

Adjusted Alert Extended Info date parsing algorithm.

parent e1ae935a
...@@ -57,7 +57,7 @@ class NWSAlertInfoParser { ...@@ -57,7 +57,7 @@ class NWSAlertInfoParser {
return dateFormatter return dateFormatter
}() }()
private func parse(dateString: String, shouldBeInThePast: Bool) -> Date? { private func parse(dateString: String, shouldBeBefore beforeDate: Date?, after afterDate: Date?) -> Date? {
// There's no year here, so we've got to guess. // There's no year here, so we've got to guess.
let currentDate = Date() let currentDate = Date()
let calendar = Calendar(identifier: .gregorian) let calendar = Calendar(identifier: .gregorian)
...@@ -65,20 +65,26 @@ class NWSAlertInfoParser { ...@@ -65,20 +65,26 @@ class NWSAlertInfoParser {
return nil return nil
} }
let dateStringWithCurrentYear = "\(currentYear) \(dateString)" let dateStringWithCurrentYear = "\(currentYear) \(dateString)"
if shouldBeInThePast { var yearAdjustment: Int? = nil
guard let dateWithCurrentYear = NWSAlertInfoParser.dateFormatter.date(from: dateStringWithCurrentYear) else { guard let dateWithCurrentYear = NWSAlertInfoParser.dateFormatter.date(from: dateStringWithCurrentYear) else {
return nil return nil
} }
if dateWithCurrentYear > currentDate { if let beforeDate = beforeDate {
let dateStringWithPreviousYear = "\(currentYear - 1) \(dateString)" if beforeDate < dateWithCurrentYear {
return NWSAlertInfoParser.dateFormatter.date(from: dateStringWithPreviousYear) yearAdjustment = -1
} }
else { }
return dateWithCurrentYear if let afterDate = afterDate {
if afterDate > dateWithCurrentYear {
yearAdjustment = 1
} }
} }
if let yearAdjustment = yearAdjustment {
let adjustedDateString = "\(currentYear + yearAdjustment) \(dateString)"
return NWSAlertInfoParser.dateFormatter.date(from: adjustedDateString)
}
else { else {
return NWSAlertInfoParser.dateFormatter.date(from: dateStringWithCurrentYear) return dateWithCurrentYear
} }
} }
...@@ -106,11 +112,11 @@ class NWSAlertInfoParser { ...@@ -106,11 +112,11 @@ class NWSAlertInfoParser {
} }
result.title = (firstLine as NSString).substring(with: firstMatch.range(withName: "title")) result.title = (firstLine as NSString).substring(with: firstMatch.range(withName: "title"))
let issuedDateString = (firstLine as NSString).substring(with: firstMatch.range(withName: "issuedDate")) let issuedDateString = (firstLine as NSString).substring(with: firstMatch.range(withName: "issuedDate"))
result.issueDate = parse(dateString: issuedDateString, shouldBeInThePast: true) result.issueDate = parse(dateString: issuedDateString, shouldBeBefore: Date(), after: nil)
let expiryDateRange = firstMatch.range(withName: "expiryDate") let expiryDateRange = firstMatch.range(withName: "expiryDate")
if expiryDateRange.location != NSNotFound { if expiryDateRange.location != NSNotFound {
let expiryDateString = (firstLine as NSString).substring(with: expiryDateRange) let expiryDateString = (firstLine as NSString).substring(with: expiryDateRange)
result.expiryDate = parse(dateString: expiryDateString, shouldBeInThePast: false) result.expiryDate = parse(dateString: expiryDateString, shouldBeBefore: nil, after: result.issueDate)
} }
result.issuer = (firstLine as NSString).substring(with: firstMatch.range(withName: "issuer")) result.issuer = (firstLine as NSString).substring(with: firstMatch.range(withName: "issuer"))
return result return result
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment