本文整理汇总了Java中java.time.Instant.ofEpochSecond方法的典型用法代码示例。如果您正苦于以下问题:Java Instant.ofEpochSecond方法的具体用法?Java Instant.ofEpochSecond怎么用?Java Instant.ofEpochSecond使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.time.Instant
的用法示例。
在下文中一共展示了Instant.ofEpochSecond方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: factory_ofInstant_maxWithMinOffset
import java.time.Instant; //导入方法依赖的package包/类
@Test
public void factory_ofInstant_maxWithMinOffset() {
long days_0000_to_1970 = (146097 * 5) - (30 * 365 + 7);
int year = Year.MAX_VALUE;
long days = (year * 365L + (year / 4 - year / 100 + year / 400)) + 365 - days_0000_to_1970;
Instant instant = Instant.ofEpochSecond((days + 1) * 24L * 60L * 60L - 1 - OFFSET_MIN.getTotalSeconds());
ZonedDateTime test = ZonedDateTime.ofInstant(instant, OFFSET_MIN);
assertEquals(test.getYear(), Year.MAX_VALUE);
assertEquals(test.getMonth().getValue(), 12);
assertEquals(test.getDayOfMonth(), 31);
assertEquals(test.getOffset(), OFFSET_MIN);
assertEquals(test.getHour(), 23);
assertEquals(test.getMinute(), 59);
assertEquals(test.getSecond(), 59);
assertEquals(test.getNano(), 0);
}
示例2: fromBytes
import java.time.Instant; //导入方法依赖的package包/类
protected static Token fromBytes(final byte[] bytes) throws IllegalTokenException {
if (bytes.length < minimumTokenBytes) {
throw new IllegalTokenException("Not enough bits to generate a Token");
}
try (final ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes)) {
final DataInputStream dataStream = new DataInputStream(inputStream);
final byte version = dataStream.readByte();
final long timestampSeconds = dataStream.readLong();
final byte[] initializationVector = read(dataStream, initializationVectorBytes);
final byte[] cipherText = read(dataStream, bytes.length - tokenStaticBytes);
final byte[] hmac = read(dataStream, signatureBytes);
if (dataStream.read() != -1) {
throw new IllegalTokenException("more bits found");
}
return new Token(version, Instant.ofEpochSecond(timestampSeconds),
new IvParameterSpec(initializationVector), cipherText, hmac);
} catch (final IOException ioe) {
// this should not happen as I/O is from memory and stream
// length is verified ahead of time
throw new RuntimeException(ioe.getMessage(), ioe);
}
}
示例3: renew
import java.time.Instant; //导入方法依赖的package包/类
/**
* Renew a session
*
* @param sessionId the existing session ID
* @param tokenString a current valid Fernet token
* @return a new Fernet token with the updated session state embedded
*/
@PUT
@Path("/api/sessions/{sessionId}/renewal")
public String renew(@PathParam("sessionId") final String sessionId, final String tokenString,
@Context final HttpServletResponse servletResponse) {
final Token inputToken = Token.fromString(tokenString);
final Session session = inputToken.validateAndDecrypt(key, validator);
if (!Objects.equals(sessionId, session.getSessionId())) {
throw new BadRequestException("SessionID mismatch.");
}
final Instant lastRenewed = Instant.ofEpochSecond(session.getLastRenewalTime());
if (session.hasLastRenewalTime() && lastRenewed.isAfter(Instant.now().minus(Duration.ofMinutes(1)))) {
// prevent denial-of-service
// if token was renewed less than a minute ago, tell the client to back off
servletResponse.addHeader("Retry-After", "60");
// Too Many Requests: https://tools.ietf.org/html/rfc6585#section-4
throw new WebApplicationException("Try again in a minute", 429);
}
// The token and session are valid, now update the session
final Builder builder = Session.newBuilder(session);
builder.setRenewalCount(session.getRenewalCount() + 1);
builder.setLastRenewalTime(Instant.now().getEpochSecond());
final Session updatedSession = builder.build();
// store the updated session in a new Fernet token
final Token retval = Token.generate(random, key, updatedSession.toByteArray());
return retval.serialise();
}
示例4: minusNanos_long
import java.time.Instant; //导入方法依赖的package包/类
@Test(dataProvider="MinusNanos")
public void minusNanos_long(long seconds, int nanos, long amount, long expectedSeconds, int expectedNanoOfSecond) {
Instant i = Instant.ofEpochSecond(seconds, nanos);
i = i.minusNanos(amount);
assertEquals(i.getEpochSecond(), expectedSeconds);
assertEquals(i.getNano(), expectedNanoOfSecond);
}
示例5: setUpTestDataAndMocks
import java.time.Instant; //导入方法依赖的package包/类
/**
* Sets up the test data and mocks.
*/
@Before
public void setUpTestDataAndMocks() {
// Sun, 27.7.2014 14:36:55 UTC
final Instant base = Instant.ofEpochSecond(1406471815L);
testCandleStickTimes = new Instant[CANDLE_STICK_COUNT];
for (int i = 0; i < CANDLE_STICK_COUNT; i++) {
testCandleStickTimes[i] = base.plus(i, MINUTES);
}
for (int i = 0; i < CANDLE_STICK_COUNT - 1; i++) {
when(m5Tf.instantOfNextFrame(testCandleStickTimes[i])).thenReturn(testCandleStickTimes[i + 1]);
}
}
示例6: test_until_TemporalUnit_negated
import java.time.Instant; //导入方法依赖的package包/类
@Test(dataProvider="periodUntilUnit")
public void test_until_TemporalUnit_negated(long seconds1, int nanos1, long seconds2, long nanos2, TemporalUnit unit, long expected) {
Instant i1 = Instant.ofEpochSecond(seconds1, nanos1);
Instant i2 = Instant.ofEpochSecond(seconds2, nanos2);
long amount = i2.until(i1, unit);
assertEquals(amount, -expected);
}
示例7: factory_ofInstant_beforeEpoch
import java.time.Instant; //导入方法依赖的package包/类
@Test
public void factory_ofInstant_beforeEpoch() {
for (int i =-1; i >= -(24 * 60 * 60); i--) {
Instant instant = Instant.ofEpochSecond(i, 8);
OffsetTime test = OffsetTime.ofInstant(instant, ZoneOffset.UTC);
assertEquals(test.getHour(), ((i + 24 * 60 * 60) / (60 * 60)) % 24);
assertEquals(test.getMinute(), ((i + 24 * 60 * 60) / 60) % 60);
assertEquals(test.getSecond(), (i + 24 * 60 * 60) % 60);
assertEquals(test.getNano(), 8);
}
}
示例8: isCredentialsNonExpired
import java.time.Instant; //导入方法依赖的package包/类
@Override
public boolean isCredentialsNonExpired() {
//consider the credentials as expired once the token expiry time has passed
//remember to keep all times in UTC, and optionally allow an extra amount
//to accomodate clock drift.
Instant now = Instant.now();
//for now, assume issued AND expiry are set, we do control the jwt creation =)
Instant expiry = Instant.ofEpochSecond(jwt.getClaims().getExpiration());
Instant issuedAt = Instant.ofEpochSecond(jwt.getClaims().getIssuedAt());
return now.isAfter(issuedAt) && now.isBefore(expiry);
}
示例9: plusMillis_long_min
import java.time.Instant; //导入方法依赖的package包/类
@Test
public void plusMillis_long_min() {
Instant t = Instant.ofEpochSecond(MIN_SECOND, 1000000);
t = t.plusMillis(-1);
assertEquals(t.getEpochSecond(), MIN_SECOND);
assertEquals(t.getNano(), 0);
}
示例10: test_until_invalidType
import java.time.Instant; //导入方法依赖的package包/类
@Test(expectedExceptions=DateTimeException.class)
public void test_until_invalidType() {
Instant start = Instant.ofEpochSecond(12, 3000);
start.until(LocalTime.of(11, 30), SECONDS);
}
示例11: plusMillis_long_overflowTooSmall
import java.time.Instant; //导入方法依赖的package包/类
@Test(expectedExceptions=DateTimeException.class)
public void plusMillis_long_overflowTooSmall() {
Instant t = Instant.ofEpochSecond(MIN_SECOND, 0);
t.plusMillis(-1);
}
示例12: factory_seconds_long_long_tooBig
import java.time.Instant; //导入方法依赖的package包/类
@Test(expectedExceptions=DateTimeException.class)
public void factory_seconds_long_long_tooBig() {
Instant.ofEpochSecond(MAX_SECOND, 1000000000);
}
示例13: minus_Duration_overflowTooSmall
import java.time.Instant; //导入方法依赖的package包/类
@Test(expectedExceptions=DateTimeException.class)
public void minus_Duration_overflowTooSmall() {
Instant i = Instant.ofEpochSecond(MIN_SECOND);
i.minus(Duration.ofSeconds(0, 1));
}
示例14: plusSeconds_long_overflowTooBig
import java.time.Instant; //导入方法依赖的package包/类
@Test(expectedExceptions=ArithmeticException.class)
public void plusSeconds_long_overflowTooBig() {
Instant t = Instant.ofEpochSecond(1, 0);
t.plusSeconds(Long.MAX_VALUE);
}
示例15: minusMillis_long_overflowTooSmall
import java.time.Instant; //导入方法依赖的package包/类
@Test(expectedExceptions=DateTimeException.class)
public void minusMillis_long_overflowTooSmall() {
Instant i = Instant.ofEpochSecond(MIN_SECOND, 0);
i.minusMillis(1);
}