Commit ac34b9ef authored by Douglas's avatar Douglas

jiodate: fixes timezone related test issues

All time objects now are handled as local time, doesn’t the test runner
timezone. Also all queries tests that once used the native Date are now
using JIODate.
parent 5caee5bd
......@@ -92,7 +92,22 @@
this.mom = moment(str);
this.setPrecision(MONTH);
} else if (str.match(/\d\d\d\d/)) {
this.mom = moment(str);
// Creating a moment with only the year will show this deprecation
// warning:
//
// Deprecation warning: moment construction falls back to js Date. This is
// discouraged and will be removed in upcoming major release. Please refer
// to https://github.com/moment/moment/issues/1407 for more info.
//
// TL;DR: parsing year-only strings with momentjs falls back to native
// Date and it won't correctly represent the year in local time if UTF
// offset is negative.
//
// The solution is to use the format parameter, so momentjs won't fall
// back to the native Date and we will have the correct year in local
// time.
//
this.mom = moment(str, 'YYYY');
this.setPrecision(YEAR);
}
......
......@@ -21,6 +21,13 @@
test("Parsing from ISO string and exposing Moment/Date objects", function () {
var d = JIODate('2012-03-04T08:52:13.746Z');
// Because the above JIODate is created in ISO string, which uses UTC time,
// it's necessary to set the moment to use the UTC mode. Otherwise different
// timezones may have different results for the getters below, as they will
// try to convert it to local time.
//
d.mom.utc();
ok(moment.isMoment(d.mom));
strictEqual(d.mom.toISOString(), '2012-03-04T08:52:13.746Z');
strictEqual(d.mom.year(), 2012);
......@@ -32,7 +39,7 @@
strictEqual(d.mom.week(), 10);
strictEqual(d.mom.isoWeek(), 9);
strictEqual(d.mom.day(), 0);
strictEqual(d.mom.hours(), 9);
strictEqual(d.mom.hours(), 8);
strictEqual(d.mom.minutes(), 52);
strictEqual(d.mom.seconds(), 13);
strictEqual(d.mom.milliseconds(), 746);
......@@ -129,14 +136,12 @@
test("Display timestamp value trucated to precision", function () {
var d = JIODate('2012-03-04T08:52:13.746Z');
// XXX No timezone
var d = JIODate('2012-03-04T08:52:13.746');
strictEqual(d.toPrecisionString(jiodate.MSEC), '2012-03-04 09:52:13.746');
strictEqual(d.toPrecisionString(jiodate.SEC), '2012-03-04 09:52:13');
strictEqual(d.toPrecisionString(jiodate.MIN), '2012-03-04 09:52');
strictEqual(d.toPrecisionString(jiodate.HOUR), '2012-03-04 09');
strictEqual(d.toPrecisionString(jiodate.MSEC), '2012-03-04 08:52:13.746');
strictEqual(d.toPrecisionString(jiodate.SEC), '2012-03-04 08:52:13');
strictEqual(d.toPrecisionString(jiodate.MIN), '2012-03-04 08:52');
strictEqual(d.toPrecisionString(jiodate.HOUR), '2012-03-04 08');
strictEqual(d.toPrecisionString(jiodate.DAY), '2012-03-04');
strictEqual(d.toPrecisionString(jiodate.MONTH), '2012-03');
strictEqual(d.toPrecisionString(jiodate.YEAR), '2012');
......@@ -150,7 +155,7 @@
);
d.setPrecision(jiodate.HOUR);
strictEqual(d.toPrecisionString(), '2012-03-04 09');
strictEqual(d.toPrecisionString(), '2012-03-04 08');
});
......@@ -183,37 +188,58 @@
d = JIODate('2012-05-02 06:07:08.989');
strictEqual(d.getPrecision(), 'millisecond');
strictEqual(d.toPrecisionString(), '2012-05-02 06:07:08.989');
strictEqual(d.mom.toISOString(), '2012-05-02T04:07:08.989Z');
strictEqual(
d.mom.toDate().valueOf(),
new Date('2012-05-02 06:07:08.989').valueOf()
);
d = JIODate('2012-05-02 06:07:08');
strictEqual(d.getPrecision(), 'second');
strictEqual(d.toPrecisionString(), '2012-05-02 06:07:08');
strictEqual(d.mom.toISOString(), '2012-05-02T04:07:08.000Z');
strictEqual(
d.mom.toDate().valueOf(),
new Date('2012-05-02 06:07:08.000').valueOf()
);
d = JIODate('2012-05-02 06:07');
strictEqual(d.getPrecision(), 'minute');
strictEqual(d.toPrecisionString(), '2012-05-02 06:07');
strictEqual(d.mom.toISOString(), '2012-05-02T04:07:00.000Z');
strictEqual(
d.mom.toDate().valueOf(),
new Date('2012-05-02 06:07:00.000').valueOf()
);
d = JIODate('2012-05-02 06');
strictEqual(d.getPrecision(), 'hour');
strictEqual(d.toPrecisionString(), '2012-05-02 06');
strictEqual(d.mom.toISOString(), '2012-05-02T04:00:00.000Z');
strictEqual(
d.mom.toDate().valueOf(),
new Date('2012-05-02 06:00:00.000').valueOf()
);
d = JIODate('2012-05-02');
strictEqual(d.getPrecision(), 'day');
strictEqual(d.toPrecisionString(), '2012-05-02');
strictEqual(d.mom.toISOString(), '2012-05-01T22:00:00.000Z');
strictEqual(
d.mom.toDate().valueOf(),
new Date('2012-05-02 00:00:00.000').valueOf()
);
d = JIODate('2012-05');
strictEqual(d.getPrecision(), 'month');
strictEqual(d.toPrecisionString(), '2012-05');
strictEqual(d.mom.toISOString(), '2012-05-01T00:00:00.000Z');
strictEqual(
d.mom.toDate().valueOf(),
new Date('2012-05-01 00:00:00.000').valueOf()
);
d = JIODate('2012');
strictEqual(d.getPrecision(), 'year');
strictEqual(d.toPrecisionString(), '2012');
strictEqual(d.mom.toISOString(), '2012-01-01T00:00:00.000Z');
strictEqual(
d.mom.toDate().valueOf(),
new Date('2012-01-01 00:00:00.000').valueOf()
);
});
......
......@@ -24,9 +24,9 @@
test('Stock comparison operators with year precision', function () {
var docList = function () {
return [
{'identifier': 'twenty ten', 'date': '2010-03-04T08:52:13.746Z'},
{'identifier': 'twenty eleven', 'date': '2011-03-04T08:52:13.746Z'},
{'identifier': 'twenty twelve', 'date': '2012-03-04T08:52:13.746Z'}
{'identifier': 'twenty ten', 'date': '2010-03-04 08:52:13.746'},
{'identifier': 'twenty eleven', 'date': '2011-03-04 08:52:13.746'},
{'identifier': 'twenty twelve', 'date': '2012-03-04 08:52:13.746'}
];
}, key_schema = {
key_set: {
......@@ -48,7 +48,7 @@
exec(docList()).
then(function (dl) {
deepEqual(dl, [
{'date': '2011-03-04T08:52:13.746Z', 'identifier': 'twenty eleven'}
{'date': '2011-03-04 08:52:13.746', 'identifier': 'twenty eleven'}
], 'Match with "date = 2011" (query tree form)');
})
);
......@@ -63,8 +63,8 @@
exec(docList()).
then(function (dl) {
deepEqual(dl, [
{'date': '2010-03-04T08:52:13.746Z', 'identifier': 'twenty ten'},
{'date': '2012-03-04T08:52:13.746Z', 'identifier': 'twenty twelve'}
{'date': '2010-03-04 08:52:13.746', 'identifier': 'twenty ten'},
{'date': '2012-03-04 08:52:13.746', 'identifier': 'twenty twelve'}
], 'Match with "date != 2011" (query tree form)');
})
);
......@@ -79,7 +79,7 @@
exec(docList()).
then(function (dl) {
deepEqual(dl, [
{'date': '2010-03-04T08:52:13.746Z', 'identifier': 'twenty ten'}
{'date': '2010-03-04 08:52:13.746', 'identifier': 'twenty ten'}
], 'Match with "date < 2011" (query tree form)');
})
);
......@@ -94,8 +94,8 @@
exec(docList()).
then(function (dl) {
deepEqual(dl, [
{'date': '2010-03-04T08:52:13.746Z', 'identifier': 'twenty ten'},
{'date': '2011-03-04T08:52:13.746Z', 'identifier': 'twenty eleven'}
{'date': '2010-03-04 08:52:13.746', 'identifier': 'twenty ten'},
{'date': '2011-03-04 08:52:13.746', 'identifier': 'twenty eleven'}
], 'Match with "date <= 2011" (query tree form)');
})
);
......@@ -110,7 +110,7 @@
exec(docList()).
then(function (dl) {
deepEqual(dl, [
{'date': '2012-03-04T08:52:13.746Z', 'identifier': 'twenty twelve'}
{'date': '2012-03-04 08:52:13.746', 'identifier': 'twenty twelve'}
], 'Match with "date > 2011" (query tree form)');
})
);
......@@ -125,8 +125,8 @@
exec(docList()).
then(function (dl) {
deepEqual(dl, [
{'date': '2011-03-04T08:52:13.746Z', 'identifier': 'twenty eleven'},
{'date': '2012-03-04T08:52:13.746Z', 'identifier': 'twenty twelve'}
{'date': '2011-03-04 08:52:13.746', 'identifier': 'twenty eleven'},
{'date': '2012-03-04 08:52:13.746', 'identifier': 'twenty twelve'}
], 'Match with "date >= 2011" (query tree form)');
})
);
......@@ -135,14 +135,14 @@
[
'date: < "2011" OR date: "2012-03"',
[
{'date': '2010-03-04T08:52:13.746Z', 'identifier': 'twenty ten'},
{'date': '2012-03-04T08:52:13.746Z', 'identifier': 'twenty twelve'}
{'date': '2010-03-04 08:52:13.746', 'identifier': 'twenty ten'},
{'date': '2012-03-04 08:52:13.746', 'identifier': 'twenty twelve'}
]
],
[
'date: >= "2011-01" AND date: != "2012-03-04T08:52:13.746Z"',
'date: >= "2011-01" AND date: != "2012-03-04 08:52:13.746"',
[
{'date': '2011-03-04T08:52:13.746Z', 'identifier': 'twenty eleven'}
{'date': '2011-03-04 08:52:13.746', 'identifier': 'twenty eleven'}
]
]
];
......
/*jslint indent: 2, maxlen: 100, nomen: true, vars: true */
/*global define, exports, require, module, jIO, window, test, ok,
deepEqual, sinon, start, stop, RSVP */
deepEqual, sinon, start, stop, RSVP, jiodate */
// define([module_name], [dependencies], module);
(function (dependencies, module) {
......@@ -9,10 +9,10 @@
return define(dependencies, module);
}
if (typeof exports === 'object') {
return module(require('jio'));
return module(require('jio'), require('jiodate'));
}
module(jIO);
}(['jio', 'qunit'], function (jIO) {
module(jIO, jiodate);
}(['jio', 'jiodate', 'qunit'], function (jIO, jiodate) {
"use strict";
module('Custom Key Queries with Schema');
......@@ -32,30 +32,26 @@
var key_schema = {
cast_lookup: {
dateType: function (obj) {
if (Object.prototype.toString.call(obj) === '[object Date]') {
// no need to clone
return obj;
}
return new Date(obj);
return new jiodate.JIODate(obj);
}
},
match_lookup: {
sameDay: function (a, b) {
return (
(a.getFullYear() === b.getFullYear()) &&
(a.getMonth() === b.getMonth()) &&
(a.getDate() === b.getDate())
(a.mom.year() === b.mom.year()) &&
(a.mom.month() === b.mom.month()) &&
(a.mom.date() === b.mom.date())
);
},
sameMonth: function (a, b) {
return (
(a.getFullYear() === b.getFullYear()) &&
(a.getMonth() === b.getMonth())
(a.mom.year() === b.mom.year()) &&
(a.mom.month() === b.mom.month())
);
},
sameYear: function (a, b) {
return (a.getFullYear() === b.getFullYear());
return (a.mom.year() === b.mom.year());
},
equalState: translationEqualityMatcher({'ouvert': 'open'})
},
......
/*jslint indent: 2, maxlen: 120, nomen: true, vars: true */
/*global define, exports, require, module, jIO, window, test, ok,
equal, deepEqual, sinon, stop, start, RSVP */
equal, deepEqual, sinon, stop, start, RSVP, jiodate */
// define([module_name], [dependencies], module);
(function (dependencies, module) {
......@@ -9,10 +9,10 @@
return define(dependencies, module);
}
if (typeof exports === 'object') {
return module(require('jio'));
return module(require('jio'), require('jiodate'));
}
module(jIO);
}(['jio', 'qunit'], function (jIO) {
module(jIO, jiodate);
}(['jio', 'jiodate', 'qunit'], function (jIO, jiodate) {
"use strict";
module('Custom Key Queries');
......@@ -76,11 +76,7 @@
var dateCast = function (obj) {
if (Object.prototype.toString.call(obj) === '[object Date]') {
// no need to clone
return obj;
}
return new Date(obj);
return new jiodate.JIODate(obj);
};
......@@ -98,21 +94,21 @@
var sameDay = function (a, b) {
return (
(a.getFullYear() === b.getFullYear()) &&
(a.getMonth() === b.getMonth()) &&
(a.getDate() === b.getDate())
(a.mom.year() === b.mom.year()) &&
(a.mom.month() === b.mom.month()) &&
(a.mom.date() === b.mom.date())
);
};
var sameMonth = function (a, b) {
return (
(a.getFullYear() === b.getFullYear()) &&
(a.getMonth() === b.getMonth())
(a.mom.year() === b.mom.year()) &&
(a.mom.month() === b.mom.month())
);
};
var sameYear = function (a, b) {
return (a.getFullYear() === b.getFullYear());
return (a.mom.year() === b.mom.year());
};
var keys = {
......@@ -324,7 +320,7 @@
read_from: 'date',
cast_to: dateCast,
equal_match: function alwaysTrue(o1) { /*, o2*/
return o1.getDate() === 2;
return o1.mom.date() === 2;
}
}
}, promise = [];
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment