|
@@ -1,7 +1,37 @@
|
|
|
import Foundation
|
|
|
|
|
|
+
|
|
|
extension Date {
|
|
|
|
|
|
+ var galleryLocalizedDescription: String {
|
|
|
+ if isInToday {
|
|
|
+ return String.localized("today")
|
|
|
+ }
|
|
|
+ if isInYesterday {
|
|
|
+ return String.localized("yesterday")
|
|
|
+ }
|
|
|
+ if isInThisWeek {
|
|
|
+ return String.localized("this_week")
|
|
|
+ }
|
|
|
+ if isInLastWeek {
|
|
|
+ return String.localized("last_week")
|
|
|
+ }
|
|
|
+ if isInThisMonth {
|
|
|
+ return String.localized("this_month")
|
|
|
+ }
|
|
|
+ if isInLastMonth {
|
|
|
+ return String.localized("last_month")
|
|
|
+ }
|
|
|
+
|
|
|
+ let monthName = DateFormatter().monthSymbols[month - 1]
|
|
|
+ let yearName: String = isInSameYear(as: Date()) ? "" : " \(year)"
|
|
|
+ return "\(monthName)\(yearName)"
|
|
|
+ }
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+private extension Date {
|
|
|
+
|
|
|
func isEqual(
|
|
|
to date: Date,
|
|
|
toGranularity component: Calendar.Component,
|
|
@@ -10,14 +40,8 @@ extension Date {
|
|
|
calendar.isDate(self, equalTo: date, toGranularity: component)
|
|
|
}
|
|
|
|
|
|
- func isInSameYear(as date: Date) -> Bool { isEqual(to: date, toGranularity: .year) }
|
|
|
- func isInSameMonth(as date: Date) -> Bool { isEqual(to: date, toGranularity: .month) }
|
|
|
- func isInSameWeek(as date: Date) -> Bool { isEqual(to: date, toGranularity: .weekOfYear) }
|
|
|
-
|
|
|
- func isInSameDay(as date: Date) -> Bool { Calendar.current.isDate(self, inSameDayAs: date) }
|
|
|
-
|
|
|
- var isInThisYear: Bool { isInSameYear(as: Date()) }
|
|
|
- var isInThisMonth: Bool { isInSameMonth(as: Date()) }
|
|
|
+ var isInToday: Bool { Calendar.current.isDateInToday(self) }
|
|
|
+ var isInYesterday: Bool { Calendar.current.isDateInYesterday(self) }
|
|
|
var isInThisWeek: Bool { isInSameWeek(as: Date()) }
|
|
|
var isInLastWeek: Bool {
|
|
|
guard let lastWeekDate = Calendar.current.date(byAdding: .weekOfYear, value: -1, to: Date()) else {
|
|
@@ -25,7 +49,7 @@ extension Date {
|
|
|
}
|
|
|
return isEqual(to: lastWeekDate, toGranularity: .weekOfYear)
|
|
|
}
|
|
|
-
|
|
|
+ var isInThisMonth: Bool { isInSameMonth(as: Date()) }
|
|
|
var isInLastMonth: Bool {
|
|
|
guard let lastMonthDate = Calendar.current.date(byAdding: .month, value: -1, to: Date()) else {
|
|
|
return false
|
|
@@ -33,13 +57,15 @@ extension Date {
|
|
|
return isEqual(to: lastMonthDate, toGranularity: .month)
|
|
|
}
|
|
|
|
|
|
+ var month: Int {
|
|
|
+ return Calendar.current.component(.month, from: self)
|
|
|
+ }
|
|
|
|
|
|
- var isInYesterday: Bool { Calendar.current.isDateInYesterday(self) }
|
|
|
- var isInToday: Bool { Calendar.current.isDateInToday(self) }
|
|
|
- var isInTomorrow: Bool { Calendar.current.isDateInTomorrow(self) }
|
|
|
-
|
|
|
- var isInTheFuture: Bool { self > Date() }
|
|
|
- var isInThePast: Bool { self < Date() }
|
|
|
-
|
|
|
+ var year: Int {
|
|
|
+ return Calendar.current.component(.year, from: self)
|
|
|
+ }
|
|
|
|
|
|
+ func isInSameMonth(as date: Date) -> Bool { isEqual(to: date, toGranularity: .month) }
|
|
|
+ func isInSameWeek(as date: Date) -> Bool { isEqual(to: date, toGranularity: .weekOfYear) }
|
|
|
+ func isInSameYear(as date: Date) -> Bool { isEqual(to: date, toGranularity: .year) }
|
|
|
}
|