From 21563377f44907d6e3f6b670318a74760678dd47 Mon Sep 17 00:00:00 2001 From: Ivan Babkin Date: Mon, 9 Sep 2019 14:54:43 +0300 Subject: [PATCH 1/2] Add method for DateFormattingService that parses date from string in one of the given formats with current region. --- CHANGELOG.md | 3 +++ LeadKit.podspec | 2 +- ...mattingService+DefaultImplementation.swift | 23 +++++++++++++++++++ Sources/Info-iOS.plist | 2 +- Sources/Info-tvOS.plist | 2 +- Sources/Info-watchOS.plist | 2 +- .../DateFormattingService.swift | 9 ++++++++ 7 files changed, 39 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0686dd8b..33571214 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,8 @@ # Changelog +### 0.9.26 +- **Add**: method `date(from string:formats:parsedIn:)` method for `DateFormattingService` that parses date from string in one of the given formats with current region. + ### 0.9.26 - **Add**: method `processResultFromConfigurationSingle` for `TotalCountCursor` that allows to get server response. - **Add**: possibility to inherit from `TotalCountCursor`. diff --git a/LeadKit.podspec b/LeadKit.podspec index c0c7eaae..241f1d0f 100644 --- a/LeadKit.podspec +++ b/LeadKit.podspec @@ -1,6 +1,6 @@ Pod::Spec.new do |s| s.name = "LeadKit" - s.version = "0.9.26" + s.version = "0.9.27" s.summary = "iOS framework with a bunch of tools for rapid development" s.homepage = "https://github.com/TouchInstinct/LeadKit" s.license = "Apache License, Version 2.0" diff --git a/Sources/Extensions/DateFormattingService/DateFormattingService+DefaultImplementation.swift b/Sources/Extensions/DateFormattingService/DateFormattingService+DefaultImplementation.swift index 1bb70f33..ce613e27 100644 --- a/Sources/Extensions/DateFormattingService/DateFormattingService+DefaultImplementation.swift +++ b/Sources/Extensions/DateFormattingService/DateFormattingService+DefaultImplementation.swift @@ -40,6 +40,18 @@ public extension DateFormattingService { return format.stringToDateFormat.toDate(string, region: region) } + func date(from string: String, formats: [DateFormatType], parsedIn: Region?) -> DateInRegion? { + let region = parsedIn ?? currentRegion + + for format in formats { + if let parsedDate = format.stringToDateFormat.toDate(string, region: region) { + return parsedDate + } + } + + return nil + } + func string(from date: DateRepresentable, format: DateFormatType) -> String { return format.dateToStringFormat.toString(date) } @@ -81,6 +93,17 @@ public extension DateFormattingService where Self: Singleton { return shared.date(from: string, format: format, parsedIn: parsedIn) } + /// Method parses date from string in one of the given formats with current region. + /// + /// - Parameters: + /// - string: String to use for date parsing. + /// - formats: Formats that should be used for date parsing. + /// - parsedIn: A region that should be used for date parsing. In case of nil defaultRegion will be used. + /// - Returns: Date parsed from given string or default date if parsing did fail. + static func date(from string: String, formats: [DateFormatType], parsedIn: Region?) -> DateInRegion? { + return shared.date(from: string, formats: formats, parsedIn: parsedIn) + } + /// Method format date in given format. /// /// - Parameters: diff --git a/Sources/Info-iOS.plist b/Sources/Info-iOS.plist index 45613ad7..fb705838 100644 --- a/Sources/Info-iOS.plist +++ b/Sources/Info-iOS.plist @@ -15,7 +15,7 @@ CFBundlePackageType FMWK CFBundleShortVersionString - 0.9.26 + 0.9.27 CFBundleVersion $(CURRENT_PROJECT_VERSION) NSPrincipalClass diff --git a/Sources/Info-tvOS.plist b/Sources/Info-tvOS.plist index 45613ad7..fb705838 100644 --- a/Sources/Info-tvOS.plist +++ b/Sources/Info-tvOS.plist @@ -15,7 +15,7 @@ CFBundlePackageType FMWK CFBundleShortVersionString - 0.9.26 + 0.9.27 CFBundleVersion $(CURRENT_PROJECT_VERSION) NSPrincipalClass diff --git a/Sources/Info-watchOS.plist b/Sources/Info-watchOS.plist index 45613ad7..fb705838 100644 --- a/Sources/Info-watchOS.plist +++ b/Sources/Info-watchOS.plist @@ -15,7 +15,7 @@ CFBundlePackageType FMWK CFBundleShortVersionString - 0.9.26 + 0.9.27 CFBundleVersion $(CURRENT_PROJECT_VERSION) NSPrincipalClass diff --git a/Sources/Protocols/DateFormatingService/DateFormattingService.swift b/Sources/Protocols/DateFormatingService/DateFormattingService.swift index b74f48c7..d415f5e3 100644 --- a/Sources/Protocols/DateFormatingService/DateFormattingService.swift +++ b/Sources/Protocols/DateFormatingService/DateFormattingService.swift @@ -49,6 +49,15 @@ public protocol DateFormattingService { /// - Returns: Date parsed from given string or default date if parsing did fail. func date(from string: String, format: DateFormatType, parsedIn: Region?) -> DateInRegion? + /// Method parses date from string in one of the given formats with current region. + /// + /// - Parameters: + /// - string: String to use for date parsing. + /// - formats: Formats that should be used for date parsing. + /// - parsedIn: A region that should be used for date parsing. In case of nil defaultRegion will be used. + /// - Returns: Date parsed from given string or default date if parsing did fail. + func date(from string: String, formats: [DateFormatType], parsedIn: Region?) -> DateInRegion? + /// Method format date in given format. /// /// - Parameters: From 31ea1157ce13f18fcd99aa427243fd16644d6112 Mon Sep 17 00:00:00 2001 From: Ivan Babkin Date: Mon, 9 Sep 2019 14:56:15 +0300 Subject: [PATCH 2/2] Fix version in changelog --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 33571214..29c113e1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,6 @@ # Changelog -### 0.9.26 +### 0.9.27 - **Add**: method `date(from string:formats:parsedIn:)` method for `DateFormattingService` that parses date from string in one of the given formats with current region. ### 0.9.26