Example usage for org.apache.commons.lang.time DateUtils getFragmentInMinutes

List of usage examples for org.apache.commons.lang.time DateUtils getFragmentInMinutes

Introduction

In this page you can find the example usage for org.apache.commons.lang.time DateUtils getFragmentInMinutes.

Prototype

public static long getFragmentInMinutes(Calendar calendar, int fragment) 

Source Link

Document

Returns the number of minutes within the fragment.

Usage

From source file:net.audumla.irrigation.EToCalculator.java

public double calculateETo(Date now) {
    // refresh if we are in a different day or an elapse time has passed
    if (!DateUtils.isSameDay(lastCalculated, now)
            || (DateUtils.getFragmentInMinutes(lastCalculated, Calendar.DAY_OF_YEAR)
                    - DateUtils.getFragmentInMinutes(now, Calendar.DAY_OF_YEAR)) > minuteTimeout) {
        double debt = 0.0;
        for (int i = 0; i < calculationDurationInDays; ++i) {
            try {
                // add up all the ETo values over the past days.
                // the current day may use forecast information
                ClimateData data = zone.getClimateObserver().getClimateData(now);
                try {
                    double eto = data.getEvapotranspiration();
                    debt += eto;/*  ww w .ja v a 2s. c om*/
                } catch (UnsupportedOperationException ex) {
                    // no ETo information available
                }

                try {
                    double rain = data.getRainfall();
                    // if the rainfall value is a forecast then we revert to the latest observation and use the value
                    // that is the rainfall since 9am.
                    if (!data.getDataSource().getType()
                            .equals(ClimateDataSource.ClimateDataSourceType.DAILY_FORECAST)) {
                        debt -= rain;
                    } else {
                        debt -= data.getObservation(now, ClimateData.ObservationMatch.CLOSEST).getRainfall();
                    }
                } catch (UnsupportedOperationException ex) {
                    // no rain information available
                }
                // add all the irrigation events for this day against the water debt
                List<Event> events = zone.getIrrigationEventsForDay(now);
                for (Event e : events) {
                    if (e.getEventStartTime().before(now)) {
                        debt -= IrrigationZone.calculateIrrigatedDepth(zone, e.getEventDuration());
                    }
                }
            } catch (UnsupportedOperationException ex) {
                logger.warn("Unable to obtain climate data for " + Time.dateFormatter.format(now), ex);
            } finally {
                now = DateUtils.addDays(now, -1);
            }
        }
        currentETo = debt;
        lastCalculated = now;
    }
    return currentETo;

}