From 5fcaca8b547222922a6b6e83bb9cbb0a6114b286 Mon Sep 17 00:00:00 2001 From: Serraniel Date: Mon, 28 Dec 2020 16:35:51 +0100 Subject: [PATCH] #147 optimization for 24h format --- src/javascript/enhancements/timeConversion.js | 40 +++++++++++++++++-- 1 file changed, 37 insertions(+), 3 deletions(-) diff --git a/src/javascript/enhancements/timeConversion.js b/src/javascript/enhancements/timeConversion.js index d0506b9..578038e 100644 --- a/src/javascript/enhancements/timeConversion.js +++ b/src/javascript/enhancements/timeConversion.js @@ -17,25 +17,59 @@ export function init() { }); } +function getSpaceTimeFormat(use24Format) { + if (use24Format) { + return '{date}. {month-short} {year} {time-24}'; + } + + return '{date}. {month-short} {year} {time}'; +} + function updateTimestamps() { let nodes = helper.findTextNodes(); nodes.forEach(node => { const REG_DATETIME = /(\d{2}(\/|\.)){2}\d{4} *\d?\d:\d{2}( (AM|PM))?/g; + const REG_TIME = /\d?\d:\d{2}/; + const REG_AMPM = /\s(am|pm)/i; + let hits = Array.from(node.textContent.matchAll(REG_DATETIME), match => match[0]); hits.forEach(hit => { + let use24Format = false; + let processedStr = hit + + // string must be converted into 12h format + if (processedStr.search(REG_AMPM) < 0) { + let timeStr = processedStr.match(REG_TIME)[0]; + let hm = timeStr.split(':'); + let hour = parseInt(hm[0]); + + if (hour >= 12) { + timeStr = timeStr.replace(`${hour}:`, `${hour - 12}:`); + timeStr += 'pm'; + } + else { + timeStr += 'am'; + } + + console.log(timeStr) + + processedStr = processedStr.replace(REG_TIME, timeStr); + use24Format = true; + } + // if time has a space before am/pm, this has to be removed for spacetime - let processedStr = hit.replace(/\s(am|pm)/i, '$1'); + processedStr = processedStr.replace(REG_AMPM, '$1'); console.log(processedStr); let datetime = spacetime(processedStr, 'UTC+1', { dmy: true }); datetime = datetime.goto(spacetime().tz); - let replaceText = datetime.format('{date}. {month-short} {year} {time}'); + let replaceText = datetime.format(getSpaceTimeFormat(use24Format)); console.log(replaceText); console.log('-------') - // node.textContent = node.textContent.replace(hit, replaceText); + node.textContent = node.textContent.replace(hit, replaceText); }) }); } \ No newline at end of file