news: date-only and offset-less sitemap dates no longer read as epoch
- Commit
e2aac3c9122061f4c856814d46371f8479816ac3- Author
- Vayun Mathur <[email protected]>
- When
- 2026-12-01 19:21 UTC (4 days ago)
- Parents
53cfd57
DateParsing tried only parsers that require a time of day and an offset (Instant, OffsetDateTime, RFC-1123, and a space-separated local date-time). W3C Datetime โ the format sitemap <lastmod> and <news:publication_date> follow โ also permits the date-only form (2026-11-22) and floating date-times (2026-11-22T09:30:00), both common in plain urlsets. Those articles parsed to null and were stored with publishedAtMillis = 0, so FeedMerger and FeedRanking sorted them below every dated article and pruneArticles deleted them first on the next refresh, even when just fetched. Fix: append two parsers (offset-less ISO LocalDateTime, then LocalDate at start-of-day), both read as UTC, after the existing offset-bearing forms so previously accepted inputs are unchanged. Regression test: new DateParsingTest covers both new forms, the end-to-end sitemap <lastmod> case, and guards the old formats and null/blank handling. Pre-fix it failed 3 of 4 tests; post-fix ./gradlew :news:testDevUnitTest is green (full suite).
Diff
news/src/main/java/com/vayunmathur/news/domain/DateParsing.kt
diff --git a/news/src/main/java/com/vayunmathur/news/domain/DateParsing.kt b/news/src/main/java/com/vayunmathur/news/domain/DateParsing.ktindex f16ee54c90..9a7c35d6c6 100644--- a/news/src/main/java/com/vayunmathur/news/domain/DateParsing.kt+++ b/news/src/main/java/com/vayunmathur/news/domain/DateParsing.kt@@ -1,6 +1,7 @@ package com.vayunmathur.news.domain import java.time.Instant+import java.time.LocalDate import java.time.LocalDateTime import java.time.OffsetDateTime import java.time.ZoneOffset@@ -22,6 +23,12 @@ object DateParsing { val fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss", Locale.ENGLISH) LocalDateTime.parse(it, fmt).toInstant(ZoneOffset.UTC).toEpochMilli() },+ // Offset-less ISO forms (W3C Datetime permits both): a floating+ // date-time and a date-only <lastmod> read as UTC. Without these,+ // plain-urlset articles landed at epoch 0 โ sorted last by the+ // merger/ranking and pruned first.+ { LocalDateTime.parse(it).toInstant(ZoneOffset.UTC).toEpochMilli() },+ { LocalDate.parse(it).atStartOfDay(ZoneOffset.UTC).toInstant().toEpochMilli() }, ) fun parseMillis(raw: String?): Long? {
news/src/test/java/com/vayunmathur/news/domain/DateParsingTest.kt
diff --git a/news/src/test/java/com/vayunmathur/news/domain/DateParsingTest.kt b/news/src/test/java/com/vayunmathur/news/domain/DateParsingTest.ktnew file mode 100644index 0000000000..f90559316d--- /dev/null+++ b/news/src/test/java/com/vayunmathur/news/domain/DateParsingTest.kt@@ -0,0 +1,51 @@+package com.vayunmathur.news.domain++import java.time.Instant+import kotlin.test.Test+import kotlin.test.assertEquals+import kotlin.test.assertNotNull++class DateParsingTest {++ @Test fun dateOnlyIsoParsesAsMidnightUtc() {+ // Sitemap <lastmod> values are W3C Datetime, which permits the+ // date-only form YYYY-MM-DD (used by plain urlsets).+ val parsed = DateParsing.parseMillis("2026-11-22")+ assertNotNull(parsed)+ assertEquals(Instant.parse("2026-11-22T00:00:00Z").toEpochMilli(), parsed)+ }++ @Test fun offsetLessIsoDateTimeParsesAsUtc() {+ val parsed = DateParsing.parseMillis("2026-11-22T09:30:00")+ assertNotNull(parsed)+ assertEquals(Instant.parse("2026-11-22T09:30:00Z").toEpochMilli(), parsed)+ }++ @Test fun sitemapArticleWithDateOnlyLastmodKeepsItsDate() {+ val xml = """+ <urlset>+ <url><loc>https://example.com/dated</loc><lastmod>2026-11-22</lastmod></url>+ </urlset>+ """.trimIndent()+ val parsed = SitemapParser.parse(xml, "ex")+ assertEquals(1, parsed.articles.size)+ assertEquals(+ Instant.parse("2026-11-22T00:00:00Z").toEpochMilli(),+ parsed.articles[0].publishedAtMillis,+ )+ }++ @Test fun existingFormatsStillParse() {+ assertEquals(+ Instant.parse("2026-11-22T09:30:00Z").toEpochMilli(),+ DateParsing.parseMillis("2026-11-22T09:30:00Z"),+ )+ assertEquals(+ Instant.parse("2026-11-22T09:30:00Z").toEpochMilli(),+ DateParsing.parseMillis("2026-11-22 09:30:00"),+ )+ assertEquals(null, DateParsing.parseMillis("not a date"))+ assertEquals(null, DateParsing.parseMillis(""))+ assertEquals(null, DateParsing.parseMillis(null))+ }+}