Skip to content

Commit

Permalink
Fix incorrect month lengths in algorihmic converter
Browse files Browse the repository at this point in the history
And test sanity of day of month and months length for all
the calendars we have.
  • Loading branch information
ebraminio committed Oct 20, 2021
1 parent 7ec02bb commit 89db005
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,13 @@ public static long toJdn(int year, int month, int day) {
}

public static int[] fromJdn(long jdn) {
jdn++; // TODO: Investigate why this is needed
long yearStart = persianNewYearOnOrBefore(jdn - projectJdnOffset);
int y = (int) (Math.floor(((yearStart - persianEpoch) / meanTropicalYearInDays) + 0.5)) + 1;

int ordinalDay = (int) (jdn - toJdn(y, 1, 1));
int m = monthFromOrdinalDay(ordinalDay);
int d = ordinalDay - daysInPreviousMonths(m) + 1; // TODO: The original one didn't need +1, see why is needed now
int d = ordinalDay - daysInPreviousMonths(m);
return new int[]{y, m, d};
}

Expand Down
48 changes: 36 additions & 12 deletions src/test/kotlin/io/github/persiancalendar/calendar/MainTests.kt
Original file line number Diff line number Diff line change
Expand Up @@ -125,34 +125,58 @@ class MainTests {
)
}

private val startJdn = CivilDate(1250, 1, 1).toJdn()
private val endJdn = CivilDate(2350, 1, 1).toJdn()

@Test
fun `Practice Persian converting back and forth`() {
assertEquals(PersianDate(1398, 1, 1).toJdn(), 2458564)
val startJdn = CivilDate(1750, 1, 1).toJdn()
val endJdn = CivilDate(2350, 1, 1).toJdn()
(startJdn..endJdn).forEach { assertEquals(it, PersianDate(it).toJdn()) }
(startJdn..endJdn).map {
val date = PersianDate(it)
assertEquals(it, date.toJdn())
assertTrue(date.month in 1..12)
assertTrue(date.dayOfMonth in 1..if (date.month in 1..6) 31 else 30)
date.dayOfMonth
}.ensureContinuity()
}

@Test
fun `Practice Islamic converting back and forth`() {
val startJdn = CivilDate(1750, 1, 1).toJdn()
val endJdn = CivilDate(2350, 1, 1).toJdn()
(startJdn..endJdn).forEach { assertEquals(it, IslamicDate(it).toJdn()) }
(startJdn..endJdn).map {
val date = IslamicDate(it)
assertEquals(it, date.toJdn())
assertTrue(date.month in 1..12)
assertTrue(date.dayOfMonth in 1..30)
date.dayOfMonth
}.ensureContinuity()
}

@Test
fun `Practice UmmAlqara converting back and forth`() {
IslamicDate.useUmmAlQura = true
val startJdn = CivilDate(1750, 1, 1).toJdn()
val endJdn = CivilDate(2350, 1, 1).toJdn()
(startJdn..endJdn).forEach { assertEquals(it, IslamicDate(it).toJdn()) }
(startJdn..endJdn).map {
val date = IslamicDate(it)
assertEquals(it, date.toJdn())
assertTrue(date.month in 1..12)
assertTrue(date.dayOfMonth in 1..30)
date.dayOfMonth
}.ensureContinuity()
IslamicDate.useUmmAlQura = false
}

@Test
fun `Practice Gregorian converting back and forth`() {
val startJdn = CivilDate(1750, 1, 1).toJdn()
val endJdn = CivilDate(2350, 1, 1).toJdn()
(startJdn..endJdn).forEach { assertEquals(it, CivilDate(it).toJdn()) }
(startJdn..endJdn).map {
val date = CivilDate(it)
assertEquals(it, date.toJdn())
assertTrue(date.month in 1..12)
assertTrue(date.dayOfMonth in 1..31)
date.dayOfMonth
}.ensureContinuity()
}

private fun List<Int>.ensureContinuity() = this.reduce { previousDayOfMonth, dayOfMonth ->
assertTrue(dayOfMonth == 1 || dayOfMonth == previousDayOfMonth + 1)
dayOfMonth
}.let {}
}

0 comments on commit 89db005

Please sign in to comment.