Skip to content

Commit

Permalink
Fixed some timezone handling in publication dates
Browse files Browse the repository at this point in the history
  • Loading branch information
spacecowboy committed Mar 14, 2024
1 parent 233d6ce commit edc8966
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -955,6 +955,21 @@ class FeedParserTest : DIAware {
}
}

@Test
fun timestampsAreParsedWithCorrectTimezone() {
runBlocking {
val feed =
feedParser.parseFeedResponse(URL("https://www.bleepingcomputer.com/feed/"), rssBleepingComputer)
.getOrElse { throw it.throwable!! }

// Thu, 14 Mar 2024 14:08:43 -0400"
assertEquals(
"2024-03-14T18:08:43Z",
feed.items!!.first().date_published,
)
}
}

@Test
fun handlesUnknownProtocols() =
runBlocking {
Expand Down Expand Up @@ -1119,6 +1134,32 @@ class FeedParserTest : DIAware {
}
}

@Language("xml")
const val rssBleepingComputer = """
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<rss version="2.0">
<channel>
<title>BleepingComputer</title>
<link>https://www.bleepingcomputer.com/</link>
<description>BleepingComputer - All Stories</description>
<pubDate>Thu, 14 Mar 2024 14:13:50 EDT</pubDate>
<generator>https://www.bleepingcomputer.com/</generator>
<language>en</language>
<atom:link href="https://www.bleepingcomputer.com/feed/" rel="self" type="application/rss+xml" />
<item>
<title>SIM swappers now stealing phone numbers from eSIMs</title>
<link>https://www.bleepingcomputer.com/news/security/sim-swappers-now-stealing-phone-numbers-from-esims/</link>
<pubDate>Thu, 14 Mar 2024 14:08:43 -0400</pubDate>
<dc:creator>Bill Toulas</dc:creator>
<category><![CDATA[Security]]></category>
<guid>https://www.bleepingcomputer.com/news/security/sim-swappers-now-stealing-phone-numbers-from-esims/</guid>
<description><![CDATA[SIM swappers have adapted their attacks to steal a target's phone number from an eSIM card, a rewritable SIM chip present on many recent smartphone models. [...]]]></description>
</item>
</channel>
</rss>
"""

@Language("xml")
const val atomRelative = """
<?xml version='1.0' encoding='UTF-8'?>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import java.net.URL
import java.time.Instant
import java.time.LocalDate
import java.time.LocalDateTime
import java.time.ZoneId
import java.time.format.DateTimeFormatter
import java.time.format.FormatStyle
import java.util.Locale
Expand Down Expand Up @@ -340,7 +341,7 @@ private fun PreviewItem.toFeedListItem() =
snippet = plainSnippet,
feedTitle = feedDisplayTitle,
unread = readTime == null,
pubDate = pubDate?.toLocalDateTime()?.formatDynamically() ?: "",
pubDate = pubDate?.withZoneSameInstant(ZoneId.systemDefault())?.toLocalDateTime()?.formatDynamically() ?: "",
image = image,
link = link,
bookmarked = bookmarked,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ import com.nononsenseapps.feeder.model.host
import com.nononsenseapps.feeder.ui.text.HtmlToPlainTextConverter
import java.net.URI
import java.time.Instant
import java.time.ZoneOffset
import java.time.ZonedDateTime

const val MAX_TITLE_LENGTH = 200
Expand Down Expand Up @@ -172,7 +171,7 @@ data class FeedItem
ZonedDateTime.parse(entry.date_published)
} catch (t: Throwable) {
// If a pubdate is missing, then don't update if one is already set
this.pubDate ?: ZonedDateTime.now(ZoneOffset.UTC)
this.pubDate ?: ZonedDateTime.now()
}
primarySortTime = minOf(firstSyncedTime, pubDate?.toInstant() ?: firstSyncedTime)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ import com.nononsenseapps.feeder.util.FilePathProvider
import com.nononsenseapps.feeder.util.unicodeWrap
import org.kodein.di.compose.LocalDI
import org.kodein.di.instance
import java.time.ZoneId
import java.time.ZonedDateTime

@Composable
Expand Down Expand Up @@ -440,7 +441,7 @@ fun ArticleContent(
viewState.author == null && viewState.pubDate != null ->
stringResource(
R.string.on_date,
(viewState.pubDate ?: ZonedDateTime.now()).format(dateTimeFormat),
(viewState.pubDate?.withZoneSameInstant(ZoneId.systemDefault()) ?: ZonedDateTime.now()).format(dateTimeFormat),
)

viewState.author != null && viewState.pubDate != null ->
Expand All @@ -449,7 +450,7 @@ fun ArticleContent(
// Must wrap author in unicode marks to ensure it formats
// correctly in RTL
context.unicodeWrap(viewState.author ?: ""),
(viewState.pubDate ?: ZonedDateTime.now()).format(dateTimeFormat),
(viewState.pubDate?.withZoneSameInstant(ZoneId.systemDefault()) ?: ZonedDateTime.now()).format(dateTimeFormat),
)

else -> null
Expand Down

0 comments on commit edc8966

Please sign in to comment.