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

Commit

Permalink
fix ordinal
Browse files Browse the repository at this point in the history
  • Loading branch information
sanity committed Oct 28, 2023
1 parent 3dc1419 commit fcc252c
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions src/main/kotlin/org/freenet/website/pages/blog/blogPage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ fun Component.blogPage(number: Int? = null) {
}
div {
it.classes("subtitle", "is-6")
it.text(discussion.createdAt.asFriendlyDate())
it.innerHTML(discussion.createdAt.asFriendlyDate())
}
div {
it.classes("content")
Expand All @@ -76,17 +76,18 @@ private fun formatForUrl(title: String): String {
}


private fun getOrdinal(n: Int): String {
private fun getOrdinalWithSuperscript(n: Int): String {
val suffixes = arrayOf("th", "st", "nd", "rd")
return when (n % 100) {
11, 12, 13 -> n.toString() + "th"
else -> n.toString() + suffixes[n % 10.coerceIn(1..3)]
val suffix = when (n % 100) {
11, 12, 13 -> "th"
else -> suffixes.getOrNull((n % 10).coerceIn(1..3) - 1).orEmpty()
}
return "<span>${n}<sup>${suffix}</sup></span>"
}

fun Instant.asFriendlyDate(): String {
val localDate = this.atZone(ZoneId.systemDefault()).toLocalDate()
val day = getOrdinal(localDate.dayOfMonth)
val day = getOrdinalWithSuperscript(localDate.dayOfMonth)
val month = localDate.month.getDisplayName(TextStyle.FULL, Locale.US)
val year = localDate.year
return "$day $month, $year"
Expand Down

0 comments on commit fcc252c

Please sign in to comment.