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
18 changes: 10 additions & 8 deletions src/main/java/org/mtransit/parser/DefaultAgencyTools.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
import org.mtransit.parser.mt.data.MTripIds;
import org.mtransit.parser.mt.data.MVerify;

import java.text.SimpleDateFormat;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
Expand Down Expand Up @@ -127,7 +127,7 @@ public class DefaultAgencyTools implements GAgencyTools {
TOMORROW = false;
}

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

@SuppressWarnings({"unused", "WeakerAccess"})
public int getTodayDateInt() {
Expand All @@ -142,6 +142,7 @@ public static void main(@NotNull String[] args) {
@Nullable
private HashSet<Integer> serviceIdInts;

@SuppressWarnings("WeakerAccess")
public void start(@NotNull String[] args) {
if (args.length < 3) {
throw new MTLog.Fatal("Invalid number(%d) of arguments! (%s)", args.length, Arrays.asList(args));
Expand Down Expand Up @@ -1223,7 +1224,7 @@ public int getThreadPoolSize() {
@Override
public Pair<Integer, Integer> getTimes(@NotNull GStopTime gStopTime,
@NotNull List<GStopTime> tripStopTimes,
@NotNull SimpleDateFormat timeFormat) {
@NotNull DateFormat timeFormat) {
if (!gStopTime.hasArrivalTime() || !gStopTime.hasDepartureTime()) {
return extractTimes(gStopTime, tripStopTimes, timeFormat);
} else {
Expand All @@ -1236,7 +1237,7 @@ public Pair<Integer, Integer> getTimes(@NotNull GStopTime gStopTime,
@NotNull
private static Pair<Integer, Integer> extractTimes(GStopTime gStopTime,
@NotNull List<GStopTime> tripStopTimes,
SimpleDateFormat timeFormat) {
DateFormat timeFormat) {
try {
Pair<Long, Long> timesInMs = extractTimeInMs(gStopTime, tripStopTimes);
long arrivalTimeInMs = timesInMs.first;
Expand Down Expand Up @@ -1650,7 +1651,7 @@ && diffLowerThan(DATE_FORMAT, c, p.getTodayStringInt(), initialTodayStringInt, M
return todayServiceIds;
}

static void parseCalendars(@NotNull List<GCalendar> gCalendars, @Nullable List<GCalendarDate> gCalendarDates, SimpleDateFormat DATE_FORMAT, Calendar c, Period p, boolean lookBackward) {
static void parseCalendars(@NotNull List<GCalendar> gCalendars, @Nullable List<GCalendarDate> gCalendarDates, DateFormat DATE_FORMAT, Calendar c, Period p, boolean lookBackward) {
findCalendarsTodayPeriod(gCalendars, gCalendarDates, p, lookBackward);
if (p.getStartDate() == null || p.getEndDate() == null) {
MTLog.log("[parse-calendars] > NO schedule available for %s in calendars. (start:%s|end:%s)", p.getTodayStringInt(), p.getStartDate(), p.getEndDate());
Expand Down Expand Up @@ -1910,7 +1911,7 @@ private static Period getEntirePeriodMinMaxDate(List<GCalendar> gCalendars, List
return p;
}

public static int incDateDays(@NotNull SimpleDateFormat dateFormat,
public static int incDateDays(@NotNull DateFormat dateFormat,
@NotNull Calendar calendar,
@Nullable Integer dateInt,
int numberOfDays) {
Expand All @@ -1923,7 +1924,8 @@ public static int incDateDays(@NotNull SimpleDateFormat dateFormat,
}
}

public static boolean diffLowerThan(@NotNull SimpleDateFormat dateFormat,
@SuppressWarnings("WeakerAccess")
public static boolean diffLowerThan(@NotNull DateFormat dateFormat,
@NotNull Calendar calendar,
@Nullable Integer startDateInt,
@Nullable Integer endDateInt,
Expand All @@ -1937,7 +1939,7 @@ public static boolean diffLowerThan(@NotNull SimpleDateFormat dateFormat,

@SuppressWarnings("WeakerAccess")
public static long diffInMs(
@NotNull SimpleDateFormat dateFormat,
@NotNull DateFormat dateFormat,
@NotNull Calendar calendar,
@Nullable Integer startDateInt,
@Nullable Integer endDateInt
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/org/mtransit/parser/gtfs/GAgencyTools.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@
import org.mtransit.parser.gtfs.data.GStopTime;
import org.mtransit.parser.gtfs.data.GTrip;
import org.mtransit.parser.mt.data.MAgency;
import org.mtransit.parser.mt.data.MDirection;
import org.mtransit.parser.mt.data.MDirectionCardinalType;
import org.mtransit.parser.mt.data.MRoute;
import org.mtransit.parser.mt.data.MDirection;

import java.text.SimpleDateFormat;
import java.text.DateFormat;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
Expand Down Expand Up @@ -319,7 +319,7 @@ public interface GAgencyTools {
@NotNull
Pair<Integer, Integer> getTimes(@NotNull GStopTime gStopTime,
@NotNull List<GStopTime> tripStopTimes,
@NotNull SimpleDateFormat timeFormat);
@NotNull DateFormat timeFormat);

// FREQUENCY
int getStartTime(@NotNull GFrequency gFrequency);
Expand Down
7 changes: 0 additions & 7 deletions src/main/java/org/mtransit/parser/gtfs/data/GCalendar.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package org.mtransit.parser.gtfs.data

import androidx.annotation.Discouraged
import org.mtransit.parser.MTLog
import org.mtransit.parser.db.SQLUtils.escapeId
import java.util.Calendar

// https://developers.google.com/transit/gtfs/reference#calendar_fields
Expand Down Expand Up @@ -74,12 +73,6 @@ data class GCalendar(
private val _serviceId: String
get() = GIDs.getString(serviceIdInt)

val escapedServiceId: String
get() = _serviceId.escapeId()

val escapedServiceIdInt: Int
get() = escapedServiceId.toGIDInt()

val dates: List<GCalendarDate> by lazy {
initAllDates(
serviceIdInt,
Expand Down
69 changes: 18 additions & 51 deletions src/main/java/org/mtransit/parser/gtfs/data/GFieldTypes.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.mtransit.parser.gtfs.data

import org.mtransit.commons.Constants.SPACE_
import java.text.DateFormat
import java.text.SimpleDateFormat
import java.util.Calendar
import java.util.Date
Expand All @@ -16,82 +17,48 @@ object GFieldTypes {

const val DATE_FORMAT_PATTERN = "yyyyMMdd"

@Deprecated(message = "NOT thread-safe", replaceWith = ReplaceWith("GFieldTypes.makeTimeFormat()"))
@JvmField
val TIME_FORMAT = makeTimeFormat()

@Deprecated(message = "NOT thread-safe", replaceWith = ReplaceWith("GFieldTypes.makeDateFormat()"))
@JvmField
val DATE_FORMAT = makeDateFormat()

@Deprecated(message = "NOT thread-safe", replaceWith = ReplaceWith("GFieldTypes.makeDateAndTimeFormat()"))
@JvmField
val DATE_TIME_FORMAT = makeDateAndTimeFormat()

@JvmStatic
fun makeTimeFormat(): SimpleDateFormat {
return SimpleDateFormat(TIME_FORMAT_PATTERN, Locale.ENGLISH)
}
fun makeTimeFormat(): DateFormat =
SimpleDateFormat(TIME_FORMAT_PATTERN, Locale.ENGLISH)

@JvmStatic
fun makeDateFormat(): SimpleDateFormat {
return SimpleDateFormat(DATE_FORMAT_PATTERN, Locale.ENGLISH)
}
fun makeDateFormat(): DateFormat =
SimpleDateFormat(DATE_FORMAT_PATTERN, Locale.ENGLISH)

@JvmStatic
fun makeDateAndTimeFormat(): SimpleDateFormat {
return SimpleDateFormat(DATE_FORMAT_PATTERN + SPACE_ + TIME_FORMAT_PATTERN, Locale.ENGLISH)
}
fun makeDateAndTimeFormat(): DateFormat =
SimpleDateFormat(DATE_FORMAT_PATTERN + SPACE_ + TIME_FORMAT_PATTERN, Locale.ENGLISH)

@Suppress("DEPRECATION")
@JvmOverloads
@JvmStatic
fun toDate(dateFormat: SimpleDateFormat = DATE_FORMAT, gDateInt: Int): Date = toDate(dateFormat, gDateInt.toString())
fun toDate(dateFormat: DateFormat, gDateInt: Int): Date = toDate(dateFormat, gDateInt.toString())

@Suppress("DEPRECATION")
@JvmOverloads
@JvmStatic
fun toDate(dateFormat: SimpleDateFormat = DATE_FORMAT, gDateString: String): Date {
return dateFormat.parse(gDateString)
}
fun toDate(dateFormat: DateFormat, gDateString: String): Date =
dateFormat.parse(gDateString)

@Suppress("DEPRECATION")
@JvmOverloads
@JvmStatic
fun fromDate(dateFormat: SimpleDateFormat = DATE_FORMAT, calendar: Calendar): String = fromDate(dateFormat, calendar.time)
fun fromDate(dateFormat: DateFormat, calendar: Calendar): String = fromDate(dateFormat, calendar.time)

@Suppress("DEPRECATION")
@JvmOverloads
@JvmStatic
fun fromDate(dateFormat: SimpleDateFormat = DATE_FORMAT, gDateString: Date): String {
return dateFormat.format(gDateString)
}
fun fromDate(dateFormat: DateFormat, gDateString: Date): String =
dateFormat.format(gDateString)

@Suppress("DEPRECATION")
@JvmOverloads
@JvmStatic
fun fromDateToInt(dateFormat: SimpleDateFormat = DATE_FORMAT, gDateString: Date) = fromDate(dateFormat, gDateString).toInt()
fun fromDateToInt(dateFormat: DateFormat, gDateString: Date) = fromDate(dateFormat, gDateString).toInt()

fun cleanTime(gTimeString: String) = gTimeString.padStart(6, '0') // "%06d".format(Locale.ENGLISH, gTimeString) NOT working??

@Suppress("DEPRECATION")
@JvmOverloads
@JvmStatic
fun toTimeStamp(dateTimeFormat: SimpleDateFormat = DATE_TIME_FORMAT, gDateInt: Int, gTimeInt: Int) =
fun toTimeStamp(dateTimeFormat: DateFormat, gDateInt: Int, gTimeInt: Int) =
toDate(dateTimeFormat, gDateInt, gTimeInt).time

@Suppress("DEPRECATION")
@JvmOverloads
@JvmStatic
fun toDate(dateTimeFormat: SimpleDateFormat = DATE_TIME_FORMAT, gDateInt: Int, gTimeInt: Int) =
fun toDate(dateTimeFormat: DateFormat, gDateInt: Int, gTimeInt: Int) =
toDate(dateTimeFormat, gDateInt.toString(), gTimeInt.toString())

@Suppress("DEPRECATION")
@JvmOverloads
@JvmStatic
fun toDate(dateTimeFormat: SimpleDateFormat = DATE_TIME_FORMAT, gDateString: String, gTimeString: String): Date {
return dateTimeFormat.parse(gDateString + SPACE_ + cleanTime(gTimeString))
}
fun toDate(dateTimeFormat: DateFormat, gDateString: String, gTimeString: String): Date =
dateTimeFormat.parse(gDateString + SPACE_ + cleanTime(gTimeString))

fun Int.isBefore(date: Int?): Boolean {
date ?: return false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
import org.mtransit.parser.mt.data.MStop;
import org.mtransit.parser.mt.data.MTrip;

import java.text.SimpleDateFormat;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
Expand Down Expand Up @@ -192,7 +192,7 @@ private MSpec doCall() {
long lastTimestamp = -1L;
if (!mServiceDatesList.isEmpty()) {
MServiceDate firstServiceDate = mServiceDatesList.get(0);
final int maxCalendarDate = GFieldTypes.fromDateToInt(new Date()) + 10 * 10_000; // max 10 years IN THE FUTURE
final int maxCalendarDate = GFieldTypes.fromDateToInt(DATE_FORMAT, new Date()) + 10 * 10_000; // max 10 years IN THE FUTURE
mServiceDatesList.removeIf(serviceDate ->
serviceDate.getCalendarDate() > maxCalendarDate
);
Expand Down Expand Up @@ -249,7 +249,6 @@ private MSpec doCall() {
&& (lastDeparture < -1 || lastDeparture < lastFrequency.getEndTime())) {
lastDeparture = lastFrequency.getEndTime();
}
final SimpleDateFormat DATE_TIME_FORMAT = GFieldTypes.makeDateAndTimeFormat();
DATE_TIME_FORMAT.setTimeZone(TimeZone.getTimeZone(mAgenciesList.get(0).getTimezone()));
try {
firstTimestamp = GFieldTypes.toTimeStamp(DATE_TIME_FORMAT, firstCalendarDate, firstDeparture);
Expand Down Expand Up @@ -727,7 +726,9 @@ private HashMap<Long, String> parseGTripStops(HashMap<String, MSchedule> mSchedu
return splitDirectionStopTimesHeadSign;
}

private final SimpleDateFormat TIME_FORMAT = GFieldTypes.makeTimeFormat();
private final DateFormat TIME_FORMAT = GFieldTypes.makeTimeFormat();
private final DateFormat DATE_TIME_FORMAT = GFieldTypes.makeDateAndTimeFormat();
private final DateFormat DATE_FORMAT = GFieldTypes.makeDateFormat();

private String parseGStopTimes(HashMap<String, MSchedule> mSchedules,
long mDirectionId,
Expand Down
Loading
Loading