private bool IsTimeInRange(ref TripTimesModel trip, DateTime timeStart, DateTime timeEnd) { DateTime tempEnd = timeEnd; bool multiDays = false; DateTime secBeforeMidnight = timeStart.Date.Add(new TimeSpan(23, 59, 59)); if (timeEnd > secBeforeMidnight) { multiDays = true; tempEnd = secBeforeMidnight; } if (trip.departure_time >= timeStart.TimeOfDay && trip.departure_time <= tempEnd.TimeOfDay) { return(true); } if (multiDays) { if (trip.departure_time <= timeEnd.TimeOfDay) { trip.departure_time = trip.departure_time.Add(new TimeSpan(1, 0, 0, 0)); return(true); } } return(false); }
protected List <TripTimesModel> GetTripsFromStopId(OpenArchive gtfsArchive, int stopId, DateTime timeStart, DateTime timeEnd) { if (timeStart > timeEnd) { throw new ArgumentException("The end time must be greater than or equal to the start time"); } else if (timeEnd.Subtract(timeStart).TotalDays > 1.0) { throw new ArgumentException("The start and end dates must be less than 1 day appart"); } if (this.timeStringList == null) { this.timeStringList = new List <StringEntry>(); using (Stream timeStream = gtfsArchive.zip.GetEntry("google_transit/stop_times.csv").Open()) { StreamReader timeReader = new StreamReader(timeStream); //skip header timeReader.ReadLine(); while (timeReader.EndOfStream == false) { string timeLine = timeReader.ReadLine(); this.timeStringList.Add(new StringEntry(timeLine)); } } } //binary search this.timeStringList for the valid entries StringEntry stopKey = new StringEntry(stopId.ToString()); int index = this.timeStringList.BinarySearch(stopKey); if (index < 0 || index > this.timeStringList.Count()) { throw new KeyNotFoundException("Failed to find a entry for the stop id:" + stopId.ToString() + " in stop_times.csv"); } //find the start of this section while (this.timeStringList[index - 1].CompareTo(stopKey) == 0) { --index; } List <TripTimesModel> tripsAtStop = new List <TripTimesModel>(); //populate the return entries while (this.timeStringList[index].CompareTo(stopKey) == 0) { TripTimesModel tripTime = new TripTimesModel(); string entry = this.timeStringList[index].Entry; string[] entries = entry.Split(','); tripTime.stop_id = int.Parse(entries[0]); tripTime.departure_time = GetTimeSpanFromString(entries[1]); tripTime.trip_id = int.Parse(entries[2]); bool isInRange = IsTimeInRange(ref tripTime, timeStart, timeEnd); if (isInRange) { IsTimeInRange(ref tripTime, timeStart, timeEnd); tripsAtStop.Add(tripTime); } ++index; } return(tripsAtStop); }