| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
package android.net.sntp; |
| 17 |
|
| 18 |
import android.text.TextUtils; |
| 19 |
|
| 20 |
import com.android.internal.annotations.VisibleForTesting; |
| 21 |
|
| 22 |
import java.time.Instant; |
| 23 |
import java.util.Objects; |
| 24 |
import java.util.Random; |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
public final class Timestamp64 { |
| 40 |
|
| 41 |
public static final Timestamp64 ZERO = fromComponents(0, 0); |
| 42 |
static final int SUB_MILLIS_BITS_TO_RANDOMIZE = 32 - 10; |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
static final long OFFSET_1900_TO_1970 = ((365L * 70L) + 17L) * 24L * 60L * 60L; |
| 47 |
static final long MAX_SECONDS_IN_ERA = 0xFFFF_FFFFL; |
| 48 |
static final long SECONDS_IN_ERA = MAX_SECONDS_IN_ERA + 1; |
| 49 |
|
| 50 |
static final int NANOS_PER_SECOND = 1_000_000_000; |
| 51 |
|
| 52 |
|
| 53 |
public static Timestamp64 fromComponents(long eraSeconds, int fractionBits) { |
| 54 |
return new Timestamp64(eraSeconds, fractionBits); |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
public static Timestamp64 fromString(String string) { |
| 59 |
final int requiredLength = 17; |
| 60 |
if (string.length() != requiredLength || string.charAt(8) != '.') { |
| 61 |
throw new IllegalArgumentException(string); |
| 62 |
} |
| 63 |
String eraSecondsString = string.substring(0, 8); |
| 64 |
String fractionString = string.substring(9); |
| 65 |
long eraSeconds = Long.parseLong(eraSecondsString, 16); |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
long fractionBitsAsLong = Long.parseLong(fractionString, 16); |
| 70 |
if (fractionBitsAsLong < 0 || fractionBitsAsLong > 0xFFFFFFFFL) { |
| 71 |
throw new IllegalArgumentException("Invalid fractionBits:" + fractionString); |
| 72 |
} |
| 73 |
return new Timestamp64(eraSeconds, (int) fractionBitsAsLong); |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
public static Timestamp64 fromInstant(Instant instant) { |
| 82 |
long ntpEraSeconds = instant.getEpochSecond() + OFFSET_1900_TO_1970; |
| 83 |
if (ntpEraSeconds < 0) { |
| 84 |
ntpEraSeconds = SECONDS_IN_ERA - (-ntpEraSeconds % SECONDS_IN_ERA); |
| 85 |
} |
| 86 |
ntpEraSeconds %= SECONDS_IN_ERA; |
| 87 |
|
| 88 |
long nanos = instant.getNano(); |
| 89 |
int fractionBits = nanosToFractionBits(nanos); |
| 90 |
|
| 91 |
return new Timestamp64(ntpEraSeconds, fractionBits); |
| 92 |
} |
| 93 |
|
| 94 |
private final long mEraSeconds; |
| 95 |
private final int mFractionBits; |
| 96 |
|
| 97 |
private Timestamp64(long eraSeconds, int fractionBits) { |
| 98 |
if (eraSeconds < 0 || eraSeconds > MAX_SECONDS_IN_ERA) { |
| 99 |
throw new IllegalArgumentException( |
| 100 |
"Invalid parameters. seconds=" + eraSeconds + ", fraction=" + fractionBits); |
| 101 |
} |
| 102 |
this.mEraSeconds = eraSeconds; |
| 103 |
this.mFractionBits = fractionBits; |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
public long getEraSeconds() { |
| 108 |
return mEraSeconds; |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
public int getFractionBits() { |
| 113 |
return mFractionBits; |
| 114 |
} |
| 115 |
|
| 116 |
@Override |
| 117 |
public String toString() { |
| 118 |
return TextUtils.formatSimple("%08x.%08x", mEraSeconds, mFractionBits); |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
public Instant toInstant(int ntpEra) { |
| 123 |
long secondsSinceEpoch = mEraSeconds - OFFSET_1900_TO_1970; |
| 124 |
secondsSinceEpoch += ntpEra * SECONDS_IN_ERA; |
| 125 |
|
| 126 |
int nanos = fractionBitsToNanos(mFractionBits); |
| 127 |
return Instant.ofEpochSecond(secondsSinceEpoch, nanos); |
| 128 |
} |
| 129 |
|
| 130 |
@Override |
| 131 |
public boolean equals(Object o) { |
| 132 |
if (this == o) { |
| 133 |
return true; |
| 134 |
} |
| 135 |
if (o == null || getClass() != o.getClass()) { |
| 136 |
return false; |
| 137 |
} |
| 138 |
Timestamp64 that = (Timestamp64) o; |
| 139 |
return mEraSeconds == that.mEraSeconds && mFractionBits == that.mFractionBits; |
| 140 |
} |
| 141 |
|
| 142 |
@Override |
| 143 |
public int hashCode() { |
| 144 |
return Objects.hash(mEraSeconds, mFractionBits); |
| 145 |
} |
| 146 |
|
| 147 |
static int fractionBitsToNanos(int fractionBits) { |
| 148 |
long fractionBitsLong = fractionBits & 0xFFFF_FFFFL; |
| 149 |
return (int) ((fractionBitsLong * NANOS_PER_SECOND) >>> 32); |
| 150 |
} |
| 151 |
|
| 152 |
static int nanosToFractionBits(long nanos) { |
| 153 |
if (nanos > NANOS_PER_SECOND) { |
| 154 |
throw new IllegalArgumentException(); |
| 155 |
} |
| 156 |
return (int) ((nanos << 32) / NANOS_PER_SECOND); |
| 157 |
} |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
public Timestamp64 randomizeSubMillis(Random random) { |
| 166 |
int randomizedFractionBits = |
| 167 |
randomizeLowestBits(random, this.mFractionBits, SUB_MILLIS_BITS_TO_RANDOMIZE); |
| 168 |
return new Timestamp64(mEraSeconds, randomizedFractionBits); |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
@VisibleForTesting |
| 176 |
public static int randomizeLowestBits(Random random, int value, int bitsToRandomize) { |
| 177 |
if (bitsToRandomize < 1 || bitsToRandomize >= Integer.SIZE) { |
| 178 |
|
| 179 |
throw new IllegalArgumentException(Integer.toString(bitsToRandomize)); |
| 180 |
} |
| 181 |
|
| 182 |
int upperBitMask = 0xFFFF_FFFF << bitsToRandomize; |
| 183 |
int lowerBitMask = ~upperBitMask; |
| 184 |
|
| 185 |
int randomValue = random.nextInt(); |
| 186 |
return (value & upperBitMask) | (randomValue & lowerBitMask); |
| 187 |
} |
| 188 |
} |