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 /ios/MullvadVPN | |
| parent | 121a8dc3a7aec6d5c017262a7f2ca237dc34059f (diff) | |
| download | mullvadvpn-640cf9af5c8ab9cbf0114341120c35eeea00ccda.tar.xz mullvadvpn-640cf9af5c8ab9cbf0114341120c35eeea00ccda.zip | |
Fix date interval formatting and update tests
Diffstat (limited to 'ios/MullvadVPN')
| -rw-r--r-- | ios/MullvadVPN/Classes/CustomDateComponentsFormatting.swift | 16 |
1 files changed, 4 insertions, 12 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) } } |
