org.threeten.bp.Period Java Examples

The following examples show how to use org.threeten.bp.Period. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DateTimeBuilder.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Resolves the builder, evaluating the date and time.
 * <p>
 * This examines the contents of the builder and resolves it to produce the best
 * available date and time, throwing an exception if a problem occurs.
 * Calling this method changes the state of the builder.
 *
 * @param resolverStyle how to resolve
 * @return {@code this}, for method chaining
 */
public DateTimeBuilder resolve(ResolverStyle resolverStyle, Set<TemporalField> resolverFields) {
    if (resolverFields != null) {
        fieldValues.keySet().retainAll(resolverFields);
    }
    // handle standard fields
    mergeInstantFields();
    mergeDate(resolverStyle);
    mergeTime(resolverStyle);
    if (resolveFields(resolverStyle)) {
        mergeInstantFields();
        mergeDate(resolverStyle);
        mergeTime(resolverStyle);
    }
    resolveTimeInferZeroes(resolverStyle);
    crossCheck();
    if (excessDays != null && excessDays.isZero() == false && date != null && time != null) {
        date = date.plus(excessDays);
        excessDays = Period.ZERO;
    }
    resolveFractional();
    resolveInstant();
    return this;
}
 
Example #2
Source File: RemoteAttestationCipher.java    From mollyim-android with GNU General Public License v3.0 5 votes vote down vote up
public static void verifyIasSignature(KeyStore trustStore, String certificates, String signatureBody, String signature, Quote quote)
    throws SignatureException
{
  if (certificates == null || certificates.isEmpty()) {
    throw new SignatureException("No certificates.");
  }

  try {
    SigningCertificate signingCertificate = new SigningCertificate(certificates, trustStore);
    signingCertificate.verifySignature(signatureBody, signature);

    SignatureBodyEntity signatureBodyEntity = JsonUtil.fromJson(signatureBody, SignatureBodyEntity.class);

    if (signatureBodyEntity.getVersion() != SIGNATURE_BODY_VERSION) {
      throw new SignatureException("Unexpected signed quote version " + signatureBodyEntity.getVersion());
    }

    if (!MessageDigest.isEqual(ByteUtil.trim(signatureBodyEntity.getIsvEnclaveQuoteBody(), 432), ByteUtil.trim(quote.getQuoteBytes(), 432))) {
      throw new SignatureException("Signed quote is not the same as RA quote: " + Hex.toStringCondensed(signatureBodyEntity.getIsvEnclaveQuoteBody()) + " vs " + Hex.toStringCondensed(quote.getQuoteBytes()));
    }

    if (!"OK".equals(signatureBodyEntity.getIsvEnclaveQuoteStatus())) {
      throw new SignatureException("Quote status is: " + signatureBodyEntity.getIsvEnclaveQuoteStatus());
    }

    if (Instant.from(ZonedDateTime.of(LocalDateTime.from(DateTimeFormatter.ofPattern("yyy-MM-dd'T'HH:mm:ss.SSSSSS").parse(signatureBodyEntity.getTimestamp())), ZoneId.of("UTC")))
               .plus(Period.ofDays(1))
               .isBefore(Instant.now()))
    {
      throw new SignatureException("Signature is expired");
    }

  } catch (CertificateException | CertPathValidatorException | IOException e) {
    throw new SignatureException(e);
  }
}
 
Example #3
Source File: ContactDiscoveryCipher.java    From libsignal-service-java with GNU General Public License v3.0 5 votes vote down vote up
public void verifyIasSignature(KeyStore trustStore, String certificates, String signatureBody, String signature, Quote quote)
    throws SignatureException
{
  if (certificates == null || certificates.isEmpty()) {
    throw new SignatureException("No certificates.");
  }

  try {
    SigningCertificate signingCertificate = new SigningCertificate(certificates, trustStore);
    signingCertificate.verifySignature(signatureBody, signature);

    SignatureBodyEntity signatureBodyEntity = JsonUtil.fromJson(signatureBody, SignatureBodyEntity.class);

    if (signatureBodyEntity.getVersion() != SIGNATURE_BODY_VERSION) {
      throw new SignatureException("Unexpected signed quote version " + signatureBodyEntity.getVersion());
    }

    if (!MessageDigest.isEqual(ByteUtil.trim(signatureBodyEntity.getIsvEnclaveQuoteBody(), 432), ByteUtil.trim(quote.getQuoteBytes(), 432))) {
      throw new SignatureException("Signed quote is not the same as RA quote: " + Hex.toStringCondensed(signatureBodyEntity.getIsvEnclaveQuoteBody()) + " vs " + Hex.toStringCondensed(quote.getQuoteBytes()));
    }

    if (!"OK".equals(signatureBodyEntity.getIsvEnclaveQuoteStatus())) {
      throw new SignatureException("Quote status is: " + signatureBodyEntity.getIsvEnclaveQuoteStatus());
    }

    if (Instant.from(ZonedDateTime.of(LocalDateTime.from(DateTimeFormatter.ofPattern("yyy-MM-dd'T'HH:mm:ss.SSSSSS").parse(signatureBodyEntity.getTimestamp())), ZoneId.of("UTC")))
               .plus(Period.ofDays(1))
               .isBefore(Instant.now()))
    {
      throw new SignatureException("Signature is expired");
    }

  } catch (CertificateException | CertPathValidatorException | IOException e) {
    throw new SignatureException(e);
  }
}
 
Example #4
Source File: DateTimeFormatter.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public Period queryFrom(TemporalAccessor temporal) {
    if (temporal instanceof DateTimeBuilder) {
        return ((DateTimeBuilder) temporal).excessDays;
    } else {
        return Period.ZERO;
    }
}
 
Example #5
Source File: MonthPagerAdapter.java    From material-calendarview with MIT License 4 votes vote down vote up
@Override public int indexOf(final CalendarDay day) {
  return (int) Period
      .between(min.getDate().withDayOfMonth(1), day.getDate().withDayOfMonth(1))
      .toTotalMonths();
}
 
Example #6
Source File: MinguoDate.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public ChronoPeriod until(ChronoLocalDate endDate) {
    Period period = isoDate.until(endDate);
    return getChronology().period(period.getYears(), period.getMonths(), period.getDays());
}
 
Example #7
Source File: ThaiBuddhistDate.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public ChronoPeriod until(ChronoLocalDate endDate) {
    Period period = isoDate.until(endDate);
    return getChronology().period(period.getYears(), period.getMonths(), period.getDays());
}
 
Example #8
Source File: JapaneseDate.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public ChronoPeriod until(ChronoLocalDate endDate) {
    Period period = isoDate.until(endDate);
    return getChronology().period(period.getYears(), period.getMonths(), period.getDays());
}
 
Example #9
Source File: DateTimeFormatter.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 2 votes vote down vote up
/**
 * A query that provides access to the excess days that were parsed.
 * <p>
 * This returns a singleton {@linkplain TemporalQuery query} that provides
 * access to additional information from the parse. The query always returns
 * a non-null period, with a zero period returned instead of null.
 * <p>
 * There are two situations where this query may return a non-zero period.
 * <ul>
 * <li>If the {@code ResolverStyle} is {@code LENIENT} and a time is parsed
 *  without a date, then the complete result of the parse consists of a
 *  {@code LocalTime} and an excess {@code Period} in days.
 *
 * <li>If the {@code ResolverStyle} is {@code SMART} and a time is parsed
 *  without a date where the time is 24:00:00, then the complete result of
 *  the parse consists of a {@code LocalTime} of 00:00:00 and an excess
 *  {@code Period} of one day.
 * </ul>
 * <p>
 * In both cases, if a complete {@code ChronoLocalDateTime} or {@code Instant}
 * is parsed, then the excess days are added to the date part.
 * As a result, this query will return a zero period.
 * <p>
 * The {@code SMART} behaviour handles the common "end of day" 24:00 value.
 * Processing in {@code LENIENT} mode also produces the same result:
 * <pre>
 *  Text to parse        Parsed object                         Excess days
 *  "2012-12-03T00:00"   LocalDateTime.of(2012, 12, 3, 0, 0)   ZERO
 *  "2012-12-03T24:00"   LocalDateTime.of(2012, 12, 4, 0, 0)   ZERO
 *  "00:00"              LocalTime.of(0, 0)                    ZERO
 *  "24:00"              LocalTime.of(0, 0)                    Period.ofDays(1)
 * </pre>
 * The query can be used as follows:
 * <pre>
 *  TemporalAccessor parsed = formatter.parse(str);
 *  LocalTime time = parsed.query(LocalTime.FROM);
 *  Period extraDays = parsed.query(DateTimeFormatter.parsedExcessDays());
 * </pre>
 * @return a query that provides access to the excess days that were parsed
 */
public static final TemporalQuery<Period> parsedExcessDays() {
    return PARSED_EXCESS_DAYS;
}