From 97c281a6cb327142e62157052a695324766e5a9f Mon Sep 17 00:00:00 2001 From: Eric Jensen Date: Sat, 26 Feb 2022 23:34:54 -0500 Subject: [PATCH 1/2] Create a local copy of the data components to avoid recalculating the components in each branch condition --- Sources/LocalizedTimeAgo/LocalizedTimeAgo.swift | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Sources/LocalizedTimeAgo/LocalizedTimeAgo.swift b/Sources/LocalizedTimeAgo/LocalizedTimeAgo.swift index 5aed971..46364a1 100644 --- a/Sources/LocalizedTimeAgo/LocalizedTimeAgo.swift +++ b/Sources/LocalizedTimeAgo/LocalizedTimeAgo.swift @@ -67,6 +67,7 @@ public extension Date { } func timeAgo(numericDates: Bool = false, numericTimes: Bool = false) -> String { + let components = self.components if let year = components.year, year > 0 { if year >= 2 { return String(format: "%d years ago".adjustedKey(forValue: year).localized(), year) } return numericDates ? "1 year ago".localized() : "Last year".localized() @@ -93,6 +94,7 @@ public extension Date { } func shortTimeAgo() -> String { + let components = self.components if let year = components.year, year > 0 { return String(format: "%dy".localized(), year) } else if let month = components.month, month > 0 { From f4e8fa8349193eea10ce728dcc5d9c2f3dd97d4d Mon Sep 17 00:00:00 2001 From: Eric Jensen Date: Sun, 27 Feb 2022 00:17:27 -0500 Subject: [PATCH 2/2] Add the option to specify the reference date --- Sources/LocalizedTimeAgo/LocalizedTimeAgo.swift | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/Sources/LocalizedTimeAgo/LocalizedTimeAgo.swift b/Sources/LocalizedTimeAgo/LocalizedTimeAgo.swift index 46364a1..36d3008 100644 --- a/Sources/LocalizedTimeAgo/LocalizedTimeAgo.swift +++ b/Sources/LocalizedTimeAgo/LocalizedTimeAgo.swift @@ -60,14 +60,13 @@ public extension Date { private var calendar: Calendar { return .current } - private var components: DateComponents { + private func components(relativeTo referenceDate: Date = Date()) -> DateComponents { let unitFlags = Set([.second,.minute,.hour,.day,.weekOfYear,.month,.year]) - let now = Date() - return calendar.dateComponents(unitFlags, from: self, to: now) + return calendar.dateComponents(unitFlags, from: self, to: referenceDate) } - func timeAgo(numericDates: Bool = false, numericTimes: Bool = false) -> String { - let components = self.components + func timeAgo(numericDates: Bool = false, numericTimes: Bool = false, relativeTo referenceDate: Date = Date()) -> String { + let components = self.components(relativeTo: referenceDate) if let year = components.year, year > 0 { if year >= 2 { return String(format: "%d years ago".adjustedKey(forValue: year).localized(), year) } return numericDates ? "1 year ago".localized() : "Last year".localized() @@ -93,8 +92,8 @@ public extension Date { return numericTimes ? "1 second ago".localized() : "Just now".localized() } - func shortTimeAgo() -> String { - let components = self.components + func shortTimeAgo(relativeTo referenceDate: Date = Date()) -> String { + let components = self.components(relativeTo: referenceDate) if let year = components.year, year > 0 { return String(format: "%dy".localized(), year) } else if let month = components.month, month > 0 {