Skip to content

Commit

Permalink
Switch from java.time.Instant to org.joda.time.Instant (#152)
Browse files Browse the repository at this point in the history
* switch from java.time.Instant to java.time.ZonedDateTime

* use joda time instead of anything from java.time

* update changelog

* dependency lint

* add unit tests for specific timezones

* woops make - format match
  • Loading branch information
jisantuc authored Sep 29, 2020
1 parent 73524cb commit 8c745a4
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 31 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
### Changed

- All implicit imports from cats are moved to specific `cats.syntax.foo` imports [#146](https://github.com/azavea/stac4s/pull/146)
- Time types are derived from `org.joda.time.Instant` instead of stock `java` time types [#152](https://github.com/azavea/stac4s/pull/152)

### Added

Expand Down
24 changes: 13 additions & 11 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -103,35 +103,37 @@ lazy val credentialSettings = Seq(
)

val coreDependencies = Seq(
"org.typelevel" %% "cats-core" % Versions.CatsVersion,
"org.typelevel" %% "cats-kernel" % Versions.CatsVersion,
"com.chuusai" %% "shapeless" % Versions.ShapelessVersion,
"com.github.tbouron" % "spdx-license-checker" % Versions.SpdxCheckerVersion,
"eu.timepit" %% "refined" % Versions.RefinedVersion,
"io.circe" %% "circe-core" % Versions.CirceVersion,
"io.circe" %% "circe-generic" % Versions.CirceVersion,
"io.circe" %% "circe-parser" % Versions.CirceVersion,
"io.circe" %% "circe-refined" % Versions.CirceVersion,
"joda-time" % "joda-time" % Versions.JodaTime,
"org.locationtech.geotrellis" %% "geotrellis-vector" % Versions.GeoTrellisVersion,
"eu.timepit" %% "refined" % Versions.RefinedVersion,
"com.github.tbouron" % "spdx-license-checker" % Versions.spdxCheckerVersion,
"com.chuusai" %% "shapeless" % Versions.ShapelessVersion,
"org.locationtech.jts" % "jts-core" % Versions.jts
"org.locationtech.jts" % "jts-core" % Versions.Jts,
"org.typelevel" %% "cats-core" % Versions.CatsVersion,
"org.typelevel" %% "cats-kernel" % Versions.CatsVersion
)

val testingDependencies = Seq(
"com.chuusai" %% "shapeless" % Versions.ShapelessVersion,
"com.github.tbouron" % "spdx-license-checker" % Versions.spdxCheckerVersion,
"com.github.tbouron" % "spdx-license-checker" % Versions.SpdxCheckerVersion,
"eu.timepit" %% "refined-scalacheck" % Versions.RefinedVersion,
"eu.timepit" %% "refined" % Versions.RefinedVersion,
"io.chrisdavenport" %% "cats-scalacheck" % Versions.scalacheckCatsVersion,
"io.chrisdavenport" %% "cats-scalacheck" % Versions.ScalacheckCatsVersion,
"io.circe" %% "circe-core" % Versions.CirceVersion,
"joda-time" % "joda-time" % Versions.JodaTime,
"org.locationtech.geotrellis" %% "geotrellis-vector" % Versions.GeoTrellisVersion,
"org.locationtech.jts" % "jts-core" % Versions.jts,
"org.scalacheck" %% "scalacheck" % Versions.scalacheckVersion,
"org.locationtech.jts" % "jts-core" % Versions.Jts,
"org.scalacheck" %% "scalacheck" % Versions.ScalacheckVersion,
"org.typelevel" %% "cats-core" % Versions.CatsVersion
)

val testRunnerDependencies = Seq(
"io.circe" %% "circe-testing" % Versions.CirceVersion,
"org.scalatest" %% "scalatest" % Versions.scalatestVersion,
"org.scalatest" %% "scalatest" % Versions.ScalatestVersion,
"org.scalatestplus" %% "scalacheck-1-14" % Versions.ScalatestPlusScalacheck
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ import geotrellis.vector.Geometry
import io.circe.syntax._
import io.circe.parser._
import io.circe.testing.{ArbitraryInstances, CodecTests}
import org.joda.time.Instant
import org.scalatest.Assertion
import org.scalatest.funsuite.AnyFunSuite
import org.scalatest.matchers.should.Matchers
import org.scalatestplus.scalacheck.Checkers
import org.typelevel.discipline.scalatest.FunSuiteDiscipline
import java.time.Instant

class SerDeSpec extends AnyFunSuite with FunSuiteDiscipline with Checkers with Matchers with ArbitraryInstances {

Expand Down Expand Up @@ -67,4 +68,28 @@ class SerDeSpec extends AnyFunSuite with FunSuiteDiscipline with Checkers with M
decode[StacLink]("""{"href":"s3://foo/item.json","rel":"item"}""")
link map { _.extensionFields } shouldBe Right(().asJsonObject)
}

// timezone parsing unit tests
private def getTimeDecodeTest(timestring: String): Assertion =
timestring.asJson.as[Instant] shouldBe (Right(Instant.parse(timestring)))

test("Instant decodes timestrings with +0x:00 timezones") {
getTimeDecodeTest("2018-01-01T00:00:00+05:00")
}

test("Instant decodes timestrings with -0x:00 timezones") {
getTimeDecodeTest("2018-01-01T00:00:00-09:00")
}

test("Instant decodes timestrings with 0000 format timezone") {
getTimeDecodeTest("2018-01-01T00:00:00+0000")
}

test("Instant decodes timestrings with +00 format timezone") {
getTimeDecodeTest("2018-01-01T00:00:00+00")
}

test("Instant decodes timestrings with -00 format timezone") {
getTimeDecodeTest("2018-01-01T00:00:00-00")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ import geotrellis.vector.Geometry
import io.circe._
import io.circe.parser.decode
import io.circe.syntax._
import org.joda.time.Instant

import java.time.Instant
import scala.util.Try

trait ForeignImplicits {

Expand All @@ -19,14 +20,11 @@ trait ForeignImplicits {
implicit val eqGeometry: Eq[Geometry] = Eq.fromUniversalEquals

// circe codecs
implicit val decodeInstant: Decoder[Instant] = Decoder.decodeString.emap { str =>
Either
.catchNonFatal(Instant.parse(str))
.leftMap(_ => "Instant")
}

implicit val encodeInstant: Encoder[Instant] =
Encoder.encodeString.contramap[Instant](_.toString)
implicit val encodeJodaInstant: Encoder[Instant] = Encoder[String].contramap(_.toString)

implicit val decodeJodaInstant: Decoder[Instant] =
Decoder[String].emap(s => Either.fromTry(Try(Instant.parse(s))).leftMap(_ => s"$s was not a valid string format"))

implicit val encodeTemporalExtent: Encoder[TemporalExtent] = _.value.map(x => x.asJson).asJson

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package com.azavea.stac4s.meta

import java.time.Instant

import eu.timepit.refined.api.Validate
import org.joda.time.Instant

final case class HasInstant()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import eu.timepit.refined.api.{Refined, RefinedTypeOps}
import eu.timepit.refined.boolean._
import eu.timepit.refined.collection.{Exists, MinSize, _}

import java.time.Instant
import org.joda.time.Instant

package object stac4s {

Expand Down
4 changes: 2 additions & 2 deletions modules/testing/src/main/scala/testing.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import io.circe.syntax._
import org.scalacheck._
import org.scalacheck.Arbitrary.arbitrary
import org.scalacheck.cats.implicits._
import java.time.Instant
import org.joda.time.Instant

package object testing extends NumericInstances {

Expand Down Expand Up @@ -51,7 +51,7 @@ package object testing extends NumericInstances {
)
}).widen

private def instantGen: Gen[Instant] = arbitrary[Int] map { x => Instant.now.plusMillis(x.toLong) }
private def instantGen: Gen[Instant] = arbitrary[Int] map { x => new Instant(x.toLong) }

private def assetCollectionExtensionGen: Gen[AssetCollectionExtension] =
possiblyEmptyMapGen(
Expand Down
13 changes: 7 additions & 6 deletions project/Versions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ object Versions {
val CatsVersion = "2.2.0"
val CirceVersion = "0.13.0"
val GeoTrellisVersion = "3.5.0"
val JodaTime = "2.10.6"
val Jts = "1.16.1"
val RefinedVersion = "0.9.17"
val ScalacheckCatsVersion = "0.3.0"
val ScalacheckVersion = "1.14.3"
val ScalatestPlusScalacheck = "3.2.2.0"
val ScalatestVersion = "3.2.2"
val ScapegoatVersion = "1.3.11"
val ShapelessVersion = "2.3.3"
val spdxCheckerVersion = "1.0.0"
val scalacheckCatsVersion = "0.3.0"
val scalatestVersion = "3.2.2"
val scalacheckVersion = "1.14.3"
val ScalatestPlusScalacheck = "3.2.2.0"
val jts = "1.16.1"
val SpdxCheckerVersion = "1.0.0"
}

0 comments on commit 8c745a4

Please sign in to comment.