Skip to content

Instantly share code, notes, and snippets.

@vkostyukov
Last active January 25, 2017 21:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vkostyukov/1ee5a2e0cd3192a77ee6b0de99260056 to your computer and use it in GitHub Desktop.
Save vkostyukov/1ee5a2e0cd3192a77ee6b0de99260056 to your computer and use it in GitHub Desktop.
Fast currentTime in seconds
import java.time.format.DateTimeFormatter
import java.time.{Instant, ZoneOffset}
object currentTime {
private[this] val formatter: DateTimeFormatter =
DateTimeFormatter.RFC_1123_DATE_TIME.withZone(ZoneOffset.UTC)
@volatile private[this] var last: (Long, String) = (0, "")
def apply(): String = {
val time = System.currentTimeMillis()
if (time - last._1 > 1000) {
last = time -> formatter.format(Instant.ofEpochMilli(time))
}
last._2
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment