diff --git a/lib/actions/api.js b/lib/actions/api.js index ef4b6ce7b..3498c8461 100644 --- a/lib/actions/api.js +++ b/lib/actions/api.js @@ -477,6 +477,15 @@ export function findStopTimesForStop (params) { let { stopId, ...otherParams } = params // If other params not provided, fall back on defaults from stop viewer config. const queryParams = { ...getStopViewerConfig(getState().otp), ...otherParams } + // If no start time is provided, pass in the current time. Note: this is not + // a required param by the back end, but if a value is not provided, the + // time defaults to the server's time, which can make it difficult to test + // scenarios when you may want to use a different date/time for your local + // testing environment. + if (!queryParams.startTime) { + const nowInSeconds = Math.floor((new Date()).getTime() / 1000) + queryParams.startTime = nowInSeconds + } dispatch(createQueryAction( `index/stops/${stopId}/stoptimes?${qs.stringify(queryParams)}`, findStopTimesForStopResponse, diff --git a/lib/components/viewers/stop-viewer.js b/lib/components/viewers/stop-viewer.js index e4816278b..6d237bb24 100644 --- a/lib/components/viewers/stop-viewer.js +++ b/lib/components/viewers/stop-viewer.js @@ -398,6 +398,7 @@ class PatternRow extends Component { } const ONE_HOUR_IN_SECONDS = 3600 +const ONE_DAY_IN_SECONDS = 86400 /** * @param {string} homeTimezone Timezone string @@ -423,19 +424,23 @@ function getFormattedStopTime (stopTime, homeTimezone, soonText = 'Due', timeFor const now = moment().tz(homeTimezone) const serviceDay = moment(stopTime.serviceDay * 1000).tz(homeTimezone) - const currentHomeSecondsAfterMidnight = getHomeSecondsAfterMidnight(homeTimezone) - const differentDay = now.date() !== serviceDay.date() - + const arrivesAfterMidnight = stopTime.realtimeDeparture >= ONE_DAY_IN_SECONDS + // Calculate current home seconds after midnight. If arrival occurs after + // midnight, adjust the value by one day in seconds. + const currentHomeSecondsAfterMidnight = getHomeSecondsAfterMidnight(homeTimezone) + (arrivesAfterMidnight ? ONE_DAY_IN_SECONDS : 0) + // Determine if arrival occurs on different day, making sure to account for an + // extra day added to the service day if it arrives after midnight. + const arrivalDay = arrivesAfterMidnight + ? serviceDay.add(1, 'day') + : serviceDay + const differentDay = now.dayOfYear() !== arrivalDay.dayOfYear() // Determine whether to show departure as countdown (e.g. "5 min") or as HH:mm time const secondsUntilDeparture = stopTime.realtimeDeparture - currentHomeSecondsAfterMidnight - const departsInFuture = stopTime.realtimeDeparture > currentHomeSecondsAfterMidnight - // Show the exact time if the departure occurs after midnight and if the - // departure happens within an hour. - // FIXME: It's unclear why this was designed to show exact time after midnight. - // We should determine whether this is the desired behavior. - const showCountdown = !differentDay && - secondsUntilDeparture < ONE_HOUR_IN_SECONDS && - departsInFuture + // Determine if vehicle arrives after midnight in order to advance the day of + // the week when showing arrival time/day. + const departsInFuture = secondsUntilDeparture > 0 + // Show the exact time if the departure if the departure happens within an hour. + const showCountdown = secondsUntilDeparture < ONE_HOUR_IN_SECONDS && departsInFuture // Use "soon text" (e.g., Due) if vehicle is approaching. const countdownString = secondsUntilDeparture < 60 @@ -447,6 +452,11 @@ function getFormattedStopTime (stopTime, homeTimezone, soonText = 'Due', timeFor // in New York, but viewing a trip planner for service based in Los Angeles). inHomeTimezone ? timeFormat : `${timeFormat} z` ) + // We only want to show the day of the week if the arrival is on a + // different day and we're not showing the countdown string. This avoids + // cases such as when it's Wednesday at 11:55pm and an arrival occurs at + // Thursday 12:19am. We don't want the time to read: 'Thursday, 24 minutes'. + const showDayOfWeek = differentDay && !showCountdown return (
@@ -459,9 +469,9 @@ function getFormattedStopTime (stopTime, homeTimezone, soonText = 'Due', timeFor style={{ color: '#888', fontSize: '0.8em', marginRight: 2 }} /> }
-
- {differentDay && -
{serviceDay.format('dddd')}
+
+ {showDayOfWeek && +
{arrivalDay.format('dddd')}
}
{showCountdown