diff options
| author | Andrej Mihajlov <and@mullvad.net> | 2023-05-04 13:04:09 +0200 |
|---|---|---|
| committer | Andrej Mihajlov <and@mullvad.net> | 2023-05-04 15:58:07 +0200 |
| commit | 640cf9af5c8ab9cbf0114341120c35eeea00ccda (patch) | |
| tree | ae672fb2f3302aded38a1d5fdd80c51c18a08831 | |
| parent | 121a8dc3a7aec6d5c017262a7f2ca237dc34059f (diff) | |
| download | mullvadvpn-640cf9af5c8ab9cbf0114341120c35eeea00ccda.tar.xz mullvadvpn-640cf9af5c8ab9cbf0114341120c35eeea00ccda.zip | |
Fix date interval formatting and update tests
| -rw-r--r-- | ios/MullvadVPN/Classes/CustomDateComponentsFormatting.swift | 16 | ||||
| -rw-r--r-- | ios/MullvadVPNTests/CustomDateComponentsFormattingTests.swift | 8 |
2 files changed, 7 insertions, 17 deletions
diff --git a/ios/MullvadVPN/Classes/CustomDateComponentsFormatting.swift b/ios/MullvadVPN/Classes/CustomDateComponentsFormatting.swift index 0034494437..ad5eed5f36 100644 --- a/ios/MullvadVPN/Classes/CustomDateComponentsFormatting.swift +++ b/ios/MullvadVPN/Classes/CustomDateComponentsFormatting.swift @@ -24,22 +24,14 @@ extension CustomDateComponentsFormatting { calendar: Calendar = Calendar.current, unitsStyle: DateComponentsFormatter.UnitsStyle ) -> String? { + let years = calendar.dateComponents([.year], from: start, to: max(start, end)).year ?? 0 + let formatter = DateComponentsFormatter() formatter.calendar = calendar formatter.unitsStyle = unitsStyle formatter.maximumUnitCount = 1 + formatter.allowedUnits = years >= 2 ? .year : .day - let dateComponents = calendar.dateComponents([.year, .day], from: start, to: end) - let years = dateComponents.year ?? 0 - let days = dateComponents.day ?? 0 - - if years >= 2 { - formatter.allowedUnits = [.year] - return formatter.string(from: dateComponents) - } else if days > 0 { - formatter.allowedUnits = [.day] - return formatter.string(from: start, to: end) - } - return formatter.string(from: DateComponents(calendar: calendar, day: 0)) + return formatter.string(from: start, to: end) } } diff --git a/ios/MullvadVPNTests/CustomDateComponentsFormattingTests.swift b/ios/MullvadVPNTests/CustomDateComponentsFormattingTests.swift index eafad06d87..3a14264b1d 100644 --- a/ios/MullvadVPNTests/CustomDateComponentsFormattingTests.swift +++ b/ios/MullvadVPNTests/CustomDateComponentsFormattingTests.swift @@ -27,10 +27,9 @@ class CustomDateComponentsFormattingTests: XCTestCase { func testLessThanTwoYearsFormatting() throws { var dateComponents = DateComponents() - dateComponents.year = 2 + dateComponents.day = 365 - var (startDate, endDate) = makeDateRange(addingComponents: dateComponents) - endDate = endDate.addingTimeInterval(-1) + let (startDate, endDate) = makeDateRange(addingComponents: dateComponents) let result = CustomDateComponentsFormatting.localizedString( from: startDate, @@ -39,8 +38,7 @@ class CustomDateComponentsFormattingTests: XCTestCase { unitsStyle: .full ) - let expectedDays = calendar.dateComponents([.day], from: startDate, to: endDate).day ?? 0 - XCTAssertEqual(result, "\(expectedDays) days") + XCTAssertEqual(result, "365 days") } func testCloseToOneDayFormatting() throws { |
