Debian Codegraph @

tasks/data-collect.js

raw symbols SCIP
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 // Skip any lines that don't match the expected format.
28 // 64-bit `zdump` with the `-v` flag can produce lines outside Moment's parseable date range:
29 //
30 // Etc/GMT-2 -9223372036854775808 = NULL
31 // Etc/GMT-2 -67768040609748001 (gmtime failed) = -67768040609748001 (localtime failed)
32 // Etc/GMT-2 -67768040609748000 (gmtime failed) = Thu Jan 1 00:00:00 -2147481748 +02 isdst=0 gmtoff=7200
33 // Etc/GMT-2 Thu Jan 1 00:00:00 -2147481748 UT = Thu Jan 1 02:00:00 -2147481748 +02 isdst=0 gmtoff=7200
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 // Use alternate `zdump` format when a fixed-offset zone has no transitions.
49 // The data-zdump task will instead generate the current timestamp for UTC and the zone,
50 // with the difference between them being used as the offset:
51 //
52 // UTC Sun May 7 06:17:50 2023 UTC
53 // Etc/GMT-2 Sun May 7 08:17:50 2023 +02
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 };