Skip to content
This repository has been archived by the owner on Nov 5, 2024. It is now read-only.

Commit

Permalink
Clean date before parsing it
Browse files Browse the repository at this point in the history
  • Loading branch information
ILIYANGERMANOV committed Apr 17, 2023
1 parent a9b3e7b commit aef99c3
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions app/src/main/java/com/ivy/wallet/ui/csv/domain/ParseFields.kt
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ private fun parsePositiveDouble(string: String): Double? {
.replace("-", "")
.replace(" ", ".")
.filter { it.isDigit() || it == '.' }

val numberFormat = NumberFormat.getInstance(Locale.getDefault())
if (numberFormat is DecimalFormat) {
try {
Expand Down Expand Up @@ -87,11 +88,15 @@ fun parseDate(
value: String,
metadata: DateMetadata
): LocalDateTime? = tryParse {
val cleanedValue = value.filter {
it.isLetterOrDigit() || it == '-' || it == '/' || it == ':' || it == ' '
}

val possibleFormats = possibleDateFormats(metadata)
if (lastSuccessfulFormat != null) {
try {
return@tryParse LocalDateTime.parse(
value,
cleanedValue,
DateTimeFormatter.ofPattern(lastSuccessfulFormat)
)
} catch (e: DateTimeParseException) {
Expand All @@ -101,7 +106,7 @@ fun parseDate(
}
for (format in possibleFormats) {
try {
return@tryParse LocalDateTime.parse(value, DateTimeFormatter.ofPattern(format))
return@tryParse LocalDateTime.parse(cleanedValue, DateTimeFormatter.ofPattern(format))
} catch (e: DateTimeParseException) {
// Ignore and continue trying other formats
}
Expand Down

0 comments on commit aef99c3

Please sign in to comment.