Skip to content

Commit eeb3da7

Browse files
committed
Add support for OffsetDateTime
1 parent 836f206 commit eeb3da7

File tree

3 files changed

+7
-3
lines changed

3 files changed

+7
-3
lines changed

src/main/scala/fi/oph/scalaschema/SchemaFactory.scala

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package fi.oph.scalaschema
22

33
import java.lang.reflect.Constructor
44
import java.sql.Timestamp
5-
import java.time.{LocalDate, LocalDateTime, ZonedDateTime}
5+
import java.time.{LocalDate, LocalDateTime, ZonedDateTime, OffsetDateTime}
66
import java.util.Date
77

88
import fi.oph.scalaschema.Annotations.findAnnotations
@@ -112,6 +112,7 @@ case class SchemaFactory() {
112112
"java.time.LocalDate" -> DateSchema(dateType = classOf[LocalDate]),
113113
"java.time.LocalDateTime" -> DateSchema(dateType = classOf[LocalDateTime]),
114114
"java.time.ZonedDateTime" -> DateSchema(dateType = classOf[ZonedDateTime]),
115+
"java.time.OffsetDateTime" -> DateSchema(dateType = classOf[OffsetDateTime]),
115116
"java.lang.String" -> StringSchema(),
116117
"scala.Boolean" -> BooleanSchema(),
117118
"scala.Int" -> NumberSchema(numberType = classOf[Int]),

src/main/scala/fi/oph/scalaschema/Serializer.scala

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package fi.oph.scalaschema
22

33
import java.time.format.DateTimeFormatter.ISO_INSTANT
4-
import java.time.{LocalDate, LocalDateTime, ZoneId, ZonedDateTime}
4+
import java.time.{LocalDate, LocalDateTime, ZoneId, ZonedDateTime, OffsetDateTime}
55
import java.util.Date
66
import fi.oph.scalaschema.SchemaPropertyProcessor.SchemaPropertyProcessor
77
import fi.oph.scalaschema.extraction.SchemaNotFoundException
@@ -97,6 +97,7 @@ object Serializer {
9797
case x: LocalDateTime => JString(x.toString)
9898
case x: Date => JString(ISO_INSTANT.format(ZonedDateTime.ofInstant(x.toInstant, ZoneId.of("UTC"))))
9999
case x: ZonedDateTime => JString(x.toString)
100+
case x: OffsetDateTime => JString(x.toString)
100101
case _ => throw new RuntimeException("Not a date: " + x)
101102
}
102103

src/main/scala/fi/oph/scalaschema/extraction/DateExtractor.scala

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package fi.oph.scalaschema.extraction
22

33
import java.sql.Timestamp
44
import java.time.format.DateTimeParseException
5-
import java.time.{LocalDate, LocalDateTime, ZonedDateTime}
5+
import java.time.{LocalDate, LocalDateTime, ZonedDateTime, OffsetDateTime}
66
import java.util.Date
77

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

0 commit comments

Comments
 (0)