summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorAndrej Mihajlov <and@mullvad.net>2023-05-04 16:18:18 +0200
committerAndrej Mihajlov <and@mullvad.net>2023-05-04 16:18:18 +0200
commitcdc97fdc2e6447db9503467f547a97e26a878787 (patch)
treeae672fb2f3302aded38a1d5fdd80c51c18a08831
parent121a8dc3a7aec6d5c017262a7f2ca237dc34059f (diff)
parent640cf9af5c8ab9cbf0114341120c35eeea00ccda (diff)
downloadmullvadvpn-cdc97fdc2e6447db9503467f547a97e26a878787.tar.xz
mullvadvpn-cdc97fdc2e6447db9503467f547a97e26a878787.zip
Merge branch 'fix-time-left-formatting-ios-152'
-rw-r--r--ios/MullvadVPN/Classes/CustomDateComponentsFormatting.swift16
-rw-r--r--ios/MullvadVPNTests/CustomDateComponentsFormattingTests.swift8
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 {