Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,40 +16,33 @@
*/
package org.apache.spark.status.api.v1

import java.text.SimpleDateFormat
import java.text.{ParseException, SimpleDateFormat}
import java.util.TimeZone
import javax.ws.rs.WebApplicationException
import javax.ws.rs.core.Response
import javax.ws.rs.core.Response.Status

import scala.util.Try

private[v1] class SimpleDateParam(val originalValue: String) {
val timestamp: Long = {
SimpleDateParam.formats.collectFirst {
case fmt if Try(fmt.parse(originalValue)).isSuccess =>
fmt.parse(originalValue).getTime()
}.getOrElse(
throw new WebApplicationException(
Response
.status(Status.BAD_REQUEST)
.entity("Couldn't parse date: " + originalValue)
.build()
)
)
}
}

private[v1] object SimpleDateParam {

val formats: Seq[SimpleDateFormat] = {

val gmtDay = new SimpleDateFormat("yyyy-MM-dd")
gmtDay.setTimeZone(TimeZone.getTimeZone("GMT"))

Seq(
new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSz"),
gmtDay
)
val timestamp: Long = {
val format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSz")
try {
format.parse(originalValue).getTime()
} catch {
case _: ParseException =>
val gmtDay = new SimpleDateFormat("yyyy-MM-dd")
gmtDay.setTimeZone(TimeZone.getTimeZone("GMT"))
try {
gmtDay.parse(originalValue).getTime()
} catch {
case _: ParseException =>
throw new WebApplicationException(
Response
.status(Status.BAD_REQUEST)
.entity("Couldn't parse date: " + originalValue)
.build()
)
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not just make this a def makeFormats or something and add a comment explaining why it needs to be a def? It would be good to not inline it because the old code is more readable.

}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
*/
package org.apache.spark.status.api.v1

import javax.ws.rs.WebApplicationException

import org.scalatest.{Matchers, FunSuite}

class SimpleDateParamSuite extends FunSuite with Matchers {
Expand All @@ -24,6 +26,9 @@ class SimpleDateParamSuite extends FunSuite with Matchers {
new SimpleDateParam("2015-02-20T23:21:17.190GMT").timestamp should be (1424474477190L)
new SimpleDateParam("2015-02-20T17:21:17.190EST").timestamp should be (1424470877190L)
new SimpleDateParam("2015-02-20").timestamp should be (1424390400000L) // GMT
intercept[WebApplicationException] {
new SimpleDateParam("invalid date")
}
}

}