mirror of
https://github.com/bitwarden/android.git
synced 2026-03-11 12:44:17 -05:00
PM-22456: Move Temporal Accessor Extensions to 'Core' module (#5324)
This commit is contained in:
@@ -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)
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user