Skip to content

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 fcc252c commit fed73a2
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 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 @@ -76,18 +76,20 @@ private fun formatForUrl(title: String): String {
}


private fun getOrdinalWithSuperscript(n: Int): String {
private fun getOrdinal(n: Int): String {
val suffixes = arrayOf("th", "st", "nd", "rd")
val suffix = when (n % 100) {
11, 12, 13 -> "th"
else -> suffixes.getOrNull((n % 10).coerceIn(1..3) - 1).orEmpty()
return when (n % 100) {
11, 12, 13 -> n.toString() + "th"
else -> {
val lastDigit = n % 10
n.toString() + suffixes.getOrNull(lastDigit.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 = getOrdinalWithSuperscript(localDate.dayOfMonth)
val day = getOrdinal(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 fed73a2

Please sign in to comment.