Example usage for org.joda.time Interval Interval

List of usage examples for org.joda.time Interval Interval

Introduction

In this page you can find the example usage for org.joda.time Interval Interval.

Prototype

public Interval(Object interval, Chronology chronology) 

Source Link

Document

Constructs a time interval by converting or copying from another object, overriding the chronology.

Usage

From source file:com.github.dbourdette.otto.data.SimpleDataTable.java

License:Apache License

public void setupRows(Interval interval, Duration step) {
    rows.clear();/* w ww.  j  ava2 s  .com*/

    DateTime current = interval.getStart();

    while (current.isBefore(interval.getEnd())) {
        rows.add(new SimpleDataTableRow(new Interval(current, step)));

        current = current.plus(step);
    }
}

From source file:com.github.dbourdette.otto.web.form.ReportForm.java

License:Apache License

public Interval getInterval() {
    if (advanced) {
        fixAdvancedParams();/*  www  .  j  av  a  2 s.  co m*/

        return new Interval(from.getTime(), new DateMidnight(to).plusDays(1).toDate().getTime());
    } else {
        return period.getInterval();
    }
}

From source file:com.github.dbourdette.otto.web.util.IntervalUtils.java

License:Apache License

public static Interval yesterday() {
    return new Interval(new DateMidnight().minusDays(1), new DateMidnight());
}

From source file:com.github.dbourdette.otto.web.util.IntervalUtils.java

License:Apache License

public static Interval today() {
    return new Interval(new DateMidnight(), new DateMidnight().plusDays(1));
}

From source file:com.github.dbourdette.otto.web.util.IntervalUtils.java

License:Apache License

public static Interval lastWeek() {
    DateMidnight monday = new DateMidnight().withDayOfWeek(1);

    return new Interval(monday.minusDays(7), monday);
}

From source file:com.github.dbourdette.otto.web.util.RandomDateUtils.java

License:Apache License

public static DateTime today() {
    return in(new Interval(new DateMidnight(), new DateMidnight().plusDays(1)));
}

From source file:com.github.dbourdette.otto.web.util.RandomDateUtils.java

License:Apache License

public static DateTime last7Days() {
    return in(new Interval(new DateMidnight().minusDays(6), new DateMidnight().plusDays(1)));
}

From source file:com.google.android.apps.paco.EsmGenerator2.java

License:Open Source License

public List<DateTime> generateForSchedule(DateTime startDate, SignalSchedule schedule) {
    this.schedule = schedule;
    this.periodStartDate = adjustStartDateToBeginningOfPeriod(startDate);
    times = new ArrayList<DateTime>();

    if (schedule.getEsmFrequency() == null || schedule.getEsmFrequency() == 0) {
        return times;
    }//  www .  j av a2  s  .  co m
    List<Integer> schedulableDays;
    switch (schedule.getEsmPeriodInDays()) {
    case SignalSchedule.ESM_PERIOD_DAY:
        if (!schedule.getEsmWeekends() && TimeUtil.isWeekend(periodStartDate)) {
            return times;
        } else {
            schedulableDays = Arrays.asList(1);
        }
        break;
    case SignalSchedule.ESM_PERIOD_WEEK:
        schedulableDays = getPeriodDaysForWeek();
        break;
    case SignalSchedule.ESM_PERIOD_MONTH:
        schedulableDays = getPeriodDaysForMonthOf(periodStartDate);
        break;
    default:
        throw new IllegalStateException("Cannot get here.");
    }

    Minutes dayLengthIntervalInMinutes = Minutes
            .minutesIn(new Interval(schedule.getEsmStartHour(), schedule.getEsmEndHour()));
    Minutes totalMinutesInPeriod = dayLengthIntervalInMinutes.multipliedBy(schedulableDays.size());
    Minutes sampleBlockTimeInMinutes = totalMinutesInPeriod.dividedBy(schedule.getEsmFrequency());
    Minutes timeoutInMinutes = Minutes.minutes(schedule.getMinimumBuffer());
    Random rand = new Random();
    for (int signal = 0; signal < schedule.getEsmFrequency(); signal++) {

        int candidateTimeInBlock;
        DateTime candidateTime;
        int periodAttempts = 1000;
        do {
            candidateTimeInBlock = rand.nextInt(sampleBlockTimeInMinutes.getMinutes());
            // map candidatePeriod and candidateTime back onto days of period
            // note, sometimes a candidate period will map across days in period
            // because start and end hours make for non-contiguous days
            int totalMinutesToAdd = sampleBlockTimeInMinutes.getMinutes() * signal + candidateTimeInBlock;
            int daysToAdd = totalMinutesToAdd / dayLengthIntervalInMinutes.getMinutes();
            int minutesToAdd = 0;
            if (totalMinutesToAdd <= dayLengthIntervalInMinutes.getMinutes()) { // within one day
                minutesToAdd = totalMinutesToAdd;
            } else {
                minutesToAdd = totalMinutesToAdd % dayLengthIntervalInMinutes.getMinutes();
            }

            DateTime plusDays = periodStartDate.plusDays(schedulableDays.get(daysToAdd) - 1);
            candidateTime = plusDays.withMillisOfDay(schedule.getEsmStartHour().intValue())
                    .plusMinutes(minutesToAdd);
            periodAttempts--;
        } while (periodAttempts > 0
                && (!isMinimalBufferedDistanceFromOtherTimes(candidateTime, timeoutInMinutes)
                        || (!schedule.getEsmWeekends() && TimeUtil.isWeekend(candidateTime))));
        if (isMinimalBufferedDistanceFromOtherTimes(candidateTime, timeoutInMinutes)
                && (schedule.getEsmWeekends() || !TimeUtil.isWeekend(candidateTime))) {
            times.add(candidateTime);
        }

    }
    return times;
}

From source file:com.google.android.apps.paco.ExperimentAlarms.java

License:Open Source License

public static List<TimeExperiment> getAllAlarmsWithinOneMinuteofNow(DateTime now, List<Experiment> experiments,
        Context context) {//from   www. ja v  a 2 s.c om
    List<TimeExperiment> times = arrangeExperimentsByNextTimeFrom(experiments, now, context);
    List<TimeExperiment> matchingTimes = new ArrayList<TimeExperiment>();
    for (TimeExperiment time : times) {
        if (new Interval(now, time.time).toDurationMillis() < 60000) {
            matchingTimes.add(time);
        }
    }
    return matchingTimes;
}

From source file:com.google.cloud.pubsub.SubscriberConnection.java

License:Open Source License

private void setupNextAckDeadlineExtensionAlarm(ExpirationInfo messageExpiration) {
    Instant possibleNextAlarmTime = messageExpiration.expiration.minus(ackExpirationPadding);
    alarmsLock.lock();//from w w w  .  j a  v a 2 s .  c  o  m
    try {
        if (nextAckDeadlineExtensionAlarmTime.isAfter(possibleNextAlarmTime)) {
            logger.debug("Scheduling next alarm time: {}, last alarm set to time: {}", possibleNextAlarmTime,
                    nextAckDeadlineExtensionAlarmTime);
            if (ackDeadlineExtensionAlarm != null) {
                logger.debug("Canceling previous alarm");
                ackDeadlineExtensionAlarm.cancel(false);
            }

            nextAckDeadlineExtensionAlarmTime = possibleNextAlarmTime;

            ackDeadlineExtensionAlarm = executor.schedule(new Runnable() {
                @Override
                public void run() {
                    alarmsLock.lock();
                    try {
                        nextAckDeadlineExtensionAlarmTime = new Instant(Long.MAX_VALUE);
                        ackDeadlineExtensionAlarm = null;
                        pendingAcksAlarm.cancel(false);
                        pendingAcksAlarm = null;
                    } finally {
                        alarmsLock.unlock();
                    }

                    Instant now = Instant.now();
                    // Rounded to the next second, so we only schedule future alarms at the second
                    // resolution.
                    Instant cutOverTime = new Instant(
                            ((long) Math.ceil(now.plus(ackExpirationPadding).plus(500).getMillis() / 1000.0))
                                    * 1000L);
                    logger.debug("Running alarm sent outstanding acks, at now time: {}, with cutover "
                            + "time: {}, padding: {}", now, cutOverTime, ackExpirationPadding);
                    ExpirationInfo nextScheduleExpiration = null;
                    List<PendingModifyAckDeadline> modifyAckDeadlinesToSend = new ArrayList<>();

                    synchronized (outstandingAckHandlers) {
                        Iterator<ExpirationInfo> expirationsIterator = outstandingAckHandlers.keySet()
                                .iterator();
                        while (expirationsIterator.hasNext() && nextScheduleExpiration == null) {
                            ExpirationInfo messageExpiration = expirationsIterator.next();
                            if (messageExpiration.expiration.compareTo(cutOverTime) <= 0) {
                                Collection<AckHandler> expiringAcks = outstandingAckHandlers
                                        .get(messageExpiration);
                                List<AckHandler> renewedAckHandlers = new ArrayList<>(expiringAcks.size());
                                Iterator<AckHandler> expiringAcksIterator = expiringAcks.iterator();
                                messageExpiration.extendExpiration();
                                int extensionSeconds = Ints
                                        .saturatedCast(new Interval(now, messageExpiration.expiration)
                                                .toDuration().getStandardSeconds());
                                while (expiringAcksIterator.hasNext()) {
                                    AckHandler ackHandler = expiringAcksIterator.next();
                                    if (ackHandler.acked.get()) {
                                        expiringAcksIterator.remove();
                                        continue;
                                    }
                                    modifyAckDeadlinesToSend.add(
                                            new PendingModifyAckDeadline(ackHandler.ackId, extensionSeconds));
                                    renewedAckHandlers.add(ackHandler);
                                }
                                if (!renewedAckHandlers.isEmpty()) {
                                    outstandingAckHandlers.putAll(messageExpiration, renewedAckHandlers);
                                }
                                expirationsIterator.remove();
                            } else {
                                nextScheduleExpiration = messageExpiration;
                                break;
                            }
                        }
                    }

                    sendOutstandingAckOperations(modifyAckDeadlinesToSend);

                    if (nextScheduleExpiration != null) {
                        logger.debug(
                                "Scheduling based on outstanding, now time: {}, " + "next schedule time: {}",
                                now, nextScheduleExpiration);
                        setupNextAckDeadlineExtensionAlarm(nextScheduleExpiration);
                    }
                }
            }, nextAckDeadlineExtensionAlarmTime.getMillis() - Instant.now().getMillis(),
                    TimeUnit.MILLISECONDS);
        }

    } finally {
        alarmsLock.unlock();
    }
}