Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save joecwu/9e6fd038364b4462f90c to your computer and use it in GitHub Desktop.
Save joecwu/9e6fd038364b4462f90c to your computer and use it in GitHub Desktop.
import com.joecwu.jcwutil.ObjFormatter._
object TestApp {
def run = {
implicit def timeZone = TIMEZONE_UTC
println(System.currentTimeMillis().toStringRFC3339)
println(System.currentTimeMillis().toStringISO8601)
}
}
package com.joecwu.jcwutil.test
import java.text.SimpleDateFormat
import java.util.{TimeZone, Locale}
import org.scalatest.FlatSpec
import com.joecwu.jcwutil.ObjFormatter._
import scalaz._, Scalaz._
/**
* Test spec for ObjFormatter
* Created by Joe on 2015/7/19.
*/
class ObjFormatterTest extends FlatSpec {
private val SDF_RFC3339 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.ENGLISH)
private val SDF_ISO8601 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX", Locale.ENGLISH)
behavior of "LongFormatter"
it should "convert Long to Date(RFC3339) format:(yyyy-MM-dd'T'HH:mm:ss'Z')" in {
implicit def timeZone = TIMEZONE_UTC
val ts = System.currentTimeMillis()
val date = new java.util.Date(ts)
SDF_RFC3339.setTimeZone(TimeZone.getTimeZone("UTC"))
val dateStr = SDF_RFC3339.format(date)
assertResult(ts.toStringRFC3339)(dateStr)
}
it should "convert Long to Date(RFC3339) format:(yyyy-MM-dd'T'HH:mm:ss'Z') with TimeZone" in {
val ts = System.currentTimeMillis()
val date = new java.util.Date(ts)
SDF_RFC3339.setTimeZone(TimeZone.getTimeZone("GMT+8"))
val dateStr = SDF_RFC3339.format(date)
implicit def timeZone = TimeZone.getTimeZone("GMT+8").some
println(ts.toStringRFC3339)
assertResult(ts.toStringRFC3339)(dateStr)
}
it should "convert Long to Date(ISO8601) format:(yyyy-MM-dd'T'HH:mm:ss.SSSXXX)" in {
implicit def timeZone = TIMEZONE_UTC
val ts = System.currentTimeMillis()
val date = new java.util.Date(ts)
SDF_ISO8601.setTimeZone(TimeZone.getTimeZone("UTC"))
val dateStr = SDF_ISO8601.format(date)
println(ts.toStringISO8601)
assertResult(ts.toStringISO8601)(dateStr)
}
it should "convert Long to Date(ISO8601) format:(yyyy-MM-dd'T'HH:mm:ss.SSSXXX) with TimeZone" in {
val ts = System.currentTimeMillis()
val date = new java.util.Date(ts)
SDF_ISO8601.setTimeZone(TimeZone.getTimeZone("GMT+8"))
val dateStr = SDF_ISO8601.format(date)
implicit def timeZone = TimeZone.getTimeZone("GMT+8").some
println(ts.toStringISO8601)
assertResult(ts.toStringISO8601)(dateStr)
}
behavior of "DateFormatter"
it should "convert Long to Date(RFC3339) format:(yyyy-MM-dd'T'HH:mm:ss'Z')" in {
implicit def timeZone = TIMEZONE_UTC
val ts = System.currentTimeMillis()
val date = new java.util.Date(ts)
SDF_RFC3339.setTimeZone(TimeZone.getTimeZone("UTC"))
val dateStr = SDF_RFC3339.format(date)
assertResult(date.toStringRFC3339)(dateStr)
}
it should "convert Long to Date(ISO8601) format:(yyyy-MM-dd'T'HH:mm:ss.SSSXXX)" in {
implicit def timeZone = TIMEZONE_UTC
val ts = System.currentTimeMillis()
val date = new java.util.Date(ts)
SDF_ISO8601.setTimeZone(TimeZone.getTimeZone("UTC"))
val dateStr = SDF_ISO8601.format(date)
assertResult(date.toStringISO8601)(dateStr)
}
}
package com.joecwu.jcwutil
import java.text.SimpleDateFormat
import java.util.{Locale, TimeZone}
import scalaz._, Scalaz._
/**
* Created by Joe on 2015/7/19.
*/
object ObjFormatter {
private val SDF_RFC3339 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.ENGLISH)
private val SDF_ISO8601 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX", Locale.ENGLISH)
val TIMEZONE_UTC = TimeZone.getTimeZone("UTC").some
implicit def LongFormatter(v:Long) = LongFormatHandler(v)
implicit def DateFormatter(date : java.util.Date) = DateFormatHandler(date)
protected case class LongFormatHandler(v:Long) {
def toStringRFC3339(implicit timeZone:Option[TimeZone]) : String = DateFormatHandler(new java.util.Date(v)).toStringRFC3339
def toStringISO8601(implicit timeZone:Option[TimeZone]) : String = DateFormatHandler(new java.util.Date(v)).toStringISO8601
}
protected case class DateFormatHandler(date:java.util.Date) {
def toStringRFC3339(implicit timeZone:Option[TimeZone]) : String = {
timeZone.map(SDF_RFC3339.setTimeZone)
SDF_RFC3339.format(date)
}
def toStringISO8601(implicit timeZone:Option[TimeZone]) : String = {
timeZone.map(SDF_ISO8601.setTimeZone)
SDF_ISO8601.format(date)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment