From 61c0f142daa236f0c9da68c3d50acf80133b5329 Mon Sep 17 00:00:00 2001 From: Dan Loewenherz Date: Sat, 9 Nov 2024 13:50:28 -0600 Subject: [PATCH] add nomatchingformat enum --- .../DateFormatter+LionheartExtensions.swift | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/Sources/Core/DateFormatter+LionheartExtensions.swift b/Sources/Core/DateFormatter+LionheartExtensions.swift index bc45ac1..c118f53 100644 --- a/Sources/Core/DateFormatter+LionheartExtensions.swift +++ b/Sources/Core/DateFormatter+LionheartExtensions.swift @@ -8,11 +8,16 @@ import Foundation +public struct NoMatchingFormat { + public var input: String + public var format: String +} + public enum DateFormatError: Error { case emptyDates case inconsistentFormat case unspecified - case noMatchingFormat + case noMatchingFormat(NoMatchingFormat?) } fileprivate struct DateFormatterString { @@ -84,7 +89,7 @@ public extension DateFormatter { formatters.append(DateFormatter(format: DateFormatterString.DateComponent6)) case 3: timeFormatStrings = DateFormatterString.ThreeSpaceFormatStrings - default: throw DateFormatError.noMatchingFormat + default: throw DateFormatError.noMatchingFormat(nil) } for dateString in DateFormatterString.NoSpaceFormatStrings { @@ -97,12 +102,20 @@ public extension DateFormatter { } } + var noMatchingFormat: NoMatchingFormat? = nil formatterLoop: for formatter in formatters { + var i = 0 for dateString in dateStrings { guard formatter.date(from: dateString) != nil else { // Skip to the next formatter if a string failed to parse with this formatter. + if i > (dateStrings.count / 2) { + noMatchingFormat = NoMatchingFormat(input: dateString, format: formatter.dateFormat) + } + continue formatterLoop } + + i += 1 } // If we got here, all of the strings worked with this formatter. @@ -110,6 +123,6 @@ public extension DateFormatter { } // No date formatter worked for everything. Fail. - throw DateFormatError.noMatchingFormat + throw DateFormatError.noMatchingFormat(noMatchingFormat) } }