Last active
January 25, 2017 21:14
-
-
Save vkostyukov/1ee5a2e0cd3192a77ee6b0de99260056 to your computer and use it in GitHub Desktop.
Fast currentTime in seconds
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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