Skip to content

Commit

Permalink
fix(stop-viewer): fix service day for arrival times after midnight
Browse files Browse the repository at this point in the history
  • Loading branch information
landonreed committed Aug 8, 2019
1 parent 1b32d03 commit 18edf52
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 14 deletions.
9 changes: 9 additions & 0 deletions lib/actions/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
38 changes: 24 additions & 14 deletions lib/components/viewers/stop-viewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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 (
<div>
<div style={{ float: 'left' }}>
Expand All @@ -459,9 +469,9 @@ function getFormattedStopTime (stopTime, homeTimezone, soonText = 'Due', timeFor
style={{ color: '#888', fontSize: '0.8em', marginRight: 2 }} />
}
</div>
<div style={{ marginLeft: 20, fontSize: differentDay ? 12 : 14 }}>
{differentDay &&
<div style={{ marginBottom: -4 }}>{serviceDay.format('dddd')}</div>
<div style={{ marginLeft: 20, fontSize: showDayOfWeek ? 12 : 14 }}>
{showDayOfWeek &&
<div style={{ marginBottom: -4 }}>{arrivalDay.format('dddd')}</div>
}
<div>
{showCountdown
Expand Down

0 comments on commit 18edf52

Please sign in to comment.