From b8bee6968480dff7885db2a79caf2172117b5f9e Mon Sep 17 00:00:00 2001 From: Martin Bueno Martin <103032231+Martin-Z1@users.noreply.github.com> Date: Wed, 22 May 2024 12:47:00 +0200 Subject: [PATCH 1/3] feat: public init from date for MonthComponent and DayComponent (#1) --- Sources/Public/Day.swift | 8 ++++++++ Sources/Public/Month.swift | 6 ++++++ 2 files changed, 14 insertions(+) diff --git a/Sources/Public/Day.swift b/Sources/Public/Day.swift index f657f43..f631bd2 100644 --- a/Sources/Public/Day.swift +++ b/Sources/Public/Day.swift @@ -44,6 +44,14 @@ public struct DayComponents: Hashable { } +extension DayComponents { + public init(from date: Date, calendar: Calendar = Calendar.current) { + let month = Month(from: date, calendar: calendar) + let day = calendar.component(.day, from: date) + self = Day(month: month, day: day) + } +} + // MARK: CustomStringConvertible extension DayComponents: CustomStringConvertible { diff --git a/Sources/Public/Month.swift b/Sources/Public/Month.swift index 6715591..f4c269c 100644 --- a/Sources/Public/Month.swift +++ b/Sources/Public/Month.swift @@ -53,6 +53,12 @@ public struct MonthComponents: Hashable { } +extension MonthComponents { + public init(from date: Date, calendar: Calendar = Calendar.current) { + self = calendar.month(containing: date) + } +} + // MARK: CustomStringConvertible extension MonthComponents: CustomStringConvertible { From bdd3a08c2e9fcdb80534b40dd456719aea15c598 Mon Sep 17 00:00:00 2001 From: Martin Date: Thu, 23 May 2024 10:18:12 +0200 Subject: [PATCH 2/3] chore: added comments and test for public month and day inits --- Sources/Public/Day.swift | 2 + Sources/Public/Month.swift | 2 + Tests/DayHelperTests.swift | 105 +++++++++++++++++++++++++++---------- Tests/MonthTests.swift | 13 +++++ 4 files changed, 93 insertions(+), 29 deletions(-) diff --git a/Sources/Public/Day.swift b/Sources/Public/Day.swift index f631bd2..115b181 100644 --- a/Sources/Public/Day.swift +++ b/Sources/Public/Day.swift @@ -44,6 +44,8 @@ public struct DayComponents: Hashable { } +// MARK: Public init from date + extension DayComponents { public init(from date: Date, calendar: Calendar = Calendar.current) { let month = Month(from: date, calendar: calendar) diff --git a/Sources/Public/Month.swift b/Sources/Public/Month.swift index f4c269c..b118e3d 100644 --- a/Sources/Public/Month.swift +++ b/Sources/Public/Month.swift @@ -53,6 +53,8 @@ public struct MonthComponents: Hashable { } +// MARK: Public init from date + extension MonthComponents { public init(from date: Date, calendar: Calendar = Calendar.current) { self = calendar.month(containing: date) diff --git a/Tests/DayHelperTests.swift b/Tests/DayHelperTests.swift index 4bb0a4a..e5acd87 100644 --- a/Tests/DayHelperTests.swift +++ b/Tests/DayHelperTests.swift @@ -14,6 +14,7 @@ // limitations under the License. import XCTest + @testable import HorizonCalendar // MARK: - DayHelperTests @@ -25,101 +26,129 @@ final class DayHelperTests: XCTestCase { func testDayComparable() { let january2020Day = Day( month: Month(era: 1, year: 2020, month: 01, isInGregorianCalendar: true), - day: 19) + day: 19 + ) let december2020Day = Day( month: Month(era: 1, year: 2020, month: 12, isInGregorianCalendar: true), - day: 05) + day: 05 + ) XCTAssert(january2020Day < december2020Day, "Expected January 19, 2020 < December 5, 2020.") let june0006Day = Day( month: Month(era: 0, year: 0006, month: 06, isInGregorianCalendar: true), - day: 10) + day: 10 + ) let january0005Day = Day( month: Month(era: 1, year: 0005, month: 01, isInGregorianCalendar: true), - day: 09) + day: 09 + ) XCTAssert(june0006Day < january0005Day, "Expected June 10, 0006 BCE < January 9, 0005 CE.") let june30Day = Day( month: Month(era: 235, year: 30, month: 06, isInGregorianCalendar: false), - day: 25) + day: 25 + ) let august01Day = Day( month: Month(era: 236, year: 01, month: 08, isInGregorianCalendar: false), - day: 30) + day: 30 + ) XCTAssert(june30Day < august01Day, "Expected June 30, 30 era 235 < August 30, 02 era 236.") } func testDayContainingDate() { let january2020Date = gregorianCalendar.date( - from: DateComponents(year: 2020, month: 01, day: 19))! + from: DateComponents(year: 2020, month: 01, day: 19) + )! let january2020Day = Day( month: Month(era: 1, year: 2020, month: 01, isInGregorianCalendar: true), - day: 19) + day: 19 + ) XCTAssert( gregorianCalendar.day(containing: january2020Date) == january2020Day, - "Expected the day to be January 19, 2020.") + "Expected the day to be January 19, 2020." + ) let december0005Date = gregorianCalendar.date( - from: DateComponents(era: 0, year: 0005, month: 12, day: 08))! + from: DateComponents(era: 0, year: 0005, month: 12, day: 08) + )! let december0005Day = Day( month: Month(era: 0, year: 0005, month: 12, isInGregorianCalendar: true), - day: 08) + day: 08 + ) XCTAssert( gregorianCalendar.day(containing: december0005Date) == december0005Day, - "Expected the day to be December 8, 0005.") + "Expected the day to be December 8, 0005." + ) let september02Date = japaneseCalendar.date( - from: DateComponents(era: 236, year: 02, month: 09, day: 21))! + from: DateComponents(era: 236, year: 02, month: 09, day: 21) + )! let september02Day = Day( month: Month(era: 236, year: 02, month: 09, isInGregorianCalendar: false), - day: 21) + day: 21 + ) XCTAssert( japaneseCalendar.day(containing: september02Date) == september02Day, - "Expected the day to be September 21, 02.") + "Expected the day to be September 21, 02." + ) } func testStartDateOfDay() { let november2020Day = Day( month: Month(era: 1, year: 2020, month: 11, isInGregorianCalendar: true), - day: 17) + day: 17 + ) let november2020Date = gregorianCalendar.date( - from: DateComponents(year: 2020, month: 11, day: 17))! + from: DateComponents(year: 2020, month: 11, day: 17) + )! XCTAssert( gregorianCalendar.startDate(of: november2020Day) == november2020Date, - "Expected the date to be the earliest possible time for November 17, 2020.") + "Expected the date to be the earliest possible time for November 17, 2020." + ) let january0100Day = Day( month: Month(era: 0, year: 0100, month: 01, isInGregorianCalendar: true), - day: 14) + day: 14 + ) let january0100Date = gregorianCalendar.date( - from: DateComponents(era: 0, year: 0100, month: 01, day: 14))! + from: DateComponents(era: 0, year: 0100, month: 01, day: 14) + )! XCTAssert( gregorianCalendar.startDate(of: january0100Day) == january0100Date, - "Expected the date to be the earliest possible time for January 14, 0100 BCE.") + "Expected the date to be the earliest possible time for January 14, 0100 BCE." + ) let june02Day = Day( month: Month(era: 236, year: 02, month: 06, isInGregorianCalendar: false), - day: 11) + day: 11 + ) let june02Date = japaneseCalendar.date( - from: DateComponents(era: 236, year: 02, month: 06, day: 11))! + from: DateComponents(era: 236, year: 02, month: 06, day: 11) + )! XCTAssert( japaneseCalendar.startDate(of: june02Day) == june02Date, - "Expected the date to be the earliest possible time for June 11, 02.") + "Expected the date to be the earliest possible time for June 11, 02." + ) } func testDayByAddingDays() { let january2021Day = Day( month: Month(era: 1, year: 2021, month: 01, isInGregorianCalendar: true), - day: 19) + day: 19 + ) let june2020Day = Day( month: Month(era: 1, year: 2020, month: 11, isInGregorianCalendar: true), - day: 16) + day: 16 + ) XCTAssert( gregorianCalendar.day(byAddingDays: -64, to: january2021Day) == june2020Day, - "Expected January 19, 2021 - 100 = June 16, 2020.") + "Expected January 19, 2021 - 100 = June 16, 2020." + ) let april0069Day = Day( month: Month(era: 0, year: 0069, month: 04, isInGregorianCalendar: true), - day: 20) + day: 20 + ) let may0069Day = Day( month: Month(era: 0, year: 0069, month: 05, isInGregorianCalendar: true), day: 01) @@ -135,7 +164,25 @@ final class DayHelperTests: XCTestCase { day: 31) XCTAssert( japaneseCalendar.day(byAddingDays: -1, to: january02Day) == december01Day, - "Expected January 1, 02 - 1 = December 31, 01.") + "Expected January 1, 02 - 1 = December 31, 01." + ) + } + + func testCreateDayFromDate() { + var components = DateComponents() + components.era = 1 + components.year = 2020 + components.month = 03 + components.day = 02 + guard let date = gregorianCalendar.date(from: components) else { + XCTFail("Expected a date created from components") + return + } + let day = Day(from: date) + XCTAssert( + day == Day(month: Month(era: 1, year: 2020, month: 03, isInGregorianCalendar: true), day: 02), + "Expected 2020-03." + ) } // MARK: Private diff --git a/Tests/MonthTests.swift b/Tests/MonthTests.swift index 9dea1ae..08191cd 100644 --- a/Tests/MonthTests.swift +++ b/Tests/MonthTests.swift @@ -54,6 +54,19 @@ final class MonthTests: XCTestCase { to: Month(era: 1, year: 2020, month: 06, isInGregorianCalendar: true)) XCTAssert(month2 == Month(era: 1, year: 2018, month: 05, isInGregorianCalendar: true), "Expected 2018-05.") } + + func testCreateMonthFromDate() { + var components = DateComponents() + components.era = 1 + components.year = 2020 + components.month = 03 + guard let date = calendar.date(from: components) else { + XCTFail("Expected a date created from components") + return + } + let month = Month(from: date) + XCTAssert(month == Month(era: 1, year: 2020, month: 03, isInGregorianCalendar: true), "Expected 2020-03.") + } // MARK: Private From 0c3d0315b6fad4f6b63ec0e4522c82b609a19241 Mon Sep 17 00:00:00 2001 From: Martin Date: Thu, 23 May 2024 11:08:06 +0200 Subject: [PATCH 3/3] refator: restore formatting --- Tests/DayHelperTests.swift | 88 +++++++++++++------------------------- 1 file changed, 29 insertions(+), 59 deletions(-) diff --git a/Tests/DayHelperTests.swift b/Tests/DayHelperTests.swift index e5acd87..dcea60b 100644 --- a/Tests/DayHelperTests.swift +++ b/Tests/DayHelperTests.swift @@ -14,7 +14,6 @@ // limitations under the License. import XCTest - @testable import HorizonCalendar // MARK: - DayHelperTests @@ -26,129 +25,101 @@ final class DayHelperTests: XCTestCase { func testDayComparable() { let january2020Day = Day( month: Month(era: 1, year: 2020, month: 01, isInGregorianCalendar: true), - day: 19 - ) + day: 19) let december2020Day = Day( month: Month(era: 1, year: 2020, month: 12, isInGregorianCalendar: true), - day: 05 - ) + day: 05) XCTAssert(january2020Day < december2020Day, "Expected January 19, 2020 < December 5, 2020.") let june0006Day = Day( month: Month(era: 0, year: 0006, month: 06, isInGregorianCalendar: true), - day: 10 - ) + day: 10) let january0005Day = Day( month: Month(era: 1, year: 0005, month: 01, isInGregorianCalendar: true), - day: 09 - ) + day: 09) XCTAssert(june0006Day < january0005Day, "Expected June 10, 0006 BCE < January 9, 0005 CE.") let june30Day = Day( month: Month(era: 235, year: 30, month: 06, isInGregorianCalendar: false), - day: 25 - ) + day: 25) let august01Day = Day( month: Month(era: 236, year: 01, month: 08, isInGregorianCalendar: false), - day: 30 - ) + day: 30) XCTAssert(june30Day < august01Day, "Expected June 30, 30 era 235 < August 30, 02 era 236.") } func testDayContainingDate() { let january2020Date = gregorianCalendar.date( - from: DateComponents(year: 2020, month: 01, day: 19) - )! + from: DateComponents(year: 2020, month: 01, day: 19))! let january2020Day = Day( month: Month(era: 1, year: 2020, month: 01, isInGregorianCalendar: true), - day: 19 - ) + day: 19) XCTAssert( gregorianCalendar.day(containing: january2020Date) == january2020Day, - "Expected the day to be January 19, 2020." - ) + "Expected the day to be January 19, 2020.") let december0005Date = gregorianCalendar.date( - from: DateComponents(era: 0, year: 0005, month: 12, day: 08) - )! + from: DateComponents(era: 0, year: 0005, month: 12, day: 08))! let december0005Day = Day( month: Month(era: 0, year: 0005, month: 12, isInGregorianCalendar: true), - day: 08 - ) + day: 08) XCTAssert( gregorianCalendar.day(containing: december0005Date) == december0005Day, - "Expected the day to be December 8, 0005." - ) + "Expected the day to be December 8, 0005.") let september02Date = japaneseCalendar.date( - from: DateComponents(era: 236, year: 02, month: 09, day: 21) - )! + from: DateComponents(era: 236, year: 02, month: 09, day: 21))! let september02Day = Day( month: Month(era: 236, year: 02, month: 09, isInGregorianCalendar: false), - day: 21 - ) + day: 21) XCTAssert( japaneseCalendar.day(containing: september02Date) == september02Day, - "Expected the day to be September 21, 02." - ) + "Expected the day to be September 21, 02.") } func testStartDateOfDay() { let november2020Day = Day( month: Month(era: 1, year: 2020, month: 11, isInGregorianCalendar: true), - day: 17 - ) + day: 17) let november2020Date = gregorianCalendar.date( - from: DateComponents(year: 2020, month: 11, day: 17) - )! + from: DateComponents(year: 2020, month: 11, day: 17))! XCTAssert( gregorianCalendar.startDate(of: november2020Day) == november2020Date, - "Expected the date to be the earliest possible time for November 17, 2020." - ) + "Expected the date to be the earliest possible time for November 17, 2020.") let january0100Day = Day( month: Month(era: 0, year: 0100, month: 01, isInGregorianCalendar: true), - day: 14 - ) + day: 14) let january0100Date = gregorianCalendar.date( - from: DateComponents(era: 0, year: 0100, month: 01, day: 14) - )! + from: DateComponents(era: 0, year: 0100, month: 01, day: 14))! XCTAssert( gregorianCalendar.startDate(of: january0100Day) == january0100Date, - "Expected the date to be the earliest possible time for January 14, 0100 BCE." - ) + "Expected the date to be the earliest possible time for January 14, 0100 BCE.") let june02Day = Day( month: Month(era: 236, year: 02, month: 06, isInGregorianCalendar: false), - day: 11 - ) + day: 11) let june02Date = japaneseCalendar.date( - from: DateComponents(era: 236, year: 02, month: 06, day: 11) - )! + from: DateComponents(era: 236, year: 02, month: 06, day: 11))! XCTAssert( japaneseCalendar.startDate(of: june02Day) == june02Date, - "Expected the date to be the earliest possible time for June 11, 02." - ) + "Expected the date to be the earliest possible time for June 11, 02.") } func testDayByAddingDays() { let january2021Day = Day( month: Month(era: 1, year: 2021, month: 01, isInGregorianCalendar: true), - day: 19 - ) + day: 19) let june2020Day = Day( month: Month(era: 1, year: 2020, month: 11, isInGregorianCalendar: true), - day: 16 - ) + day: 16) XCTAssert( gregorianCalendar.day(byAddingDays: -64, to: january2021Day) == june2020Day, - "Expected January 19, 2021 - 100 = June 16, 2020." - ) + "Expected January 19, 2021 - 100 = June 16, 2020.") let april0069Day = Day( month: Month(era: 0, year: 0069, month: 04, isInGregorianCalendar: true), - day: 20 - ) + day: 20) let may0069Day = Day( month: Month(era: 0, year: 0069, month: 05, isInGregorianCalendar: true), day: 01) @@ -164,8 +135,7 @@ final class DayHelperTests: XCTestCase { day: 31) XCTAssert( japaneseCalendar.day(byAddingDays: -1, to: january02Day) == december01Day, - "Expected January 1, 02 - 1 = December 31, 01." - ) + "Expected January 1, 02 - 1 = December 31, 01.") } func testCreateDayFromDate() {