Don't allocate a new Set every time timeAgo is computed
This commit is contained in:
parent
e67f6b2ad8
commit
41403c84f9
|
@ -10,11 +10,11 @@ import Foundation
|
||||||
|
|
||||||
extension Date {
|
extension Date {
|
||||||
|
|
||||||
|
private static let unitFlags = Set<Calendar.Component>([.second, .minute, .hour, .day, .weekOfYear, .month, .year])
|
||||||
|
|
||||||
func timeAgo() -> (Int, Calendar.Component) {
|
func timeAgo() -> (Int, Calendar.Component) {
|
||||||
let calendar = NSCalendar.current
|
let calendar = NSCalendar.current
|
||||||
let unitFlags = Set<Calendar.Component>([.second, .minute, .hour, .day, .weekOfYear, .month, .year])
|
let components = calendar.dateComponents(Date.unitFlags, from: self, to: Date())
|
||||||
|
|
||||||
let components = calendar.dateComponents(unitFlags, from: self, to: Date())
|
|
||||||
|
|
||||||
if components.year! >= 1 {
|
if components.year! >= 1 {
|
||||||
return (components.year!, .year)
|
return (components.year!, .year)
|
||||||
|
|
Loading…
Reference in New Issue