Created
January 11, 2016 18:10
-
-
Save aroberts/8966121bb6cf62b1723e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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