Skip to content

Commit 0bc3a65

Browse files
committed
fix: correct timezone discrepancies and time formatting in stop&trip viewers
1 parent ce1559c commit 0bc3a65

File tree

3 files changed

+41
-23
lines changed

3 files changed

+41
-23
lines changed

lib/components/viewers/stop-viewer.js

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -403,21 +403,30 @@ function getHomeTime (homeTimezone = 'America/New_York') {
403403
return now.tz(homeTimezone).diff(now.clone().startOf('day'), 'seconds')
404404
}
405405

406-
// helper method to generate stop time w/ status icon
406+
/**
407+
* Helper method to generate stop time w/ status icon
408+
*
409+
* @param {object} stopTime A stopTime object as received from a transit index API
410+
* @param {string} [homeTimezone] If configured, the timezone of the area
411+
* @param {string} [soonText='Due'] The text to display for departure times
412+
* about to depart in a short amount of time
413+
* @param {string} timeFormat A valid moment.js formatting string
414+
*/
407415
function getFormattedStopTime (stopTime, homeTimezone, soonText = 'Due', timeFormat) {
408-
const now = moment()
409-
const serviceDay = moment(stopTime.serviceDay * 1000)
410-
const currentHomeTime = getHomeTime(homeTimezone)
411-
const differentDay = now.date() !== serviceDay.date()
412-
413416
// use a bit of hackery to force a specific timezone to be used during testing
414417
const userTimeZone = process.env.NODE_ENV === 'test'
415418
? process.env.TZ
416419
: moment.tz.guess()
417420
const inHomeTimezone = homeTimezone && homeTimezone === userTimeZone
421+
422+
const now = moment().tz(userTimeZone)
423+
const serviceDay = moment(stopTime.serviceDay * 1000).tz(userTimeZone)
424+
const currentHomeSecondsAfterMidnight = getHomeTime(homeTimezone)
425+
const differentDay = now.date() !== serviceDay.date()
426+
418427
// Determine whether to show departure as countdown (e.g. "5 min") or as HH:mm time
419-
const secondsUntilDeparture = stopTime.realtimeDeparture - currentHomeTime
420-
const departsInFuture = stopTime.realtimeDeparture > currentHomeTime
428+
const secondsUntilDeparture = stopTime.realtimeDeparture - currentHomeSecondsAfterMidnight
429+
const departsInFuture = stopTime.realtimeDeparture > currentHomeSecondsAfterMidnight
421430
// Show the exact time if the departure occurs after midnight and if the
422431
// departure happens within an hour.
423432
// FIXME: It's unclear why this was designed to show exact time after midnight.
@@ -430,10 +439,13 @@ function getFormattedStopTime (stopTime, homeTimezone, soonText = 'Due', timeFor
430439
const countdownString = secondsUntilDeparture < 60
431440
? soonText
432441
: formatDuration(secondsUntilDeparture)
433-
// Only show timezone (e.g., PDT) if user is not in home time zone (e.g., user
434-
// in New York, but viewing a trip planner for service based in Los Angeles).
435-
const tzToDisplay = inHomeTimezone ? null : homeTimezone
436-
const formattedTime = formatStopTime(stopTime.realtimeDeparture, tzToDisplay, timeFormat)
442+
const formattedTime = formatStopTime(
443+
stopTime.realtimeDeparture,
444+
// Only show timezone (e.g., PDT) if user is not in home time zone (e.g., user
445+
// in New York, but viewing a trip planner for service based in Los Angeles).
446+
inHomeTimezone ? timeFormat : `${timeFormat} z`,
447+
homeTimezone
448+
)
437449
return (
438450
<div>
439451
<div style={{ float: 'left' }}>

lib/components/viewers/trip-viewer.js

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { setViewedTrip } from '../../actions/ui'
1010
import { findTrip } from '../../actions/api'
1111
import { setLocation } from '../../actions/map'
1212

13-
import { formatStopTime } from '../../util/time'
13+
import { formatStopTime, getTimeFormat } from '../../util/time'
1414

1515
class TripViewer extends Component {
1616
static propTypes = {
@@ -28,7 +28,13 @@ class TripViewer extends Component {
2828
}
2929

3030
render () {
31-
const { viewedTrip, tripData, hideBackButton, languageConfig } = this.props
31+
const {
32+
hideBackButton,
33+
languageConfig,
34+
timeFormat,
35+
tripData,
36+
viewedTrip
37+
} = this.props
3238

3339
return (
3440
<div className='trip-viewer'>
@@ -93,7 +99,7 @@ class TripViewer extends Component {
9399
<div key={i}>
94100
{/* the departure time */}
95101
<div className='stop-time'>
96-
{formatStopTime(tripData.stopTimes[i].scheduledDeparture)}
102+
{formatStopTime(tripData.stopTimes[i].scheduledDeparture, timeFormat)}
97103
</div>
98104

99105
{/* the vertical strip map */}
@@ -128,9 +134,10 @@ class TripViewer extends Component {
128134
const mapStateToProps = (state, ownProps) => {
129135
const viewedTrip = state.otp.ui.viewedTrip
130136
return {
137+
languageConfig: state.otp.config.language,
138+
timeFormat: getTimeFormat(state.otp.config),
131139
tripData: state.otp.transitIndex.trips[viewedTrip.tripId],
132-
viewedTrip,
133-
languageConfig: state.otp.config.language
140+
viewedTrip
134141
}
135142
}
136143

lib/util/time.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,14 @@ export function formatTime (ms, options) {
5252

5353
/**
5454
* Formats a stop time value for display in narrative
55-
* @param {[type]} seconds time since midnight in seconds
56-
* @param {[type]} [timezone=null] optional time zone to include in result
57-
* @param {string} [timeFormat='h:mm a'] time format
55+
* @param {number} seconds time since midnight in seconds
56+
* @param {string} timeFormat A valid moment.js time format
57+
* @param {string} [timezone=null] optional time zone to initialize moment with
5858
* @return {string} formatted text representation
5959
*/
60-
export function formatStopTime (seconds, timezone = null, timeFormat = 'h:mm a') {
60+
export function formatStopTime (seconds, timeFormat, timezone = null) {
6161
const m = timezone ? moment().tz(timezone) : moment()
62-
const format = timezone ? `${timeFormat} z` : timeFormat
63-
return m.startOf('day').seconds(seconds).format(format)
62+
return m.startOf('day').seconds(seconds).format(timeFormat)
6463
}
6564

6665
/**

0 commit comments

Comments
 (0)