From 039cdf6b747ae82d4b8291359f6dcf00e81434fc Mon Sep 17 00:00:00 2001 From: firelight <147925818+fire-light42@users.noreply.github.com> Date: Tue, 14 Jul 2026 13:23:44 +0000 Subject: [PATCH] more robust date parsing --- .../com/lagradost/cloudstream3/MainAPI.kt | 12 +++++++++-- .../lagradost/cloudstream3/EpisodeDateTest.kt | 20 +++++++++++++++++++ 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/library/src/commonMain/kotlin/com/lagradost/cloudstream3/MainAPI.kt b/library/src/commonMain/kotlin/com/lagradost/cloudstream3/MainAPI.kt index 3e8774db2..ca024b463 100644 --- a/library/src/commonMain/kotlin/com/lagradost/cloudstream3/MainAPI.kt +++ b/library/src/commonMain/kotlin/com/lagradost/cloudstream3/MainAPI.kt @@ -2576,13 +2576,21 @@ constructor( @OptIn(FormatStringsInDatetimeFormats::class) fun Episode.addDate(date: String?, format: String = "yyyy-MM-dd") { - if (date == null) return + if (date.isNullOrBlank()) return this.date = runCatching { // First try standard ISO 8601 (e.g. "2026-01-01T12:30:00.000Z", "2026-05-17T14:35+02:00") runCatching { Instant.parse(date).toEpochMilliseconds() } .getOrElse { val fmt = DateTimeComponents.Format { byUnicodePattern(format) } - val components = DateTimeComponents.parse(date, fmt) + + // Try parsing the full date first then only parse the beginning of the string + // May lose time, but better than failing. + val components = runCatching { + DateTimeComponents.parse(date, fmt) + }.recoverCatching { + DateTimeComponents.parse(date.trimStart().take(format.length), fmt) + }.getOrThrow() + /** * Try multiple conversions in order of precision for non-ISO-8601 formats, * since the date string may or may not include time and/or timezone offset: diff --git a/library/src/commonTest/kotlin/com/lagradost/cloudstream3/EpisodeDateTest.kt b/library/src/commonTest/kotlin/com/lagradost/cloudstream3/EpisodeDateTest.kt index 4dc56978e..422b4cd93 100644 --- a/library/src/commonTest/kotlin/com/lagradost/cloudstream3/EpisodeDateTest.kt +++ b/library/src/commonTest/kotlin/com/lagradost/cloudstream3/EpisodeDateTest.kt @@ -31,6 +31,26 @@ class EpisodeDateTest { assertEquals(expected, ep.date) } + @Test + fun addDateDefaultFormatParsesExtraTime() { + val ep = episode() + ep.addDate("2026-05-17 12:30:45") + val expected = LocalDate(2026, 5, 17) + .atStartOfDayIn(TimeZone.currentSystemDefault()) + .toEpochMilliseconds() + assertEquals(expected, ep.date) + } + + @Test + fun addDateDefaultFormatParsesExtraJunk() { + val ep = episode() + ep.addDate(" 2026-05-17 random data") + val expected = LocalDate(2026, 5, 17) + .atStartOfDayIn(TimeZone.currentSystemDefault()) + .toEpochMilliseconds() + assertEquals(expected, ep.date) + } + @Test fun addDateNullDoesNotSetDate() { val ep = episode()