com.gravity.gdk.util.date.package.scala Maven / Gradle / Ivy
The newest version!
package com.gravity.gdk.util
import org.joda.time.DateTime
import play.api.libs.json.{JsError, JsSuccess, Json, Reads}
/*
___...---''
___...---'\'___
'' _.-'' _`'.______\\.
/_.) )..- __..--'\\
( __..--''
'-''\@
Ⓐ Ⓐ Ⓐ Ⓐ Ⓐ Ⓐ Ⓐ Ⓐ Ⓐ Ⓐ Ⓐ
*/
package object date {
val unixTimestampJsonReader: Reads[DateTime] = Reads[DateTime](jsVal => jsVal.validate[Long] match {
case success: JsSuccess[_] => success.map(new DateTime(_))
// Failed as Long
case _ =>
// Try as String => Long => DateTime
jsVal.validate[String].flatMap(timestampStr => {
try
JsSuccess(new DateTime(timestampStr.toLong))
catch {
case _: NumberFormatException => JsError(s"$timestampStr not Longable")
}
})
})
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy