Example usage for org.joda.time DateTime minus

List of usage examples for org.joda.time DateTime minus

Introduction

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

Prototype

public DateTime minus(ReadablePeriod period) 

Source Link

Document

Returns a copy of this datetime with the specified period taken away.

Usage

From source file:com.arpnetworking.tsdcore.tailer.FilePositionStore.java

License:Apache License

private void flush() {
    // Age out old state
    final DateTime now = DateTime.now();
    final DateTime oldest = now.minus(_retention);
    final long sizeBefore = _state.size();
    Maps.filterEntries(_state, new Predicate<Map.Entry<String, Descriptor>>() {
        @Override//from  w  ww  .j  a  v a 2  s.c  o  m
        public boolean apply(final Map.Entry<String, Descriptor> entry) {
            return oldest.isBefore(entry.getValue().getLastUpdated());
        }
    });
    final long sizeAfter = _state.size();
    if (sizeBefore != sizeAfter) {
        LOGGER.debug(String.format("Removed old entries from file position store; sizeBefore=%d, sizeAfter=%d",
                Long.valueOf(sizeBefore), Long.valueOf(sizeAfter)));
    }

    // Persist the state to disk
    try {
        OBJECT_MAPPER.writeValue(_file, _state);

        LOGGER.debug(String.format("Persisted file position state to disk; size=%d, file=%s",
                Long.valueOf(_state.size()), _file));
    } catch (final IOException ioe) {
        Throwables.propagate(ioe);
    } finally {
        _lastFlush = now;
    }
}

From source file:com.bjond.utilities.JodaTimeUtils.java

License:Open Source License

public static DateTime ago(DateTime anchor, int... valueUnitPairs) throws Exception {
    val period = toPeriod(valueUnitPairs);
    return anchor.minus(period);
}

From source file:com.daemon.Master.java

License:Open Source License

public void run() {
    // Connect to database
    _transactor.connect();/*from   ww  w . j  a va  2s  .co  m*/
    System.out.println(Localization.now() + " Connected to database.");

    // start sentiment regression updater
    sentimentClassifier.startRegressionSentimentUpdater();

    // Start the timer for the treasurer
    _treasurerTimer.start();

    // Indicates whether the master should sleep a while or not
    boolean shouldSleep = false;

    while (true) {
        try {
            if (shouldSleep) {
                Thread.sleep(_props.sleepDuration);
                shouldSleep = false;
            }
        } catch (InterruptedException ex) {
            _logger.log(Localization.now() + " Waiting interrupted.");
        }

        // Clear the rate limit of a Twitter profile if applicable
        DateTime now = new DateTime();
        for (TwitterProfile profile : _twitterProfiles) {
            // If the profile is already a fresh one, skip it
            if (profile.getLastUsed() == null) {
                continue;
            }

            // Reset rate limit and last start if cooldown is up
            if (now.minus(profile.getLastUsed().getMillis()).getMillis() >= _props.profileResetTime
                    .getMillis()) {
                profile.setLastUsed(null);
                profile.setUsedRateLimit(0);
            }
        }

        // Short terms are those terms, that will hopefully only fetch once, while
        // long terms are those that we expect to fetch multiply times until
        // the rate limit is used up
        List<SearchTerm> shortTerms = new LinkedList<SearchTerm>();
        List<SearchTerm> longTerms = new LinkedList<SearchTerm>();

        // Lock the access to get only actualized search terms
        _mutex.lock();

        try {
            // Get search terms
            List<SearchTerm> searchTerms = _transactor.getSearchTerms(true);
            _localSearchTerms = makeSearchTermsConsistent(_localSearchTerms, searchTerms, true);
        } catch (Exception ex) {
            _logger.log(
                    "An error occured while trying fetch the search terms. Consult Master.log for further information.");
            _logger.logStackTrace(ex);
        } finally {
            _mutex.unlock();
        }

        // Extract new search terms and start a special minion for those
        List<SearchTerm> newSearchTerms = getNewSearchTerms(_localSearchTerms);

        if (_props.spawnLimitedMinion && newSearchTerms.size() > 0) {
            startLimitedMinion(newSearchTerms);
        }

        // Fill short and long term list
        fillShortAndLongList(_localSearchTerms, shortTerms, longTerms, _props);

        // Sort the lists
        sortListByDateTime(shortTerms);
        sortListByDateTime(longTerms);

        // Now lock the mutex again
        _mutex.lock();

        // Do we need to spawn a new minion?
        // We do so, if we have any search term that is currently not in use AND
        // there are no too many treasurers working / waiting in the treasury.
        if ((shortTerms.size() > 0 || longTerms.size() > 0)
                && (_searchTermInUse.size() < searchableSearchTerms(_localSearchTerms).size())
                && _treasury.countTreasuresWaiting() < _props.maxTreasurers) {
            // Yes, so let's check, if we actually can spawn a new minion

            // Search for a valid profile and spawn a new minion using that
            // profile.
            // A profile is valid if its rate limit is not exceeded AND the
            // profile is not in use, yet
            TwitterProfile validProfile = null;
            for (int i = 0; (i < _twitterProfiles.length) && validProfile == null; ++i) {
                if (!_twitterProfiles[i].isInUse()
                        && _twitterProfiles[i].getUsedRateLimit() < _props.maxRateLimit) {
                    validProfile = _twitterProfiles[i];
                }
            }

            // If there is no valid profile currently, start new iteration
            if (validProfile == null) {
                // No valid profile found, so we go sleepy sleepy
                shouldSleep = true;

                // Release the lock
                _mutex.unlock();

                continue;
            }

            // This profile will be used and cannot be used by another minion
            validProfile.setIsInUse(true);

            // We now have a valid profile. Before spawning a new minion, we have to pick
            // the correct amount of short and long terms.
            List<SearchTerm> minionShortTerms = new LinkedList<SearchTerm>();
            List<SearchTerm> minionLongTerms = new LinkedList<SearchTerm>();

            // Short terms
            for (int i = 0, j = 0; j < _props.maxShortTermsCount && i < shortTerms.size(); ++i) {
                // If the current short term is already in use by another minion,
                // skip it
                try {
                    if (!ListUtil.contains(_searchTermInUse, shortTerms.get(i).getId())) {
                        minionShortTerms.add(shortTerms.get(i));
                        _searchTermInUse.add(shortTerms.get(i).getId());
                        ++j;
                    }
                } catch (Exception ex) {
                }
            }

            // Long terms
            for (int i = 0, j = 0; j < _props.maxLongTermsCount && i < longTerms.size(); ++i) {
                // If the current long term is already in use by another minion,
                // skip it
                try {
                    if (!ListUtil.contains(_searchTermInUse, longTerms.get(i).getId())) {
                        minionLongTerms.add(longTerms.get(i));
                        _searchTermInUse.add(longTerms.get(i).getId());
                        ++j;
                    }
                } catch (Exception ex) {
                }
            }

            // Create the list of search terms used by the minion to be spawned
            List<SearchTerm> minionSearchTerms = new LinkedList<SearchTerm>();
            for (SearchTerm shortTerm : minionShortTerms) {
                minionSearchTerms.add(shortTerm);
            }
            for (SearchTerm longTerm : minionLongTerms) {
                minionSearchTerms.add(longTerm);
            }

            // Fill up the long terms as that has not been done, yet
            minionLongTerms = fillUpList(minionLongTerms);

            // If this a reseted profile, set last used
            if (validProfile.getLastUsed() == null) {
                validProfile.setLastUsed(new DateTime());
            }

            // Actualize the profile's rate limit
            try {
                int rateLimit = validProfile.getSearchRateLimit();

                // Set the profile's rate limit and subtract the rate limit buffer, because Twitter
                // returns the actual rate limit without the buffer
                validProfile.setUsedRateLimit(_props.maxRateLimit - rateLimit);

                // Start the minion in a new thread
                System.out.println(Localization.now() + " Master spawns a new minion.");

                // Note: _minionPool might be null, if we have no profiles at all. BUT in that case
                // we would have exited the if-case long before this line, so there cannot not be thrown
                // a NullPointerException.
                _minionPool.execute(new MinionThread(this, validProfile, minionSearchTerms, minionShortTerms,
                        minionLongTerms));
            } catch (TwitterException ex) {
                // If there is something wrong with twitter, we are unable to do anything about it
                LogManager.getLogger("logs/Minions.log").logStackTrace(ex, validProfile.getScreenName());

                System.err.println(Localization.DATEANDTIME_FORMATTER.format(new Date()) + " "
                        + validProfile.getScreenName()
                        + " Error during communicating with Twitter. Consult 'logs/Minions.log' for further information.");
            }
        } else {
            // We have no search terms to be searched left, so go to sleep
            shouldSleep = true;
        }

        // Unlock as we are done updating
        _mutex.unlock();
    }
}

From source file:com.daemon.Master.java

License:Open Source License

/**
 * Creates a copy of terms and adds search terms at the end of the copy according to their
 * expiration date and expiration factor.
 * @param terms The list of search terms to be processed.
 * @return Returns the processed list as a copy.
 *//*from  w ww  . jav a  2s  .c o  m*/
public List<SearchTerm> fillUpList(List<SearchTerm> terms) {
    List<SearchTerm> filledList = new LinkedList<SearchTerm>();

    // Copy all terms into the list
    for (SearchTerm term : terms) {
        filledList.add(term);
    }

    DateTime now = new DateTime();

    // Add terms at the end of the list according to the expiration factor
    for (SearchTerm term : terms) {
        int i;
        boolean isYounger = true;

        // Test for expiration beginning with the oldest date and moving to the youngest
        for (i = _props.expirationDurations.length - 1; i >= 0 && isYounger; i--) {
            if (now.minus(term.getWhenCreated().getMillis()).getMillis() >= _props.expirationDurations[i]
                    .getMillis()) {
                isYounger = false;
            }
        }

        // If isYounger is still true, that means that the search term is younger than
        // _expirationDurations[0] (minimal expiration date), so we have to decrease i one
        // more time, because the for loop does not account for that.
        if (isYounger) {
            i--;
        }

        // Add terms at the end of the list according to the factor
        // We add 1 to i, because the upper for loop decreases i always by 1 too much (because
        // we do not break, but wait for the condition to fail.
        // We also add 1, because the array _expirationFactor has one more entry
        // than the _expirationDurations array. So we add 2 to i in total.
        for (int j = 0; j < _props.expirationFactor[i + 2] - 1; j++) {
            filledList.add(term);
        }
    }

    return filledList;
}

From source file:com.daemon.Minion.java

License:Open Source License

/**
 * Tests whether the date of the tweet is older than the given old starting date.
 * @param tweetDate The date of the tweet to be tested.
 * @param oldStart The date to be checked against.
 * @return True, if the tweet date is older than the date of old start.
 *//*from   w  w w  .  j  ava2  s . co  m*/
public static boolean tweetIsTooOld(DateTime tweetDate, DateTime oldStart) {
    return tweetDate.minus(oldStart.getMillis()).getMillis() < 0;
}

From source file:com.ebay.pulsar.analytics.datasource.PulsarDataSourceRoutingStrategy.java

License:MIT License

@Override
public String getDataSourceName(String tableName, DateRange range, String molapName, String rtolapName) {
    PulsarDataSourceRule rule = configuration.get(tableName);
    if (!Strings.isNullOrEmpty(molapName) && rule != null) {
        Period period = PulsarDateTimeFormatter.ISO_PERIOD_FORMATTER.parsePeriod(rule.getPeriod());
        DateTime start = range.getStart();
        DateTime now = new DateTime(PulsarDateTimeFormatter.MST_TIMEZONE);
        DateTime closeDate = now.minus(period);
        //MOLAP//from   w  ww .  ja va 2 s.co m
        if (start.isBefore(closeDate)) {
            return molapName;
        } else {
            return rtolapName;
        }
    } else
        return rtolapName;
}

From source file:com.enonic.cms.business.portal.datasource.expressionfunctions.ExpressionFunctions.java

License:Open Source License

public String currentDateMinusOffset(String format, String periodStr) {
    DateTime nowDateTime = timeService.getNowAsDateTime();
    PeriodFormatter periodFormatter = ISOPeriodFormat.standard();
    Period period = periodFormatter.parsePeriod(periodStr);
    DateTime offsetDateTime = nowDateTime.minus(period);

    SimpleDateFormat fmt = new SimpleDateFormat(format);
    return fmt.format(offsetDateTime.toDate());
}

From source file:com.facebook.presto.execution.SqlTaskManager.java

License:Apache License

public void failAbandonedTasks() {
    DateTime now = DateTime.now();
    DateTime oldestAllowedHeartbeat = now.minus(clientTimeout.toMillis());
    for (TaskExecution taskExecution : tasks.values()) {
        try {/* ww w .  j  av  a2s. co m*/
            TaskInfo taskInfo = taskExecution.getTaskInfo();
            if (taskInfo.getState().isDone()) {
                continue;
            }
            DateTime lastHeartbeat = taskInfo.getLastHeartbeat();
            if (lastHeartbeat != null && lastHeartbeat.isBefore(oldestAllowedHeartbeat)) {
                log.info("Failing abandoned task %s", taskExecution.getTaskId());
                taskExecution.fail(new AbandonedException("Task " + taskInfo.getTaskId(), lastHeartbeat, now));

                // trigger caching
                getTaskInfo(taskExecution);
            }
        } catch (RuntimeException e) {
            log.warn(e, "Error while inspecting age of task %s", taskExecution.getTaskId());
        }
    }
}

From source file:com.facebook.util.TimeInterval.java

License:Apache License

/**
 * Subtracts the supplied multiples of this interval from the supplied instant. If the
 * TimeInterval is {@link #INFINITE} the epoch in the timezone of {@code instant} is returned
 *
 * @param instant the instant to subtract from
 * @param multiple the multiple value//from w w  w. ja v a 2  s .  c  o  m
 * @throws IllegalArgumentException if multiple is less than one.
 */
public DateTime minus(DateTime instant, int multiple) {
    if (this == INFINITE) {
        throw new IllegalStateException("minus() function is not supported on an infinite TimeInterval");
    } else if (this == ZERO) {
        return instant;
    }

    validateMultiple(multiple);

    if (type == null) {
        return instant.minus(multiple * getLength());
    } else {
        return instant.minus(type.toPeriod(FieldUtils.safeMultiplyToInt(multiple, getLength())));
    }
}

From source file:com.grepcurl.random.BaseGenerator.java

License:Apache License

public Date randomDate(String fromDate, String toDate) {
    if (tryOdds(DEFAULT_CHANCE_OF_NULL_DATE)) {
        return null;
    }/* w w  w.  j a va  2 s  . c o m*/
    Validate.notNull(fromDate);
    Validate.notNull(toDate);
    long lowerBound = _dateTimeFormatter.parseDateTime(fromDate).getMillis();
    DateTime upperBoundDateTime = _dateTimeFormatter.parseDateTime(toDate);
    long upperBound = upperBoundDateTime.getMillis();
    long range = upperBound - lowerBound + 1;
    return upperBoundDateTime.minus(randomLong(0, range)).toDate();
}