@@ -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+ */
407415function 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' } } >
0 commit comments