Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: melvitax/DateHelper
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 5.0.1
Choose a base ref
...
head repository: melvitax/DateHelper
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: master
Choose a head ref
  • 4 commits
  • 2 files changed
  • 4 contributors

Commits on Mar 20, 2024

  1. return (#114)

    fixed a return bug
    pec1985 authored Mar 20, 2024
    Copy the full SHA
    4d72de2 View commit details
  2. Copy the full SHA
    f75829d View commit details
  3. Feat/nanosecond precision (#122)

    * feat: added support for adjusting time with nanosecond precision.
    
    * feat: change `endOfDay` to 23:59:59:999
    yyjim authored Mar 20, 2024
    Copy the full SHA
    27eb16d View commit details
  4. Fixed passing wrong offset value for compare (#124)

    Fixed passing `offset` value that is calculated based on the current date and given date. The offset to compare should correspond to the `DateComparisonType`.
    iranjith4 authored Mar 20, 2024
    Copy the full SHA
    573c18a View commit details
Showing with 31 additions and 17 deletions.
  1. +30 −16 Sources/DateHelper/DateHelper.swift
  2. +1 −1 Tests/DateHelperTests/DateAdjustingTests.swift
46 changes: 30 additions & 16 deletions Sources/DateHelper/DateHelper.swift
Original file line number Diff line number Diff line change
@@ -50,6 +50,7 @@ public extension Date {
let formatter = Date.cachedDateFormatters.cachedFormatter(format.stringFormat, timeZone: zone, locale: locale, isLenient: isLenient),
let date = formatter.date(from: string) {
self.init(timeInterval: 0, since: date)
return
}
return nil
}
@@ -213,23 +214,27 @@ public extension Date {
/// Compares dates to see if they are equal while ignoring time.
// swiftlint:disable:next cyclomatic_complexity function_body_length
func compare(_ comparison: DateComparisonType) -> Bool {
let time = self.timeIntervalSince1970
let now = Date().timeIntervalSince1970
let isPast = now - time > 0
let offset = isPast ? -1 : 1
switch comparison {
case .isToday:
return compare(.isSameDay(as: Date()))
case .isTomorrow, .isYesterday:
if let comparison = Date().offset(.day, value: offset) {
case .isYesterday:
if let comparison = Date().offset(.day, value: -1) {
return compare(.isSameDay(as: comparison))
}
case .isTomorrow:
if let comparison = Date().offset(.day, value: 1) {
return compare(.isSameDay(as: comparison))
}
case .isSameDay(let date):
return component(.year) == date.component(.year) && component(.month) == date.component(.month) && component(.day) == date.component(.day)
case .isThisWeek:
return self.compare(.isSameWeek(as: Date()))
case .isNextWeek, .isLastWeek:
if let comparison = Date().offset(.week, value: offset) {
case .isLastWeek:
if let comparison = Date().offset(.week, value: -1) {
return compare(.isSameWeek(as: comparison))
}
case .isNextWeek:
if let comparison = Date().offset(.week, value: 1) {
return compare(.isSameWeek(as: comparison))
}
case .isSameWeek(let date):
@@ -240,16 +245,24 @@ public extension Date {
return abs(self.timeIntervalSince(date)) < DateComponentType.week.inSeconds
case .isThisMonth:
return self.compare(.isSameMonth(as: Date()))
case .isNextMonth, .isLastMonth:
if let comparison = Date().offset(.month, value: offset) {
case .isLastMonth:
if let comparison = Date().offset(.month, value: -1) {
return compare(.isSameMonth(as: comparison))
}
case .isNextMonth:
if let comparison = Date().offset(.month, value: 1) {
return compare(.isSameMonth(as: comparison))
}
case .isSameMonth(let date):
return component(.year) == date.component(.year) && component(.month) == date.component(.month)
case .isThisYear:
return self.compare(.isSameYear(as: Date()))
case .isNextYear, .isLastYear:
if let comparison = Date().offset(.year, value: offset) {
case .isLastYear:
if let comparison = Date().offset(.year, value: -1) {
return compare(.isSameYear(as: comparison))
}
case .isNextYear:
if let comparison = Date().offset(.year, value: 1) {
return compare(.isSameYear(as: comparison))
}
case .isSameYear(let date):
@@ -259,9 +272,9 @@ public extension Date {
case .isInThePast:
return self.compare(.isEarlier(than: Date()))
case .isEarlier(let date):
return (self as NSDate).earlierDate(date) == self
return timeIntervalSince1970 < date.timeIntervalSince1970
case .isLater(let date):
return (self as NSDate).laterDate(date) == self
return timeIntervalSince1970 > date.timeIntervalSince1970
case .isWeekday:
return !compare(.isWeekend)
case .isWeekend:
@@ -301,13 +314,14 @@ public extension Date {
}

/// Sets a new value to the specified component and returns as a new date
func adjust(hour: Int? = nil, minute: Int? = nil, second: Int? = nil, day: Int? = nil, month: Int? = nil) -> Date? {
func adjust(hour: Int? = nil, minute: Int? = nil, second: Int? = nil, nanosecond: Int? = nil, day: Int? = nil, month: Int? = nil) -> Date? {
var comp = Date.components(self)
comp.month = month ?? comp.month
comp.day = day ?? comp.day
comp.hour = hour ?? comp.hour
comp.minute = minute ?? comp.minute
comp.second = second ?? comp.second
comp.nanosecond = nanosecond ?? comp.nanosecond
return Calendar.current.date(from: comp)
}

@@ -319,7 +333,7 @@ public extension Date {
case .startOfDay:
return adjust(hour: 0, minute: 0, second: 0)
case .endOfDay:
return adjust(hour: 23, minute: 59, second: 59)
return adjust(hour: 23, minute: 59, second: 59, nanosecond: 999_000_000) // 23:59:59:999
case .startOfWeek:
return calendar.date(from: calendar.dateComponents([.yearForWeekOfYear, .weekOfYear, .hour, .minute, .second, .nanosecond], from: self))
case .endOfWeek:
2 changes: 1 addition & 1 deletion Tests/DateHelperTests/DateAdjustingTests.swift
Original file line number Diff line number Diff line change
@@ -105,7 +105,7 @@ final class DateHelperAdjustingTests: XCTestCase {
func testDateAdjustEndOfDay() throws {
guard let original = Calendar.current.date(from: DateComponents(year: 2009, month: 12, day: 06, hour: 18, minute: 14, second: 41)),
let adjusted = original.adjust(for: .endOfDay),
let comparison = Calendar.current.date(from: DateComponents(year: 2009, month: 12, day: 06, hour: 23, minute: 59, second: 59)) else {
let comparison = Calendar.current.date(from: DateComponents(year: 2009, month: 12, day: 06, hour: 23, minute: 59, second: 59, nanosecond: 999_000_000)) else {
return
}
XCTAssertEqual(adjusted, comparison)