diff --git a/Sources/HandySwift/Types/GregorianDay.swift b/Sources/HandySwift/Types/GregorianDay.swift index d0c4db0..c61cceb 100644 --- a/Sources/HandySwift/Types/GregorianDay.swift +++ b/Sources/HandySwift/Types/GregorianDay.swift @@ -89,7 +89,7 @@ public struct GregorianDay { /// ``` public func advanced(byMonths months: Int) -> Self { let (overflowingYears, newMonth) = (self.month + months - 1).quotientAndRemainder(dividingBy: 12) - return self.with { $0.year += overflowingYears; $0.month = newMonth } + return self.with { $0.year += overflowingYears; $0.month = newMonth + 1 } } /// Reverses the date by the specified number of months. @@ -133,7 +133,7 @@ public struct GregorianDay { /// let yesterday = GregorianDay.today.reversed(byYears: 1) /// ``` public func reversed(byYears years: Int) -> Self { - self.advanced(byMonths: -years) + self.advanced(byYears: -years) } /// Returns the start of the day represented by the date. diff --git a/Tests/HandySwiftTests/Structs/GregorianDayTests.swift b/Tests/HandySwiftTests/Structs/GregorianDayTests.swift new file mode 100644 index 0000000..15da86a --- /dev/null +++ b/Tests/HandySwiftTests/Structs/GregorianDayTests.swift @@ -0,0 +1,24 @@ +import Foundation + +@testable import HandySwift +import XCTest + +final class GregorianDayTests: XCTestCase { + func testAdvancedByMonths() { + let day = GregorianDay(year: 2024, month: 03, day: 26) + let advancedByAMonth = day.advanced(byMonths: 1) + + XCTAssertEqual(advancedByAMonth.year, 2024) + XCTAssertEqual(advancedByAMonth.month, 04) + XCTAssertEqual(advancedByAMonth.day, 26) + } + + func testReversedByYears() { + let day = GregorianDay(year: 2024, month: 03, day: 26) + let reversedByTwoYears = day.reversed(byYears: 2) + + XCTAssertEqual(reversedByTwoYears.year, 2022) + XCTAssertEqual(reversedByTwoYears.month, 03) + XCTAssertEqual(reversedByTwoYears.day, 26) + } +}