blob: d9aa29d59d53661f762207cff1076d1d276ebad1 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
//
// CustomDateComponentsFormatting.swift
// MullvadVPN
//
// Created by pronebird on 14/05/2020.
// Copyright © 2025 Mullvad VPN AB. All rights reserved.
//
import Foundation
enum CustomDateComponentsFormatting {}
extension CustomDateComponentsFormatting {
/// Format a duration between the given dates returning a string that only contains one unit.
///
/// The behaviour of that method differs from `DateComponentsFormatter`:
///
/// 1. Intervals of less than a day return a custom string.
/// 2. Intervals of two years or more are formatted in years quantity.
/// 3. Otherwise intervals matching none of the above are formatted in days quantity.
///
static func localizedString(
from start: Date,
to end: Date,
calendar: Calendar = Calendar.current,
unitsStyle: DateComponentsFormatter.UnitsStyle
) -> String? {
let dateComponents = calendar.dateComponents([.year, .day], from: start, to: max(start, end))
guard !isLessThanOneDay(dateComponents: dateComponents) else {
return NSLocalizedString("Less than a day", comment: "")
}
let formatter = DateComponentsFormatter()
formatter.calendar = calendar
formatter.unitsStyle = unitsStyle
formatter.maximumUnitCount = 1
formatter.allowedUnits = (dateComponents.year ?? 0) >= 2 ? .year : .day
return formatter.string(from: start, to: max(start, end))
}
private static func isLessThanOneDay(dateComponents: DateComponents) -> Bool {
let year = dateComponents.year ?? 0
let day = dateComponents.day ?? 0
return (year == 0) && (day == 0)
}
}
|