Skip to content

Commit 308563c

Browse files
committed
Initial commit
1 parent c464d5e commit 308563c

File tree

3 files changed

+153
-0
lines changed

3 files changed

+153
-0
lines changed

generator.js

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
const moment = require("moment-timezone");
2+
const _ = require("lodash");
3+
4+
5+
module.exports = function (start, end, timezone) {
6+
7+
// get range as moments
8+
start = moment(start);
9+
end = moment(end);
10+
11+
12+
// get zone information
13+
const zone = moment.tz.zone(timezone);
14+
15+
16+
// find timezone offset boundaries for the specified range
17+
const boundaries = [];
18+
_.each(zone.untils, (until, i) => {
19+
const offset = zone.offsets[i];
20+
if (until > start.valueOf()) {
21+
boundaries.push({ until, offset });
22+
if (until > end.valueOf()) {
23+
return false;
24+
}
25+
}
26+
});
27+
28+
29+
// recursively build condition structure
30+
function getCondition(boundaries) {
31+
const boundary = boundaries.shift();
32+
const until = boundary.until;
33+
const offset = boundary.offset * 60 * 1000; // minutes -> milliseconds
34+
35+
if (boundaries.length == 0) {
36+
return { $subtract: ["$time", offset] };
37+
}
38+
39+
return {
40+
$cond: {
41+
if: { $lt: ["$time", new Date(until)] },
42+
then: { $subtract: ["$time", offset] },
43+
else: getCondition(boundaries)
44+
}
45+
}
46+
}
47+
48+
49+
return getCondition(boundaries);
50+
};
51+

package.json

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"name": "tzoffset",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "test.js",
6+
"scripts": {
7+
"start": "node ./test.js"
8+
},
9+
"author": "",
10+
"license": "ISC",
11+
"dependencies": {
12+
"async": "^2.0.1",
13+
"lodash": "^4.15.0",
14+
"moment": "^2.14.1",
15+
"moment-timezone": "^0.5.5",
16+
"mongodb": "^2.2.9"
17+
}
18+
}

test.js

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
const async = require("async");
2+
const moment = require("moment");
3+
const mongo = require("mongodb");
4+
const client = mongo.MongoClient;
5+
6+
const generator = require("./generator");
7+
const DB_URL = "mongodb://<username>:<password>@<hostname>:<port>/<database>";
8+
const DB_COLLECTION = "tzoffset_test";
9+
10+
11+
let db, res;
12+
async.waterfall([
13+
14+
// connect to database
15+
function (done) {
16+
client.connect(DB_URL, (err, _db) => {
17+
db = _db;
18+
done(err);
19+
});
20+
},
21+
22+
//------------------------------------------------------
23+
// drop test collection
24+
//function (done) {
25+
// db.collection(DB_COLLECTION).drop((err) => {
26+
// done(err);
27+
// });
28+
//},
29+
//------------------------------------------------------
30+
31+
//------------------------------------------------------
32+
// insert test documents
33+
//function (done) {
34+
// const docs = [
35+
// { time: new Date("2016-03-27T00:59:59Z"), name: "CET" },
36+
// { time: new Date("2016-03-27T01:00:00Z"), name: "CEST" },
37+
// { time: new Date("2016-03-27T01:00:01Z"), name: "CEST" },
38+
// { time: new Date("2016-10-30T00:59:59Z"), name: "CEST" },
39+
// { time: new Date("2016-10-30T01:00:00Z"), name: "CET" },
40+
// { time: new Date("2016-10-30T01:00:01Z"), name: "CET" }
41+
// ];
42+
// db.collection(DB_COLLECTION).insert(docs, (err, res) => {
43+
// done(err);
44+
// })
45+
//},
46+
//------------------------------------------------------
47+
48+
// perform test query
49+
function (done) {
50+
const start = moment.utc("2016-01-01T00:00:00Z");
51+
const end = moment.utc("2017-01-01T00:00:00Z");
52+
const tz = "Europe/Berlin";
53+
54+
const cond = generator(start, end, tz);
55+
db.collection(DB_COLLECTION).aggregate([
56+
{
57+
$project: {
58+
name: "$name",
59+
time_orig: "$time",
60+
time_conv: cond
61+
}
62+
},
63+
{
64+
$limit: 100
65+
}
66+
]).toArray((err, _res) => {
67+
res = _res;
68+
done(err);
69+
});
70+
}
71+
72+
], (err) => {
73+
74+
if (err) {
75+
console.error(err);
76+
} else {
77+
console.log(res);
78+
}
79+
80+
if (db) {
81+
db.close();
82+
}
83+
84+
});

0 commit comments

Comments
 (0)