CorpGlory / csv2influx

A CLI tool for importing CSV files into Influxdb
MIT License
16 stars 6 forks source link

Column Merge #48

Closed silentApplause closed 6 years ago

silentApplause commented 6 years ago

I have two column Date and Time, Date looks like 20180101 and Time looks like 12:01:01, how can I merge these two column into timestamps?

silentApplause commented 6 years ago

I have wrote a function to deal with this special case

jonyrock commented 6 years ago

Hey @silentApplause it would be interesting so see how you did it

silentApplause commented 6 years ago

@jonyrock the timestamps function is in the class Importer inside importer.js. I wrote a simple function to check the date like '20180131' and change it into '2018-01-31'.

 _dealEightDigit(x) {
    if (this._isInteger(x)) {
      if (x > 19800101 && x < 29990000) {
        var strX = x.toString()
        var result = strX.substring(0, 4) + "-" + strX.substring(4, 6) + '-' + strX.substring(6, 8)
        return result
      }
    } else if (typeof (x) === 'string') {
      try {
        var yy = parseInt(x.trim())
        if (yy != NaN) {
          if (yy > 19800101 && yy < 29990000) {
            var result = x.substring(0, 4) + "-" + x.substring(4, 6) + '-' + x.substring(6, 8)
            return result
          }
        }
      } catch (err) {
        return x
      }
    }
    return x
  }

  _isInteger(obj) {
    return typeof obj === 'number' && obj % 1 === 0
  }

.....
el => timestamp.push(this._dealEightDigit(record[el]))
.......