Skip to content

Commit c067ab1

Browse files
committed
Add new date utilities
1 parent 175d742 commit c067ab1

File tree

3 files changed

+36
-22
lines changed

3 files changed

+36
-22
lines changed

RELEASE_NOTES.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,13 @@ SwiftUIKit makes its best effort to honor semver, but breaking changes can occur
88

99
This version deprecates progress types, since a native `Gauge` can be used instead.
1010

11+
### ✨ Features
12+
13+
* `Date` has a new calendar-based comparison extensions.
14+
1115
### 🗑️ Deprecations
1216

17+
* `Calendar` extensions have been deprecated.
1318
* `CircularProgressBar` has been deprecated.
1419
* `LinearProgressBar` has been deprecated.
1520
* `Pasteboard` has been deprecated.

Sources/SwiftUIKit/Date/Date+Compare.swift

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,37 @@ import Foundation
1111
/// This extension provides more readable date comparisons.
1212
public extension Date {
1313

14-
/// Whether or not the date occurs after another date.
14+
/// Whether the date occurs after another date.
1515
func isAfter(_ date: Date) -> Bool {
1616
self > date
1717
}
1818

19-
/// Whether or not the date occurs before another date.
19+
/// Whether the date occurs before another date.
2020
func isBefore(_ date: Date) -> Bool {
2121
self < date
2222
}
2323

24-
/// Whether or not the date is the same as another date.
24+
/// Whether the date is the same as another date.
2525
func isSame(as date: Date) -> Bool {
2626
self == date
2727
}
28+
29+
/// Whether the date is the same as another date, for an
30+
/// explicity granularity, like `.day`.
31+
func isSame(
32+
_ granularity: Calendar.Component,
33+
as date: Date,
34+
for calendar: Calendar = .current
35+
) -> Bool {
36+
calendar.isDate(self, equalTo: date, toGranularity: granularity)
37+
}
38+
39+
/// Whether the date is the same as another date, for an
40+
/// explicity granularity, like `.day`.
41+
func isThis(
42+
_ granularity: Calendar.Component,
43+
for calendar: Calendar = .current
44+
) -> Bool {
45+
isSame(granularity, as: Date(), for: calendar)
46+
}
2847
}

Sources/SwiftUIKit/Date/Calendar+Date.swift renamed to Sources/SwiftUIKit/_Deprecated/Calendar+Date.swift

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,45 +8,35 @@
88

99
import Foundation
1010

11+
@available(*, deprecated, message: "Use Date extensions instead.")
1112
public extension Calendar {
1213

13-
/**
14-
Whether or not this calendar thinks that a certain date
15-
is the same day as another date.
16-
*/
14+
/// Whether the date is the same day as another date.
1715
func isDate(
1816
_ date1: Date,
1917
sameDayAs date2: Date
2018
) -> Bool {
2119
isDate(date1, equalTo: date2, toGranularity: .day)
2220
}
23-
24-
/**
25-
Whether or not this calendar thinks that a certain date
26-
is the same month as another date.
27-
*/
21+
22+
/// Whether the date is the same month as another date.
2823
func isDate(
2924
_ date1: Date,
3025
sameMonthAs date2: Date
3126
) -> Bool {
3227
isDate(date1, equalTo: date2, toGranularity: .month)
3328
}
34-
35-
/**
36-
Whether or not this calendar thinks that a certain date
37-
is the same week as another date.
38-
*/
29+
30+
/// Whether the date is the same week as another date.
3931
func isDate(
4032
_ date1: Date,
4133
sameWeekAs date2: Date
4234
) -> Bool {
4335
isDate(date1, equalTo: date2, toGranularity: .weekOfYear)
4436
}
45-
46-
/**
47-
Whether or not this calendar thinks that a certain date
48-
is the same year as another date.
49-
*/
37+
38+
39+
/// Whether the date is the same year as another date.
5040
func isDate(
5141
_ date1: Date,
5242
sameYearAs date2: Date

0 commit comments

Comments
 (0)