Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Public initialisers for DayComponent and MonthComponent to support pre-selected date range in the calendar. #306

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Sources/Public/Day.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,16 @@ 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)
let day = calendar.component(.day, from: date)
self = Day(month: month, day: day)
}
}

// MARK: CustomStringConvertible

extension DayComponents: CustomStringConvertible {
Expand Down
8 changes: 8 additions & 0 deletions Sources/Public/Month.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,14 @@ 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)
}
}

// MARK: CustomStringConvertible

extension MonthComponents: CustomStringConvertible {
Expand Down
17 changes: 17 additions & 0 deletions Tests/DayHelperTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,23 @@ final class DayHelperTests: XCTestCase {
"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

private lazy var gregorianCalendar = Calendar(identifier: .gregorian)
Expand Down
13 changes: 13 additions & 0 deletions Tests/MonthTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down