Skip to content

Commit

Permalink
Add support for OffsetDateTime
Browse files Browse the repository at this point in the history
  • Loading branch information
ilkkahanninen committed Mar 18, 2024
1 parent 836f206 commit eeb3da7
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 3 deletions.
3 changes: 2 additions & 1 deletion src/main/scala/fi/oph/scalaschema/SchemaFactory.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package fi.oph.scalaschema

import java.lang.reflect.Constructor
import java.sql.Timestamp
import java.time.{LocalDate, LocalDateTime, ZonedDateTime}
import java.time.{LocalDate, LocalDateTime, ZonedDateTime, OffsetDateTime}
import java.util.Date

import fi.oph.scalaschema.Annotations.findAnnotations
Expand Down Expand Up @@ -112,6 +112,7 @@ case class SchemaFactory() {
"java.time.LocalDate" -> DateSchema(dateType = classOf[LocalDate]),
"java.time.LocalDateTime" -> DateSchema(dateType = classOf[LocalDateTime]),
"java.time.ZonedDateTime" -> DateSchema(dateType = classOf[ZonedDateTime]),
"java.time.OffsetDateTime" -> DateSchema(dateType = classOf[OffsetDateTime]),
"java.lang.String" -> StringSchema(),
"scala.Boolean" -> BooleanSchema(),
"scala.Int" -> NumberSchema(numberType = classOf[Int]),
Expand Down
3 changes: 2 additions & 1 deletion src/main/scala/fi/oph/scalaschema/Serializer.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package fi.oph.scalaschema

import java.time.format.DateTimeFormatter.ISO_INSTANT
import java.time.{LocalDate, LocalDateTime, ZoneId, ZonedDateTime}
import java.time.{LocalDate, LocalDateTime, ZoneId, ZonedDateTime, OffsetDateTime}
import java.util.Date
import fi.oph.scalaschema.SchemaPropertyProcessor.SchemaPropertyProcessor
import fi.oph.scalaschema.extraction.SchemaNotFoundException
Expand Down Expand Up @@ -97,6 +97,7 @@ object Serializer {
case x: LocalDateTime => JString(x.toString)
case x: Date => JString(ISO_INSTANT.format(ZonedDateTime.ofInstant(x.toInstant, ZoneId.of("UTC"))))
case x: ZonedDateTime => JString(x.toString)
case x: OffsetDateTime => JString(x.toString)
case _ => throw new RuntimeException("Not a date: " + x)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package fi.oph.scalaschema.extraction

import java.sql.Timestamp
import java.time.format.DateTimeParseException
import java.time.{LocalDate, LocalDateTime, ZonedDateTime}
import java.time.{LocalDate, LocalDateTime, ZonedDateTime, OffsetDateTime}
import java.util.Date

import fi.oph.scalaschema.{DateSchema, ExtractionContext, JsonCursor, Metadata}
Expand Down Expand Up @@ -34,6 +34,8 @@ object DateExtractor {
Date.from(java.time.ZonedDateTime.parse(dateString).toInstant)
} else if (dateType == classOf[DateTime]) {
ISODateTimeFormat.dateTimeParser.withZoneUTC.parseDateTime(dateString)
} else if (dateType == classOf[OffsetDateTime]) {
OffsetDateTime.parse(dateString)
} else {
throw new UnsupportedOperationException("Unrecognized Date type: " + dateType.getName)
}
Expand Down

0 comments on commit eeb3da7

Please sign in to comment.