/** * Returns a new period with the specified number of days. * <p> * This period instance is immutable and unaffected by this method call. * * @param days the amount of days to add, may be negative * @return the new period with the increased days * @throws UnsupportedOperationException if the field is not supported */ public Period withDays(int days) { int[] values = getValues(); // cloned getPeriodType().setIndexedField(this, PeriodType.DAY_INDEX, values, days); return new Period(values, getPeriodType()); }
/** * @param dataInterval interval containing the actual data * @param period time will be offset by a multiple of the given period * until there is at least a full period ending within the data interval * @param origin origin to be used to align time periods * (e.g. to determine on what day of the week a weekly period starts) */ @JsonCreator public TimewarpOperator( @JsonProperty("dataInterval") Interval dataInterval, @JsonProperty("period") Period period, @JsonProperty("origin") DateTime origin ) { this.originMillis = origin.getMillis(); this.dataInterval = dataInterval; // this will fail for periods that do not map to millis (e.g. P1M) this.periodMillis = period.toStandardDuration().getMillis(); }
/** * Set the timeout for launching the Gobblin job from ISO-style period. */ public EmbeddedGobblin setLaunchTimeout(String timeout) { return setLaunchTimeout(Period.parse(timeout).getSeconds(), TimeUnit.SECONDS); }
private static boolean periodIsDayMultiple(final Period period) { return period.getMillis() == 0 && period.getSeconds() == 0 && period.getMinutes() == 0 && period.getHours() == 0 && (period.getDays() > 0 || period.getWeeks() > 0 || period.getMonths() > 0 || period.getYears() > 0); }
@Override public Duration coerce(String value) { return new Period(value).toStandardDuration(); } };
public long getTotalSeconds() { Period period = duration.toPeriod(); return period.getHours()*3600 + period.getMinutes()*60 + period.getSeconds(); } }
@Test public void testIterableMonth() { final DateTimeZone tz = DateTimes.inferTzFromString("America/Los_Angeles"); final DateTime baseTime = new DateTime("2012-11-03T10:00:00", tz); assertSameInterval( Lists.newArrayList( new DateTime("2012-11-01T00:00:00.000-07:00", tz), new DateTime("2012-12-01T00:00:00.000-08:00", tz), new DateTime("2013-01-01T00:00:00.000-08:00", tz), new DateTime("2013-02-01T00:00:00.000-08:00", tz) ), new PeriodGranularity(new Period("P1M"), null, tz) .getIterable(new Interval(baseTime, baseTime.plus(Months.months(3)))) ); }
@Override public Date resolveDuedate(String duedate, int maxIterations) { try { // check if due period was specified if(duedate.startsWith("P")){ return new DateTime(clockReader.getCurrentTime()).plus(Period.parse(duedate)).toDate(); } return DateTime.parse(duedate).toDate(); } catch (Exception e) { throw new ActivitiException("couldn't resolve duedate: " + e.getMessage(), e); } } }
@Test public void testDurationToDateTime() { final DateTime origin = DateTimes.of("2012-01-02T05:00:00.000-08:00"); Granularity gran = new DurationGranularity( new Period("PT12H5M").toStandardDuration().getMillis(), origin ); Assert.assertEquals( DateTimes.of("2012-01-01T05:00:04.123-08:00"), gran.toDateTime(DateTimes.of("2012-01-01T05:00:04.123-08:00").getMillis()) ); }
final long windowMillis = windowPeriod.toStandardDuration().getMillis(); DateTimes.nowUtc().plus( new Duration( System.currentTimeMillis(), segmentGranularity.increment(truncatedNow).getMillis() + windowMillis Duration initialDelay = new Duration( System.currentTimeMillis(), segmentGranularity.increment(truncatedNow).getMillis() + windowMillis ); Duration rate = new Duration(truncatedNow, segmentGranularity.increment(truncatedNow));
@Test public void testAccept() { Period period = new Period("PT10M"); RejectionPolicy rejectionPolicy = new ServerTimeRejectionPolicyFactory().create(period); DateTime now = DateTimes.nowUtc(); DateTime past = now.minus(period).minus(100); DateTime future = now.plus(period).plus(100); Assert.assertTrue(rejectionPolicy.accept(now.getMillis())); Assert.assertFalse(rejectionPolicy.accept(past.getMillis())); Assert.assertFalse(rejectionPolicy.accept(future.getMillis())); } }
private boolean hasTaskPendingBeyondThreshold(Collection<? extends TaskRunnerWorkItem> pendingTasks) { long now = System.currentTimeMillis(); for (TaskRunnerWorkItem pendingTask : pendingTasks) { final Duration durationSinceInsertion = new Duration(pendingTask.getQueueInsertionTime().getMillis(), now); final Duration timeoutDuration = config.getPendingTaskTimeout().toStandardDuration(); if (durationSinceInsertion.isEqual(timeoutDuration) || durationSinceInsertion.isLongerThan(timeoutDuration)) { return true; } } return false; }
@Test public void testTimePlusYearMonthInterval() { final Period period = new Period("P1Y1M"); testExpression( rexBuilder.makeCall( SqlStdOperatorTable.DATETIME_PLUS, inputRef("t"), rexBuilder.makeIntervalLiteral( new BigDecimal(13), // YEAR-MONTH literals value is months new SqlIntervalQualifier(TimeUnit.YEAR, TimeUnit.MONTH, SqlParserPos.ZERO) ) ), DruidExpression.of( null, "timestamp_shift(\"t\",concat('P', 13, 'M'),1)" ), DateTimes.of("2000-02-03T04:05:06").plus(period).getMillis() ); }
@Test public void testAppliesToPeriod() { DateTime now = DateTimes.of("2012-12-31T01:00:00"); PeriodLoadRule rule = new PeriodLoadRule( new Period("P1M"), false, ImmutableMap.of("", 0) ); Assert.assertTrue(rule.appliesTo(builder.interval(new Interval(now.minusWeeks(1), now)).build(), now)); Assert.assertTrue( rule.appliesTo( builder.interval(new Interval(now.minusDays(1), now.plusDays(1))) .build(), now ) ); Assert.assertFalse( rule.appliesTo( builder.interval(new Interval(now.plusDays(1), now.plusDays(2))) .build(), now ) ); }
/** * Converts this period to a duration assuming a * 7 day week, 24 hour day, 60 minute hour and 60 second minute. * <p> * This method allows you to convert from a period to a duration. * However to achieve this it makes the assumption that all * weeks are 7 days, all days are 24 hours, all hours are 60 minutes and * all minutes are 60 seconds. This is not true when daylight savings time * is considered, and may also not be true for some unusual chronologies. * However, it is included as it is a useful operation for many * applications and business rules. * <p> * If the period contains years or months, an exception will be thrown. * * @return a duration equivalent to this period * @throws UnsupportedOperationException if the period contains years or months * @since 1.5 */ public Duration toStandardDuration() { checkYearsAndMonths("Duration"); long millis = getMillis(); // no overflow can happen, even with Integer.MAX_VALUEs millis += (((long) getSeconds()) * ((long) DateTimeConstants.MILLIS_PER_SECOND)); millis += (((long) getMinutes()) * ((long) DateTimeConstants.MILLIS_PER_MINUTE)); millis += (((long) getHours()) * ((long) DateTimeConstants.MILLIS_PER_HOUR)); millis += (((long) getDays()) * ((long) DateTimeConstants.MILLIS_PER_DAY)); millis += (((long) getWeeks()) * ((long) DateTimeConstants.MILLIS_PER_WEEK)); return new Duration(millis); }
); final String version = DateTimes.nowUtc().toString(); if (remaininInterval.toDuration().isLongerThan(spec.segmentPeriod.toStandardDuration())) { segmentInterval = new Interval(spec.segmentPeriod, remaininInterval.getEnd()); } else { segmentInterval = remaininInterval;
@Test public void testIncludeFuture() { DateTime now = DateTimes.of("2012-12-31T01:00:00"); PeriodLoadRule includeFutureRule = new PeriodLoadRule( new Period("P2D"), true, ImmutableMap.of("", 0) ); PeriodLoadRule notIncludeFutureRule = new PeriodLoadRule( new Period("P2D"), false, ImmutableMap.of("", 0) ); Assert.assertTrue( includeFutureRule.appliesTo( builder.interval(new Interval(now.plusDays(1), now.plusDays(2))).build(), now ) ); Assert.assertFalse( notIncludeFutureRule.appliesTo( builder.interval(new Interval(now.plusDays(1), now.plusDays(2))).build(), now ) ); }
new Period("P1M"), false ); builder.interval(new Interval(now.minusWeeks(1), now.minusDays(1))) .build(), now Assert.assertTrue( rule.appliesTo( builder.interval(new Interval(now.minusDays(1), now)) .build(), now Assert.assertFalse( rule.appliesTo( builder.interval(new Interval(now.minusYears(1), now.minusDays(1))) .build(), now