Commit d8119ca4 authored by Douglas Camata's avatar Douglas Camata

JIODate: switching between local and UTC modes

JIODate now has 2 methods to switch between local and UTC modes:
localMode() and utcMode(). These were added because JIODate also
exposes methods that can be affected my messing with the moment’s mode
and the user can not be aware of this.

JIODate constructor also now takes into account a second parameter when
parsing date-only strings (i.e., 2016-02-01) to create the moment
object in local or UTC mode. This allows tests that use date-only
strings to be timezone agnostic by using the UTC mode parsing.
parent 19429d4d
...@@ -41,10 +41,12 @@ ...@@ -41,10 +41,12 @@
JIODate; JIODate;
JIODate = function (str) { JIODate = function (str, mode) {
mode = mode || 'local';
// in case of forgotten 'new' // in case of forgotten 'new'
if (!(this instanceof JIODate)) { if (!(this instanceof JIODate)) {
return new JIODate(str); return new JIODate(str, mode);
} }
if (str instanceof JIODate) { if (str instanceof JIODate) {
...@@ -86,13 +88,13 @@ ...@@ -86,13 +88,13 @@
this.mom = moment(str); this.mom = moment(str);
this.setPrecision(HOUR); this.setPrecision(HOUR);
} else if (str.match(/\d\d\d\d-\d\d-\d\d/)) { } else if (str.match(/\d\d\d\d-\d\d-\d\d/)) {
this.mom = moment(str); this.mom = mode === 'utc' ? moment.utc(str) : moment(str);
this.setPrecision(DAY); this.setPrecision(DAY);
} else if (str.match(/\d\d\d\d-\d\d/)) { } else if (str.match(/\d\d\d\d-\d\d/)) {
this.mom = moment(str); this.mom = mode === 'utc' ? moment.utc(str) : moment(str);
this.setPrecision(MONTH); this.setPrecision(MONTH);
} else if (str.match(/\d\d\d\d/)) { } else if (str.match(/\d\d\d\d/)) {
this.mom = moment(str); this.mom = mode === 'utc' ? moment.utc(str) : moment(str);
this.setPrecision(YEAR); this.setPrecision(YEAR);
} }
...@@ -121,6 +123,16 @@ ...@@ -121,6 +123,16 @@
}; };
JIODate.prototype.utcMode = function () {
this.mom.utc();
};
JIODate.prototype.localMode = function () {
this.mom.local();
};
JIODate.prototype.toPrecisionString = function (precision) { JIODate.prototype.toPrecisionString = function (precision) {
var fmt; var fmt;
......
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
var test = QUnit.test, var test = QUnit.test,
ok = QUnit.ok, ok = QUnit.ok,
strictEqual = QUnit.strictEqual, strictEqual = QUnit.strictEqual,
notStrictEqual = QUnit.notStrictEqual,
equal = QUnit.equal, equal = QUnit.equal,
module = QUnit.module, module = QUnit.module,
JIODate = jiodate.JIODate, JIODate = jiodate.JIODate,
...@@ -18,10 +19,40 @@ ...@@ -18,10 +19,40 @@
ok(JIODate() instanceof JIODate); ok(JIODate() instanceof JIODate);
}); });
test("A JIODate can be in UTC or local time mode", function () {
var d = JIODate('2012-03-04T08:52:13.746Z');
ok(moment.isMoment(d.mom));
d.localMode();
equal(d.mom._isUTC, false);
notStrictEqual(d.mom.toISOString(), '2012-03-04T08:52:13.746');
d.utcMode();
equal(d.mom._isUTC, true);
strictEqual(d.mom.toISOString(), '2012-03-04T08:52:13.746Z');
});
// This test is to ensure there is a way to tell JIODate if the date is in
// UTC or local time for date-only strings. The main purpose of this feature
// is to make our tests timezone agnostic.
test("Parsing only-date string can be in utc or local mode", function () {
var d_local = JIODate('2016-02-01'),
d_utc = JIODate('2016-02-01', 'utc');
equal(d_local.mom._isUTC, false);
notStrictEqual(d_local.mom.toISOString(), '2016-02-01T00:00:00.000Z');
equal(d_utc.mom._isUTC, true);
strictEqual(d_utc.mom.toISOString(), '2016-02-01T00:00:00.000Z');
});
test("Parsing from ISO string and exposing Moment/Date objects", function () { test("Parsing from ISO string and exposing Moment/Date objects", function () {
var d = JIODate('2012-03-04T08:52:13.746Z'); var d = JIODate('2012-03-04T08:52:13.746Z');
ok(moment.isMoment(d.mom)); ok(moment.isMoment(d.mom));
// Putting JIODate into UTC time mode to ignore timezone differences
d.utcMode();
strictEqual(d.mom.toISOString(), '2012-03-04T08:52:13.746Z'); strictEqual(d.mom.toISOString(), '2012-03-04T08:52:13.746Z');
strictEqual(d.mom.year(), 2012); strictEqual(d.mom.year(), 2012);
strictEqual(d.mom.month(), 2); strictEqual(d.mom.month(), 2);
...@@ -32,7 +63,7 @@ ...@@ -32,7 +63,7 @@
strictEqual(d.mom.week(), 10); strictEqual(d.mom.week(), 10);
strictEqual(d.mom.isoWeek(), 9); strictEqual(d.mom.isoWeek(), 9);
strictEqual(d.mom.day(), 0); strictEqual(d.mom.day(), 0);
strictEqual(d.mom.hours(), 9); strictEqual(d.mom.hours(), 8);
strictEqual(d.mom.minutes(), 52); strictEqual(d.mom.minutes(), 52);
strictEqual(d.mom.seconds(), 13); strictEqual(d.mom.seconds(), 13);
strictEqual(d.mom.milliseconds(), 746); strictEqual(d.mom.milliseconds(), 746);
...@@ -130,16 +161,18 @@ ...@@ -130,16 +161,18 @@
test("Display timestamp value trucated to precision", function () { test("Display timestamp value trucated to precision", function () {
var d = JIODate('2012-03-04T08:52:13.746Z'); var d = JIODate('2012-03-04T08:52:13.746Z');
d.utcMode();
// XXX No timezone strictEqual(
d.toPrecisionString(jiodate.MSEC, true),
strictEqual(d.toPrecisionString(jiodate.MSEC), '2012-03-04 09:52:13.746'); '2012-03-04 08: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.SEC, true), '2012-03-04 08:52:13');
strictEqual(d.toPrecisionString(jiodate.HOUR), '2012-03-04 09'); strictEqual(d.toPrecisionString(jiodate.MIN, true), '2012-03-04 08:52');
strictEqual(d.toPrecisionString(jiodate.DAY), '2012-03-04'); strictEqual(d.toPrecisionString(jiodate.HOUR, true), '2012-03-04 08');
strictEqual(d.toPrecisionString(jiodate.MONTH), '2012-03'); strictEqual(d.toPrecisionString(jiodate.DAY, true), '2012-03-04');
strictEqual(d.toPrecisionString(jiodate.YEAR), '2012'); strictEqual(d.toPrecisionString(jiodate.MONTH, true), '2012-03');
strictEqual(d.toPrecisionString(jiodate.YEAR, true), '2012');
throws( throws(
function () { function () {
...@@ -150,7 +183,7 @@ ...@@ -150,7 +183,7 @@
); );
d.setPrecision(jiodate.HOUR); d.setPrecision(jiodate.HOUR);
strictEqual(d.toPrecisionString(), '2012-03-04 09'); strictEqual(d.toPrecisionString(), '2012-03-04 08');
}); });
...@@ -180,37 +213,41 @@ ...@@ -180,37 +213,41 @@
test("Parsing of partial timestamp values with any precision", function () { test("Parsing of partial timestamp values with any precision", function () {
var d; var d;
d = JIODate('2012-05-02 06:07:08.989'); d = JIODate('2012-05-02 06:07:08.989Z');
d.utcMode();
strictEqual(d.getPrecision(), 'millisecond'); strictEqual(d.getPrecision(), 'millisecond');
strictEqual(d.toPrecisionString(), '2012-05-02 06:07:08.989'); strictEqual(d.toPrecisionString(), '2012-05-02 06:07:08.989');
strictEqual(d.mom.toISOString(), '2012-05-02T04:07:08.989Z'); strictEqual(d.mom.toISOString(), '2012-05-02T06:07:08.989Z');
d = JIODate('2012-05-02 06:07:08'); d = JIODate('2012-05-02 06:07:08Z');
d.utcMode();
strictEqual(d.getPrecision(), 'second'); strictEqual(d.getPrecision(), 'second');
strictEqual(d.toPrecisionString(), '2012-05-02 06:07:08'); strictEqual(d.toPrecisionString(), '2012-05-02 06:07:08');
strictEqual(d.mom.toISOString(), '2012-05-02T04:07:08.000Z'); strictEqual(d.mom.toISOString(), '2012-05-02T06:07:08.000Z');
d = JIODate('2012-05-02 06:07'); d = JIODate('2012-05-02 06:07Z');
d.utcMode();
strictEqual(d.getPrecision(), 'minute'); strictEqual(d.getPrecision(), 'minute');
strictEqual(d.toPrecisionString(), '2012-05-02 06:07'); strictEqual(d.toPrecisionString(), '2012-05-02 06:07');
strictEqual(d.mom.toISOString(), '2012-05-02T04:07:00.000Z'); strictEqual(d.mom.toISOString(), '2012-05-02T06:07:00.000Z');
d = JIODate('2012-05-02 06'); d = JIODate('2012-05-02 06Z');
d.utcMode();
strictEqual(d.getPrecision(), 'hour'); strictEqual(d.getPrecision(), 'hour');
strictEqual(d.toPrecisionString(), '2012-05-02 06'); strictEqual(d.toPrecisionString(), '2012-05-02 06');
strictEqual(d.mom.toISOString(), '2012-05-02T04:00:00.000Z'); strictEqual(d.mom.toISOString(), '2012-05-02T06:00:00.000Z');
d = JIODate('2012-05-02'); d = JIODate('2012-05-02', 'utc');
strictEqual(d.getPrecision(), 'day'); strictEqual(d.getPrecision(), 'day');
strictEqual(d.toPrecisionString(), '2012-05-02'); strictEqual(d.toPrecisionString(), '2012-05-02');
strictEqual(d.mom.toISOString(), '2012-05-01T22:00:00.000Z'); strictEqual(d.mom.toISOString(), '2012-05-02T00:00:00.000Z');
d = JIODate('2012-05'); d = JIODate('2012-05', 'utc');
strictEqual(d.getPrecision(), 'month'); strictEqual(d.getPrecision(), 'month');
strictEqual(d.toPrecisionString(), '2012-05'); strictEqual(d.toPrecisionString(), '2012-05');
strictEqual(d.mom.toISOString(), '2012-05-01T00:00:00.000Z'); strictEqual(d.mom.toISOString(), '2012-05-01T00:00:00.000Z');
d = JIODate('2012'); d = JIODate('2012', 'utc');
strictEqual(d.getPrecision(), 'year'); strictEqual(d.getPrecision(), 'year');
strictEqual(d.toPrecisionString(), '2012'); strictEqual(d.toPrecisionString(), '2012');
strictEqual(d.mom.toISOString(), '2012-01-01T00:00:00.000Z'); strictEqual(d.mom.toISOString(), '2012-01-01T00:00:00.000Z');
...@@ -227,6 +264,10 @@ ...@@ -227,6 +264,10 @@
dmonth = JIODate('2012-05'), dmonth = JIODate('2012-05'),
dyear = JIODate('2012'); dyear = JIODate('2012');
[dmsec, dsec, dmin, dhour, dday, dmonth, dyear].map(function (jiodate) {
jiodate.utcMode();
});
strictEqual(dmsec.cmp(dsec), 0); strictEqual(dmsec.cmp(dsec), 0);
strictEqual(dmsec.cmp(dmin), 0); strictEqual(dmsec.cmp(dmin), 0);
strictEqual(dmsec.cmp(dhour), 0); strictEqual(dmsec.cmp(dhour), 0);
......
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