2022-11-23 22:34:02 +01:00
|
|
|
//
|
|
|
|
|
// DateFormatterHelper.swift
|
|
|
|
|
// SideStore
|
|
|
|
|
//
|
|
|
|
|
// Created by Fabian Thies on 20.11.22.
|
|
|
|
|
// Copyright © 2022 Fabian Thies. All rights reserved.
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
|
|
|
|
|
|
struct DateFormatterHelper {
|
|
|
|
|
|
|
|
|
|
private static let appExpirationDateFormatter: DateComponentsFormatter = {
|
|
|
|
|
let dateComponentsFormatter = DateComponentsFormatter()
|
|
|
|
|
dateComponentsFormatter.zeroFormattingBehavior = [.pad]
|
|
|
|
|
dateComponentsFormatter.collapsesLargestUnit = false
|
|
|
|
|
return dateComponentsFormatter
|
|
|
|
|
}()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static func string(forExpirationDate date: Date) -> String {
|
|
|
|
|
let startDate = Date()
|
|
|
|
|
let interval = date.timeIntervalSince(startDate)
|
|
|
|
|
guard interval > 0 else {
|
2023-01-31 22:37:37 +01:00
|
|
|
return "EXPIRED"
|
2022-11-23 22:34:02 +01:00
|
|
|
}
|
|
|
|
|
|
2023-01-31 22:37:37 +01:00
|
|
|
if interval < (24 * 60 * 60) {
|
2022-11-23 22:34:02 +01:00
|
|
|
self.appExpirationDateFormatter.unitsStyle = .positional
|
|
|
|
|
self.appExpirationDateFormatter.allowedUnits = [.minute, .second]
|
2023-01-31 22:37:37 +01:00
|
|
|
} else {
|
2022-11-23 22:34:02 +01:00
|
|
|
self.appExpirationDateFormatter.unitsStyle = .full
|
|
|
|
|
self.appExpirationDateFormatter.allowedUnits = [.day]
|
2023-01-31 22:37:37 +01:00
|
|
|
}
|
2022-11-23 22:34:02 +01:00
|
|
|
|
2023-01-31 22:37:37 +01:00
|
|
|
return self.appExpirationDateFormatter.string(from: startDate, to: date) ?? ""
|
2022-11-23 22:34:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return self.appExpirationDateFormatter.string(from: startDate, to: date) ?? ""
|
|
|
|
|
}
|
|
|
|
|
}
|