Skip to content

Instantly share code, notes, and snippets.

@aroberts
Created January 11, 2016 18:10
Show Gist options
  • Save aroberts/8966121bb6cf62b1723e to your computer and use it in GitHub Desktop.
Save aroberts/8966121bb6cf62b1723e to your computer and use it in GitHub Desktop.
import java.time.{Instant, ZoneId, LocalDateTime}
import io.circe.{Encoder, Decoder}
import io.circe.syntax._
trait LocalDateTimeCodec {
val UTC = ZoneId.of("UTC")
implicit val dateTimeEncoder: Encoder[LocalDateTime] = Encoder.instance(
dt => dt.atZone(UTC).toInstant.toEpochMilli.asJson
)
implicit val dateTimeDecoder: Decoder[LocalDateTime] = Decoder.instance { cursor =>
for {
millis <- cursor.as[Long]
} yield LocalDateTime.ofInstant(Instant.ofEpochMilli(millis), UTC)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment