Java Code Examples for java.time.Instant.ofEpochSecond()

The following are Jave code examples for showing how to use ofEpochSecond() of the java.time.Instant class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: jdk8u-jdk   File: TCKZonedDateTime.java   View Source Code Vote up 6 votes
@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);
}
 
Example 2
Project: fernet-java8   File: Token.java   View Source Code Vote up 5 votes
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);
    }
}
 
Example 3
Project: fernet-java8   File: ProtocolBuffersExampleIT.java   View Source Code Vote up 5 votes
/**
 * 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();
}
 
Example 4
Project: openjdk-jdk10   File: TCKInstant.java   View Source Code Vote up 5 votes
@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);
}
 
Example 5
Project: trading4j   File: TimeFrameConveterTest.java   View Source Code Vote up 5 votes
/**
 * 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]);
    }
}
 
Example 6
Project: jdk8u-jdk   File: TCKInstant.java   View Source Code Vote up 5 votes
@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);
}
 
Example 7
Project: openjdk-jdk10   File: TCKOffsetTime.java   View Source Code Vote up 5 votes
@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);
    }
}
 
Example 8
Project: sample.microservices.security.jwt   File: JwtUserDetails.java   View Source Code Vote up 5 votes
@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);
}
 
Example 9
Project: openjdk-jdk10   File: TCKInstant.java   View Source Code Vote up 5 votes
@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);
}
 
Example 10
Project: jdk8u-jdk   File: TCKInstant.java   View Source Code Vote up 5 votes
@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);
}
 
Example 11
Project: jdk8u-jdk   File: TCKInstant.java   View Source Code Vote up 4 votes
@Test(expectedExceptions=DateTimeException.class)
public void test_until_invalidType() {
    Instant start = Instant.ofEpochSecond(12, 3000);
    start.until(LocalTime.of(11, 30), SECONDS);
}
 
Example 12
Project: jdk8u-jdk   File: TCKInstant.java   View Source Code Vote up 4 votes
@Test(expectedExceptions=DateTimeException.class)
public void plusMillis_long_overflowTooSmall() {
    Instant t = Instant.ofEpochSecond(MIN_SECOND, 0);
    t.plusMillis(-1);
}
 
Example 13
Project: openjdk-jdk10   File: TCKInstant.java   View Source Code Vote up 4 votes
@Test(expectedExceptions=DateTimeException.class)
public void factory_seconds_long_long_tooBig() {
    Instant.ofEpochSecond(MAX_SECOND, 1000000000);
}
 
Example 14
Project: jdk8u-jdk   File: TCKInstant.java   View Source Code Vote up 4 votes
@Test(expectedExceptions=DateTimeException.class)
public void minus_Duration_overflowTooSmall() {
    Instant i = Instant.ofEpochSecond(MIN_SECOND);
    i.minus(Duration.ofSeconds(0, 1));
}
 
Example 15
Project: openjdk-jdk10   File: TCKInstant.java   View Source Code Vote up 4 votes
@Test(expectedExceptions=DateTimeException.class)
public void plusMillis_long_overflowTooSmall() {
    Instant t = Instant.ofEpochSecond(MIN_SECOND, 0);
    t.plusMillis(-1);
}
 
Example 16
Project: jdk8u-jdk   File: TCKInstant.java   View Source Code Vote up 4 votes
@Test(expectedExceptions=ArithmeticException.class)
public void plusSeconds_long_overflowTooBig() {
    Instant t = Instant.ofEpochSecond(1, 0);
    t.plusSeconds(Long.MAX_VALUE);
}
 
Example 17
Project: jdk8u-jdk   File: TCKInstant.java   View Source Code Vote up 4 votes
@Test(expectedExceptions=DateTimeException.class)
public void minusMillis_long_overflowTooSmall() {
    Instant i = Instant.ofEpochSecond(MIN_SECOND, 0);
    i.minusMillis(1);
}
 
Example 18
Project: jdk8u-jdk   File: TCKDuration.java   View Source Code Vote up 4 votes
@Test(expectedExceptions=NullPointerException.class)
public void factory_between__TemporalTemporal_endNull() {
    Instant start = Instant.ofEpochSecond(1);
    Duration.between(start, null);
}
 
Example 19
Project: openjdk-jdk10   File: TCKInstant.java   View Source Code Vote up 4 votes
@Test(expectedExceptions=NullPointerException.class)
public void test_isBefore_ObjectNull() {
    Instant a = Instant.ofEpochSecond(0L, 0);
    a.isBefore(null);
}
 
Example 20
Project: jdk8u-jdk   File: ChronoLocalDateTime.java   View Source Code Vote up 2 votes
/**
 * Converts this date-time to an {@code Instant}.
 * <p>
 * This combines this local date-time and the specified offset to form
 * an {@code Instant}.
 * <p>
 * This default implementation calculates from the epoch-day of the date and the
 * second-of-day of the time.
 *
 * @param offset  the offset to use for the conversion, not null
 * @return an {@code Instant} representing the same instant, not null
 */
default Instant toInstant(ZoneOffset offset) {
    return Instant.ofEpochSecond(toEpochSecond(offset), toLocalTime().getNano());
}