use of org.onebusaway.gtfs.model.calendar.ServiceDate in project onebusaway-gtfs-modules by OneBusAway.
the class ShiftNegativeStopTimesUpdateStrategyTest method test.
@Test
public void test() throws IOException {
_gtfs.putAgencies(1);
_gtfs.putStops(3);
_gtfs.putRoutes(1);
_gtfs.putCalendars(1, "start_date=20120903", "end_date=20121016", "mask=1111100");
_gtfs.putTrips(1, "r0", "sid0");
_gtfs.putLines("stop_times.txt", "trip_id,stop_id,stop_sequence,arrival_time,departure_time", "t0,s0,0,-01:00:00,-01:05:00", "t0,s1,1,-01:30:00,-01:30:00", "t0,s2,2,00:30:00,00:30:00");
GtfsMutableRelationalDao dao = _gtfs.read();
_strategy.run(new TransformContext(), dao);
Trip trip = dao.getTripForId(_gtfs.id("t0"));
assertEquals("sid0 -1", trip.getServiceId().getId());
List<StopTime> stopTimes = dao.getStopTimesForTrip(trip);
assertEquals(3, stopTimes.size());
{
StopTime stopTime = stopTimes.get(0);
assertEquals(stopTime.getArrivalTime(), StopTimeFieldMappingFactory.getStringAsSeconds("23:00:00"));
assertEquals(stopTime.getDepartureTime(), StopTimeFieldMappingFactory.getStringAsSeconds("23:05:00"));
}
{
StopTime stopTime = stopTimes.get(1);
assertEquals(stopTime.getArrivalTime(), StopTimeFieldMappingFactory.getStringAsSeconds("23:30:00"));
assertEquals(stopTime.getDepartureTime(), StopTimeFieldMappingFactory.getStringAsSeconds("23:30:00"));
}
{
StopTime stopTime = stopTimes.get(2);
assertEquals(stopTime.getArrivalTime(), StopTimeFieldMappingFactory.getStringAsSeconds("24:30:00"));
assertEquals(stopTime.getDepartureTime(), StopTimeFieldMappingFactory.getStringAsSeconds("24:30:00"));
}
ServiceCalendar c = dao.getCalendarForServiceId(trip.getServiceId());
assertEquals(c.getStartDate(), new ServiceDate(2012, 9, 2));
assertEquals(c.getEndDate(), new ServiceDate(2012, 10, 15));
assertEquals(1, c.getMonday());
assertEquals(1, c.getTuesday());
assertEquals(1, c.getWednesday());
assertEquals(1, c.getThursday());
assertEquals(0, c.getFriday());
assertEquals(0, c.getSaturday());
assertEquals(1, c.getSunday());
}
use of org.onebusaway.gtfs.model.calendar.ServiceDate in project onebusaway-gtfs-modules by OneBusAway.
the class CalendarServiceDataFactoryImpl method createData.
@Override
public CalendarServiceData createData() {
CalendarServiceData data = new CalendarServiceData();
setTimeZonesForAgencies(data);
List<AgencyAndId> serviceIds = _dao.getAllServiceIds();
int index = 0;
for (AgencyAndId serviceId : serviceIds) {
index++;
_log.info("serviceId=" + serviceId + " (" + index + "/" + serviceIds.size() + ")");
TimeZone serviceIdTimeZone = data.getTimeZoneForAgencyId(serviceId.getAgencyId());
if (serviceIdTimeZone == null) {
serviceIdTimeZone = TimeZone.getDefault();
}
Set<ServiceDate> activeDates = getServiceDatesForServiceId(serviceId, serviceIdTimeZone);
List<ServiceDate> serviceDates = new ArrayList<ServiceDate>(activeDates);
Collections.sort(serviceDates);
data.putServiceDatesForServiceId(serviceId, serviceDates);
List<String> tripAgencyIds = _dao.getTripAgencyIdsReferencingServiceId(serviceId);
Set<TimeZone> timeZones = new HashSet<TimeZone>();
for (String tripAgencyId : tripAgencyIds) {
TimeZone timeZone = data.getTimeZoneForAgencyId(tripAgencyId);
if (timeZone == null) {
timeZone = serviceIdTimeZone;
}
timeZones.add(timeZone);
}
for (TimeZone timeZone : timeZones) {
List<Date> dates = new ArrayList<Date>(serviceDates.size());
for (ServiceDate serviceDate : serviceDates) try {
if (timeZone == null)
timeZone = TimeZone.getDefault();
dates.add(serviceDate.getAsDate(timeZone));
} catch (Exception any) {
_log.error(" calendar load failed for " + serviceDate + " for serviceId " + serviceId);
}
LocalizedServiceId id = new LocalizedServiceId(serviceId, timeZone);
data.putDatesForLocalizedServiceId(id, dates);
}
}
return data;
}
use of org.onebusaway.gtfs.model.calendar.ServiceDate in project onebusaway-gtfs-modules by OneBusAway.
the class CalendarServiceImpl method search.
private int search(List<Date> serviceDates, ServiceIdOp op, int indexFrom, int indexTo, Date key) {
if (indexTo == indexFrom)
return indexFrom;
int index = (indexFrom + indexTo) / 2;
Date serviceDate = op.getServiceDate(serviceDates, index);
int rc = op.compare(key, serviceDate);
if (rc == 0)
return index;
if (rc < 0)
return search(serviceDates, op, indexFrom, index, key);
else
return search(serviceDates, op, index + 1, indexTo, key);
}
use of org.onebusaway.gtfs.model.calendar.ServiceDate in project onebusaway-gtfs-modules by OneBusAway.
the class CalendarServiceImpl method getServiceDates.
private List<Date> getServiceDates(CalendarServiceData allData, LocalizedServiceId serviceId, ServiceInterval interval, ServiceIdOp op, Date from, Date to, boolean includeNextDateIfNeeded) {
List<Date> serviceDates = allData.getDatesForLocalizedServiceId(serviceId);
List<Date> resultsForServiceId = new ArrayList<Date>();
Date nextDate = null;
if (serviceDates == null)
return resultsForServiceId;
Date target = op.shiftTime(interval, from);
int index = search(serviceDates, op, 0, serviceDates.size(), target);
if (index == serviceDates.size())
index--;
while (0 <= index) {
Date serviceDate = op.getServiceDate(serviceDates, index);
int rc = op.compareInterval(interval, serviceDate, from, to);
if (rc > 0) {
nextDate = serviceDate;
} else if (rc == 0) {
resultsForServiceId.add(serviceDate);
} else if (rc < 0) {
break;
}
index--;
}
if (includeNextDateIfNeeded && resultsForServiceId.isEmpty() && nextDate != null)
resultsForServiceId.add(nextDate);
return resultsForServiceId;
}
use of org.onebusaway.gtfs.model.calendar.ServiceDate in project onebusaway-gtfs-modules by OneBusAway.
the class CalendarServiceImpl method getServiceDatesForServiceId.
@Override
public Set<ServiceDate> getServiceDatesForServiceId(AgencyAndId serviceId) {
Set<ServiceDate> dates = new HashSet<ServiceDate>();
CalendarServiceData allData = getData();
List<ServiceDate> serviceDates = allData.getServiceDatesForServiceId(serviceId);
if (serviceDates != null)
dates.addAll(serviceDates);
return dates;
}
Aggregations