Reorganise colouring of schedule times, add 'delay' column (E, RT, D). Including reusable function to assist.
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
import type { ApiTrainsTrainDetails } from '@owlboard/owlboard-ts';
|
||||
/**
|
||||
* Converts ISO/JSON time to UK-formatted HH:MM string.
|
||||
* Ensures Europe/London timezone irrespective of browser timezone.
|
||||
@@ -15,3 +16,34 @@ export function formatUkTime(dateStr: string | Date | undefined): string {
|
||||
timeZone: 'Europe/London'
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates a 'delay' value, in the formats:
|
||||
* RT, 1E, 7L, etc.
|
||||
* @param 'Schedule Location' object
|
||||
* @returns Delay string for departure boards
|
||||
*/
|
||||
export function calculateDelay(loc: ApiTrainsTrainDetails.ServiceLocation): {val: string, type: string} {
|
||||
const pairs = [
|
||||
{ actual: loc.atd, sched: loc.ptd ?? loc.wtd },
|
||||
{ actual: loc.ata, sched: loc.pta ?? loc.wta },
|
||||
{ actual: loc.atp, sched: loc.wtp }
|
||||
];
|
||||
|
||||
const match = pairs.find(p => p.actual && p.sched);
|
||||
|
||||
if (!match || !match.actual || !match.sched) return {val: '', type: 'none'};
|
||||
|
||||
const diffMinutes = Math.round(
|
||||
(Date.parse(match.actual) - Date.parse(match.sched)) / 60000
|
||||
);
|
||||
|
||||
if (diffMinutes === 0) return {val: 'RT', type: 'ontime'};
|
||||
|
||||
const absDiff = Math.abs(diffMinutes);
|
||||
if (diffMinutes > 0) {
|
||||
return { val: `${absDiff}L`, type: 'late' };
|
||||
} else {
|
||||
return { val: `${absDiff}E`, type: 'early' };
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user