[ABANDONED] Mastodon iOS client.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

77 lines
2.1 KiB

//
// Date+TimeAgo.swift
// elpha-ios
//
// Created by Dwayne Harris on 10/1/18.
// Copyright © 2018 Elpha. All rights reserved.
//
import Foundation
extension Date {
func timeAgo() -> String {
let calendar = Calendar.current
let now = Date()
let earliest = self < now ? self : now
let latest = self > now ? self : now
let unitFlags: Set<Calendar.Component> = [.second, .minute, .hour, .day, .weekOfMonth, .month, .year]
let components: DateComponents = calendar.dateComponents(unitFlags, from: earliest, to: latest)
if let year = components.year {
if (year >= 2) {
return "\(year) years"
} else if (year >= 1) {
return "last year"
}
}
if let month = components.month {
if (month >= 2) {
return "\(month) months"
} else if (month >= 1) {
return "last month"
}
}
if let weekOfMonth = components.weekOfMonth {
if (weekOfMonth >= 2) {
return "\(weekOfMonth) weeks"
} else if (weekOfMonth >= 1) {
return "last week"
}
}
if let day = components.day {
if (day >= 2) {
return "\(day) days"
} else if (day >= 1) {
return "yesterday"
}
}
if let hour = components.hour {
if (hour >= 2) {
return "\(hour) hours"
} else if (hour >= 1) {
return "an hour ago"
}
}
if let minute = components.minute {
if (minute >= 2) {
return "\(minute) mins"
} else if (minute >= 1) {
return "a minute ago"
}
}
if let second = components.second {
if (second >= 3) {
return "\(second) seconds"
}
}
return "just now"
}
}