From 9a4b11f06a24b3361134994d02b36087782ae434 Mon Sep 17 00:00:00 2001 From: Leo Tumwattana Date: Tue, 19 Sep 2023 17:45:23 +0800 Subject: [PATCH 1/3] Make Month CaseIterable --- Sources/SwiftDate/Supports/TimeStructures.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/SwiftDate/Supports/TimeStructures.swift b/Sources/SwiftDate/Supports/TimeStructures.swift index 448b611f..833ecbe7 100644 --- a/Sources/SwiftDate/Supports/TimeStructures.swift +++ b/Sources/SwiftDate/Supports/TimeStructures.swift @@ -92,7 +92,7 @@ public struct Year: CustomStringConvertible, Equatable { // MARK: - Month /// Defines months in a year -public enum Month: Int, CustomStringConvertible, Equatable { +public enum Month: Int, CaseIterable, CustomStringConvertible, Equatable { case january = 0, february, march, april, may, june, july, august, september, october, november, december public var description: String { From 2d2b0f882b3bbe15fcabfdae4ec0433dadb5b6af Mon Sep 17 00:00:00 2001 From: Leo Tumwattana Date: Tue, 19 Sep 2023 17:59:22 +0800 Subject: [PATCH 2/3] Add overload of Month.numberOfDays:isLeapYear --- Sources/SwiftDate/Supports/TimeStructures.swift | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/Sources/SwiftDate/Supports/TimeStructures.swift b/Sources/SwiftDate/Supports/TimeStructures.swift index 833ecbe7..1dcac0a2 100644 --- a/Sources/SwiftDate/Supports/TimeStructures.swift +++ b/Sources/SwiftDate/Supports/TimeStructures.swift @@ -148,5 +148,20 @@ public enum Month: Int, CaseIterable, CustomStringConvertible, Equatable { return 31 } } + + /// Returns the number of days in this month given if it is a leap year + /// + /// - Parameter isLeapYear: whether the year is a leapYear. + /// - Returns: The number of days in this month. + public func numberOfDays(isLeapYear: Bool) -> Int { + switch self { + case .february: + return isLeapYear ? 29 : 28 + case .april, .june, .september, .november: + return 30 + default: + return 31 + } + } } From 85cfa43d01c62310251df1cfc243f6174c4f7296 Mon Sep 17 00:00:00 2001 From: Leo Tumwattana Date: Tue, 19 Sep 2023 17:59:38 +0800 Subject: [PATCH 3/3] Fix comment grammar --- Sources/SwiftDate/Supports/TimeStructures.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/SwiftDate/Supports/TimeStructures.swift b/Sources/SwiftDate/Supports/TimeStructures.swift index 1dcac0a2..a2ced9d1 100644 --- a/Sources/SwiftDate/Supports/TimeStructures.swift +++ b/Sources/SwiftDate/Supports/TimeStructures.swift @@ -134,7 +134,7 @@ public enum Month: Int, CaseIterable, CustomStringConvertible, Equatable { return add(months: -(months % 12)) } - /// Returns the number of days in a this month for a given year + /// Returns the number of days in this month for a given year /// /// - Parameter year: reference year. /// - Returns: The number of days in this month.