| 1 |
"use strict"; |
| 2 |
|
| 3 |
var path = require('path'), |
| 4 |
moment = require('moment'), |
| 5 |
populations = require('./population.json'); |
| 6 |
|
| 7 |
module.exports = function (grunt) { |
| 8 |
grunt.registerTask('data-collect', '5. Collect all data from zdump(8) into a single json file.', function (version) { |
| 9 |
version = version || 'latest'; |
| 10 |
|
| 11 |
var files = grunt.file.expand({ filter : 'isFile', cwd : 'temp/zdump/' + version }, '**/*.zdump'), |
| 12 |
meta = grunt.file.readJSON('data/meta/' + version + '.json'), |
| 13 |
data = []; |
| 14 |
|
| 15 |
var format = "MMM D HH:mm:ss YYYY", |
| 16 |
invalidLine = /failed|-2147481748|2147485547/; |
| 17 |
|
| 18 |
files.forEach(function (file) { |
| 19 |
var lines = grunt.file.read(path.join('temp/zdump/' + version, file)).split('\n'), |
| 20 |
name = file.replace(/\.zdump$/, ''), |
| 21 |
abbrs = [], |
| 22 |
untils = [], |
| 23 |
offsets = [], |
| 24 |
countries = []; |
| 25 |
|
| 26 |
lines.forEach(function (line) { |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
var parts = line.split(/\s+/); |
| 35 |
if (invalidLine.test(line) || parts.length < 13) { |
| 36 |
return; |
| 37 |
} |
| 38 |
|
| 39 |
var utc = moment.utc(parts.slice(2, 6).join(' '), format), |
| 40 |
local = moment.utc(parts.slice(9, 13).join(' '), format); |
| 41 |
|
| 42 |
offsets.push(+utc.diff(local, 'minutes', true).toFixed(4)); |
| 43 |
untils.push(+utc); |
| 44 |
abbrs.push(parts[13]); |
| 45 |
}); |
| 46 |
|
| 47 |
if (offsets.length === 0 && lines.length === 3 && lines[2].length === 0) { |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
var utcParts = lines[0].split(/\s+/), |
| 55 |
localParts = lines[1].split(/\s+/); |
| 56 |
|
| 57 |
var utc = moment.utc(utcParts.slice(2, 6).join(' '), format), |
| 58 |
local = moment.utc(localParts.slice(2, 6).join(' '), format); |
| 59 |
|
| 60 |
offsets.push(+utc.diff(local, 'minutes', true).toFixed(4)); |
| 61 |
untils.push(null); |
| 62 |
abbrs.push(localParts[6]); |
| 63 |
} |
| 64 |
|
| 65 |
if (meta.zones[name]) { |
| 66 |
countries = meta.zones[name].countries; |
| 67 |
} |
| 68 |
|
| 69 |
data.push({ |
| 70 |
name : name, |
| 71 |
abbrs : abbrs, |
| 72 |
untils : untils, |
| 73 |
offsets : offsets, |
| 74 |
population : populations[name] | 0, |
| 75 |
countries : countries |
| 76 |
}); |
| 77 |
}); |
| 78 |
|
| 79 |
grunt.file.write('temp/collect/' + version + '.json', JSON.stringify(data, null, 2)); |
| 80 |
|
| 81 |
grunt.log.ok('Collected data for ' + version); |
| 82 |
}); |
| 83 |
}; |