From a28c6fdb03098e0cd87fb97c6ac6903efdd8d9fb Mon Sep 17 00:00:00 2001 From: Stefan Dej Date: Sun, 1 Oct 2023 09:54:22 +0200 Subject: [PATCH] feat: add 12-hour time format in printers overview (#1571) --- src/store/farm/printer/getters.ts | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/src/store/farm/printer/getters.ts b/src/store/farm/printer/getters.ts index d87d44b64..2b135b1a6 100644 --- a/src/store/farm/printer/getters.ts +++ b/src/store/farm/printer/getters.ts @@ -166,7 +166,7 @@ export const getters: GetterTree = { return [] }, - getPrinterPreview: (state, getters) => { + getPrinterPreview: (state, getters, rootState, rootGetters) => { if (!state.server.klippy_connected) return [] const output = [] @@ -215,19 +215,28 @@ export const getters: GetterTree = { }) } - if ( - 'print_stats' in state.data && - 'state' in state.data.print_stats && - state.data.print_stats.state === 'printing' && - getters['getPrintPercent'] > 0 - ) { + if ((state.data?.print_stats?.state ?? '') === 'printing' && getters['getPrintPercent'] > 0) { + const hours12Format = rootGetters['gui/getHours12Format'] ?? false const eta = new Date(getters.estimated_time_eta) - const h = eta.getHours() >= 10 ? eta.getHours() : '0' + eta.getHours() - const m = eta.getMinutes() >= 10 ? eta.getMinutes() : '0' + eta.getMinutes() + + const date = new Date(eta) + let am = true + let h: string | number = date.getHours() + + if (hours12Format && h > 11) am = false + if (hours12Format && h > 12) h -= 12 + if (h < 10) h = '0' + h + + const m = date.getMinutes() >= 10 ? date.getMinutes() : '0' + date.getMinutes() + + const diff = date.getTime() - new Date().getTime() + let outputOutput = h + ':' + m + if (hours12Format) outputOutput += ` ${am ? 'AM' : 'PM'}` + if (diff > 60 * 60 * 24 * 1000) outputOutput += `+${Math.trunc(diff / (60 * 60 * 24 * 1000))}` output.push({ name: 'ETA', - value: getters.estimated_time_eta > 0 ? h + ':' + m : '--', + value: getters.estimated_time_eta > 0 ? outputOutput : '--', file: getters.estimated_time_file, filament: getters.estimated_time_filament, slicer: getters.estimated_time_slicer,