2021-06-01 08:31:31 +02:00
|
|
|
//
|
|
|
|
// Date.swift
|
|
|
|
// Mastodon
|
|
|
|
//
|
|
|
|
// Created by MainasuK Cirno on 2021-6-1.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
import DateToolsSwift
|
|
|
|
|
|
|
|
extension Date {
|
|
|
|
|
2021-08-09 09:14:08 +02:00
|
|
|
static let relativeTimestampFormatter: RelativeDateTimeFormatter = {
|
|
|
|
let formatter = RelativeDateTimeFormatter()
|
|
|
|
formatter.dateTimeStyle = .numeric
|
2021-08-09 13:44:04 +02:00
|
|
|
formatter.unitsStyle = .full
|
2021-08-09 09:14:08 +02:00
|
|
|
return formatter
|
|
|
|
}()
|
|
|
|
|
|
|
|
var localizedSlowedTimeAgoSinceNow: String {
|
2021-08-09 13:51:11 +02:00
|
|
|
return self.localizedTimeAgo(since: Date(), isSlowed: true, isAbbreviated: true)
|
2021-06-01 08:31:31 +02:00
|
|
|
}
|
|
|
|
|
2021-08-09 13:44:04 +02:00
|
|
|
var localizedTimeAgoSinceNow: String {
|
2021-08-09 13:51:11 +02:00
|
|
|
return self.localizedTimeAgo(since: Date(), isSlowed: false, isAbbreviated: false)
|
2021-08-09 13:44:04 +02:00
|
|
|
}
|
|
|
|
|
2021-08-09 13:51:11 +02:00
|
|
|
func localizedTimeAgo(since date: Date, isSlowed: Bool, isAbbreviated: Bool) -> String {
|
2021-06-01 08:31:31 +02:00
|
|
|
let earlierDate = date < self ? date : self
|
2021-08-09 09:14:08 +02:00
|
|
|
let latestDate = earlierDate == date ? self : date
|
2021-06-01 08:31:31 +02:00
|
|
|
|
2021-08-09 13:44:04 +02:00
|
|
|
if isSlowed, earlierDate.timeIntervalSince(latestDate) >= -60 {
|
2021-06-01 08:31:31 +02:00
|
|
|
return L10n.Common.Controls.Timeline.Timestamp.now
|
|
|
|
} else {
|
2021-08-09 13:51:11 +02:00
|
|
|
if isAbbreviated {
|
2021-08-16 08:48:24 +02:00
|
|
|
return latestDate.localizedShortTimeAgo(since: earlierDate)
|
2021-08-09 13:51:11 +02:00
|
|
|
} else {
|
|
|
|
return Date.relativeTimestampFormatter.localizedString(for: earlierDate, relativeTo: latestDate)
|
|
|
|
}
|
2021-06-01 08:31:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-09 13:44:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
extension Date {
|
|
|
|
|
2021-08-16 08:48:24 +02:00
|
|
|
func localizedShortTimeAgo(since date: Date) -> String {
|
|
|
|
let earlierDate = date < self ? date : self
|
|
|
|
let latestDate = earlierDate == date ? self : date
|
|
|
|
|
|
|
|
let components = Calendar.current.dateComponents([.year, .month, .day, .hour, .minute, .second], from: earlierDate, to: latestDate)
|
|
|
|
|
|
|
|
if components.year! > 0 {
|
|
|
|
return L10n.Date.Year.Ago.abbr(components.year!)
|
|
|
|
} else if components.month! > 0 {
|
|
|
|
return L10n.Date.Month.Ago.abbr(components.month!)
|
|
|
|
} else if components.day! > 0 {
|
|
|
|
return L10n.Date.Day.Ago.abbr(components.day!)
|
|
|
|
} else if components.hour! > 0 {
|
|
|
|
return L10n.Date.Hour.Ago.abbr(components.hour!)
|
|
|
|
} else if components.minute! > 0 {
|
|
|
|
return L10n.Date.Minute.Ago.abbr(components.minute!)
|
|
|
|
} else if components.second! > 0 {
|
|
|
|
return L10n.Date.Year.Ago.abbr(components.second!)
|
|
|
|
} else {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-09 13:44:04 +02:00
|
|
|
func localizedTimeLeft() -> String {
|
|
|
|
let date = Date()
|
|
|
|
let earlierDate = date < self ? date : self
|
|
|
|
let latestDate = earlierDate == date ? self : date
|
|
|
|
|
2021-08-11 05:32:35 +02:00
|
|
|
let components = Calendar.current.dateComponents([.year, .month, .day, .hour, .minute, .second], from: earlierDate, to: latestDate)
|
2021-08-09 13:44:04 +02:00
|
|
|
|
|
|
|
if components.year! > 0 {
|
2021-08-16 08:48:24 +02:00
|
|
|
return L10n.Date.Year.left(components.year!)
|
2021-08-09 13:44:04 +02:00
|
|
|
} else if components.month! > 0 {
|
|
|
|
return L10n.Date.Month.left(components.month!)
|
|
|
|
} else if components.day! > 0 {
|
|
|
|
return L10n.Date.Day.left(components.day!)
|
|
|
|
} else if components.hour! > 0 {
|
|
|
|
return L10n.Date.Hour.left(components.hour!)
|
|
|
|
} else if components.minute! > 0 {
|
|
|
|
return L10n.Date.Minute.left(components.minute!)
|
|
|
|
} else if components.second! > 0 {
|
|
|
|
return L10n.Date.Year.left(components.second!)
|
|
|
|
} else {
|
|
|
|
return ""
|
|
|
|
}
|
2021-08-09 09:14:08 +02:00
|
|
|
}
|
|
|
|
|
2021-06-01 08:31:31 +02:00
|
|
|
}
|