Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 7 additions & 14 deletions src/main/java/org/mtransit/parser/DefaultAgencyTools.java
Original file line number Diff line number Diff line change
Expand Up @@ -129,12 +129,6 @@ public class DefaultAgencyTools implements GAgencyTools {
OVERRIDE_DATE = null; // yyyyMMdd
}

private static final boolean TOMORROW;

static {
TOMORROW = false;
}

private static final DateFormat DATE_FORMAT = GFieldTypes.makeDateFormat();

@Nullable
Expand All @@ -143,6 +137,11 @@ public class DefaultAgencyTools implements GAgencyTools {
@SuppressWarnings("WeakerAccess")
public int getTodayDateInt() {
if (todayDateInt == null) {
//noinspection ConstantValue
if (OVERRIDE_DATE != null) {
todayDateInt = OVERRIDE_DATE;
return todayDateInt;
}
todayDateInt = Integer.parseInt(DATE_FORMAT.format(Calendar.getInstance().getTime()));
}
return todayDateInt;
Expand Down Expand Up @@ -1742,14 +1741,8 @@ public static HashSet<Integer> extractUsefulServiceIdInts(
boolean isCurrent = "current_".equalsIgnoreCase(args[2]);
boolean isNext = "next_".equalsIgnoreCase(args[2]);
boolean isCurrentOrNext = isCurrent || isNext;
Calendar c = Calendar.getInstance();
if (!isCurrentOrNext && TOMORROW) {
c.add(Calendar.DAY_OF_MONTH, 1); // TOMORROW (too late to publish today's schedule)
}
usefulPeriod.setTodayStringInt(Integer.valueOf(DATE_FORMAT.format(c.getTime())));
if (!isCurrentOrNext && OVERRIDE_DATE != null) {
usefulPeriod.setTodayStringInt(OVERRIDE_DATE);
}
final Calendar c = Calendar.getInstance();
usefulPeriod.setTodayStringInt(agencyTools.getTodayDateInt());
GSpec gtfs = GReader.readGtfsZipFile(args[0], agencyTools, !agencyFilter, agencyFilter);
MDataChangedManager.avoidCalendarDatesDataChanged(lastServiceDates, gtfs, agencyTools);
if (agencyFilter) {
Expand Down
33 changes: 24 additions & 9 deletions src/main/java/org/mtransit/parser/gtfs/GReader.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package org.mtransit.parser.gtfs;

import static org.mtransit.commons.Constants.EMPTY;
import static org.mtransit.parser.gtfs.data.GSpecExtKt.getCalendarsMaxEndDate;
import static org.mtransit.parser.gtfs.data.GSpecExtKt.getCalendarsMinStartDate;
import static org.mtransit.parser.gtfs.data.GSpecExtKt.isInsideGCalendars;

import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVParser;
Expand Down Expand Up @@ -90,14 +93,16 @@ public static GSpec readGtfsZipFile(
processAgency(agencyTools, gSpec, line)
);
}
// CALENDAR DATES (-> non-excluded service IDs)
boolean hasCalendarDates = readFile(gtfsDir, GCalendarDate.FILENAME, false, line ->
processCalendarDate(agencyTools, gSpec, line)
);
// CALENDAR (-> non-excluded service IDs)
// CALENDAR (-> non-excluded service IDs) (before calendar dates)
boolean hasCalendars = readFile(gtfsDir, GCalendar.FILENAME, false, line ->
processCalendar(agencyTools, gSpec, line)
);
final @Nullable Integer calendarsMinStartDate = getCalendarsMinStartDate(gSpec);
final @Nullable Integer calendarsMaxEndDate = getCalendarsMaxEndDate(gSpec);
// CALENDAR DATES (-> non-excluded service IDs) (after calendars)
boolean hasCalendarDates = readFile(gtfsDir, GCalendarDate.FILENAME, false, line ->
processCalendarDate(agencyTools, gSpec, line, calendarsMinStartDate, calendarsMaxEndDate)
);
boolean hasCalendar = hasCalendarDates || hasCalendars;
if (!hasCalendar) {
throw new MTLog.Fatal("'%s' & '%s' file do not exist!", GCalendar.FILENAME, GCalendarDate.FILENAME);
Expand Down Expand Up @@ -431,18 +436,28 @@ private static void processAgency(GAgencyTools agencyTools, GSpec gSpec, HashMap
DateUtils.getEndOfYear(DateUtils.addYears(new Date(), 3)) // 3 years // else local DB slow to deploy
));

private static void processCalendarDate(GAgencyTools agencyTools, GSpec gSpec, HashMap<String, String> line) {
private static void processCalendarDate(
GAgencyTools agencyTools,
GSpec gSpec,
HashMap<String, String> line,
@Nullable Integer calendarsMinStartDate,
@Nullable Integer calendarsMaxEndDate
) {
try {
final GCalendarDate gCalendarDate = GCalendarDate.fromLine(line);
if (gCalendarDate == null) {
MTLog.log("Empty calendar dates ignored (%s).", line);
MTLog.log("Empty calendar date ignored (%s).", line);
return;
}
if (Boolean.FALSE.equals(isInsideGCalendars(gSpec, gCalendarDate, () -> calendarsMinStartDate, () -> calendarsMaxEndDate))) {
MTLog.logDebug("Out of calendar coverage calendar date ignored (%s).", line);
return;
}
if (gCalendarDate.isBefore(MIN_CALENDAR_DATE)) {
MTLog.log("Too old calendar dates ignored (%s).", line);
MTLog.log("Too old calendar date ignored (%s).", line);
return;
} else if (gCalendarDate.isAfter(MAX_CALENDAR_DATE)) {
MTLog.log("Too much in the future calendar dates ignored (%s).", line);
MTLog.log("Too much in the future calendar date ignored (%s).", line);
return;
}
if (agencyTools.excludeCalendarDate(gCalendarDate)) {
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/org/mtransit/parser/gtfs/data/GSpecExt.kt
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
package org.mtransit.parser.gtfs.data

fun GSpec.getRoute(gTrip: GTrip) = this.getRoute(gTrip.routeIdInt)

val GSpec.calendarsMinStartDate: Int? get() = this.allCalendars.takeIf { it.isNotEmpty() }?.minByOrNull { it.startDate }?.startDate
val GSpec.calendarsMaxEndDate: Int? get() = this.allCalendars.takeIf { it.isNotEmpty() }?.maxByOrNull { it.endDate }?.endDate

fun GSpec.isInsideGCalendars(
gCalendarDate: GCalendarDate,
calendarsMinStartDate: () -> Int? = this::calendarsMinStartDate,
calendarsMaxEndDate: () -> Int? = this::calendarsMaxEndDate
): Boolean? {
val calendarsMinStartDate = calendarsMinStartDate() ?: return null // no calendars (only calendar dates inside GTFS)
val calendarsMaxEndDate = calendarsMaxEndDate() ?: return null // no calendars (only calendar dates inside GTFS)
return gCalendarDate.isBetween(calendarsMinStartDate, calendarsMaxEndDate)
}