Skip to content

Commit 2cd0887

Browse files
committed
Add regime sensitive month calculation
1 parent 339921f commit 2cd0887

1 file changed

Lines changed: 34 additions & 9 deletions

File tree

Sources/Calendars/RomanCalendar.swift

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -483,16 +483,41 @@ public struct RomanCalendar : CalendarProtocol {
483483
private static let table = RomanCalendarFactory.make()
484484

485485
public func months(forYear year: Int, mode: YearMode) -> [ResolvedMonth] {
486-
let months = RomanCalendar.table.monthRows(forYear: year)
487-
488-
var result: [ResolvedMonth] = []
489-
for (i, month) in zip(1...months.count, months) {
490-
result.append(ResolvedMonth(spec: romanMonths[month.monthInfoIndex],
491-
index: i, mode: mode,
492-
firstDay: 1, length: month.length))
486+
switch regime(forYear: year) {
487+
case .extrapolated:
488+
return []
489+
case .reconstructed:
490+
let months = RomanCalendar.table.monthRows(forYear: year)
491+
492+
var result: [ResolvedMonth] = []
493+
for (i, month) in zip(1...months.count, months) {
494+
result.append(ResolvedMonth(spec: romanMonths[month.monthInfoIndex],
495+
index: i, mode: mode,
496+
firstDay: 1, length: month.length))
497+
}
498+
499+
return result
500+
case .ruleBased:
501+
502+
var result: [ResolvedMonth] = []
503+
504+
for (i, (d, m)) in zip(1...12, [(31, RomanMonth.IAN), (28, RomanMonth.FEB), (31, RomanMonth.MAR), (30, RomanMonth.APR), (31, RomanMonth.MAI), (30, RomanMonth.IUN), (31, RomanMonth.QUI), (31, RomanMonth.SEX), (30, RomanMonth.SEP), (31, RomanMonth.OCT), (30, RomanMonth.NOV), (31, RomanMonth.DEC)]) {
505+
506+
// We are one year out of phase with Julian year counting.
507+
if m == .FEB && (year + 1) % 4 == 0 {
508+
result.append(ResolvedMonth(spec: romanMonths[m.slot],
509+
index: i, mode: .civil,
510+
firstDay: 1, length: d + 1))
511+
512+
} else {
513+
result.append(ResolvedMonth(spec: romanMonths[m.slot],
514+
index: i, mode: .civil,
515+
firstDay: 1, length: d))
516+
}
517+
}
518+
519+
return result
493520
}
494-
495-
return result
496521
}
497522

498523
public func isValidDate(year: Int, month: Int, day: Int) -> Bool {

0 commit comments

Comments
 (0)