Skip to content

Commit

Permalink
Make date comparison more flexible with different formats
Browse files Browse the repository at this point in the history
  • Loading branch information
francoisforster committed Dec 5, 2021
1 parent 7318dfd commit 5aa1b43
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 5 deletions.
Binary file modified gedcom-cleanup.jar
Binary file not shown.
Binary file modified gedcom-compare.jar
Binary file not shown.
36 changes: 35 additions & 1 deletion src/Event.kt
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
import java.time.LocalDate
import java.time.format.DateTimeFormatter
import java.time.format.DateTimeFormatterBuilder
import java.time.format.DateTimeParseException

private val shortFormatter = getFormatter("d MMM yyyy")
private val longFormatter = getFormatter("d MMMM yyyy")

fun getFormatter(pattern: String): DateTimeFormatter {
val builder = DateTimeFormatterBuilder()
builder.parseCaseInsensitive()
builder.appendPattern(pattern)
return builder.toFormatter()
}

data class Event(
val parentReferenceId: String?,
val record: Record,
Expand All @@ -23,6 +38,25 @@ data class Event(
if (other?.place?.contains(',') == true) {
otherShortPlace = other.place.substring(0, other.place.indexOf(","))
}
return date == other?.date && shortPlace == otherShortPlace
return datesMatch(date, other?.date) && shortPlace == otherShortPlace
}

private fun datesMatch(date: String?, otherDate: String?): Boolean {
if (date == null || otherDate == null) {
return date == otherDate
}
return try {
parseDate(date) == parseDate(otherDate)
} catch (e: DateTimeParseException) {
date == otherDate
}
}

private fun parseDate(date: String): LocalDate {
return try {
LocalDate.parse(date, shortFormatter)
} catch (e: DateTimeParseException) {
LocalDate.parse(date, longFormatter)
}
}
}
8 changes: 4 additions & 4 deletions src/GedcomCompare.kt
Original file line number Diff line number Diff line change
Expand Up @@ -334,13 +334,13 @@ class GedcomCompare(
if (leftFamily.getMarriage()?.matches(rightFamily.getMarriage()) == true) {
result += 40
}
var leftHusband = leftGedcom.getIndividual(leftFamily.getHusband())
var rightHusband = rightGedcom.getIndividual(rightFamily.getHusband())
val leftHusband = leftGedcom.getIndividual(leftFamily.getHusband())
val rightHusband = rightGedcom.getIndividual(rightFamily.getHusband())
if (leftHusband != null && rightHusband != null) {
result += matchIndividualScore(leftHusband, rightHusband)
}
var leftWife = leftGedcom.getIndividual(leftFamily.getWife())
var rightWife = rightGedcom.getIndividual(rightFamily.getWife())
val leftWife = leftGedcom.getIndividual(leftFamily.getWife())
val rightWife = rightGedcom.getIndividual(rightFamily.getWife())
if (leftWife != null && rightWife != null) {
result += matchIndividualScore(leftWife, rightWife)
}
Expand Down

0 comments on commit 5aa1b43

Please sign in to comment.