fix for bad format in old data master
authorMauro Scomparin <scompo@gmail.com>
Thu, 7 Jan 2021 20:06:53 +0000 (21:06 +0100)
committerMauro Scomparin <scompo@gmail.com>
Thu, 7 Jan 2021 20:06:53 +0000 (21:06 +0100)
utils/fix-bad-dates.js [new file with mode: 0644]

diff --git a/utils/fix-bad-dates.js b/utils/fix-bad-dates.js
new file mode 100644 (file)
index 0000000..26705c3
--- /dev/null
@@ -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()