Commit f2156b33 by Demid Merzlyakov

Adjusted Alert Extended Info date parsing algorithm.

parent e1ae935a
......@@ -57,7 +57,7 @@ class NWSAlertInfoParser {
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.
let currentDate = Date()
let calendar = Calendar(identifier: .gregorian)
......@@ -65,20 +65,26 @@ class NWSAlertInfoParser {
return nil
}
let dateStringWithCurrentYear = "\(currentYear) \(dateString)"
if shouldBeInThePast {
guard let dateWithCurrentYear = NWSAlertInfoParser.dateFormatter.date(from: dateStringWithCurrentYear) else {
return nil
}
if dateWithCurrentYear > currentDate {
let dateStringWithPreviousYear = "\(currentYear - 1) \(dateString)"
return NWSAlertInfoParser.dateFormatter.date(from: dateStringWithPreviousYear)
var yearAdjustment: Int? = nil
guard let dateWithCurrentYear = NWSAlertInfoParser.dateFormatter.date(from: dateStringWithCurrentYear) else {
return nil
}
if let beforeDate = beforeDate {
if beforeDate < dateWithCurrentYear {
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 {
return NWSAlertInfoParser.dateFormatter.date(from: dateStringWithCurrentYear)
return dateWithCurrentYear
}
}
......@@ -106,11 +112,11 @@ class NWSAlertInfoParser {
}
result.title = (firstLine as NSString).substring(with: firstMatch.range(withName: "title"))
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")
if expiryDateRange.location != NSNotFound {
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"))
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