Created
September 28, 2018 23:27
-
-
Save RChehowski/edf930ed3b10c44952288d1a2a66752b to your computer and use it in GitHub Desktop.
lol
This file contains hidden or 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
package com.vizor.unreal.util.util; | |
import java.time.ZonedDateTime; | |
public class GarbageFreeClock | |
{ | |
private static final int msInSec = 1000; | |
private static final int msInMin = msInSec * 60; | |
private static final int msInHou = msInMin * 60; | |
private static final int msInDay = msInHou * 24; | |
private static final String pattern = "HH:MM:SS:ZZZ"; | |
private static final char ZERO_CHAR = '0'; | |
private static final int msZoneOffset = ZonedDateTime.now().getOffset().getTotalSeconds() * msInSec; | |
private static String concatenateArray(final int h, final int m, final int s, final int z) | |
{ | |
int i = 0; | |
final char[] buf = new char[pattern.length()]; | |
// hour | |
i = shiftedNumber(h, buf, i); | |
buf[i++] = ':'; | |
// min | |
i = shiftedNumber(m, buf, i); | |
buf[i++] = ':'; | |
// sec | |
i = shiftedNumber(s, buf, i); | |
buf[i++] = ':'; | |
// ms | |
if (z < 100) | |
{ | |
buf[i++] = ZERO_CHAR; | |
shiftedNumber(z, buf, i); | |
} | |
else | |
{ | |
buf[i++] = (char)(ZERO_CHAR + (z / 100)); | |
shiftedNumber(z % 100, buf, i); | |
} | |
return new String(buf); | |
} | |
private static int shiftedNumber(final int time, char[] buf, int i) | |
{ | |
if (time < 10) | |
{ | |
buf[i++] = ZERO_CHAR; | |
buf[i++] = (char) (ZERO_CHAR + time); | |
} | |
else | |
{ | |
buf[i++] = (char) (ZERO_CHAR + (time / 10)); | |
buf[i++] = (char) (ZERO_CHAR + (time % 10)); | |
} | |
return i; | |
} | |
public static String currentTimeUTC() | |
{ | |
return currentTime(false); | |
} | |
public static String currentTime(boolean locallyShifted) | |
{ | |
final long currentTimeMillis = System.currentTimeMillis(); | |
// Always can fit in int, because it is lesser than msInDay | |
int z; | |
if (locallyShifted) | |
z = (int)((currentTimeMillis + msZoneOffset) % msInDay); | |
else | |
z = (int)(currentTimeMillis % msInDay); | |
// Disintegrate into parts | |
final int h = z / msInHou; | |
z -= (h * msInHou); | |
final int m = z / msInMin; | |
z -= (m * msInMin); | |
final int s = z / msInSec; | |
z -= (s * msInSec); | |
return concatenateArray(h, m, s, z); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment