Skip to content

Commit

Permalink
refactor: move time string utility to common module (#49944)
Browse files Browse the repository at this point in the history
  • Loading branch information
jdpgrailsdev authored Dec 19, 2024
1 parent 8819bd1 commit c5bbe33
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 73 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Copyright (c) 2024 Airbyte, Inc., all rights reserved.
*/

package io.airbyte.cdk.load.data.iceberg.parquet
package io.airbyte.cdk.load.util

import io.airbyte.cdk.load.data.TimeStringToInteger
import java.time.LocalDate
Expand All @@ -13,12 +13,17 @@ import java.time.OffsetTime
import java.time.ZoneOffset
import java.time.ZonedDateTime

/** Collection of time/date string to time/date object conversion utilities. */
object TimeStringUtility {

fun toLocalDate(dateString: String): LocalDate {
return LocalDate.parse(dateString, TimeStringToInteger.DATE_TIME_FORMATTER)
}

fun toLocalDateTime(dateString: String): LocalDateTime {
return LocalDateTime.parse(dateString, TimeStringToInteger.DATE_TIME_FORMATTER)
}

fun toOffset(timeString: String): LocalTime {
return try {
toMicrosOfDayWithTimezone(timeString)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/*
* Copyright (c) 2024 Airbyte, Inc., all rights reserved.
*/

package io.airbyte.cdk.load.util

import io.airbyte.cdk.load.data.TimeStringToInteger
import java.time.LocalDate
import java.time.LocalDateTime
import java.time.LocalTime
import java.time.OffsetTime
import java.time.ZoneOffset
import java.time.ZonedDateTime
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertThrows
import org.junit.jupiter.api.Test

internal class TimeStringUtilityTest {

@Test
fun testToLocalDate() {
val localDateString = "2024-11-18"
val localDate = TimeStringUtility.toLocalDate(localDateString)
assertEquals(
LocalDate.parse(localDateString, TimeStringToInteger.DATE_TIME_FORMATTER),
localDate
)
}

@Test
fun testToLocalDateInvalidDateString() {
val invalidDateStr = "invalid-date"
assertThrows(java.time.format.DateTimeParseException::class.java) {
TimeStringUtility.toLocalDate(invalidDateStr)
}
}

@Test
fun testToLocalDateTime() {
val localDateTimeString = "2024-11-18T12:34:56Z"
val localDateTime = TimeStringUtility.toLocalDateTime(localDateTimeString)
assertEquals(
LocalDateTime.parse(localDateTimeString, TimeStringToInteger.DATE_TIME_FORMATTER),
localDateTime
)
}

@Test
fun testToOffsetWithTimezone() {
val offsetWithTimezoneString = "12:34:56Z"
val offsetWithTimezone = TimeStringUtility.toOffset(offsetWithTimezoneString)
assertEquals(
OffsetTime.parse(offsetWithTimezoneString, TimeStringToInteger.TIME_FORMATTER)
.toLocalTime(),
offsetWithTimezone
)
}

@Test
fun testToOffsetWithoutTimezone() {
val offsetWithoutTimezoneString = "12:34:56"
val offsetWithoutTimezone = TimeStringUtility.toOffset(offsetWithoutTimezoneString)
assertEquals(
LocalTime.parse(offsetWithoutTimezoneString, TimeStringToInteger.TIME_FORMATTER),
offsetWithoutTimezone
)
}

@Test
fun testToOffsetDateTimeWithTimezone() {
val offsetWithTimezoneString = "2024-11-18T12:34:56Z"
val offsetWithTimezone = TimeStringUtility.toOffsetDateTime(offsetWithTimezoneString)
assertEquals(
ZonedDateTime.parse(offsetWithTimezoneString, TimeStringToInteger.DATE_TIME_FORMATTER)
.toOffsetDateTime(),
offsetWithTimezone
)
}

@Test
fun testToOffsetDateTimeWithoutTimezone() {
val offsetWithoutTimezoneString = "2024-11-18T12:34:56"
val offsetWithoutTimezone = TimeStringUtility.toOffsetDateTime(offsetWithoutTimezoneString)
assertEquals(
LocalDateTime.parse(
offsetWithoutTimezoneString,
TimeStringToInteger.DATE_TIME_FORMATTER
)
.atOffset(ZoneOffset.UTC),
offsetWithoutTimezone
)
}

@Test
fun testToOffsetDateTimeInvalidFormat() {
val invalidDateTime = "invalid-datetime"
assertThrows(java.time.format.DateTimeParseException::class.java) {
TimeStringUtility.toOffsetDateTime(invalidDateTime)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import io.airbyte.cdk.load.data.StringValue
import io.airbyte.cdk.load.data.TimeValue
import io.airbyte.cdk.load.data.TimestampValue
import io.airbyte.cdk.load.data.UnknownValue
import io.airbyte.cdk.load.util.TimeStringUtility
import org.apache.iceberg.Schema
import org.apache.iceberg.data.GenericRecord
import org.apache.iceberg.types.Type
Expand Down

This file was deleted.

0 comments on commit c5bbe33

Please sign in to comment.