From fa0a24a95e9ab59170090c59094d360b046b2b70 Mon Sep 17 00:00:00 2001 From: Imthath M Date: Fri, 31 May 2019 12:29:27 +0530 Subject: [PATCH 1/8] find difference in date components between dates. extend date in region to create more dates. --- Sources/SwiftDate/Date/Date+Compare.swift | 75 +++++++++++++++++ .../DateInRegion/DateInRegion+Create.swift | 84 ++++++++++++++++++- SwiftDate.xcodeproj/project.pbxproj | 4 + Tests/SwiftDateTests/TestDate.swift | 35 ++++++++ 4 files changed, 194 insertions(+), 4 deletions(-) create mode 100644 Tests/SwiftDateTests/TestDate.swift diff --git a/Sources/SwiftDate/Date/Date+Compare.swift b/Sources/SwiftDate/Date/Date+Compare.swift index f02080d9..c3ee75e5 100644 --- a/Sources/SwiftDate/Date/Date+Compare.swift +++ b/Sources/SwiftDate/Date/Date+Compare.swift @@ -113,3 +113,78 @@ public extension Date { } } + +extension Date { + + /// Returns the difference in the calendar component given (like day, month or year) + /// with respect to the other date as a positive integer + public func difference(in component: Calendar.Component, from other: Date) -> Int? { + let (max, min) = orderDate(with: other) + let result = calendar.dateComponents([component], from: min, to: max) + return getValue(of: component, from: result) + } + + /// Returns the differences in the calendar components given (like day, month and year) + /// with respect to the other date as dictionary with the calendar component as the key + /// and the diffrence as a positive integer as the value + public func differences(in components: Set, from other: Date) -> [Calendar.Component: Int] { + let (max, min) = orderDate(with: other) + let differenceInDates = calendar.dateComponents(components, from: min, to: max) + var result = [Calendar.Component: Int]() + for component in components { + if let value = getValue(of: component, from: differenceInDates) { + result[component] = value + } + } + return result + } + + private func getValue(of component: Calendar.Component, from dateComponents: DateComponents) -> Int? { + switch component { + case .era: + return dateComponents.era + case .year: + return dateComponents.year + case .month: + return dateComponents.month + case .day: + return dateComponents.day + case .hour: + return dateComponents.hour + case .minute: + return dateComponents.minute + case .second: + return dateComponents.second + case .weekday: + return dateComponents.weekday + case .weekdayOrdinal: + return dateComponents.weekdayOrdinal + case .quarter: + return dateComponents.quarter + case .weekOfMonth: + return dateComponents.weekOfMonth + case .weekOfYear: + return dateComponents.weekOfYear + case .yearForWeekOfYear: + return dateComponents.yearForWeekOfYear + case .nanosecond: + return dateComponents.nanosecond + case .calendar, .timeZone: + return nil + @unknown default: + assert(false, "unknown date component") + } + + } + + private func orderDate(with other: Date) -> (Date, Date) { + let first = self.timeIntervalSince1970 + let second = other.timeIntervalSince1970 + + if first >= second { + return (self, other) + } + + return (other, self) + } +} diff --git a/Sources/SwiftDate/DateInRegion/DateInRegion+Create.swift b/Sources/SwiftDate/DateInRegion/DateInRegion+Create.swift index 15769858..008229d2 100644 --- a/Sources/SwiftDate/DateInRegion/DateInRegion+Create.swift +++ b/Sources/SwiftDate/DateInRegion/DateInRegion+Create.swift @@ -566,8 +566,32 @@ public extension DateInRegion { return result } - - /// Returns the next weekday preserving smaller components (hour, minute, seconds) + + /// Returns the date on the given day of month preserving smaller components + func dateAt(dayOfMonth: Int, monthNumber: Int? = nil, + yearNumber: Int? = nil) -> DateInRegion { + let monthNum = monthNumber ?? month + let yearNum = yearNumber ?? year + + let result = DateInRegion(year: yearNum, month: monthNum, day: dayOfMonth, + hour: hour, minute: minute, second: second, + nanosecond: nanosecond, region: region) + + return result + } + + /// Returns the date after given number of weeks on the given day of week + func dateAfter(weeks count: Int, on weekday: WeekDay) -> DateInRegion { + var result = self.dateByAdding(count, .weekOfMonth) + if result.weekday == weekday.rawValue { + return result + } else if result.weekday > weekday.rawValue { + result = result.dateByAdding(-1, .weekOfMonth) + } + return result.next(weekday) + } + + /// Returns the next weekday preserving smaller components /// /// - Parameters: /// - weekday: weekday to get. @@ -579,12 +603,64 @@ public extension DateInRegion { components.hour = hour components.second = second components.minute = minute - + guard let next = region.calendar.nextDate(after: date, matching: components, matchingPolicy: .nextTimePreservingSmallerComponents) else { return self } - + + return DateInRegion(next, region: region) + } + + /// Returns the next weekday preserving smaller components + func next(_ weekday: WeekDay) -> DateInRegion { + var components = DateComponents() + components.weekday = weekday.rawValue + components.hour = hour + components.second = second + components.minute = minute + + guard let next = region.calendar.nextDate(after: date, matching: components, + matchingPolicy: .nextTimePreservingSmallerComponents) else { + return self + } + + return DateInRegion(next, region: region) + } + + /// Returns next date with the given weekday and the given week number + func next(_ weekday: WeekDay, withWeekOfMonth weekNumber: Int, + andMonthNumber monthNumber: Int? = nil) -> DateInRegion { + var result = self.dateAt(weekdayOrdinal: weekNumber, weekday: weekday, monthNumber: monthNumber) + + if result <= self { + + if let monthNum = monthNumber { + result = self.dateAt(weekdayOrdinal: weekNumber, weekday: weekday, + monthNumber: monthNum, yearNumber: self.year + 1) + } else { + result = self.dateAt(weekdayOrdinal: weekNumber, weekday: weekday, monthNumber: self.month + 1) + } + + } + + return result + } + + /// Returns the next day of month preserving smaller components (hour, minute, seconds) + func next(dayOfMonth: Int, monthOfYear: Int? = nil) -> DateInRegion { + var components = DateComponents() + components.day = dayOfMonth + components.month = monthOfYear + components.hour = hour + components.second = second + components.minute = minute + + guard let next = region.calendar.nextDate(after: date, matching: components, + matchingPolicy: .nextTimePreservingSmallerComponents) else { + return self + } + return DateInRegion(next, region: region) } } diff --git a/SwiftDate.xcodeproj/project.pbxproj b/SwiftDate.xcodeproj/project.pbxproj index e1b67f16..98230802 100644 --- a/SwiftDate.xcodeproj/project.pbxproj +++ b/SwiftDate.xcodeproj/project.pbxproj @@ -228,6 +228,7 @@ 64EF3E0F20D65478002793C6 /* TestDateInRegion+Compare.swift in Sources */ = {isa = PBXBuildFile; fileRef = 64EF3E0E20D65478002793C6 /* TestDateInRegion+Compare.swift */; }; 64EF3E1020D65478002793C6 /* TestDateInRegion+Compare.swift in Sources */ = {isa = PBXBuildFile; fileRef = 64EF3E0E20D65478002793C6 /* TestDateInRegion+Compare.swift */; }; 64EF3E1120D65478002793C6 /* TestDateInRegion+Compare.swift in Sources */ = {isa = PBXBuildFile; fileRef = 64EF3E0E20D65478002793C6 /* TestDateInRegion+Compare.swift */; }; + A89F3FAF22A00019002D1BD0 /* TestDate.swift in Sources */ = {isa = PBXBuildFile; fileRef = A89F3FAE22A00019002D1BD0 /* TestDate.swift */; }; DD7502881C68FEDE006590AF /* SwiftDate.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 52D6DA0F1BF000BD002C0205 /* SwiftDate.framework */; }; DD7502921C690C7A006590AF /* SwiftDate.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 52D6D9F01BEFFFBE002C0205 /* SwiftDate.framework */; }; /* End PBXBuildFile section */ @@ -319,6 +320,7 @@ 64EF3E0620D56038002793C6 /* TestDateInRegion.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TestDateInRegion.swift; sourceTree = ""; }; 64EF3E0A20D65329002793C6 /* TestDateInRegion+Create.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "TestDateInRegion+Create.swift"; sourceTree = ""; }; 64EF3E0E20D65478002793C6 /* TestDateInRegion+Compare.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "TestDateInRegion+Compare.swift"; sourceTree = ""; }; + A89F3FAE22A00019002D1BD0 /* TestDate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TestDate.swift; sourceTree = ""; }; AD2FAA261CD0B6D800659CF4 /* SwiftDate.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = SwiftDate.plist; sourceTree = ""; }; AD2FAA281CD0B6E100659CF4 /* SwiftDateTests.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = SwiftDateTests.plist; sourceTree = ""; }; DD75027A1C68FCFC006590AF /* SwiftDate-macOS Tests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = "SwiftDate-macOS Tests.xctest"; sourceTree = BUILT_PRODUCTS_DIR; }; @@ -566,6 +568,7 @@ 6439232520D91D170098EC03 /* TestFormatters.swift */, 64BAB12720E6411100FEED79 /* TestSwiftDate.swift */, 647AD65B21F4851F00CF787E /* TestDataStructures.swift */, + A89F3FAE22A00019002D1BD0 /* TestDate.swift */, ); name = Tests; path = Tests/SwiftDateTests; @@ -1161,6 +1164,7 @@ files = ( 64BAB12520E63A3A00FEED79 /* TestDateInRegion+Langs.swift in Sources */, 647AD65D21F4851F00CF787E /* TestDataStructures.swift in Sources */, + A89F3FAF22A00019002D1BD0 /* TestDate.swift in Sources */, 6439232320D912670098EC03 /* TestDateInRegion+Math.swift in Sources */, 64EF3E1020D65478002793C6 /* TestDateInRegion+Compare.swift in Sources */, 6439232720D91D170098EC03 /* TestFormatters.swift in Sources */, diff --git a/Tests/SwiftDateTests/TestDate.swift b/Tests/SwiftDateTests/TestDate.swift new file mode 100644 index 00000000..eaa420e0 --- /dev/null +++ b/Tests/SwiftDateTests/TestDate.swift @@ -0,0 +1,35 @@ +// +// TestDate.swift +// SwiftDate-macOS Tests +// +// Created by Imthath M on 30/05/19. +// Copyright © 2019 SwiftDate. All rights reserved. +// + +import XCTest + +class TestDate: XCTestCase { + + override func setUp() { + // Put setup code here. This method is called before the invocation of each test method in the class. + } + + override func tearDown() { + // Put teardown code here. This method is called after the invocation of each test method in the class. + } + + func testDifferencesBetweenDates() { + let date = Date() + let date2 = "2019-01-05".toDate()!.date + let result = date.differences(in: [.hour, .day, .month], from: date2) + print(result) + } + + func testDifferenceBetweenDates() { + let date = Date() + let date2 = "2019-01-05".toDate()!.date + let result = date.difference(in: .day, from: date2) + print(result!) + } + +} From 3fadac2e9091d912e8a6d2c31cdd3696daec9078 Mon Sep 17 00:00:00 2001 From: Imthath M Date: Fri, 31 May 2019 14:34:27 +0530 Subject: [PATCH 2/8] remove duplicate method --- .../DateInRegion/DateInRegion+Create.swift | 16 ---------------- 1 file changed, 16 deletions(-) diff --git a/Sources/SwiftDate/DateInRegion/DateInRegion+Create.swift b/Sources/SwiftDate/DateInRegion/DateInRegion+Create.swift index 008229d2..a8d1ead2 100644 --- a/Sources/SwiftDate/DateInRegion/DateInRegion+Create.swift +++ b/Sources/SwiftDate/DateInRegion/DateInRegion+Create.swift @@ -612,22 +612,6 @@ public extension DateInRegion { return DateInRegion(next, region: region) } - /// Returns the next weekday preserving smaller components - func next(_ weekday: WeekDay) -> DateInRegion { - var components = DateComponents() - components.weekday = weekday.rawValue - components.hour = hour - components.second = second - components.minute = minute - - guard let next = region.calendar.nextDate(after: date, matching: components, - matchingPolicy: .nextTimePreservingSmallerComponents) else { - return self - } - - return DateInRegion(next, region: region) - } - /// Returns next date with the given weekday and the given week number func next(_ weekday: WeekDay, withWeekOfMonth weekNumber: Int, andMonthNumber monthNumber: Int? = nil) -> DateInRegion { From ae8eb54d1c610ee8b9e5be9df834762b6522fb4f Mon Sep 17 00:00:00 2001 From: Diego Revilla Date: Sat, 29 Jun 2019 13:22:00 +0200 Subject: [PATCH 3/8] Variable to constants Just to clean things up a bit --- Sources/SwiftDate/TimePeriod/Groups/TimePeriodGroup.swift | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Sources/SwiftDate/TimePeriod/Groups/TimePeriodGroup.swift b/Sources/SwiftDate/TimePeriod/Groups/TimePeriodGroup.swift index 45d1f6ac..3320325d 100644 --- a/Sources/SwiftDate/TimePeriod/Groups/TimePeriodGroup.swift +++ b/Sources/SwiftDate/TimePeriod/Groups/TimePeriodGroup.swift @@ -91,7 +91,7 @@ open class TimePeriodGroup: Sequence, Equatable { return false // No need to sorting if they already have different counts } - var compArray1: [TimePeriodProtocol] = array1.sorted { (period1: TimePeriodProtocol, period2: TimePeriodProtocol) -> Bool in + let compArray1: [TimePeriodProtocol] = array1.sorted { (period1: TimePeriodProtocol, period2: TimePeriodProtocol) -> Bool in if period1.start == nil && period2.start == nil { return false } else if period1.start == nil { @@ -102,7 +102,7 @@ open class TimePeriodGroup: Sequence, Equatable { return period2.start! < period1.start! } } - var compArray2: [TimePeriodProtocol] = array2.sorted { (period1: TimePeriodProtocol, period2: TimePeriodProtocol) -> Bool in + let compArray2: [TimePeriodProtocol] = array2.sorted { (period1: TimePeriodProtocol, period2: TimePeriodProtocol) -> Bool in if period1.start == nil && period2.start == nil { return false } else if period1.start == nil { From bb02e8f15d857669da53ba84a9f086c189ad3033 Mon Sep 17 00:00:00 2001 From: Daniele Margutti Date: Sat, 14 Sep 2019 14:03:00 +0200 Subject: [PATCH 4/8] Fixed method call --- Sources/SwiftDate/DateInRegion/DateInRegion+Create.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/SwiftDate/DateInRegion/DateInRegion+Create.swift b/Sources/SwiftDate/DateInRegion/DateInRegion+Create.swift index a8d1ead2..8d384829 100644 --- a/Sources/SwiftDate/DateInRegion/DateInRegion+Create.swift +++ b/Sources/SwiftDate/DateInRegion/DateInRegion+Create.swift @@ -588,7 +588,7 @@ public extension DateInRegion { } else if result.weekday > weekday.rawValue { result = result.dateByAdding(-1, .weekOfMonth) } - return result.next(weekday) + return result.nextWeekday(weekday) } /// Returns the next weekday preserving smaller components From 645e46ca1037100fbf0534bbf61cff9a20eb5f04 Mon Sep 17 00:00:00 2001 From: Daniele Margutti Date: Sat, 14 Sep 2019 15:35:40 +0200 Subject: [PATCH 5/8] #676 Added documentation for difference/differences functions along with DateInRegion counterpart methods --- Documentation/3.Manipulate_Date.md | 20 +++++++++++++++++++ .../DateInRegion/DateInRegion+Compare.swift | 13 ++++++++++++ 2 files changed, 33 insertions(+) diff --git a/Documentation/3.Manipulate_Date.md b/Documentation/3.Manipulate_Date.md index 09e96049..3e35d78f 100644 --- a/Documentation/3.Manipulate_Date.md +++ b/Documentation/3.Manipulate_Date.md @@ -24,6 +24,7 @@ - [3.13 - Sort Dates](3.Manipulate_Date.md#sort) - [3.14 - Get the next weekday](3.Manipulate_Date.md#nextWeekDay) - [3.15 - Get date at given week number/weekday](3.Manipulate_Date.md#dateAtWeeknumberWeekday) +- [3.16 - Difference between dates with components](3.Manipulate_Date.md#differenceBetweenDates) Dates can be manipulated as you need by using classic math operators and readable time units. @@ -505,4 +506,23 @@ let date = DateInRegion("2019-05-11 00:00:00", format: dateFormat, region: regio let _ = date1.dateAt(weekdayOrdinal: 3, weekday: .friday, monthNumber: date1.month + 1) // 2019-06-21T00:00:00+02:00 ``` + + +## 3.16 - Difference between dates with components + +To get differences between two dates using given time components you can choose between two methods: + +- `difference(in:from:)`: Returns the difference in the calendar component given (like day, month or year) with respect to the other date as a positive integer. +- `differences(in:from:)`: Returns the differences in the calendar components given (like day, month and year) with respect to the other date as dictionary with the calendar component as the key and the diffrence as a positive integer as the value. + + +Example: + +```swift +let format = "yyyy-MM-dd HH:mm" +let d1 = "2019-01-01 00:00".toDate(format, region: Region.current) +let d2 = "2019-01-10 14:20".toDate(format, region: Region.current) +let diff = d1!.differences(in: [.day, .hour], from: d2!) // 9 days, 14 hours +``` + [**Next Chapter**: Compare Dates](#4.CompareDates.md) diff --git a/Sources/SwiftDate/DateInRegion/DateInRegion+Compare.swift b/Sources/SwiftDate/DateInRegion/DateInRegion+Compare.swift index 99a3e164..5f7a3ea5 100644 --- a/Sources/SwiftDate/DateInRegion/DateInRegion+Compare.swift +++ b/Sources/SwiftDate/DateInRegion/DateInRegion+Compare.swift @@ -310,4 +310,17 @@ public extension DateInRegion { return self.date.timeIntervalSince(date.date) >= 0 ? self : date } + /// Returns the difference in the calendar component given (like day, month or year) + /// with respect to the other date as a positive integer + func difference(in component: Calendar.Component, from other: DateInRegion) -> Int? { + return self.date.difference(in: component, from: other.date) + } + + /// Returns the differences in the calendar components given (like day, month and year) + /// with respect to the other date as dictionary with the calendar component as the key + /// and the diffrence as a positive integer as the value + func differences(in components: Set, from other: DateInRegion) -> [Calendar.Component: Int] { + return self.date.differences(in: components, from: other.date) + } + } From 14f01786ef9d833bf200cfc187c0cd85bb42424c Mon Sep 17 00:00:00 2001 From: Daniele Margutti Date: Sat, 14 Sep 2019 15:55:26 +0200 Subject: [PATCH 6/8] #676 Completed documentation --- Documentation/3.Manipulate_Date.md | 40 ++++++++++++++++++++++++++++++ Documentation/4.Compare_Dates.md | 1 + Documentation/Index.md | 4 +++ 3 files changed, 45 insertions(+) diff --git a/Documentation/3.Manipulate_Date.md b/Documentation/3.Manipulate_Date.md index 3e35d78f..8271b4fc 100644 --- a/Documentation/3.Manipulate_Date.md +++ b/Documentation/3.Manipulate_Date.md @@ -25,6 +25,9 @@ - [3.14 - Get the next weekday](3.Manipulate_Date.md#nextWeekDay) - [3.15 - Get date at given week number/weekday](3.Manipulate_Date.md#dateAtWeeknumberWeekday) - [3.16 - Difference between dates with components](3.Manipulate_Date.md#differenceBetweenDates) +- [3.17 - Create date at components preserving small components](3.Manipulate_Date.md#dateAtComponents) +- [3.18 - Date at given weekday after # weeks](3.Manipulate_Date.md#dateAfterWeeks) +- [3.19 - Next Date](3.Manipulate_Date.md#nextDate) Dates can be manipulated as you need by using classic math operators and readable time units. @@ -524,5 +527,42 @@ let d1 = "2019-01-01 00:00".toDate(format, region: Region.current) let d2 = "2019-01-10 14:20".toDate(format, region: Region.current) let diff = d1!.differences(in: [.day, .hour], from: d2!) // 9 days, 14 hours ``` +[^ Top](#index) + + + +## 3.17 - Create date at components preserving small components + +You can create a derivated date from a receiver by preserving small components (hour, minute and second) by using the: `dateAt(dayOfMonth:monthNumber:yearNumber:)` method: + +```swift +let date = "2019-01-01 00:00".toDate("yyyy-MM-dd HH:mm", region: Region.current)! // Rome zone +let onMarch = date.dateAt(dayOfMonth: 31, monthNumber: 3) // 2019-03-31T00:00:00+01:00 +``` + +[^ Top](#index) + + + +## 3.18 - Date at given weekday after # weeks + +Returns the date after given number of weeks on the given day of week using `dateAfter(weeks:on:)` function. + +```swift +let d = "2019-09-14 00:00".toDate("yyyy-MM-dd HH:mm", region: Region.current)! // Rome Region +let nextTwoMondays = d.dateAfter(weeks: 2, on: .monday) // 2019-09-23T00:00:00+02:00 +``` + +[^ Top](#index) + + + +## 3.19 - Next Date + +There are 3 functions to get the next date starting from a receiver date: + +- `nextWeekday()`: Returns the next weekday preserving smaller components +- `nextWeekday(:withWeekOfMonth:andMonthNumber)`: Returns next date with the given weekday and the given week number +- `next(dayOfMonth:monthOfYear:)`: Returns the next day of month preserving smaller components (hour, minute, seconds) [**Next Chapter**: Compare Dates](#4.CompareDates.md) diff --git a/Documentation/4.Compare_Dates.md b/Documentation/4.Compare_Dates.md index 5827ff8c..e6571190 100644 --- a/Documentation/4.Compare_Dates.md +++ b/Documentation/4.Compare_Dates.md @@ -178,4 +178,5 @@ let _ = testDate.isInRange(date: lowerBound, and: upperBound, orEqual: true, gra [^ Top](#index) + [**Next Chapter**: Date Formatting](#5.Date_Formatting.md) diff --git a/Documentation/Index.md b/Documentation/Index.md index c76a2610..3418ecb6 100644 --- a/Documentation/Index.md +++ b/Documentation/Index.md @@ -54,6 +54,10 @@ The following documentation explore all the major features of the library. If yo - [3.13 - Sort Dates](3.Manipulate_Date.md#sort) - [3.14 - Get the next weekday](3.Manipulate_Date.md#nextWeekDay) - [3.15 - Get date at given week number/weekday](3.Manipulate_Date.md#dateAtWeeknumberWeekday) +- [3.16 - Difference between dates with components](3.Manipulate_Date.md#differenceBetweenDates) +- [3.17 - Create date at components preserving small components](3.Manipulate_Date.md#dateAtComponents) +- [3.18 - Date at given weekday after # weeks](3.Manipulate_Date.md#dateAfterWeeks) +- [3.19 - Next Date](3.Manipulate_Date.md#nextDate) ### [4 - Compare Dates](4.Compare_Dates.md) From eb49f62a6490f106ff74f9f75a0f93250335d98d Mon Sep 17 00:00:00 2001 From: Daniele Margutti Date: Sat, 14 Sep 2019 15:57:08 +0200 Subject: [PATCH 7/8] Version bump to 6.1.0 --- Documentation/Index.md | 5 ++--- SwiftDate.podspec | 2 +- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/Documentation/Index.md b/Documentation/Index.md index 3418ecb6..9639bec8 100644 --- a/Documentation/Index.md +++ b/Documentation/Index.md @@ -2,9 +2,8 @@ ## Documentation -- Reference Version: **5.0.0** -- Last Update: **July 1, 2018** -- Code Coverage: **90.68%** +- Reference Version: **6.1.0** +- Last Update: **Sept 2019** The following documentation explore all the major features of the library. If you are interested in a detailed, method by method documentation you can refeer to the Jazzy documentation generated by CocoaPods (you can also install in Dash). diff --git a/SwiftDate.podspec b/SwiftDate.podspec index b4a2f2e9..c0673d94 100644 --- a/SwiftDate.podspec +++ b/SwiftDate.podspec @@ -1,6 +1,6 @@ Pod::Spec.new do |s| s.name = "SwiftDate" - s.version = "6.0.3" + s.version = "6.1.0" s.summary = "The best way to deal with Dates & Time Zones in Swift" s.homepage = "https://github.com/malcommac/SwiftDate.git" s.license = { :type => "MIT", :file => "LICENSE" } From 8b1a7b3fe17f153f6d4249d0a0865fb67d329cf5 Mon Sep 17 00:00:00 2001 From: Daniele Margutti Date: Sat, 14 Sep 2019 16:09:08 +0200 Subject: [PATCH 8/8] Fixed unknown result in method --- Sources/SwiftDate/Date/Date+Compare.swift | 8 ++--- .../DateInRegion/DateInRegion+Compare.swift | 4 +-- .../DateInRegion/DateInRegion+Create.swift | 30 +++++++++---------- Tests/SwiftDateTests/TestDate.swift | 2 +- .../TestDateInRegion+Compare.swift | 2 +- 5 files changed, 23 insertions(+), 23 deletions(-) diff --git a/Sources/SwiftDate/Date/Date+Compare.swift b/Sources/SwiftDate/Date/Date+Compare.swift index cb744c1a..9faa59d4 100644 --- a/Sources/SwiftDate/Date/Date+Compare.swift +++ b/Sources/SwiftDate/Date/Date+Compare.swift @@ -115,7 +115,7 @@ public extension Date { } extension Date { - + /// Returns the difference in the calendar component given (like day, month or year) /// with respect to the other date as a positive integer public func difference(in component: Calendar.Component, from other: Date) -> Int? { @@ -123,7 +123,7 @@ extension Date { let result = calendar.dateComponents([component], from: min, to: max) return getValue(of: component, from: result) } - + /// Returns the differences in the calendar components given (like day, month and year) /// with respect to the other date as dictionary with the calendar component as the key /// and the diffrence as a positive integer as the value @@ -138,7 +138,7 @@ extension Date { } return result } - + private func getValue(of component: Calendar.Component, from dateComponents: DateComponents) -> Int? { switch component { case .era: @@ -174,7 +174,7 @@ extension Date { @unknown default: assert(false, "unknown date component") } - + return nil } private func orderDate(with other: Date) -> (Date, Date) { diff --git a/Sources/SwiftDate/DateInRegion/DateInRegion+Compare.swift b/Sources/SwiftDate/DateInRegion/DateInRegion+Compare.swift index 5f7a3ea5..775d86f6 100644 --- a/Sources/SwiftDate/DateInRegion/DateInRegion+Compare.swift +++ b/Sources/SwiftDate/DateInRegion/DateInRegion+Compare.swift @@ -315,12 +315,12 @@ public extension DateInRegion { func difference(in component: Calendar.Component, from other: DateInRegion) -> Int? { return self.date.difference(in: component, from: other.date) } - + /// Returns the differences in the calendar components given (like day, month and year) /// with respect to the other date as dictionary with the calendar component as the key /// and the diffrence as a positive integer as the value func differences(in components: Set, from other: DateInRegion) -> [Calendar.Component: Int] { return self.date.differences(in: components, from: other.date) } - + } diff --git a/Sources/SwiftDate/DateInRegion/DateInRegion+Create.swift b/Sources/SwiftDate/DateInRegion/DateInRegion+Create.swift index 8d384829..6951f5e6 100644 --- a/Sources/SwiftDate/DateInRegion/DateInRegion+Create.swift +++ b/Sources/SwiftDate/DateInRegion/DateInRegion+Create.swift @@ -566,20 +566,20 @@ public extension DateInRegion { return result } - + /// Returns the date on the given day of month preserving smaller components func dateAt(dayOfMonth: Int, monthNumber: Int? = nil, yearNumber: Int? = nil) -> DateInRegion { let monthNum = monthNumber ?? month let yearNum = yearNumber ?? year - + let result = DateInRegion(year: yearNum, month: monthNum, day: dayOfMonth, hour: hour, minute: minute, second: second, nanosecond: nanosecond, region: region) - + return result } - + /// Returns the date after given number of weeks on the given day of week func dateAfter(weeks count: Int, on weekday: WeekDay) -> DateInRegion { var result = self.dateByAdding(count, .weekOfMonth) @@ -590,7 +590,7 @@ public extension DateInRegion { } return result.nextWeekday(weekday) } - + /// Returns the next weekday preserving smaller components /// /// - Parameters: @@ -603,34 +603,34 @@ public extension DateInRegion { components.hour = hour components.second = second components.minute = minute - + guard let next = region.calendar.nextDate(after: date, matching: components, matchingPolicy: .nextTimePreservingSmallerComponents) else { return self } - + return DateInRegion(next, region: region) } - + /// Returns next date with the given weekday and the given week number func next(_ weekday: WeekDay, withWeekOfMonth weekNumber: Int, andMonthNumber monthNumber: Int? = nil) -> DateInRegion { var result = self.dateAt(weekdayOrdinal: weekNumber, weekday: weekday, monthNumber: monthNumber) - + if result <= self { - + if let monthNum = monthNumber { result = self.dateAt(weekdayOrdinal: weekNumber, weekday: weekday, monthNumber: monthNum, yearNumber: self.year + 1) } else { result = self.dateAt(weekdayOrdinal: weekNumber, weekday: weekday, monthNumber: self.month + 1) } - + } - + return result } - + /// Returns the next day of month preserving smaller components (hour, minute, seconds) func next(dayOfMonth: Int, monthOfYear: Int? = nil) -> DateInRegion { var components = DateComponents() @@ -639,12 +639,12 @@ public extension DateInRegion { components.hour = hour components.second = second components.minute = minute - + guard let next = region.calendar.nextDate(after: date, matching: components, matchingPolicy: .nextTimePreservingSmallerComponents) else { return self } - + return DateInRegion(next, region: region) } } diff --git a/Tests/SwiftDateTests/TestDate.swift b/Tests/SwiftDateTests/TestDate.swift index eaa420e0..50f2f07f 100644 --- a/Tests/SwiftDateTests/TestDate.swift +++ b/Tests/SwiftDateTests/TestDate.swift @@ -24,7 +24,7 @@ class TestDate: XCTestCase { let result = date.differences(in: [.hour, .day, .month], from: date2) print(result) } - + func testDifferenceBetweenDates() { let date = Date() let date2 = "2019-01-05".toDate()!.date diff --git a/Tests/SwiftDateTests/TestDateInRegion+Compare.swift b/Tests/SwiftDateTests/TestDateInRegion+Compare.swift index f535bb56..589e11b5 100644 --- a/Tests/SwiftDateTests/TestDateInRegion+Compare.swift +++ b/Tests/SwiftDateTests/TestDateInRegion+Compare.swift @@ -262,7 +262,7 @@ class TestDateInRegion_Compare: XCTestCase { XCTAssert( (date1.earlierDate(date3) == date1), "Failed to get .earlierDate()") XCTAssert( (date1.date.earlierDate(date2.date) == date1.date), "Failed to get .earlierDate()") XCTAssert( (date1.date.earlierDate(date3.date) == date1.date), "Failed to get .earlierDate()") - + // laterDate() XCTAssert( (date1.laterDate(date2) == date2), "Failed to get .laterDate()") XCTAssert( (date1.laterDate(date3) == date3), "Failed to get .laterDate()")