Skip to content

Commit 18edf52

Browse files
committed
fix(stop-viewer): fix service day for arrival times after midnight
fix ibi-group/trimet-mod-otp#235
1 parent 1b32d03 commit 18edf52

File tree

2 files changed

+33
-14
lines changed

2 files changed

+33
-14
lines changed

lib/actions/api.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,15 @@ export function findStopTimesForStop (params) {
477477
let { stopId, ...otherParams } = params
478478
// If other params not provided, fall back on defaults from stop viewer config.
479479
const queryParams = { ...getStopViewerConfig(getState().otp), ...otherParams }
480+
// If no start time is provided, pass in the current time. Note: this is not
481+
// a required param by the back end, but if a value is not provided, the
482+
// time defaults to the server's time, which can make it difficult to test
483+
// scenarios when you may want to use a different date/time for your local
484+
// testing environment.
485+
if (!queryParams.startTime) {
486+
const nowInSeconds = Math.floor((new Date()).getTime() / 1000)
487+
queryParams.startTime = nowInSeconds
488+
}
480489
dispatch(createQueryAction(
481490
`index/stops/${stopId}/stoptimes?${qs.stringify(queryParams)}`,
482491
findStopTimesForStopResponse,

lib/components/viewers/stop-viewer.js

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,7 @@ class PatternRow extends Component {
398398
}
399399

400400
const ONE_HOUR_IN_SECONDS = 3600
401+
const ONE_DAY_IN_SECONDS = 86400
401402

402403
/**
403404
* @param {string} homeTimezone Timezone string
@@ -423,19 +424,23 @@ function getFormattedStopTime (stopTime, homeTimezone, soonText = 'Due', timeFor
423424

424425
const now = moment().tz(homeTimezone)
425426
const serviceDay = moment(stopTime.serviceDay * 1000).tz(homeTimezone)
426-
const currentHomeSecondsAfterMidnight = getHomeSecondsAfterMidnight(homeTimezone)
427-
const differentDay = now.date() !== serviceDay.date()
428-
427+
const arrivesAfterMidnight = stopTime.realtimeDeparture >= ONE_DAY_IN_SECONDS
428+
// Calculate current home seconds after midnight. If arrival occurs after
429+
// midnight, adjust the value by one day in seconds.
430+
const currentHomeSecondsAfterMidnight = getHomeSecondsAfterMidnight(homeTimezone) + (arrivesAfterMidnight ? ONE_DAY_IN_SECONDS : 0)
431+
// Determine if arrival occurs on different day, making sure to account for an
432+
// extra day added to the service day if it arrives after midnight.
433+
const arrivalDay = arrivesAfterMidnight
434+
? serviceDay.add(1, 'day')
435+
: serviceDay
436+
const differentDay = now.dayOfYear() !== arrivalDay.dayOfYear()
429437
// Determine whether to show departure as countdown (e.g. "5 min") or as HH:mm time
430438
const secondsUntilDeparture = stopTime.realtimeDeparture - currentHomeSecondsAfterMidnight
431-
const departsInFuture = stopTime.realtimeDeparture > currentHomeSecondsAfterMidnight
432-
// Show the exact time if the departure occurs after midnight and if the
433-
// departure happens within an hour.
434-
// FIXME: It's unclear why this was designed to show exact time after midnight.
435-
// We should determine whether this is the desired behavior.
436-
const showCountdown = !differentDay &&
437-
secondsUntilDeparture < ONE_HOUR_IN_SECONDS &&
438-
departsInFuture
439+
// Determine if vehicle arrives after midnight in order to advance the day of
440+
// the week when showing arrival time/day.
441+
const departsInFuture = secondsUntilDeparture > 0
442+
// Show the exact time if the departure if the departure happens within an hour.
443+
const showCountdown = secondsUntilDeparture < ONE_HOUR_IN_SECONDS && departsInFuture
439444

440445
// Use "soon text" (e.g., Due) if vehicle is approaching.
441446
const countdownString = secondsUntilDeparture < 60
@@ -447,6 +452,11 @@ function getFormattedStopTime (stopTime, homeTimezone, soonText = 'Due', timeFor
447452
// in New York, but viewing a trip planner for service based in Los Angeles).
448453
inHomeTimezone ? timeFormat : `${timeFormat} z`
449454
)
455+
// We only want to show the day of the week if the arrival is on a
456+
// different day and we're not showing the countdown string. This avoids
457+
// cases such as when it's Wednesday at 11:55pm and an arrival occurs at
458+
// Thursday 12:19am. We don't want the time to read: 'Thursday, 24 minutes'.
459+
const showDayOfWeek = differentDay && !showCountdown
450460
return (
451461
<div>
452462
<div style={{ float: 'left' }}>
@@ -459,9 +469,9 @@ function getFormattedStopTime (stopTime, homeTimezone, soonText = 'Due', timeFor
459469
style={{ color: '#888', fontSize: '0.8em', marginRight: 2 }} />
460470
}
461471
</div>
462-
<div style={{ marginLeft: 20, fontSize: differentDay ? 12 : 14 }}>
463-
{differentDay &&
464-
<div style={{ marginBottom: -4 }}>{serviceDay.format('dddd')}</div>
472+
<div style={{ marginLeft: 20, fontSize: showDayOfWeek ? 12 : 14 }}>
473+
{showDayOfWeek &&
474+
<div style={{ marginBottom: -4 }}>{arrivalDay.format('dddd')}</div>
465475
}
466476
<div>
467477
{showCountdown

0 commit comments

Comments
 (0)