#147 Update dates

This commit is contained in:
Serraniel 2020-12-28 16:48:51 +01:00
parent c76aebda94
commit 139e1e0cff
Signed by: Serraniel
GPG key ID: 3690B4E7364525D3

View file

@ -21,7 +21,7 @@ export function init() {
}); });
} }
function getSpaceTimeFormat(use24Format) { function getSpaceDateTimeFormat(use24Format) {
if (use24Format) { if (use24Format) {
return '{date}. {month-short} {year} {time-24}'; return '{date}. {month-short} {year} {time-24}';
} }
@ -29,16 +29,21 @@ function getSpaceTimeFormat(use24Format) {
return '{date}. {month-short} {year} {time}'; return '{date}. {month-short} {year} {time}';
} }
function updateTimestamps(node) { function getSpaceDateFormat() {
let nodes = helper.findTextNodes(node); return '{date}. {month-short} {year}';
}
nodes.forEach(node => { function updateDateTime(node) {
const REG_DATETIME = /(\d{2}(\/|\.)){2}\d{4} *\d?\d:\d{2}( (AM|PM))?/g; const REG_DATETIME = /(\d{2}(\/|\.)){2}\d{4} *\d?\d:\d{2}( (AM|PM))?/g;
const REG_TIME = /\d?\d:\d{2}/; const REG_TIME = /\d?\d:\d{2}/;
const REG_AMPM = /\s(am|pm)/i; const REG_AMPM = /\s(am|pm)/i;
let hits = Array.from(node.textContent.matchAll(REG_DATETIME), match => match[0]); let hits = Array.from(node.textContent.matchAll(REG_DATETIME), match => match[0]);
if (hits.length === 0) {
return false;
}
hits.forEach(hit => { hits.forEach(hit => {
let use24Format = false; let use24Format = false;
let processedStr = hit let processedStr = hit
@ -66,9 +71,44 @@ function updateTimestamps(node) {
let datetime = spacetime(processedStr, 'UTC+1', { dmy: true }); let datetime = spacetime(processedStr, 'UTC+1', { dmy: true });
datetime = datetime.goto(spacetime().tz); datetime = datetime.goto(spacetime().tz);
let replaceText = datetime.format(getSpaceTimeFormat(use24Format)); let replaceText = datetime.format(getSpaceDateTimeFormat(use24Format));
node.textContent = node.textContent.replace(hit, replaceText); node.textContent = node.textContent.replace(hit, replaceText);
}) });
return true;
}
function updateDate(node) {
const REG_DATE = /(\d{2}(\/|\.)){2}\d{4}/g;
let hits = Array.from(node.textContent.matchAll(REG_DATE), match => match[0]);
if (hits.length === 0) {
return false;
}
hits.forEach(hit => {
let datetime = spacetime(hit, 'UTC+1', { dmy: true });
datetime = datetime.goto(spacetime().tz);
let replaceText = datetime.format(getSpaceDateFormat());
node.textContent = node.textContent.replace(hit, replaceText);
});
return true;
}
function updateTimestamps(node) {
let nodes = helper.findTextNodes(node);
nodes.forEach(node => {
if (updateDateTime(node)) {
return;
}
if (updateDate(node)) {
return;
}
}); });
} }