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);
}
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);
}
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)));
}
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));
}
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);
}
Aggregations