Search in sources :

Example 36 with ChronoUnit

use of java.time.temporal.ChronoUnit in project dhis2-core by dhis2.

the class DateUtils method getDuration.

/**
 * Parses the given string into a {@link java.time.Duration} object. The
 * string syntax is [amount][unit]. The supported units are:
 * <p>
 * <ul>
 * <li>"d": Days</li>
 * <li>"h": Hours</li>
 * <li>"m": Minutes</li>
 * <li>"s": Seconds</li>
 * </ul>
 *
 * @param duration the duration string, an example describing 12 days is
 *        "12d".
 * @return a Duration object, or null if the duration string is invalid.
 */
public static Duration getDuration(String duration) {
    Matcher matcher = DURATION_PATTERN.matcher(duration);
    if (!matcher.find()) {
        return null;
    }
    long amount = Long.valueOf(matcher.group(1));
    String unit = matcher.group(2);
    ChronoUnit chronoUnit = TEMPORAL_MAP.get(unit);
    if (chronoUnit == null) {
        return null;
    }
    return Duration.of(amount, chronoUnit);
}
Also used : Matcher(java.util.regex.Matcher) ChronoUnit(java.time.temporal.ChronoUnit)

Example 37 with ChronoUnit

use of java.time.temporal.ChronoUnit in project graylog2-server by Graylog2.

the class JavaDurationConverter method convertFrom.

@Override
public Duration convertFrom(String value) {
    // ISO8601 format
    if (value.startsWith("P")) {
        final Period period = Period.parse(value);
        return Duration.parse(period.toString());
    }
    // number + unit formats
    final com.github.joschi.jadconfig.util.Duration jadDuration = com.github.joschi.jadconfig.util.Duration.parse(value);
    final ChronoUnit chronoUnit = toChronoUnit(jadDuration.getUnit());
    return Duration.of(jadDuration.getQuantity(), chronoUnit);
}
Also used : Period(org.joda.time.Period) ChronoUnit(java.time.temporal.ChronoUnit)

Example 38 with ChronoUnit

use of java.time.temporal.ChronoUnit in project aeron by real-logic.

the class ArchiveEventLoggerTest method logSessionStateChange.

@Test
void logSessionStateChange() {
    final int offset = ALIGNMENT * 4;
    logBuffer.putLong(CAPACITY + TAIL_POSITION_OFFSET, offset);
    final ChronoUnit from = ChronoUnit.CENTURIES;
    final ChronoUnit to = ChronoUnit.MICROS;
    final long id = 555_000_000_000L;
    final String payload = from.name() + STATE_SEPARATOR + to.name();
    final int captureLength = SIZE_OF_LONG + SIZE_OF_INT + payload.length();
    logger.logSessionStateChange(CONTROL_SESSION_STATE_CHANGE, from, to, id);
    verifyLogHeader(logBuffer, offset, CONTROL_SESSION_STATE_CHANGE.toEventCodeId(), captureLength, captureLength);
    assertEquals(id, logBuffer.getLong(encodedMsgOffset(offset + LOG_HEADER_LENGTH), LITTLE_ENDIAN));
    assertEquals(payload, logBuffer.getStringAscii(encodedMsgOffset(offset + LOG_HEADER_LENGTH + SIZE_OF_LONG)));
}
Also used : ChronoUnit(java.time.temporal.ChronoUnit) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 39 with ChronoUnit

use of java.time.temporal.ChronoUnit in project aeron by real-logic.

the class ClusterEventEncoderTest method testStateChangeLength.

@Test
void testStateChangeLength() {
    final ChronoUnit from = ChronoUnit.CENTURIES;
    final ChronoUnit to = ChronoUnit.HALF_DAYS;
    final String payload = from.name() + STATE_SEPARATOR + to.name();
    assertEquals(payload.length() + (SIZE_OF_INT * 2), stateChangeLength(from, to));
}
Also used : ChronoUnit(java.time.temporal.ChronoUnit) Test(org.junit.jupiter.api.Test)

Example 40 with ChronoUnit

use of java.time.temporal.ChronoUnit in project simba-os by cegeka.

the class UserTokenFactoryTest method resetPasswordUserToken_GebruiktResetPasswordExpirationTime_ConfigurationParameter.

@Test
public void resetPasswordUserToken_GebruiktResetPasswordExpirationTime_ConfigurationParameter() {
    when(coreConfigurationService.getValue(RESET_PASSWORD_USERTOKEN_EXPIRATION_TIME)).thenReturn(10L);
    ChronoUnit RESET_PASSWORD_USERTOKEN_EXPIRATION_TIME_UNIT = ChronoUnit.valueOf(RESET_PASSWORD_USERTOKEN_EXPIRATION_TIME.getChronoUnit().name());
    Token token = generateToken();
    LocalDateTime tokenGenerationTime = on(2017, 12, 8, 13, 18, 45);
    systemDate.freeze(tokenGenerationTime);
    ResetPasswordUserToken resetPasswordUserToken = userTokenFactory.resetPasswordUserToken(token, 1337);
    assertThat(resetPasswordUserToken.getToken()).isEqualTo(token);
    LocalDateTime expirationTime = tokenGenerationTime.plus(10, RESET_PASSWORD_USERTOKEN_EXPIRATION_TIME_UNIT);
    assertThat(resetPasswordUserToken.getExpiresOn()).isEqualTo(expirationTime);
}
Also used : LocalDateTime(java.time.LocalDateTime) Token.generateToken(org.simbasecurity.core.domain.communication.token.Token.generateToken) ChronoUnit(java.time.temporal.ChronoUnit) Test(org.junit.Test)

Aggregations

ChronoUnit (java.time.temporal.ChronoUnit)42 Test (org.junit.jupiter.api.Test)11 Test (org.junit.Test)6 LocalDateTime (java.time.LocalDateTime)5 Matcher (java.util.regex.Matcher)5 Duration (java.time.Duration)4 Map (java.util.Map)4 ImmutableMap (com.google.common.collect.ImmutableMap)3 ChronoLocalDateTime (java.time.chrono.ChronoLocalDateTime)3 ArrayList (java.util.ArrayList)3 Pattern (java.util.regex.Pattern)3 Given (cucumber.api.java.en.Given)2 CommandSubscriber (de.nikos410.discordBot.util.modular.annotations.CommandSubscriber)2 CommandSubscriber (de.nikos410.discordbot.framework.annotations.CommandSubscriber)2 CommandUtils (de.nikos410.discordbot.util.CommandUtils)2 FaultToleranceService (fish.payara.microprofile.faulttolerance.FaultToleranceService)2 Instant (java.time.Instant)2 ZonedDateTime (java.time.ZonedDateTime)2 DateTimeFormatter (java.time.format.DateTimeFormatter)2 ScheduledFuture (java.util.concurrent.ScheduledFuture)2