Example usage for org.joda.time DateTime minusDays

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

Introduction

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

Prototype

public DateTime minusDays(int days) 

Source Link

Document

Returns a copy of this datetime minus the specified number of days.

Usage

From source file:updaters.FeedStatsCalculator.java

License:Open Source License

/**
 * Apply not just the feed stats, but also the 
 */// www .j a v a 2s  .c o m

private void calculateStartAndEnd() throws Exception {
    // First, read feed_info.txt
    // TODO is 1 ever not the correct value?
    FeedInfo feedInfo = store.getFeedInfoForId(1);
    if (feedInfo != null) {
        ServiceDate d;
        d = feedInfo.getStartDate();
        if (d != null) {
            Calendar c = d.getAsCalendar(timezone);
            // move to GTFS noon, which will always be during the day. This accounts for both
            // multitimezone feeds and for daylight savings time 
            c.add(Calendar.HOUR_OF_DAY, 12);
            startDate = c.getTime();
        }

        d = feedInfo.getEndDate();
        if (d != null) {
            Calendar c = d.getAsCalendar(timezone);
            c.add(Calendar.HOUR_OF_DAY, 12);
            endDate = c.getTime();
        }
    }

    // we have an authoritative answer
    if (startDate != null && endDate != null)
        return;

    // let OBA deal with the complexities of interactions between
    // calendar.txt and
    // calendar_dates.txt
    // This code is lifted and slightly modified from
    // https://github.com/demory/otp_gtfs/blob/master/java/gtfsmetrics/src/main/java/org/openplans/gtfsmetrics/CalendarStatus.java
    Map<AgencyAndId, Set<ServiceDate>> addExceptions = new HashMap<AgencyAndId, Set<ServiceDate>>();
    Map<AgencyAndId, Set<String>> removeExceptions = new HashMap<AgencyAndId, Set<String>>();
    for (ServiceCalendarDate date : store.getAllCalendarDates()) {
        if (date.getExceptionType() == ServiceCalendarDate.EXCEPTION_TYPE_ADD) {
            Set<ServiceDate> dateSet = addExceptions.get(date.getServiceId());
            if (dateSet == null) {
                dateSet = new HashSet<ServiceDate>();
                addExceptions.put(date.getServiceId(), dateSet);
            }
            dateSet.add(date.getDate());
        } else if (date.getExceptionType() == ServiceCalendarDate.EXCEPTION_TYPE_REMOVE) {
            Set<String> dateSet = removeExceptions.get(date.getServiceId());
            if (dateSet == null) {
                dateSet = new HashSet<String>();
                removeExceptions.put(date.getServiceId(), dateSet);
            }
            dateSet.add(constructMDYString(date.getDate()));
        }
    }

    DateTime latestEnd = new DateTime(0);
    DateTime earliestStart = null;

    for (ServiceCalendar svcCal : store.getAllCalendars()) {

        Calendar c;
        c = svcCal.getStartDate().getAsCalendar(timezone);
        c.add(Calendar.HOUR_OF_DAY, 12);
        DateTime start = new DateTime(c.getTime());

        c = svcCal.getEndDate().getAsCalendar(timezone);
        c.add(Calendar.HOUR_OF_DAY, 12);
        DateTime end = new DateTime(c.getTime());

        int totalDays = Days.daysBetween(start, end).getDays();
        for (int d = 0; d < totalDays; d++) {
            int gd = getDay(svcCal, end.dayOfWeek().get());// dateCal.get(Calendar.DAY_OF_WEEK));
            boolean removeException = false;
            Set<String> dateSet = removeExceptions.get(svcCal.getServiceId());
            if (dateSet != null) {
                removeException = dateSet.contains(constructMDYString(end));
            }
            if (gd == 1 && !removeException)
                break;
            end = end.minusDays(1);
        }
        if (end.isAfter(latestEnd))
            latestEnd = end;

        totalDays = Days.daysBetween(start, end).getDays();
        for (int d = 0; d < totalDays; d++) {
            int gd = getDay(svcCal, start.dayOfWeek().get());// dateCal.get(Calendar.DAY_OF_WEEK));
            boolean removeException = false;
            Set<String> dateSet = removeExceptions.get(svcCal.getServiceId());
            if (dateSet != null) {
                removeException = dateSet.contains(constructMDYString(start));
            }
            if (gd == 1 && !removeException)
                break;
            start = start.plusDays(1);
        }
        if (earliestStart == null || start.isBefore(earliestStart))
            earliestStart = start;

    }

    // now, expand based on calendar_dates.txt
    for (Set<ServiceDate> dateSet : addExceptions.values()) {
        for (ServiceDate sd : dateSet) {
            DateTime dt = new DateTime(sd.getAsDate(timezone).getTime());
            if (dt.isAfter(latestEnd))
                latestEnd = dt;
            if (dt.isBefore(earliestStart))
                earliestStart = dt;
        }
    }

    this.startDate = earliestStart.toDate();
    this.endDate = latestEnd.toDate();
}