diff options
| author | Mojgan <Mojgan.jelodar@codic.se> | 2023-04-20 11:43:12 +0200 |
|---|---|---|
| committer | Emīls <emils@mullvad.net> | 2023-04-20 13:43:43 +0100 |
| commit | 8f666f9309a27f012a65dcdaedb18bf2da6ae36a (patch) | |
| tree | ceef5a459b86ee47758fdfad6cbc6e87c73e45ac | |
| parent | c985d81959dc482d5277e9a76bf8a8f74663df3e (diff) | |
| download | mullvadvpn-8f666f9309a27f012a65dcdaedb18bf2da6ae36a.tar.xz mullvadvpn-8f666f9309a27f012a65dcdaedb18bf2da6ae36a.zip | |
Change time left to days and years
| -rw-r--r-- | ios/MullvadVPN/Classes/CustomDateComponentsFormatting.swift | 27 | ||||
| -rw-r--r-- | ios/MullvadVPNTests/CustomDateComponentsFormattingTests.swift | 18 |
2 files changed, 6 insertions, 39 deletions
diff --git a/ios/MullvadVPN/Classes/CustomDateComponentsFormatting.swift b/ios/MullvadVPN/Classes/CustomDateComponentsFormatting.swift index 556c445ec1..307978927b 100644 --- a/ios/MullvadVPN/Classes/CustomDateComponentsFormatting.swift +++ b/ios/MullvadVPN/Classes/CustomDateComponentsFormatting.swift @@ -16,10 +16,7 @@ extension CustomDateComponentsFormatting { /// The behaviour of that method differs from `DateComponentsFormatter`: /// /// 1. Intervals of two years or more are formatted in years quantity. - /// 2. Intervals between 23h 30m - 23h 59m are rounded to 1 day to fix the iOS SDK bug which - /// results in the wrong output ("0 months"). - /// 3. Produce "Less than a minute" message for intervals below 1 minute. - /// 4. Intervals matching none of the above are formatted in days quantity. + /// 2. Otherwise intervals matching none of the above are formatted in days quantity. /// static func localizedString( from start: Date, @@ -30,33 +27,19 @@ extension CustomDateComponentsFormatting { let formatter = DateComponentsFormatter() formatter.calendar = calendar formatter.unitsStyle = unitsStyle - formatter.allowedUnits = [.minute, .hour, .day, .month, .year] formatter.maximumUnitCount = 1 - let dateComponents = calendar - .dateComponents([.year, .day, .hour, .minute, .second], from: start, to: end) - + let dateComponents = calendar.dateComponents([.year, .day], from: start, to: end) let years = dateComponents.year ?? 0 - let days = dateComponents.day ?? 0 - let hours = dateComponents.hour ?? 0 - let minutes = dateComponents.minute ?? 0 - let seconds = dateComponents.second ?? 0 + var days = dateComponents.day ?? 0 if years >= 2 { formatter.allowedUnits = [.year] return formatter.string(from: dateComponents) - } else if days == 0, hours == 23, minutes >= 30 { - return formatter.string(from: DateComponents(calendar: calendar, day: 1)) - } else if days == 0, hours == 0, minutes == 0, seconds < 60 { - return NSLocalizedString( - "LESS_THAN_ONE_MINUTE", - tableName: "CustomDateComponentsFormatting", - value: "Less than a minute", - comment: "Phrase used for less than 1 minute duration." - ) - } else { + } else if days > 0 { formatter.allowedUnits = [.day] return formatter.string(from: start, to: end) } + return formatter.string(from: DateComponents(calendar: calendar, day: 0)) } } diff --git a/ios/MullvadVPNTests/CustomDateComponentsFormattingTests.swift b/ios/MullvadVPNTests/CustomDateComponentsFormattingTests.swift index 57d7c6f4d6..eafad06d87 100644 --- a/ios/MullvadVPNTests/CustomDateComponentsFormattingTests.swift +++ b/ios/MullvadVPNTests/CustomDateComponentsFormattingTests.swift @@ -57,23 +57,7 @@ class CustomDateComponentsFormattingTests: XCTestCase { unitsStyle: .full ) - XCTAssertEqual(result, "1 day") - } - - func testLessThanOneMinuteFormatting() throws { - var dateComponents = DateComponents() - dateComponents.second = 59 - - let (startDate, endDate) = makeDateRange(addingComponents: dateComponents) - - let result = CustomDateComponentsFormatting.localizedString( - from: startDate, - to: endDate, - calendar: calendar, - unitsStyle: .full - ) - - XCTAssertEqual(result, "Less than a minute") + XCTAssertEqual(result, "0 days") } private func makeDateRange(addingComponents dateComponents: DateComponents) -> (Date, Date) { |
