Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support 4 more logical types (time-micro, timestamp-micro, local-timestamp-micros, local-timestamp-millis) #183

Merged
merged 1 commit into from
Oct 8, 2023

Conversation

LeonPoon
Copy link
Contributor

@LeonPoon LeonPoon commented Oct 3, 2023

final case class LogicalSc(..., var timeMicros: java.time.LocalTime, var timestampMicros: java.time.ZonedDateTime, var localTimestampMicros: java.time.LocalDateTime, var localTimestampMillis: java.time.LocalDateTime) extends org.apache.avro.specific.SpecificRecordBase {
  def this() = this(..., java.time.LocalTime.MIDNIGHT, java.time.ZonedDateTime.of(java.time.LocalDateTime.MIN, java.time.ZoneId.of("UTC")), java.time.LocalDateTime.MIN, java.time.LocalDateTime.MIN)
  def get(field$: Int): AnyRef = {
    (field$: @switch) match {
      ...
      case 7 => {
        // avro time-micros long stores the number of microseconds after midnight, 00:00:00.000000
        {
          timeMicros.toNanoOfDay / 1000L
        }
      }.asInstanceOf[AnyRef]
      case 8 => {
        // avro timestamp-micros long stores the number of microseconds from the unix epoch, 1 January 1970 00:00:00.000000 UTC
        {
          timestampMicros.toEpochSecond * 1000000L + (timestampMicros.getNano / 1000L)
        }
      }.asInstanceOf[AnyRef]
      case 9 => {
        // avro local-timestamp-micros long stores the number of microseconds, from 1 January 1970 00:00:00.000000
        {
          localTimestampMicros.toEpochSecond(java.time.ZoneOffset.UTC) * 1000000L + (localTimestampMicros.getNano / 1000L)
        }
      }.asInstanceOf[AnyRef]
      case 10 => {
        // avro local-timestamp-millis long stores the number of millis, from 1 January 1970 00:00:00.000000
        {
          localTimestampMillis.toEpochSecond(java.time.ZoneOffset.UTC) * 1000L + (localTimestampMillis.getNano / 1000000L)
        }
      }.asInstanceOf[AnyRef]
      case _ => new org.apache.avro.AvroRuntimeException("Bad index")
    }
  }
  def put(field$: Int, value: Any): Unit = {
    (field$: @switch) match {
      ...
      case 7 => this.timeMicros = {
        // avro time-micros long stores the number of microseconds after midnight, 00:00:00.000000
        value match {
          case (l: Long) => {
            java.time.LocalTime.ofNanoOfDay(l * 1000L)
          }
        }
      }.asInstanceOf[java.time.LocalTime]
      case 8 => this.timestampMicros = {
        // avro timestamp-micros long stores the number of microseconds from the unix epoch, 1 January 1970 00:00:00.000000 UTC
        value match {
          case (l: Long) => {
            java.time.ZonedDateTime.of(java.time.LocalDateTime.ofEpochSecond(l / 1000000L, (l % 1000000L).toInt * 1000, java.time.ZoneOffset.UTC), java.time.ZoneId.of("UTC"))
          }
        }
      }.asInstanceOf[java.time.ZonedDateTime]
      case 9 => this.localTimestampMicros = {
        // avro local-timestamp-micros long stores the number of microseconds, from 1 January 1970 00:00:00.000000
        value match {
          case (l: Long) => {
            java.time.LocalDateTime.ofEpochSecond(l / 1000000L, (l % 1000000L).toInt * 1000, java.time.ZoneOffset.UTC)
          }
        }
      }.asInstanceOf[java.time.LocalDateTime]
      case 10 => this.localTimestampMillis = {
        // avro local-timestamp-millis long stores the number of millis, from 1 January 1970 00:00:00.000000
        value match {
          case (l: Long) => {
            java.time.LocalDateTime.ofEpochSecond(l / 1000L, (l % 1000L).toInt * 1000000, java.time.ZoneOffset.UTC)
          }
        }
      }.asInstanceOf[java.time.LocalDateTime]
      case _ => new org.apache.avro.AvroRuntimeException("Bad index")
    }
    ()
  }
}

@julianpeeters julianpeeters merged commit b8d4689 into julianpeeters:main Oct 8, 2023
6 checks passed
@julianpeeters
Copy link
Owner

Thanks once again for the contribution. Released in avrohugger 1.6.0 and sbt-avrohugger 2.6.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants