diff --git a/build.sbt b/build.sbt index 5a10cf21..3533cb45 100644 --- a/build.sbt +++ b/build.sbt @@ -125,8 +125,9 @@ lazy val core = crossProject(JSPlatform, JVMPlatform) .settings(publishSettings) .jsSettings( libraryDependencies ++= Seq( - "org.scala-js" %%% "scalajs-dom" % "0.9.5", - "io.scalajs.npm" %%% "node-fetch" % "0.4.2" + "org.scala-js" %%% "scalajs-dom" % "0.9.5", + "io.scalajs.npm" %%% "node-fetch" % "0.4.2", + "io.github.cquiroz" %%% "scala-java-time" % "2.0.0-M13" ), npmDependencies in Test += "node-fetch" -> "2.1.2" ) diff --git a/core/js/src/main/scala/hammock/hi/platformspecific.scala b/core/js/src/main/scala/hammock/hi/platformspecific.scala index 91fbcb1b..750b1c95 100644 --- a/core/js/src/main/scala/hammock/hi/platformspecific.scala +++ b/core/js/src/main/scala/hammock/hi/platformspecific.scala @@ -1,15 +1,15 @@ package hammock package hi -import java.util.{Date => JavaDate} +import java.time.ZonedDateTime import scalajs.js.{Date => JsDate} object platformspecific { - def convert(d: JavaDate): JsDate = new JsDate(d.getTime().toDouble) + def convert(d: ZonedDateTime): JsDate = new JsDate(d.toInstant.toEpochMilli.toDouble) implicit object JSDateFormatter extends DateFormatter { - def format(date: JavaDate): String = fmt(convert(date)) + def format(date: ZonedDateTime): String = fmt(convert(date)) - def fmt(date: JsDate): String = date.toString + def fmt(date: JsDate): String = date.toUTCString } } diff --git a/core/jvm/src/main/scala/hammock/hi/platformspecific.scala b/core/jvm/src/main/scala/hammock/hi/platformspecific.scala index e14e95e5..6e59eada 100644 --- a/core/jvm/src/main/scala/hammock/hi/platformspecific.scala +++ b/core/jvm/src/main/scala/hammock/hi/platformspecific.scala @@ -1,12 +1,12 @@ package hammock package hi -import java.text.SimpleDateFormat -import java.util.Date +import java.time.ZonedDateTime +import java.time.format.DateTimeFormatter object platformspecific { implicit object JVMDateFormatter extends DateFormatter { - private val fmt = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z") - def format(date: Date): String = fmt.format(date) + private val fmt = DateTimeFormatter.ofPattern("EEE, dd MMM yyyy HH:mm:ss O") + def format(date: ZonedDateTime): String = date.format(fmt) } } diff --git a/core/shared/src/main/scala/hammock/hi/Cookie.scala b/core/shared/src/main/scala/hammock/hi/Cookie.scala index 933154a6..05a16744 100644 --- a/core/shared/src/main/scala/hammock/hi/Cookie.scala +++ b/core/shared/src/main/scala/hammock/hi/Cookie.scala @@ -1,7 +1,7 @@ package hammock package hi -import java.util.Date +import java.time.ZonedDateTime import cats._ import cats.implicits._ @@ -13,7 +13,7 @@ import monocle.macros.Lenses @Lenses case class Cookie( name: String, value: String, - expires: Option[Date] = None, + expires: Option[ZonedDateTime] = None, maxAge: Option[Int] = None, domain: Option[String] = None, path: Option[String] = None, @@ -24,7 +24,7 @@ import monocle.macros.Lenses ) object Cookie { - val expiresOpt: Optional[Cookie, Date] = Optional[Cookie, Date] { + val expiresOpt: Optional[Cookie, ZonedDateTime] = Optional[Cookie, ZonedDateTime] { _.expires } { date => { @@ -134,7 +134,7 @@ object Cookie { */ def render(cookie: Cookie)(implicit fmt: DateFormatter): String = { def renderPair[S: Show](k: String)(v: S) = k ++ "=" ++ Show[S].show(v) - def maybeShowDate(date: Option[Date]): Option[String] = date map (date => fmt.format(date)) + def maybeShowDate(date: Option[ZonedDateTime]): Option[String] = date map (date => fmt.format(date)) def expires = maybeShowDate(cookie.expires) map renderPair("Expires") def maxAge = cookie.maxAge map renderPair("MaxAge") def domain = cookie.domain map renderPair("Domain") diff --git a/core/shared/src/main/scala/hammock/hi/DateFormatter.scala b/core/shared/src/main/scala/hammock/hi/DateFormatter.scala index 4b61321e..6b1ed02a 100644 --- a/core/shared/src/main/scala/hammock/hi/DateFormatter.scala +++ b/core/shared/src/main/scala/hammock/hi/DateFormatter.scala @@ -1,8 +1,8 @@ package hammock package hi -import java.util.Date +import java.time.ZonedDateTime trait DateFormatter { - def format(date: Date): String + def format(date: ZonedDateTime): String } diff --git a/core/shared/src/test/scala/hammock/hi/CookieSpec.scala b/core/shared/src/test/scala/hammock/hi/CookieSpec.scala index bcea1324..5babf344 100644 --- a/core/shared/src/test/scala/hammock/hi/CookieSpec.scala +++ b/core/shared/src/test/scala/hammock/hi/CookieSpec.scala @@ -1,9 +1,10 @@ package hammock package hi -import org.scalatest.{Matchers, WordSpec} -import java.util.Date +import java.time.ZonedDateTime + import cats._ +import org.scalatest.{Matchers, WordSpec} class CookieSpec extends WordSpec with Matchers { @@ -14,11 +15,11 @@ class CookieSpec extends WordSpec with Matchers { Show[Cookie].show(cookie) shouldEqual "name=value" } - "render a complex cookie in the correct format" ignore { + "render a complex cookie in the correct format" in { val cookie = Cookie( "name", "value", - Some(new Date(234234234)), + Some(ZonedDateTime.parse("2020-01-04T17:03:54.000Z")), Some(123), Some("pepegar.com"), Some("/blog"), @@ -26,7 +27,7 @@ class CookieSpec extends WordSpec with Matchers { Some(true), Some(Cookie.SameSite.Strict)) - Show[Cookie].show(cookie) shouldEqual "name=value; Expires=Sat, 03 Jan 1970 17:03:54 +0000; MaxAge=123; Domain=pepegar.com; Path=/blog; Secure=false; HttpOnly=true; SameSite=Strict" + Show[Cookie].show(cookie) shouldEqual "name=value; Expires=Sat, 04 Jan 2020 17:03:54 GMT; MaxAge=123; Domain=pepegar.com; Path=/blog; Secure=false; HttpOnly=true; SameSite=Strict" } "render a cookie with custom values in the correct format" in {