Skip to content

Instantly share code, notes, and snippets.

@etorreborre
Created November 16, 2011 21:40
Show Gist options
  • Save etorreborre/1371518 to your computer and use it in GitHub Desktop.
Save etorreborre/1371518 to your computer and use it in GitHub Desktop.
Tagged Epochtime and Daytime
package object time {
// Unboxed newtypes, credit to @milessabin and @retronym
type Tagged[U] = { type Tag = U }
type @@[T, U] = T with Tagged[U]
class Tagger[U] { def apply[T](t : T) : T @@ U = t.asInstanceOf[T @@ U] }
def tag[U] = new Tagger[U]
trait Day
trait Epoch
// java.lang.Long needs to be used here in order to be used in a case class
// @see http://issues.scala-lang.org/browse/SI-5183
type Epochtime = java.lang.Long @@ Epoch
type Daytime = java.lang.Long @@ Day
def daytime(i: java.lang.Long): Daytime = tag(i)
def epochtime(i: java.lang.Long): Epochtime = tag(i)
/** @return the number of elapsed millis since the beginning of day for that time */
def epochTimeToDaytime(time: Long): Daytime = {
val calendar = Calendar.getInstance
calendar.setTime(new Date(time))
daytime(((calendar.get(HOUR_OF_DAY)*60+calendar.get(MINUTE))*60+calendar.get(SECOND))*1000 + calendar.get(MILLISECOND))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment