100 lines
2.4 KiB
Swift
100 lines
2.4 KiB
Swift
import Foundation
|
|
|
|
extension Date {
|
|
|
|
var seconds: Int {
|
|
timeIntervalSince1970.roundedInt
|
|
}
|
|
|
|
var secondsToNow: Int {
|
|
-timeIntervalSinceNow.roundedInt
|
|
}
|
|
|
|
init(seconds: Int) {
|
|
self.init(timeIntervalSince1970: TimeInterval(seconds))
|
|
}
|
|
|
|
var shortTimePassedText: String {
|
|
let secs = secondsToNow
|
|
guard secs > 1 else {
|
|
return "Now"
|
|
}
|
|
guard secs >= 60 else {
|
|
return "\(secs) s ago"
|
|
}
|
|
let minutes = secs / 60
|
|
guard minutes >= 60 else {
|
|
return "\(minutes) min ago"
|
|
}
|
|
let hours = minutes / 60
|
|
guard hours > 1 else {
|
|
return "1 hour ago"
|
|
}
|
|
guard hours >= 60 else {
|
|
return "\(hours) hours ago"
|
|
}
|
|
let days = hours / 24
|
|
guard days > 1 else {
|
|
return "1 day ago"
|
|
}
|
|
return "\(days) days ago"
|
|
}
|
|
|
|
var timePassedText: String {
|
|
let secs = secondsToNow
|
|
guard secs > 1 else {
|
|
return "Now"
|
|
}
|
|
guard secs >= 60 else {
|
|
return "\(secs) seconds ago"
|
|
}
|
|
let minutes = secs / 60
|
|
guard minutes > 1 else {
|
|
return "1 minute ago"
|
|
}
|
|
guard minutes >= 60 else {
|
|
return "\(minutes) minutes ago"
|
|
}
|
|
let hours = minutes / 60
|
|
guard hours > 1 else {
|
|
return "1 hour ago"
|
|
}
|
|
guard hours >= 60 else {
|
|
return "\(hours) hours ago"
|
|
}
|
|
let days = hours / 24
|
|
guard days > 1 else {
|
|
return "1 day ago"
|
|
}
|
|
return "\(days) days ago"
|
|
}
|
|
|
|
var dateIndex: Int {
|
|
let components = Calendar.current.dateComponents([.day, .month, .year], from: self)
|
|
return components.year! * 10000 + components.month! * 100 + components.day!
|
|
}
|
|
|
|
init(dateIndex: Int) {
|
|
let year = dateIndex / 10000
|
|
let month = (dateIndex % 10000) / 100
|
|
let day = dateIndex % 100
|
|
self = Calendar.current.date(from: .init(year: year, month: month, day: day))!
|
|
}
|
|
|
|
var startOfDay: Date {
|
|
Calendar.current.startOfDay(for: self)
|
|
}
|
|
|
|
var startOfNextDay: Date {
|
|
startOfDay.addingTimeInterval(86400)
|
|
}
|
|
|
|
func adding(seconds: Int) -> Date {
|
|
addingTimeInterval(TimeInterval(seconds))
|
|
}
|
|
|
|
var nearestSecond: Date {
|
|
.init(seconds: seconds)
|
|
}
|
|
}
|