PM-22456: Move Temporal Accessor Extensions to 'Core' module (#5324)

This commit is contained in:
David Perez
2025-06-06 12:29:02 -05:00
committed by GitHub
parent a0ff94195f
commit f769900976
26 changed files with 25 additions and 48 deletions

View File

@@ -0,0 +1,22 @@
package com.bitwarden.core.data.util
import java.time.Clock
import java.time.ZoneId
import java.time.format.DateTimeFormatter
import java.time.temporal.TemporalAccessor
/**
* Converts the [TemporalAccessor] to a formatted string based on the provided pattern and timezone.
*/
fun TemporalAccessor.toFormattedPattern(
pattern: String,
zone: ZoneId,
): String = DateTimeFormatter.ofPattern(pattern).withZone(zone).format(this)
/**
* Converts the [TemporalAccessor] to a formatted string based on the provided pattern and timezone.
*/
fun TemporalAccessor.toFormattedPattern(
pattern: String,
clock: Clock = Clock.systemDefaultZone(),
): String = toFormattedPattern(pattern = pattern, zone = clock.zone)

View File

@@ -0,0 +1,36 @@
package com.bitwarden.core.data.util
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test
import java.time.Clock
import java.time.Instant
import java.time.ZoneId
import java.time.ZoneOffset
class TemporalAccessorExtensionsTest {
@Test
fun `toFormattedPattern should return correctly formatted string with timezone`() {
val instant = Instant.parse("2023-12-10T15:30:00Z")
val pattern = "MM/dd/yyyy hh:mm a"
val zone = ZoneId.of("UTC")
val expectedFormattedString = "12/10/2023 03:30 PM"
val formattedString = instant.toFormattedPattern(pattern, zone)
assertEquals(expectedFormattedString, formattedString)
}
@Test
fun `toFormattedPattern should return correctly formatted string with clock`() {
val instant = Instant.parse("2023-12-10T15:30:00Z")
val pattern = "MM/dd/yyyy hh:mm a"
val clock: Clock = Clock.fixed(
Instant.parse("2023-10-27T12:00:00Z"),
ZoneOffset.UTC,
)
val expectedFormattedString = "12/10/2023 03:30 PM"
val formattedString = instant.toFormattedPattern(pattern, clock)
assertEquals(expectedFormattedString, formattedString)
}
}