From 95eca901644a7177b826019aed48175a3f28c0dd Mon Sep 17 00:00:00 2001 From: Mauro Scomparin Date: Thu, 7 Jan 2021 21:06:53 +0100 Subject: [PATCH] fix for bad format in old data --- utils/fix-bad-dates.js | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 utils/fix-bad-dates.js diff --git a/utils/fix-bad-dates.js b/utils/fix-bad-dates.js new file mode 100644 index 0000000..26705c3 --- /dev/null +++ b/utils/fix-bad-dates.js @@ -0,0 +1,42 @@ +const fs = require('fs') + +function badDate(line) { + // -20.00;2016/8/27;;old stadio + const tokens = line.split(';') + const date = tokens[1] + const dateTokens = date.split('/') + return dateTokens[2].length !== 4 +} + +function fixDate(line) { + // -20.00;2016/8/27;;old stadio + const tokens = line.split(';') + const date = tokens[1] + const dateTokens = date.split('/') + const dateOk = dateTokens[2].padStart(2, '0') + '/' + dateTokens[1].padStart(2, '0') + '/' + dateTokens[0].padStart(4, '0') + const output = tokens[0] + ';' + dateOk + ';' + tokens[2] + ';' + tokens[3] + return output +} + +async function main() { + const inputFile = process.argv.slice(2)[0] + fs.readFile(inputFile, 'utf-8', (err, data) => { + if(err){ + console.error(err) + return err + } + const lines = data.split(/\r?\n/) + for(line of lines) { + if(line) { + const dateIsBad = badDate(line) + if(badDate(line)) { + console.log(fixDate(line)) + }else{ + console.log(line) + } + } + } + }) +} + +main() -- 2.25.1