[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

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. //
  2. // Date+TimeAgo.swift
  3. // elpha-ios
  4. //
  5. // Created by Dwayne Harris on 10/1/18.
  6. // Copyright © 2018 Elpha. All rights reserved.
  7. //
  8. import Foundation
  9. extension Date {
  10. func timeAgo() -> String {
  11. let calendar = Calendar.current
  12. let now = Date()
  13. let earliest = self < now ? self : now
  14. let latest = self > now ? self : now
  15. let unitFlags: Set<Calendar.Component> = [.second, .minute, .hour, .day, .weekOfMonth, .month, .year]
  16. let components: DateComponents = calendar.dateComponents(unitFlags, from: earliest, to: latest)
  17. if let year = components.year {
  18. if (year >= 2) {
  19. return "\(year) years"
  20. } else if (year >= 1) {
  21. return "last year"
  22. }
  23. }
  24. if let month = components.month {
  25. if (month >= 2) {
  26. return "\(month) months"
  27. } else if (month >= 1) {
  28. return "last month"
  29. }
  30. }
  31. if let weekOfMonth = components.weekOfMonth {
  32. if (weekOfMonth >= 2) {
  33. return "\(weekOfMonth) weeks"
  34. } else if (weekOfMonth >= 1) {
  35. return "last week"
  36. }
  37. }
  38. if let day = components.day {
  39. if (day >= 2) {
  40. return "\(day) days"
  41. } else if (day >= 1) {
  42. return "yesterday"
  43. }
  44. }
  45. if let hour = components.hour {
  46. if (hour >= 2) {
  47. return "\(hour) hours"
  48. } else if (hour >= 1) {
  49. return "an hour ago"
  50. }
  51. }
  52. if let minute = components.minute {
  53. if (minute >= 2) {
  54. return "\(minute) mins"
  55. } else if (minute >= 1) {
  56. return "a minute ago"
  57. }
  58. }
  59. if let second = components.second {
  60. if (second >= 3) {
  61. return "\(second) seconds"
  62. }
  63. }
  64. return "just now"
  65. }
  66. }