Don't allocate a new Set every time timeAgo is computed

This commit is contained in:
Shadowfacts 2020-12-29 11:54:02 -05:00
parent e67f6b2ad8
commit 41403c84f9
1 changed files with 3 additions and 3 deletions

View File

@ -10,11 +10,11 @@ import Foundation
extension Date {
private static let unitFlags = Set<Calendar.Component>([.second, .minute, .hour, .day, .weekOfYear, .month, .year])
func timeAgo() -> (Int, Calendar.Component) {
let calendar = NSCalendar.current
let unitFlags = Set<Calendar.Component>([.second, .minute, .hour, .day, .weekOfYear, .month, .year])
let components = calendar.dateComponents(unitFlags, from: self, to: Date())
let components = calendar.dateComponents(Date.unitFlags, from: self, to: Date())
if components.year! >= 1 {
return (components.year!, .year)