Skip to content

Commit

Permalink
Merge pull request #1 from fhasse95/Start-Of-Year-Next-Date-Bugfix
Browse files Browse the repository at this point in the history
Bugfix: Calculation of next date when date is start of year
  • Loading branch information
pixyzehn authored Dec 11, 2024
2 parents d390a61 + 41c3c29 commit 4fe0244
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Sources/CustomRepeatDate/CustomRepeatDate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ public extension Calendar {
afterDateComponents.day = day

// Set the start of the day -1s to be able to include the start of the year
let startingAfter = startOfDay(for: startOfYear(for: afterDate))
let startingAfter = startOfDay(for: startOfYear(for: afterDate)).addingTimeInterval(-1)
enumerateDates(
startingAfter: startingAfter,
matching: afterDateComponents,
Expand Down
32 changes: 32 additions & 0 deletions Tests/CustomRepeatDateTests/CustomRepeatDateTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,38 @@ final class CustomRepeatDateTests: XCTestCase {
XCTAssertEqual(repeat5, date(year: 2034, month: 6, day: 12))
}

func testYearlyCustomRepeatDateDaysOfYearIsStartOfYear() {
let option = CustomRepeatDateOption.yearly(frequency: 1, option: .daysOfYear(months: [1]))

let repeat1 = calendar.nextDate(after: date(year: 2022, month: 1, day: 1), option: option)!
let repeat2 = calendar.nextDate(after: repeat1, option: option)!
let repeat3 = calendar.nextDate(after: repeat2, option: option)!
let repeat4 = calendar.nextDate(after: repeat3, option: option)!
let repeat5 = calendar.nextDate(after: repeat4, option: option)!

XCTAssertEqual(repeat1, date(year: 2023, month: 1, day: 1))
XCTAssertEqual(repeat2, date(year: 2024, month: 1, day: 1))
XCTAssertEqual(repeat3, date(year: 2025, month: 1, day: 1))
XCTAssertEqual(repeat4, date(year: 2026, month: 1, day: 1))
XCTAssertEqual(repeat5, date(year: 2027, month: 1, day: 1))
}

func testYearlyCustomRepeatDateDaysOfYearIsEndOfYear() {
let option = CustomRepeatDateOption.yearly(frequency: 1, option: .daysOfYear(months: [12]))

let repeat1 = calendar.nextDate(after: date(year: 2022, month: 12, day: 31), option: option)!
let repeat2 = calendar.nextDate(after: repeat1, option: option)!
let repeat3 = calendar.nextDate(after: repeat2, option: option)!
let repeat4 = calendar.nextDate(after: repeat3, option: option)!
let repeat5 = calendar.nextDate(after: repeat4, option: option)!

XCTAssertEqual(repeat1, date(year: 2023, month: 12, day: 31))
XCTAssertEqual(repeat2, date(year: 2024, month: 12, day: 31))
XCTAssertEqual(repeat3, date(year: 2025, month: 12, day: 31))
XCTAssertEqual(repeat4, date(year: 2026, month: 12, day: 31))
XCTAssertEqual(repeat5, date(year: 2027, month: 12, day: 31))
}

func testYearlyCustomRepeatDateDaysOfYearWithSkippingCases() {
let option = CustomRepeatDateOption.yearly(frequency: 3, option: .daysOfYear(months: [1, 4]))

Expand Down

0 comments on commit 4fe0244

Please sign in to comment.