Skip to content
Snippets Groups Projects
Unverified Commit a1a0c6c5 authored by chris-cwa's avatar chris-cwa Committed by GitHub
Browse files

Contact diary date formatting (DEV) (#2109)


* extracted patterns

* - unexpected empty line

* fixed encoding issues

* was too complicated before

* trying to live with different implementations

* make everybody happy

* pattern test

* more normalizing

* improved readability

* team wants to hardcode everything

* ktlint

Co-authored-by: default avatarRalf Gehrer <ralfgehrer@users.noreply.github.com>
parent 6e925dc6
No related branches found
No related tags found
No related merge requests found
package de.rki.coronawarnapp.contactdiary.util
import org.joda.time.format.DateTimeFormat
import java.util.Locale
object CWADateTimeFormatPatternFactory {
fun Locale.shortDatePattern() = when {
this == Locale.GERMANY -> "dd.MM.yy"
this == Locale.UK -> "dd/MM/yyyy"
this == Locale.US -> "M/d/yy"
this == Locale("bg", "BG") -> "d.MM.yy 'г'."
this == Locale("ro", "RO") -> "dd.MM.yyyy"
this == Locale("pl", "PL") -> "dd.MM.yyyy"
this == Locale("tr", "TR") -> "d.MM.yyyy"
else -> DateTimeFormat.patternForStyle("S-", this)
}
}
...@@ -9,6 +9,7 @@ import androidx.core.view.AccessibilityDelegateCompat ...@@ -9,6 +9,7 @@ import androidx.core.view.AccessibilityDelegateCompat
import androidx.core.view.ViewCompat import androidx.core.view.ViewCompat
import androidx.core.view.accessibility.AccessibilityNodeInfoCompat import androidx.core.view.accessibility.AccessibilityNodeInfoCompat
import androidx.viewpager2.widget.ViewPager2 import androidx.viewpager2.widget.ViewPager2
import de.rki.coronawarnapp.contactdiary.util.CWADateTimeFormatPatternFactory.shortDatePattern
import org.joda.time.LocalDate import org.joda.time.LocalDate
import org.joda.time.format.DateTimeFormat import org.joda.time.format.DateTimeFormat
import java.util.Locale import java.util.Locale
...@@ -31,12 +32,7 @@ fun Context.getLocale(): Locale { ...@@ -31,12 +32,7 @@ fun Context.getLocale(): Locale {
} }
} }
fun LocalDate.toFormattedDay(locale: Locale): String { fun LocalDate.toFormattedDay(locale: Locale): String = toString("EEEE, ${locale.shortDatePattern()}", locale)
// Use two different methods to get the final date format (Weekday, Shortdate)
// because the custom pattern of toString() does not localize characters like "/" or "."
return "${toString("EEEE", locale)}, " +
DateTimeFormat.shortDate().withLocale(locale).print(this)
}
fun LocalDate.toFormattedDayForAccessibility(locale: Locale): String { fun LocalDate.toFormattedDayForAccessibility(locale: Locale): String {
// Use two different methods to get the final date format (Weekday, Longdate) // Use two different methods to get the final date format (Weekday, Longdate)
......
package de.rki.coronawarnapp.contactdiary.util
import de.rki.coronawarnapp.contactdiary.util.CWADateTimeFormatPatternFactory.shortDatePattern
import io.kotest.matchers.shouldBe
import org.junit.Test
import java.util.Locale
class CWADateTimeFormatPatternFactoryTest {
@Test
fun `pattern for german date`() {
Locale.GERMANY.shortDatePattern() shouldBe "dd.MM.yy"
}
@Test
fun `pattern for bulgarian date`() {
Locale("bg", "BG").shortDatePattern() shouldBe "d.MM.yy 'г'."
}
@Test
fun `pattern for gb date`() {
Locale.UK.shortDatePattern() shouldBe "dd/MM/yyyy"
}
@Test
fun `pattern for us date`() {
Locale.US.shortDatePattern() shouldBe "M/d/yy"
}
@Test
fun `pattern for romanian date`() {
Locale("ro", "RO").shortDatePattern() shouldBe "dd.MM.yyyy"
}
@Test
fun `pattern for polish date`() {
Locale("pl", "PL").shortDatePattern() shouldBe "dd.MM.yyyy"
}
@Test
fun `pattern for turkish date`() {
Locale("tr", "TR").shortDatePattern() shouldBe "d.MM.yyyy"
}
}
package de.rki.coronawarnapp.contactdiary.util package de.rki.coronawarnapp.contactdiary.util
import io.kotest.matchers.collections.shouldBeIn
import io.kotest.matchers.shouldBe import io.kotest.matchers.shouldBe
import org.joda.time.LocalDate import org.joda.time.LocalDate
import org.junit.jupiter.api.Test import org.junit.jupiter.api.Test
...@@ -11,7 +12,7 @@ class ContactDiaryDateFormatterExtensionTest { ...@@ -11,7 +12,7 @@ class ContactDiaryDateFormatterExtensionTest {
fun `format bulgarian date`() { fun `format bulgarian date`() {
LocalDate("2021-01-01").toFormattedDay( LocalDate("2021-01-01").toFormattedDay(
Locale("bg", "BG") Locale("bg", "BG")
) shouldBe "петък, 1.01.21 г." ) shouldBeIn arrayOf("петък, 1.01.21 г.", "Петък, 1.01.21 г.")
} }
@Test @Test
...@@ -40,7 +41,7 @@ class ContactDiaryDateFormatterExtensionTest { ...@@ -40,7 +41,7 @@ class ContactDiaryDateFormatterExtensionTest {
fun `format romanian date`() { fun `format romanian date`() {
LocalDate("2021-01-05").toFormattedDay( LocalDate("2021-01-05").toFormattedDay(
Locale("ro", "RO") Locale("ro", "RO")
) shouldBe "marți, 05.01.2021" ) shouldBeIn arrayOf("marţi, 05.01.2021", "marți, 05.01.2021")
} }
@Test @Test
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment