Add frontend assets and plugin bundles

Add the legacy frontend themes, scripts, and plugin assets required by the main SPOTA interfaces.
This commit is contained in:
Power BI Dev
2026-05-02 10:09:32 +07:00
parent efdb11db3f
commit a52c2a8462
2061 changed files with 513282 additions and 0 deletions

View File

@@ -0,0 +1,246 @@
var moment = require("../../moment");
exports.add = {
"add short" : function (test) {
test.expect(12);
var a = moment(), b, c;
a.year(2011);
a.month(9);
a.date(12);
a.hours(6);
a.minutes(7);
a.seconds(8);
a.milliseconds(500);
test.equal(a.add({ms: 50}).milliseconds(), 550, 'Add milliseconds');
test.equal(a.add({s: 1}).seconds(), 9, 'Add seconds');
test.equal(a.add({m: 1}).minutes(), 8, 'Add minutes');
test.equal(a.add({h: 1}).hours(), 7, 'Add hours');
test.equal(a.add({d: 1}).date(), 13, 'Add date');
test.equal(a.add({w: 1}).date(), 20, 'Add week');
test.equal(a.add({M: 1}).month(), 10, 'Add month');
test.equal(a.add({y: 1}).year(), 2012, 'Add year');
b = moment([2010, 0, 31]).add({M: 1});
c = moment([2010, 1, 28]).subtract({M: 1});
test.equal(b.month(), 1, 'add month, jan 31st to feb 28th');
test.equal(b.date(), 28, 'add month, jan 31st to feb 28th');
test.equal(c.month(), 0, 'subtract month, feb 28th to jan 28th');
test.equal(c.date(), 28, 'subtract month, feb 28th to jan 28th');
test.done();
},
"add long" : function (test) {
test.expect(8);
var a = moment();
a.year(2011);
a.month(9);
a.date(12);
a.hours(6);
a.minutes(7);
a.seconds(8);
a.milliseconds(500);
test.equal(a.add({milliseconds: 50}).milliseconds(), 550, 'Add milliseconds');
test.equal(a.add({seconds: 1}).seconds(), 9, 'Add seconds');
test.equal(a.add({minutes: 1}).minutes(), 8, 'Add minutes');
test.equal(a.add({hours: 1}).hours(), 7, 'Add hours');
test.equal(a.add({days: 1}).date(), 13, 'Add date');
test.equal(a.add({weeks: 1}).date(), 20, 'Add week');
test.equal(a.add({months: 1}).month(), 10, 'Add month');
test.equal(a.add({years: 1}).year(), 2012, 'Add year');
test.done();
},
"add long singular" : function (test) {
test.expect(8);
var a = moment();
a.year(2011);
a.month(9);
a.date(12);
a.hours(6);
a.minutes(7);
a.seconds(8);
a.milliseconds(500);
test.equal(a.add({millisecond: 50}).milliseconds(), 550, 'Add milliseconds');
test.equal(a.add({second: 1}).seconds(), 9, 'Add seconds');
test.equal(a.add({minute: 1}).minutes(), 8, 'Add minutes');
test.equal(a.add({hour: 1}).hours(), 7, 'Add hours');
test.equal(a.add({day: 1}).date(), 13, 'Add date');
test.equal(a.add({week: 1}).date(), 20, 'Add week');
test.equal(a.add({month: 1}).month(), 10, 'Add month');
test.equal(a.add({year: 1}).year(), 2012, 'Add year');
test.done();
},
"add string long" : function (test) {
test.expect(9);
var a = moment(), b;
a.year(2011);
a.month(9);
a.date(12);
a.hours(6);
a.minutes(7);
a.seconds(8);
a.milliseconds(500);
b = a.clone();
test.equal(a.add('millisecond', 50).milliseconds(), 550, 'Add milliseconds');
test.equal(a.add('second', 1).seconds(), 9, 'Add seconds');
test.equal(a.add('minute', 1).minutes(), 8, 'Add minutes');
test.equal(a.add('hour', 1).hours(), 7, 'Add hours');
test.equal(a.add('day', 1).date(), 13, 'Add date');
test.equal(a.add('week', 1).date(), 20, 'Add week');
test.equal(a.add('month', 1).month(), 10, 'Add month');
test.equal(a.add('year', 1).year(), 2012, 'Add year');
test.equal(b.add('day', '01').date(), 13, 'Add date');
test.done();
},
"add string long singular" : function (test) {
test.expect(9);
var a = moment(), b;
a.year(2011);
a.month(9);
a.date(12);
a.hours(6);
a.minutes(7);
a.seconds(8);
a.milliseconds(500);
b = a.clone();
test.equal(a.add('milliseconds', 50).milliseconds(), 550, 'Add milliseconds');
test.equal(a.add('seconds', 1).seconds(), 9, 'Add seconds');
test.equal(a.add('minutes', 1).minutes(), 8, 'Add minutes');
test.equal(a.add('hours', 1).hours(), 7, 'Add hours');
test.equal(a.add('days', 1).date(), 13, 'Add date');
test.equal(a.add('weeks', 1).date(), 20, 'Add week');
test.equal(a.add('months', 1).month(), 10, 'Add month');
test.equal(a.add('years', 1).year(), 2012, 'Add year');
test.equal(b.add('days', '01').date(), 13, 'Add date');
test.done();
},
"add string short" : function (test) {
test.expect(8);
var a = moment();
a.year(2011);
a.month(9);
a.date(12);
a.hours(6);
a.minutes(7);
a.seconds(8);
a.milliseconds(500);
test.equal(a.add('ms', 50).milliseconds(), 550, 'Add milliseconds');
test.equal(a.add('s', 1).seconds(), 9, 'Add seconds');
test.equal(a.add('m', 1).minutes(), 8, 'Add minutes');
test.equal(a.add('h', 1).hours(), 7, 'Add hours');
test.equal(a.add('d', 1).date(), 13, 'Add date');
test.equal(a.add('w', 1).date(), 20, 'Add week');
test.equal(a.add('M', 1).month(), 10, 'Add month');
test.equal(a.add('y', 1).year(), 2012, 'Add year');
test.done();
},
"add string long reverse args" : function (test) {
test.expect(8);
var a = moment();
a.year(2011);
a.month(9);
a.date(12);
a.hours(6);
a.minutes(7);
a.seconds(8);
a.milliseconds(500);
test.equal(a.add(50, 'millisecond').milliseconds(), 550, 'Add milliseconds');
test.equal(a.add(1, 'second', 1).seconds(), 9, 'Add seconds');
test.equal(a.add(1, 'minute', 1).minutes(), 8, 'Add minutes');
test.equal(a.add(1, 'hour', 1).hours(), 7, 'Add hours');
test.equal(a.add(1, 'day', 1).date(), 13, 'Add date');
test.equal(a.add(1, 'week', 1).date(), 20, 'Add week');
test.equal(a.add(1, 'month', 1).month(), 10, 'Add month');
test.equal(a.add(1, 'year', 1).year(), 2012, 'Add year');
test.done();
},
"add string long singular reverse args" : function (test) {
test.expect(8);
var a = moment();
a.year(2011);
a.month(9);
a.date(12);
a.hours(6);
a.minutes(7);
a.seconds(8);
a.milliseconds(500);
test.equal(a.add(50, 'milliseconds').milliseconds(), 550, 'Add milliseconds');
test.equal(a.add(1, 'seconds').seconds(), 9, 'Add seconds');
test.equal(a.add(1, 'minutes').minutes(), 8, 'Add minutes');
test.equal(a.add(1, 'hours').hours(), 7, 'Add hours');
test.equal(a.add(1, 'days').date(), 13, 'Add date');
test.equal(a.add(1, 'weeks').date(), 20, 'Add week');
test.equal(a.add(1, 'months').month(), 10, 'Add month');
test.equal(a.add(1, 'years').year(), 2012, 'Add year');
test.done();
},
"add string short reverse args" : function (test) {
test.expect(8);
var a = moment();
a.year(2011);
a.month(9);
a.date(12);
a.hours(6);
a.minutes(7);
a.seconds(8);
a.milliseconds(500);
test.equal(a.add(50, 'ms').milliseconds(), 550, 'Add milliseconds');
test.equal(a.add(1, 's').seconds(), 9, 'Add seconds');
test.equal(a.add(1, 'm').minutes(), 8, 'Add minutes');
test.equal(a.add(1, 'h').hours(), 7, 'Add hours');
test.equal(a.add(1, 'd').date(), 13, 'Add date');
test.equal(a.add(1, 'w').date(), 20, 'Add week');
test.equal(a.add(1, 'M').month(), 10, 'Add month');
test.equal(a.add(1, 'y').year(), 2012, 'Add year');
test.done();
},
"add across DST" : function (test) {
test.expect(3);
var a = moment(new Date(2011, 2, 12, 5, 0, 0)),
b = moment(new Date(2011, 2, 12, 5, 0, 0)),
c = moment(new Date(2011, 2, 12, 5, 0, 0)),
d = moment(new Date(2011, 2, 12, 5, 0, 0));
a.add('days', 1);
b.add('hours', 24);
c.add('months', 1);
test.equal(a.hours(), 5, 'adding days over DST difference should result in the same hour');
if (b.isDST() && !d.isDST()) {
test.equal(b.hours(), 6, 'adding hours over DST difference should result in a different hour');
} else if (!b.isDST() && d.isDST()) {
test.equal(b.hours(), 4, 'adding hours over DST difference should result in a different hour');
} else {
test.equal(b.hours(), 5, 'adding hours over DST difference should result in a same hour if the timezone does not have daylight savings time');
}
test.equal(c.hours(), 5, 'adding months over DST difference should result in the same hour');
test.done();
}
};

View File

@@ -0,0 +1,531 @@
var moment = require("../../moment");
exports.create = {
"array" : function (test) {
test.expect(8);
test.ok(moment([2010]).toDate() instanceof Date, "[2010]");
test.ok(moment([2010, 1]).toDate() instanceof Date, "[2010, 1]");
test.ok(moment([2010, 1, 12]).toDate() instanceof Date, "[2010, 1, 12]");
test.ok(moment([2010, 1, 12, 1]).toDate() instanceof Date, "[2010, 1, 12, 1]");
test.ok(moment([2010, 1, 12, 1, 1]).toDate() instanceof Date, "[2010, 1, 12, 1, 1]");
test.ok(moment([2010, 1, 12, 1, 1, 1]).toDate() instanceof Date, "[2010, 1, 12, 1, 1, 1]");
test.ok(moment([2010, 1, 12, 1, 1, 1, 1]).toDate() instanceof Date, "[2010, 1, 12, 1, 1, 1, 1]");
test.equal(+moment(new Date(2010, 1, 14, 15, 25, 50, 125)), +moment([2010, 1, 14, 15, 25, 50, 125]), "constructing with array === constructing with new Date()");
test.done();
},
"array copying": function (test) {
var importantArray = [2009, 11];
test.expect(1);
moment(importantArray);
test.deepEqual(importantArray, [2009, 11], "initializer should not mutate the original array");
test.done();
},
"object" : function (test) {
test.expect(10);
test.ok(moment({year: 2010}).toDate() instanceof Date, "{year: 2010}");
test.ok(moment({year: 2010, month: 1}).toDate() instanceof Date, "{year: 2010, month: 1}");
test.ok(moment({year: 2010, month: 1, day: 12}).toDate() instanceof Date, "{year: 2010, month: 1, day: 12}");
test.ok(moment({year: 2010, month: 1, day: 12, hours: 1}).toDate() instanceof Date, "{year: 2010, month: 1, day: 12, hours: 1}");
test.ok(moment({year: 2010, month: 1, day: 12, hours: 1, minutes: 1}).toDate() instanceof Date, "{year: 2010, month: 1, hours: 12, minutes: 1, seconds: 1}");
test.ok(moment({year: 2010, month: 1, day: 12, hours: 1, minutes: 1, seconds: 1}).toDate() instanceof Date, "{year: 2010, month: 1, day: 12, hours: 1, minutes: 1, seconds: 1}");
test.ok(moment({year: 2010, month: 1, day: 12, hours: 1, minutes: 1, seconds: 1, milliseconds: 1}).toDate() instanceof Date, "{year: 2010, month: 1, day: 12, hours: 1, minutes: 1, seconds: 1, milliseconds: 1}");
test.equal(+moment(new Date(2010, 1, 14, 15, 25, 50, 125)), +moment({years: 2010, months: 1, days: 14, hours: 15, minutes: 25, seconds: 50, milliseconds: 125}), "constructing with object (long plural) === constructing with new Date()");
test.equal(+moment(new Date(2010, 1, 14, 15, 25, 50, 125)), +moment({year: 2010, month: 1, day: 14, hour: 15, minute: 25, second: 50, millisecond: 125}), "constructing with object (long) === constructing with new Date()");
test.equal(+moment(new Date(2010, 1, 14, 15, 25, 50, 125)), +moment({y: 2010, M: 1, d: 14, h: 15, m: 25, s: 50, ms: 125}), "constructing with object (short) === constructing with new Date()");
test.done();
},
"multi format array copying": function (test) {
var importantArray = ['MM/DD/YYYY', 'YYYY-MM-DD', 'MM-DD-YYYY'];
test.expect(1);
moment('1999-02-13', importantArray);
test.deepEqual(importantArray, ['MM/DD/YYYY', 'YYYY-MM-DD', 'MM-DD-YYYY'], "initializer should not mutate the original array");
test.done();
},
"number" : function (test) {
test.expect(3);
test.ok(moment(1000).toDate() instanceof Date, "1000");
test.ok((moment(1000).valueOf() === 1000), "testing valueOf");
test.ok((moment.utc(1000).valueOf() === 1000), "testing valueOf");
test.done();
},
"unix" : function (test) {
test.expect(8);
test.equal(moment.unix(1).valueOf(), 1000, "1 unix timestamp == 1000 Date.valueOf");
test.equal(moment(1000).unix(), 1, "1000 Date.valueOf == 1 unix timestamp");
test.equal(moment.unix(1000).valueOf(), 1000000, "1000 unix timestamp == 1000000 Date.valueOf");
test.equal(moment(1500).unix(), 1, "1500 Date.valueOf == 1 unix timestamp");
test.equal(moment(1900).unix(), 1, "1900 Date.valueOf == 1 unix timestamp");
test.equal(moment(2100).unix(), 2, "2100 Date.valueOf == 2 unix timestamp");
test.equal(moment(1333129333524).unix(), 1333129333, "1333129333524 Date.valueOf == 1333129333 unix timestamp");
test.equal(moment(1333129333524000).unix(), 1333129333524, "1333129333524000 Date.valueOf == 1333129333524 unix timestamp");
test.done();
},
"date" : function (test) {
test.expect(1);
test.ok(moment(new Date()).toDate() instanceof Date, "new Date()");
test.done();
},
"date mutation" : function (test) {
test.expect(1);
var a = new Date();
test.ok(moment(a).toDate() !== a, "the date moment uses should not be the date passed in");
test.done();
},
"moment" : function (test) {
test.expect(2);
test.ok(moment(moment()).toDate() instanceof Date, "moment(moment())");
test.ok(moment(moment(moment())).toDate() instanceof Date, "moment(moment(moment()))");
test.done();
},
"cloning moment should only copy own properties" : function (test) {
test.expect(2);
test.ok(!moment().clone().hasOwnProperty('month'), "Should not clone prototype methods");
test.ok(!moment().clone().hasOwnProperty('_lang'), "Should not clone prototype objects");
test.done();
},
"undefined" : function (test) {
test.expect(1);
test.ok(moment().toDate() instanceof Date, "undefined");
test.done();
},
"string without format" : function (test) {
test.expect(3);
test.ok(moment("Aug 9, 1995").toDate() instanceof Date, "Aug 9, 1995");
test.ok(moment("Mon, 25 Dec 1995 13:30:00 GMT").toDate() instanceof Date, "Mon, 25 Dec 1995 13:30:00 GMT");
test.equal(new Date(2013, 8, 13, 7, 26).valueOf(), moment("2013-09-13 7:26 am").valueOf(), "2013-09-13 7:26 am");
test.done();
},
"string without format - json" : function (test) {
test.expect(5);
test.equal(moment("Date(1325132654000)").valueOf(), 1325132654000, "Date(1325132654000)");
test.equal(moment("Date(-1325132654000)").valueOf(), -1325132654000, "Date(-1325132654000)");
test.equal(moment("/Date(1325132654000)/").valueOf(), 1325132654000, "/Date(1325132654000)/");
test.equal(moment("/Date(1325132654000+0700)/").valueOf(), 1325132654000, "/Date(1325132654000+0700)/");
test.equal(moment("/Date(1325132654000-0700)/").valueOf(), 1325132654000, "/Date(1325132654000-0700)/");
test.done();
},
"string with format dropped am/pm bug" : function (test) {
moment.lang('en');
test.expect(3);
test.equal(moment('05/1/2012', 'MM/DD/YYYY h:m:s a').format('MM/DD/YYYY'), '05/01/2012', 'should not break if am/pm is left off from the parsing tokens');
test.equal(moment('05/1/2012 12:25:00 am', 'MM/DD/YYYY h:m:s a').format('MM/DD/YYYY'), '05/01/2012', 'should not break if am/pm is left off from the parsing tokens');
test.equal(moment('05/1/2012 12:25:00 pm', 'MM/DD/YYYY h:m:s a').format('MM/DD/YYYY'), '05/01/2012', 'should not break if am/pm is left off from the parsing tokens');
test.done();
},
"empty string with formats" : function (test) {
test.expect(3);
var currentDate = moment().startOf('day').format('YYYY-MM-DD HH:mm:ss');
test.equal(moment(' ', 'MM').format('YYYY-MM-DD HH:mm:ss'), currentDate, 'should not break if input is an empty string');
test.equal(moment(' ', 'DD').format('YYYY-MM-DD HH:mm:ss'), currentDate, 'should not break if input is an empty string');
test.equal(moment(' ', ['MM', "DD"]).format('YYYY-MM-DD HH:mm:ss'), currentDate, 'should not break if input is an empty string');
test.done();
},
"defaulting to current date" : function (test) {
test.expect(4);
var now = moment();
test.equal(moment('12:13:14', 'hh:mm:ss').format('YYYY-MM-DD hh:mm:ss'),
now.clone().hour(12).minute(13).second(14).format('YYYY-MM-DD hh:mm:ss'),
'given only time default to current date');
test.equal(moment('05', 'DD').format('YYYY-MM-DD'),
now.clone().date(5).format('YYYY-MM-DD'),
'given day of month default to current month, year');
test.equal(moment('05', 'MM').format('YYYY-MM-DD'),
now.clone().month(4).date(1).format('YYYY-MM-DD'),
'given month default to current year');
test.equal(moment('1996', 'YYYY').format('YYYY-MM-DD'),
now.clone().year(1996).month(0).date(1).format('YYYY-MM-DD'),
'given year do not default');
test.done();
},
"matching am/pm" : function (test) {
test.expect(13);
test.equal(moment('2012-09-03T03:00PM', 'YYYY-MM-DDThh:mmA').format('YYYY-MM-DDThh:mmA'), '2012-09-03T03:00PM', 'am/pm should parse correctly for PM');
test.equal(moment('2012-09-03T03:00P.M.', 'YYYY-MM-DDThh:mmA').format('YYYY-MM-DDThh:mmA'), '2012-09-03T03:00PM', 'am/pm should parse correctly for P.M.');
test.equal(moment('2012-09-03T03:00P', 'YYYY-MM-DDThh:mmA').format('YYYY-MM-DDThh:mmA'), '2012-09-03T03:00PM', 'am/pm should parse correctly for P');
test.equal(moment('2012-09-03T03:00pm', 'YYYY-MM-DDThh:mmA').format('YYYY-MM-DDThh:mmA'), '2012-09-03T03:00PM', 'am/pm should parse correctly for pm');
test.equal(moment('2012-09-03T03:00p.m.', 'YYYY-MM-DDThh:mmA').format('YYYY-MM-DDThh:mmA'), '2012-09-03T03:00PM', 'am/pm should parse correctly for p.m.');
test.equal(moment('2012-09-03T03:00p', 'YYYY-MM-DDThh:mmA').format('YYYY-MM-DDThh:mmA'), '2012-09-03T03:00PM', 'am/pm should parse correctly for p');
test.equal(moment('2012-09-03T03:00AM', 'YYYY-MM-DDThh:mmA').format('YYYY-MM-DDThh:mmA'), '2012-09-03T03:00AM', 'am/pm should parse correctly for AM');
test.equal(moment('2012-09-03T03:00A.M.', 'YYYY-MM-DDThh:mmA').format('YYYY-MM-DDThh:mmA'), '2012-09-03T03:00AM', 'am/pm should parse correctly for A.M.');
test.equal(moment('2012-09-03T03:00A', 'YYYY-MM-DDThh:mmA').format('YYYY-MM-DDThh:mmA'), '2012-09-03T03:00AM', 'am/pm should parse correctly for A');
test.equal(moment('2012-09-03T03:00am', 'YYYY-MM-DDThh:mmA').format('YYYY-MM-DDThh:mmA'), '2012-09-03T03:00AM', 'am/pm should parse correctly for am');
test.equal(moment('2012-09-03T03:00a.m.', 'YYYY-MM-DDThh:mmA').format('YYYY-MM-DDThh:mmA'), '2012-09-03T03:00AM', 'am/pm should parse correctly for a.m.');
test.equal(moment('2012-09-03T03:00a', 'YYYY-MM-DDThh:mmA').format('YYYY-MM-DDThh:mmA'), '2012-09-03T03:00AM', 'am/pm should parse correctly for a');
test.equal(moment('5:00p.m.March 4 2012', 'h:mmAMMMM D YYYY').format('YYYY-MM-DDThh:mmA'), '2012-03-04T05:00PM', 'am/pm should parse correctly before month names');
test.done();
},
"string with format" : function (test) {
moment.lang('en');
var a = [
['MM-DD-YYYY', '12-02-1999'],
['DD-MM-YYYY', '12-02-1999'],
['DD/MM/YYYY', '12/02/1999'],
['DD_MM_YYYY', '12_02_1999'],
['DD:MM:YYYY', '12:02:1999'],
['D-M-YY', '2-2-99'],
['YY', '99'],
['DDD-YYYY', '300-1999'],
['DD-MM-YYYY h:m:s', '12-02-1999 2:45:10'],
['DD-MM-YYYY h:m:s a', '12-02-1999 2:45:10 am'],
['DD-MM-YYYY h:m:s a', '12-02-1999 2:45:10 pm'],
['h:mm a', '12:00 pm'],
['h:mm a', '12:30 pm'],
['h:mm a', '12:00 am'],
['h:mm a', '12:30 am'],
['HH:mm', '12:00'],
['YYYY-MM-DDTHH:mm:ss', '2011-11-11T11:11:11'],
['MM-DD-YYYY [M]', '12-02-1999 M'],
['ddd MMM DD HH:mm:ss YYYY', 'Tue Apr 07 22:52:51 2009'],
['HH:mm:ss', '12:00:00'],
['HH:mm:ss', '12:30:00'],
['HH:mm:ss', '00:00:00'],
['HH:mm:ss S', '00:30:00 1'],
['HH:mm:ss SS', '00:30:00 12'],
['HH:mm:ss SSS', '00:30:00 123'],
['HH:mm:ss S', '00:30:00 7'],
['HH:mm:ss SS', '00:30:00 78'],
['HH:mm:ss SSS', '00:30:00 789'],
['X.SSS', '1234567890.123'],
['LT', '12:30 AM'],
['L', '09/02/1999'],
['l', '9/2/1999'],
['LL', 'September 2 1999'],
['ll', 'Sep 2 1999'],
['LLL', 'September 2 1999 12:30 AM'],
['lll', 'Sep 2 1999 12:30 AM'],
['LLLL', 'Thursday, September 2 1999 12:30 AM'],
['llll', 'Thu, Sep 2 1999 12:30 AM']
],
i;
test.expect(a.length);
for (i = 0; i < a.length; i++) {
test.equal(moment(a[i][1], a[i][0]).format(a[i][0]), a[i][1], a[i][0] + ' ---> ' + a[i][1]);
}
test.done();
},
"unix timestamp format" : function (test) {
var formats = ['X', 'X.S', 'X.SS', 'X.SSS'], i, format;
test.expect(formats.length * 4);
for (i = 0; i < formats.length; i++) {
format = formats[i];
test.equal(moment('1234567890', format).valueOf(), 1234567890 * 1000, format + " matches timestamp without milliseconds");
test.equal(moment('1234567890.1', format).valueOf(), 1234567890 * 1000 + 100, format + " matches timestamp with deciseconds");
test.equal(moment('1234567890.12', format).valueOf(), 1234567890 * 1000 + 120, format + " matches timestamp with centiseconds");
test.equal(moment('1234567890.123', format).valueOf(), 1234567890 * 1000 + 123, format + " matches timestamp with milliseconds");
}
test.done();
},
"string with format no separators" : function (test) {
moment.lang('en');
var a = [
['MMDDYYYY', '12021999'],
['DDMMYYYY', '12021999'],
['YYYYMMDD', '19991202'],
['DDMMMYYYY', '10Sep2001']
], i;
test.expect(a.length);
for (i = 0; i < a.length; i++) {
test.equal(moment(a[i][1], a[i][0]).format(a[i][0]), a[i][1], a[i][0] + ' ---> ' + a[i][1]);
}
test.done();
},
"string with format (timezone)" : function (test) {
test.expect(8);
test.equal(moment('5 -0700', 'H ZZ').toDate().getUTCHours(), 12, 'parse hours "5 -0700" ---> "H ZZ"');
test.equal(moment('5 -07:00', 'H Z').toDate().getUTCHours(), 12, 'parse hours "5 -07:00" ---> "H Z"');
test.equal(moment('5 -0730', 'H ZZ').toDate().getUTCMinutes(), 30, 'parse hours "5 -0730" ---> "H ZZ"');
test.equal(moment('5 -07:30', 'H Z').toDate().getUTCMinutes(), 30, 'parse hours "5 -07:0" ---> "H Z"');
test.equal(moment('5 +0100', 'H ZZ').toDate().getUTCHours(), 4, 'parse hours "5 +0100" ---> "H ZZ"');
test.equal(moment('5 +01:00', 'H Z').toDate().getUTCHours(), 4, 'parse hours "5 +01:00" ---> "H Z"');
test.equal(moment('5 +0130', 'H ZZ').toDate().getUTCMinutes(), 30, 'parse hours "5 +0130" ---> "H ZZ"');
test.equal(moment('5 +01:30', 'H Z').toDate().getUTCMinutes(), 30, 'parse hours "5 +01:30" ---> "H Z"');
test.done();
},
"string with format (timezone offset)" : function (test) {
var a, b, c, d, e, f;
test.expect(4);
a = new Date(Date.UTC(2011, 0, 1, 1));
b = moment('2011 1 1 0 -01:00', 'YYYY MM DD HH Z');
test.equal(a.getHours(), b.hours(), 'date created with utc == parsed string with timezone offset');
test.equal(+a, +b, 'date created with utc == parsed string with timezone offset');
c = moment('2011 2 1 10 -05:00', 'YYYY MM DD HH Z');
d = moment('2011 2 1 8 -07:00', 'YYYY MM DD HH Z');
test.equal(c.hours(), d.hours(), '10 am central time == 8 am pacific time');
e = moment.utc('Fri, 20 Jul 2012 17:15:00', 'ddd, DD MMM YYYY HH:mm:ss');
f = moment.utc('Fri, 20 Jul 2012 10:15:00 -0700', 'ddd, DD MMM YYYY HH:mm:ss ZZ');
test.equal(e.hours(), f.hours(), 'parse timezone offset in utc');
test.done();
},
"string with array of formats" : function (test) {
test.expect(16);
test.equal(moment('11-02-1999', ['MM-DD-YYYY', 'DD-MM-YYYY']).format('MM DD YYYY'), '11 02 1999', 'switching month and day');
test.equal(moment('02-11-1999', ['MM/DD/YYYY', 'YYYY MM DD', 'MM-DD-YYYY']).format('MM DD YYYY'), '02 11 1999', 'year last');
test.equal(moment('1999-02-11', ['MM/DD/YYYY', 'YYYY MM DD', 'MM-DD-YYYY']).format('MM DD YYYY'), '02 11 1999', 'year first');
test.equal(moment('02-11-1999', ['MM/DD/YYYY', 'YYYY MM DD']).format('MM DD YYYY'), '02 11 1999', 'year last');
test.equal(moment('1999-02-11', ['MM/DD/YYYY', 'YYYY MM DD']).format('MM DD YYYY'), '02 11 1999', 'year first');
test.equal(moment('02-11-1999', ['YYYY MM DD', 'MM/DD/YYYY']).format('MM DD YYYY'), '02 11 1999', 'year last');
test.equal(moment('1999-02-11', ['YYYY MM DD', 'MM/DD/YYYY']).format('MM DD YYYY'), '02 11 1999', 'year first');
test.equal(moment('13-11-1999', ['MM/DD/YYYY', 'DD/MM/YYYY']).format('MM DD YYYY'), '11 13 1999', 'second must be month');
test.equal(moment('11-13-1999', ['MM/DD/YYYY', 'DD/MM/YYYY']).format('MM DD YYYY'), '11 13 1999', 'first must be month');
test.equal(moment('13-14-1999', ['MM/DD/YYYY', 'DD/MM/YYYY']).format('MM DD YYYY'), '01 14 2000', 'either can be a month, month first format');
test.equal(moment('13-14-1999', ['DD/MM/YYYY', 'MM/DD/YYYY']).format('MM DD YYYY'), '02 13 2000', 'either can be a month, day first format');
test.equal(moment('11-02-10', ['MM/DD/YY', 'YY MM DD', 'DD-MM-YY']).format('MM DD YYYY'), '02 11 2010', 'all unparsed substrings have influence on format penalty');
test.equal(moment('11-02-10', ['MM.DD.YY', 'DD-MM-YY']).format('MM DD YYYY'), '02 11 2010', 'escape RegExp special characters on comparing');
test.equal(moment('13-14-98', ['DD MM YY', 'DD MM YYYY'])._f, 'DD MM YY', 'use two digit year');
test.equal(moment('13-14-1998', ['DD MM YY', 'DD MM YYYY'])._f, 'DD MM YYYY', 'use four digit year');
test.equal(moment('01', ["MM", "DD"])._f, "MM", "Should use first valid format");
test.done();
},
"string with format - years" : function (test) {
test.expect(4);
test.equal(moment('67', 'YY').format('YYYY'), '2067', '67 > 2067');
test.equal(moment('68', 'YY').format('YYYY'), '2068', '68 > 2068');
test.equal(moment('69', 'YY').format('YYYY'), '1969', '69 > 1969');
test.equal(moment('70', 'YY').format('YYYY'), '1970', '70 > 1970');
test.done();
},
"implicit cloning" : function (test) {
test.expect(2);
var momentA = moment([2011, 10, 10]),
momentB = moment(momentA);
momentA.month(5);
test.equal(momentB.month(), 10, "Calling moment() on a moment will create a clone");
test.equal(momentA.month(), 5, "Calling moment() on a moment will create a clone");
test.done();
},
"explicit cloning" : function (test) {
test.expect(2);
var momentA = moment([2011, 10, 10]),
momentB = momentA.clone();
momentA.month(5);
test.equal(momentB.month(), 10, "Calling moment() on a moment will create a clone");
test.equal(momentA.month(), 5, "Calling moment() on a moment will create a clone");
test.done();
},
"cloning carrying over utc mode" : function (test) {
test.expect(8);
test.equal(moment().local().clone()._isUTC, false, "An explicit cloned local moment should have _isUTC == false");
test.equal(moment().utc().clone()._isUTC, true, "An cloned utc moment should have _isUTC == true");
test.equal(moment().clone()._isUTC, false, "An explicit cloned local moment should have _isUTC == false");
test.equal(moment.utc().clone()._isUTC, true, "An explicit cloned utc moment should have _isUTC == true");
test.equal(moment(moment().local())._isUTC, false, "An implicit cloned local moment should have _isUTC == false");
test.equal(moment(moment().utc())._isUTC, true, "An implicit cloned utc moment should have _isUTC == true");
test.equal(moment(moment())._isUTC, false, "An implicit cloned local moment should have _isUTC == false");
test.equal(moment(moment.utc())._isUTC, true, "An implicit cloned utc moment should have _isUTC == true");
test.done();
},
"parsing iso" : function (test) {
var offset = moment([2011, 9, 08]).zone(),
pad = function (input) {
if (input < 10) {
return '0' + input;
}
return '' + input;
},
hourOffset = (offset > 0) ? Math.floor(offset / 60) : Math.ceil(offset / 60),
minOffset = offset - (hourOffset * 60),
tz = (offset > 0) ? '-' + pad(hourOffset) + ':' + pad(minOffset) : '+' + pad(-hourOffset) + ':' + pad(-minOffset),
tz2 = tz.replace(':', ''),
formats = [
['2011-10-08', '2011-10-08T00:00:00.000' + tz],
['2011-10-08T18', '2011-10-08T18:00:00.000' + tz],
['2011-10-08T18:04', '2011-10-08T18:04:00.000' + tz],
['2011-10-08T18:04:20', '2011-10-08T18:04:20.000' + tz],
['2011-10-08T18:04' + tz, '2011-10-08T18:04:00.000' + tz],
['2011-10-08T18:04:20' + tz, '2011-10-08T18:04:20.000' + tz],
['2011-10-08T18:04' + tz2, '2011-10-08T18:04:00.000' + tz],
['2011-10-08T18:04:20' + tz2, '2011-10-08T18:04:20.000' + tz],
['2011-10-08T18:04:20.1' + tz2, '2011-10-08T18:04:20.100' + tz],
['2011-10-08T18:04:20.11' + tz2, '2011-10-08T18:04:20.110' + tz],
['2011-10-08T18:04:20.111' + tz2, '2011-10-08T18:04:20.111' + tz],
['2011-10-08 18', '2011-10-08T18:00:00.000' + tz],
['2011-10-08 18:04', '2011-10-08T18:04:00.000' + tz],
['2011-10-08 18:04:20', '2011-10-08T18:04:20.000' + tz],
['2011-10-08 18:04' + tz, '2011-10-08T18:04:00.000' + tz],
['2011-10-08 18:04:20' + tz, '2011-10-08T18:04:20.000' + tz],
['2011-10-08 18:04' + tz2, '2011-10-08T18:04:00.000' + tz],
['2011-10-08 18:04:20' + tz2, '2011-10-08T18:04:20.000' + tz],
['2011-10-08 18:04:20.1' + tz2, '2011-10-08T18:04:20.100' + tz],
['2011-10-08 18:04:20.11' + tz2, '2011-10-08T18:04:20.110' + tz],
['2011-10-08 18:04:20.111' + tz2, '2011-10-08T18:04:20.111' + tz]
], i;
test.expect(formats.length);
for (i = 0; i < formats.length; i++) {
test.equal(formats[i][1], moment(formats[i][0]).format('YYYY-MM-DDTHH:mm:ss.SSSZ'), "moment should be able to parse ISO " + formats[i][0]);
}
test.done();
},
"parsing iso with T" : function (test) {
test.expect(9);
test.equal(moment('2011-10-08T18')._f, "YYYY-MM-DDTHH", "should include 'T' in the format");
test.equal(moment('2011-10-08T18:20')._f, "YYYY-MM-DDTHH:mm", "should include 'T' in the format");
test.equal(moment('2011-10-08T18:20:13')._f, "YYYY-MM-DDTHH:mm:ss", "should include 'T' in the format");
test.equal(moment('2011-10-08T18:20:13.321')._f, "YYYY-MM-DDTHH:mm:ss.S", "should include 'T' in the format");
test.equal(moment('2011-10-08 18')._f, "YYYY-MM-DD HH", "should not include 'T' in the format");
test.equal(moment('2011-10-08 18:20')._f, "YYYY-MM-DD HH:mm", "should not include 'T' in the format");
test.equal(moment('2011-10-08 18:20:13')._f, "YYYY-MM-DD HH:mm:ss", "should not include 'T' in the format");
test.equal(moment('2011-10-08 18:20:13.321')._f, "YYYY-MM-DD HH:mm:ss.S", "should not include 'T' in the format");
test.ok(moment("2013-04-23 15:23:47 UTC").isValid(), "including a trailing UTC in the input should work");
test.done();
},
"parsing iso Z timezone" : function (test) {
var i,
formats = [
['2011-10-08T18:04Z', '2011-10-08T18:04:00.000+00:00'],
['2011-10-08T18:04:20Z', '2011-10-08T18:04:20.000+00:00'],
['2011-10-08T18:04:20.111Z', '2011-10-08T18:04:20.111+00:00']
];
test.expect(formats.length);
for (i = 0; i < formats.length; i++) {
test.equal(moment.utc(formats[i][0]).format('YYYY-MM-DDTHH:mm:ss.SSSZ'), formats[i][1], "moment should be able to parse ISO " + formats[i][0]);
}
test.done();
},
"parsing iso Z timezone into local" : function (test) {
test.expect(1);
var m = moment('2011-10-08T18:04:20.111Z');
test.equal(m.utc().format('YYYY-MM-DDTHH:mm:ss.SSS'), '2011-10-08T18:04:20.111', "moment should be able to parse ISO 2011-10-08T18:04:20.111Z");
test.done();
},
"null" : function (test) {
test.expect(3);
test.equal(moment(''), null, "Calling moment('')");
test.equal(moment(null), null, "Calling moment(null)");
test.equal(moment('', 'YYYY-MM-DD'), null, "Calling moment('', 'YYYY-MM-DD')");
test.done();
},
"first century" : function (test) {
test.expect(9);
test.equal(moment([0, 0, 1]).format("YYYY-MM-DD"), "0000-01-01", "Year AD 0");
test.equal(moment([99, 0, 1]).format("YYYY-MM-DD"), "0099-01-01", "Year AD 99");
test.equal(moment([999, 0, 1]).format("YYYY-MM-DD"), "0999-01-01", "Year AD 999");
test.equal(moment('0 1 1', 'YYYY MM DD').format("YYYY-MM-DD"), "0000-01-01", "Year AD 0");
test.equal(moment('99 1 1', 'YYYY MM DD').format("YYYY-MM-DD"), "0099-01-01", "Year AD 99");
test.equal(moment('999 1 1', 'YYYY MM DD').format("YYYY-MM-DD"), "0999-01-01", "Year AD 999");
test.equal(moment('0 1 1', 'YYYYY MM DD').format("YYYYY-MM-DD"), "00000-01-01", "Year AD 0");
test.equal(moment('99 1 1', 'YYYYY MM DD').format("YYYYY-MM-DD"), "00099-01-01", "Year AD 99");
test.equal(moment('999 1 1', 'YYYYY MM DD').format("YYYYY-MM-DD"), "00999-01-01", "Year AD 999");
test.done();
},
"six digit years" : function (test) {
test.expect(8);
test.equal(moment([-270000, 0, 1]).format("YYYYY-MM-DD"), "-270000-01-01", "format BC 270,001");
test.equal(moment([ 270000, 0, 1]).format("YYYYY-MM-DD"), "270000-01-01", "format AD 270,000");
test.equal(moment("-270000-01-01", "YYYYY-MM-DD").toDate().getFullYear(), -270000, "parse BC 270,001");
test.equal(moment("270000-01-01", "YYYYY-MM-DD").toDate().getFullYear(), 270000, "parse AD 270,000");
test.equal(moment("+270000-01-01", "YYYYY-MM-DD").toDate().getFullYear(), 270000, "parse AD +270,000");
test.equal(moment.utc("-270000-01-01", "YYYYY-MM-DD").toDate().getUTCFullYear(), -270000, "parse utc BC 270,001");
test.equal(moment.utc("270000-01-01", "YYYYY-MM-DD").toDate().getUTCFullYear(), 270000, "parse utc AD 270,000");
test.equal(moment.utc("+270000-01-01", "YYYYY-MM-DD").toDate().getUTCFullYear(), 270000, "parse utc AD +270,000");
test.done();
},
"negative four digit years" : function (test) {
test.expect(2);
test.equal(moment("-1000-01-01", "YYYYY-MM-DD").toDate().getFullYear(), -1000, "parse BC 1,001");
test.equal(moment.utc("-1000-01-01", "YYYYY-MM-DD").toDate().getUTCFullYear(), -1000, "parse utc BC 1,001");
test.done();
},
"strict parsing" : function (test) {
test.expect(10);
test.equal(moment("2012-05", "YYYY-MM", true).format("YYYY-MM"), "2012-05", "parse correct string");
test.equal(moment(" 2012-05", "YYYY-MM", true).isValid(), false, "fail on extra whitespace");
test.equal(moment("foo 2012-05", "[foo] YYYY-MM", true).format('YYYY-MM'), "2012-05", "handle fixed text");
test.equal(moment("2012 05", "YYYY-MM", true).isValid(), false, "fail on different separator");
test.equal(moment("05 30 2010", ["DD MM YYYY", "MM DD YYYY"], true).format("MM DD YYYY"), "05 30 2010", "array with bad date");
test.equal(moment("05 30 2010", ["", "MM DD YYYY"], true).format("MM DD YYYY"), "05 30 2010", "array with invalid format");
test.equal(moment("05 30 2010", [" DD MM YYYY", "MM DD YYYY"], true).format("MM DD YYYY"), "05 30 2010", "array with non-matching format");
test.equal(moment("2010.*...", "YYYY.*", true).isValid(), false, "invalid format with regex chars");
test.equal(moment("2010.*", "YYYY.*", true).year(), 2010, "valid format with regex chars");
test.equal(moment(".*2010.*", ".*YYYY.*", true).year(), 2010, "valid format with regex chars on both sides");
test.done();
},
"parsing into a language" : function (test) {
test.expect(2);
moment.lang('parselang', {
months : "one_two_three_four_five_six_seven_eight_nine_ten_eleven_twelve".split('_'),
monthsShort : "one_two_three_four_five_six_seven_eight_nine_ten_eleven_twelve".split("_")
});
moment.lang('en');
test.equal(moment('2012 seven', 'YYYY MMM', 'parselang').month(), 6, "should be able to parse in a specific language");
moment.lang('parselang');
test.equal(moment('2012 july', 'YYYY MMM', 'en').month(), 6, "should be able to parse in a specific language");
moment.lang('parselang', null);
test.done();
}
};

View File

@@ -0,0 +1,28 @@
var moment = require("../../moment");
exports.days_in_month = {
"days in month" : function (test) {
test.expect(24);
var months = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31], i;
for (i = 0; i < 12; i++) {
test.equal(moment([2012, i]).daysInMonth(),
months[i],
moment([2012, i]).format('L') + " should have " + months[i] + " days. (beginning of month " + i + ')');
}
for (i = 0; i < 12; i++) {
test.equal(moment([2012, i, months[i]]).daysInMonth(),
months[i],
moment([2012, i, months[i]]).format('L') + " should have " + months[i] + " days. (end of month " + i + ')');
}
test.done();
},
"days in month leap years" : function (test) {
test.expect(4);
test.equal(moment([2010, 1]).daysInMonth(), 28, "Feb 2010 should have 28 days");
test.equal(moment([2100, 1]).daysInMonth(), 28, "Feb 2100 should have 28 days");
test.equal(moment([2008, 1]).daysInMonth(), 29, "Feb 2008 should have 29 days");
test.equal(moment([2000, 1]).daysInMonth(), 29, "Feb 2000 should have 29 days");
test.done();
}
};

View File

@@ -0,0 +1,239 @@
var moment = require("../../moment");
function equal(test, a, b, message) {
test.ok(Math.abs(a - b) < 0.00000001, "(" + a + " === " + b + ") " + message);
}
function dstForYear(year) {
var start = moment([year]),
end = moment([year + 1]),
current = start.clone(),
last;
while (current < end) {
last = current.clone();
current.add(24, 'hour');
if (last.zone() !== current.zone()) {
end = current.clone();
current = last.clone();
break;
}
}
while (current < end) {
last = current.clone();
current.add(1, 'hour');
if (last.zone() !== current.zone()) {
return {
moment : last,
diff : (current.zone() - last.zone()) / 60
};
}
}
}
exports.diff = {
"diff" : function (test) {
test.expect(5);
test.equal(moment(1000).diff(0), 1000, "1 second - 0 = 1000");
test.equal(moment(1000).diff(500), 500, "1 second - 0.5 seconds = 500");
test.equal(moment(0).diff(1000), -1000, "0 - 1 second = -1000");
test.equal(moment(new Date(1000)).diff(1000), 0, "1 second - 1 second = 0");
var oneHourDate = new Date(),
nowDate = new Date(+oneHourDate);
oneHourDate.setHours(oneHourDate.getHours() + 1);
test.equal(moment(oneHourDate).diff(nowDate), 60 * 60 * 1000, "1 hour from now = 3600000");
test.done();
},
"diff key after" : function (test) {
test.expect(10);
test.equal(moment([2010]).diff([2011], 'years'), -1, "year diff");
test.equal(moment([2010]).diff([2010, 2], 'months'), -2, "month diff");
test.equal(moment([2010]).diff([2010, 0, 7], 'weeks'), 0, "week diff");
test.equal(moment([2010]).diff([2010, 0, 8], 'weeks'), -1, "week diff");
test.equal(moment([2010]).diff([2010, 0, 21], 'weeks'), -2, "week diff");
test.equal(moment([2010]).diff([2010, 0, 22], 'weeks'), -3, "week diff");
test.equal(moment([2010]).diff([2010, 0, 4], 'days'), -3, "day diff");
test.equal(moment([2010]).diff([2010, 0, 1, 4], 'hours'), -4, "hour diff");
test.equal(moment([2010]).diff([2010, 0, 1, 0, 5], 'minutes'), -5, "minute diff");
test.equal(moment([2010]).diff([2010, 0, 1, 0, 0, 6], 'seconds'), -6, "second diff");
test.done();
},
"diff key before" : function (test) {
test.expect(10);
test.equal(moment([2011]).diff([2010], 'years'), 1, "year diff");
test.equal(moment([2010, 2]).diff([2010], 'months'), 2, "month diff");
test.equal(moment([2010, 0, 4]).diff([2010], 'days'), 3, "day diff");
test.equal(moment([2010, 0, 7]).diff([2010], 'weeks'), 0, "week diff");
test.equal(moment([2010, 0, 8]).diff([2010], 'weeks'), 1, "week diff");
test.equal(moment([2010, 0, 21]).diff([2010], 'weeks'), 2, "week diff");
test.equal(moment([2010, 0, 22]).diff([2010], 'weeks'), 3, "week diff");
test.equal(moment([2010, 0, 1, 4]).diff([2010], 'hours'), 4, "hour diff");
test.equal(moment([2010, 0, 1, 0, 5]).diff([2010], 'minutes'), 5, "minute diff");
test.equal(moment([2010, 0, 1, 0, 0, 6]).diff([2010], 'seconds'), 6, "second diff");
test.done();
},
"diff key before singular" : function (test) {
test.expect(10);
test.equal(moment([2011]).diff([2010], 'year'), 1, "year diff singular");
test.equal(moment([2010, 2]).diff([2010], 'month'), 2, "month diff singular");
test.equal(moment([2010, 0, 4]).diff([2010], 'day'), 3, "day diff singular");
test.equal(moment([2010, 0, 7]).diff([2010], 'week'), 0, "week diff singular");
test.equal(moment([2010, 0, 8]).diff([2010], 'week'), 1, "week diff singular");
test.equal(moment([2010, 0, 21]).diff([2010], 'week'), 2, "week diff singular");
test.equal(moment([2010, 0, 22]).diff([2010], 'week'), 3, "week diff singular");
test.equal(moment([2010, 0, 1, 4]).diff([2010], 'hour'), 4, "hour diff singular");
test.equal(moment([2010, 0, 1, 0, 5]).diff([2010], 'minute'), 5, "minute diff singular");
test.equal(moment([2010, 0, 1, 0, 0, 6]).diff([2010], 'second'), 6, "second diff singular");
test.done();
},
"diff key before abbreviated" : function (test) {
test.expect(10);
test.equal(moment([2011]).diff([2010], 'y'), 1, "year diff abbreviated");
test.equal(moment([2010, 2]).diff([2010], 'M'), 2, "month diff abbreviated");
test.equal(moment([2010, 0, 4]).diff([2010], 'd'), 3, "day diff abbreviated");
test.equal(moment([2010, 0, 7]).diff([2010], 'w'), 0, "week diff abbreviated");
test.equal(moment([2010, 0, 8]).diff([2010], 'w'), 1, "week diff abbreviated");
test.equal(moment([2010, 0, 21]).diff([2010], 'w'), 2, "week diff abbreviated");
test.equal(moment([2010, 0, 22]).diff([2010], 'w'), 3, "week diff abbreviated");
test.equal(moment([2010, 0, 1, 4]).diff([2010], 'h'), 4, "hour diff abbreviated");
test.equal(moment([2010, 0, 1, 0, 5]).diff([2010], 'm'), 5, "minute diff abbreviated");
test.equal(moment([2010, 0, 1, 0, 0, 6]).diff([2010], 's'), 6, "second diff abbreviated");
test.done();
},
"diff month" : function (test) {
test.expect(1);
test.equal(moment([2011, 0, 31]).diff([2011, 2, 1], 'months'), -1, "month diff");
test.done();
},
"diff across DST" : function (test) {
var dst = dstForYear(2012), a, b, daysInMonth;
if (!dst) {
console.log("No DST?");
test.done();
return;
}
test.expect(16);
a = dst.moment;
b = a.clone().utc().add(12, 'hours').local();
daysInMonth = (a.daysInMonth() + b.daysInMonth()) / 2;
equal(test, b.diff(a, 'ms', true), 12 * 60 * 60 * 1000, "ms diff across DST");
equal(test, b.diff(a, 's', true), 12 * 60 * 60, "second diff across DST");
equal(test, b.diff(a, 'm', true), 12 * 60, "minute diff across DST");
equal(test, b.diff(a, 'h', true), 12, "hour diff across DST");
equal(test, b.diff(a, 'd', true), (12 - dst.diff) / 24, "day diff across DST");
equal(test, b.diff(a, 'w', true), (12 - dst.diff) / 24 / 7, "week diff across DST");
equal(test, b.diff(a, 'M', true), (12 - dst.diff) / 24 / daysInMonth, "month diff across DST");
equal(test, b.diff(a, 'y', true), (12 - dst.diff) / 24 / daysInMonth / 12, "year diff across DST");
a = dst.moment;
b = a.clone().utc().add(12 + dst.diff, 'hours').local();
daysInMonth = (a.daysInMonth() + b.daysInMonth()) / 2;
equal(test, b.diff(a, 'ms', true), (12 + dst.diff) * 60 * 60 * 1000, "ms diff across DST");
equal(test, b.diff(a, 's', true), (12 + dst.diff) * 60 * 60, "second diff across DST");
equal(test, b.diff(a, 'm', true), (12 + dst.diff) * 60, "minute diff across DST");
equal(test, b.diff(a, 'h', true), (12 + dst.diff), "hour diff across DST");
equal(test, b.diff(a, 'd', true), 12 / 24, "day diff across DST");
equal(test, b.diff(a, 'w', true), 12 / 24 / 7, "week diff across DST");
equal(test, b.diff(a, 'M', true), 12 / 24 / daysInMonth, "month diff across DST");
equal(test, b.diff(a, 'y', true), 12 / 24 / daysInMonth / 12, "year diff across DST");
test.done();
},
"diff overflow" : function (test) {
test.expect(4);
test.equal(moment([2011]).diff([2010], 'months'), 12, "month diff");
test.equal(moment([2010, 0, 2]).diff([2010], 'hours'), 24, "hour diff");
test.equal(moment([2010, 0, 1, 2]).diff([2010], 'minutes'), 120, "minute diff");
test.equal(moment([2010, 0, 1, 0, 4]).diff([2010], 'seconds'), 240, "second diff");
test.done();
},
"diff between utc and local" : function (test) {
test.expect(7);
test.equal(moment([2012]).utc().diff([2011], 'years'), 1, "year diff");
test.equal(moment([2010, 2, 2]).utc().diff([2010, 0, 2], 'months'), 2, "month diff");
test.equal(moment([2010, 0, 4]).utc().diff([2010], 'days'), 3, "day diff");
test.equal(moment([2010, 0, 22]).utc().diff([2010], 'weeks'), 3, "week diff");
test.equal(moment([2010, 0, 1, 4]).utc().diff([2010], 'hours'), 4, "hour diff");
test.equal(moment([2010, 0, 1, 0, 5]).utc().diff([2010], 'minutes'), 5, "minute diff");
test.equal(moment([2010, 0, 1, 0, 0, 6]).utc().diff([2010], 'seconds'), 6, "second diff");
test.done();
},
"diff floored" : function (test) {
test.expect(7);
test.equal(moment([2010, 0, 1, 23]).diff([2010], 'day'), 0, "23 hours = 0 days");
test.equal(moment([2010, 0, 1, 23, 59]).diff([2010], 'day'), 0, "23:59 hours = 0 days");
test.equal(moment([2010, 0, 1, 24]).diff([2010], 'day'), 1, "24 hours = 1 day");
test.equal(moment([2010, 0, 2]).diff([2011, 0, 1], 'year'), 0, "year rounded down");
test.equal(moment([2011, 0, 1]).diff([2010, 0, 2], 'year'), 0, "year rounded down");
test.equal(moment([2010, 0, 2]).diff([2011, 0, 2], 'year'), -1, "year rounded down");
test.equal(moment([2011, 0, 2]).diff([2010, 0, 2], 'year'), 1, "year rounded down");
test.done();
},
"year diffs include dates" : function (test) {
test.expect(1);
test.ok(moment([2012, 1, 19]).diff(moment([2002, 1, 20]), 'years', true) < 10, "year diff should include date of month");
test.done();
},
"month diffs" : function (test) {
test.expect(8);
// due to floating point math errors, these tests just need to be accurate within 0.00000001
equal(test, moment([2012, 0, 1]).diff([2012, 1, 1], 'months', true), -1, 'Jan 1 to Feb 1 should be 1 month');
equal(test, moment([2012, 0, 1]).diff([2012, 0, 1, 12], 'months', true), -0.5 / 31, 'Jan 1 to Jan 1 noon should be 0.5 / 31 months');
equal(test, moment([2012, 0, 15]).diff([2012, 1, 15], 'months', true), -1, 'Jan 15 to Feb 15 should be 1 month');
equal(test, moment([2012, 0, 28]).diff([2012, 1, 28], 'months', true), -1, 'Jan 28 to Feb 28 should be 1 month');
equal(test, moment([2012, 0, 31]).diff([2012, 1, 29], 'months', true), -1 + (2 / 30), 'Jan 31 to Feb 29 should be 1 - (2 / 30) months');
equal(test, moment([2012, 0, 31]).diff([2012, 2, 1], 'months', true), -2 + (30 / 31), 'Jan 31 to Mar 1 should be 2 - (30 / 31) months');
equal(test, moment([2012, 0, 31]).diff([2012, 2, 1, 12], 'months', true), -2 + (29.5 / 31), 'Jan 31 to Mar 1 should be 2 - (29.5 / 31) months');
equal(test, moment([2012, 0, 1]).diff([2012, 0, 31], 'months', true), -(30 / 31), 'Jan 1 to Jan 31 should be 30 / 31 months');
test.done();
},
"year diffs" : function (test) {
test.expect(10);
// due to floating point math errors, these tests just need to be accurate within 0.00000001
equal(test, moment([2012, 0, 1]).diff([2013, 0, 1], 'years', true), -1, 'Jan 1 2012 to Jan 1 2013 should be 1 year');
equal(test, moment([2012, 1, 28]).diff([2013, 1, 28], 'years', true), -1, 'Feb 28 2012 to Feb 28 2013 should be 1 year');
equal(test, moment([2012, 2, 1]).diff([2013, 2, 1], 'years', true), -1, 'Mar 1 2012 to Mar 1 2013 should be 1 year');
equal(test, moment([2012, 11, 1]).diff([2013, 11, 1], 'years', true), -1, 'Dec 1 2012 to Dec 1 2013 should be 1 year');
equal(test, moment([2012, 11, 31]).diff([2013, 11, 31], 'years', true), -1, 'Dec 31 2012 to Dec 31 2013 should be 1 year');
equal(test, moment([2012, 0, 1]).diff([2013, 6, 1], 'years', true), -1.5, 'Jan 1 2012 to Jul 1 2013 should be 1.5 years');
equal(test, moment([2012, 0, 31]).diff([2013, 6, 31], 'years', true), -1.5, 'Jan 31 2012 to Jul 31 2013 should be 1.5 years');
equal(test, moment([2012, 0, 1]).diff([2013, 0, 1, 12], 'years', true), -1 - (0.5 / 31) / 12, 'Jan 1 2012 to Jan 1 2013 noon should be 1+(0.5 / 31) / 12 years');
equal(test, moment([2012, 0, 1]).diff([2013, 6, 1, 12], 'years', true), -1.5 - (0.5 / 31) / 12, 'Jan 1 2012 to Jul 1 2013 noon should be 1.5+(0.5 / 31) / 12 years');
equal(test, moment([2012, 1, 29]).diff([2013, 1, 28], 'years', true), -1 + (1 / 28.5) / 12, 'Feb 29 2012 to Feb 28 2013 should be 1-(1 / 28.5) / 12 years');
test.done();
}
};

View File

@@ -0,0 +1,472 @@
var moment = require("../../moment");
exports.duration = {
"object instantiation" : function (test) {
var d = moment.duration({
years: 2,
months: 3,
weeks: 2,
days: 1,
hours: 8,
minutes: 9,
seconds: 20,
milliseconds: 12
});
test.expect(8);
test.equal(d.years(), 2, "years");
test.equal(d.months(), 3, "months");
test.equal(d.weeks(), 2, "weeks");
test.equal(d.days(), 15, "days"); // two weeks + 1 day
test.equal(d.hours(), 8, "hours");
test.equal(d.minutes(), 9, "minutes");
test.equal(d.seconds(), 20, "seconds");
test.equal(d.milliseconds(), 12, "milliseconds");
test.done();
},
"object instantiation with strings" : function (test) {
var d = moment.duration({
years: '2',
months: '3',
weeks: '2',
days: '1',
hours: '8',
minutes: '9',
seconds: '20',
milliseconds: '12'
});
test.expect(8);
test.equal(d.years(), 2, "years");
test.equal(d.months(), 3, "months");
test.equal(d.weeks(), 2, "weeks");
test.equal(d.days(), 15, "days"); // two weeks + 1 day
test.equal(d.hours(), 8, "hours");
test.equal(d.minutes(), 9, "minutes");
test.equal(d.seconds(), 20, "seconds");
test.equal(d.milliseconds(), 12, "milliseconds");
test.done();
},
"milliseconds instantiation" : function (test) {
test.expect(1);
test.equal(moment.duration(72).milliseconds(), 72, "milliseconds");
test.done();
},
"instantiation by type" : function (test) {
test.expect(16);
test.equal(moment.duration(1, "years").years(), 1, "years");
test.equal(moment.duration(1, "y").years(), 1, "y");
test.equal(moment.duration(2, "months").months(), 2, "months");
test.equal(moment.duration(2, "M").months(), 2, "M");
test.equal(moment.duration(3, "weeks").weeks(), 3, "weeks");
test.equal(moment.duration(3, "w").weeks(), 3, "weeks");
test.equal(moment.duration(4, "days").days(), 4, "days");
test.equal(moment.duration(4, "d").days(), 4, "d");
test.equal(moment.duration(5, "hours").hours(), 5, "hours");
test.equal(moment.duration(5, "h").hours(), 5, "h");
test.equal(moment.duration(6, "minutes").minutes(), 6, "minutes");
test.equal(moment.duration(6, "m").minutes(), 6, "m");
test.equal(moment.duration(7, "seconds").seconds(), 7, "seconds");
test.equal(moment.duration(7, "s").seconds(), 7, "s");
test.equal(moment.duration(8, "milliseconds").milliseconds(), 8, "milliseconds");
test.equal(moment.duration(8, "ms").milliseconds(), 8, "ms");
test.done();
},
"shortcuts" : function (test) {
test.expect(8);
test.equal(moment.duration({y: 1}).years(), 1, "years = y");
test.equal(moment.duration({M: 2}).months(), 2, "months = M");
test.equal(moment.duration({w: 3}).weeks(), 3, "weeks = w");
test.equal(moment.duration({d: 4}).days(), 4, "days = d");
test.equal(moment.duration({h: 5}).hours(), 5, "hours = h");
test.equal(moment.duration({m: 6}).minutes(), 6, "minutes = m");
test.equal(moment.duration({s: 7}).seconds(), 7, "seconds = s");
test.equal(moment.duration({ms: 8}).milliseconds(), 8, "milliseconds = ms");
test.done();
},
"generic getter" : function (test) {
test.expect(24);
test.equal(moment.duration(1, "years").get("years"), 1, "years");
test.equal(moment.duration(1, "years").get("year"), 1, "years = year");
test.equal(moment.duration(1, "years").get("y"), 1, "years = y");
test.equal(moment.duration(2, "months").get("months"), 2, "months");
test.equal(moment.duration(2, "months").get("month"), 2, "months = month");
test.equal(moment.duration(2, "months").get("M"), 2, "months = M");
test.equal(moment.duration(3, "weeks").get("weeks"), 3, "weeks");
test.equal(moment.duration(3, "weeks").get("week"), 3, "weeks = week");
test.equal(moment.duration(3, "weeks").get("w"), 3, "weeks = w");
test.equal(moment.duration(4, "days").get("days"), 4, "days");
test.equal(moment.duration(4, "days").get("day"), 4, "days = day");
test.equal(moment.duration(4, "days").get("d"), 4, "days = d");
test.equal(moment.duration(5, "hours").get("hours"), 5, "hours");
test.equal(moment.duration(5, "hours").get("hour"), 5, "hours = hour");
test.equal(moment.duration(5, "hours").get("h"), 5, "hours = h");
test.equal(moment.duration(6, "minutes").get("minutes"), 6, "minutes");
test.equal(moment.duration(6, "minutes").get("minute"), 6, "minutes = minute");
test.equal(moment.duration(6, "minutes").get("m"), 6, "minutes = m");
test.equal(moment.duration(7, "seconds").get("seconds"), 7, "seconds");
test.equal(moment.duration(7, "seconds").get("second"), 7, "seconds = second");
test.equal(moment.duration(7, "seconds").get("s"), 7, "seconds = s");
test.equal(moment.duration(8, "milliseconds").get("milliseconds"), 8, "milliseconds");
test.equal(moment.duration(8, "milliseconds").get("millisecond"), 8, "milliseconds = millisecond");
test.equal(moment.duration(8, "milliseconds").get("ms"), 8, "milliseconds = ms");
test.done();
},
"instantiation from another duration" : function (test) {
var simple = moment.duration(1234),
lengthy = moment.duration(60 * 60 * 24 * 360 * 1e3),
complicated = moment.duration({
years: 2,
months: 3,
weeks: 4,
days: 1,
hours: 8,
minutes: 9,
seconds: 20,
milliseconds: 12
});
test.expect(3);
test.deepEqual(moment.duration(simple), simple, "simple clones are equal");
test.deepEqual(moment.duration(lengthy), lengthy, "lengthy clones are equal");
test.deepEqual(moment.duration(complicated), complicated, "complicated clones are equal");
test.done();
},
"instantiation from 24-hour time zero" : function (test) {
test.expect(6);
test.equal(moment.duration("00:00").years(), 0, "0 years");
test.equal(moment.duration("00:00").days(), 0, "0 days");
test.equal(moment.duration("00:00").hours(), 0, "0 hours");
test.equal(moment.duration("00:00").minutes(), 0, "0 minutes");
test.equal(moment.duration("00:00").seconds(), 0, "0 seconds");
test.equal(moment.duration("00:00").milliseconds(), 0, "0 milliseconds");
test.done();
},
"instantiation from 24-hour time <24 hours" : function (test) {
test.expect(6);
test.equal(moment.duration("06:45").years(), 0, "0 years");
test.equal(moment.duration("06:45").days(), 0, "0 days");
test.equal(moment.duration("06:45").hours(), 6, "6 hours");
test.equal(moment.duration("06:45").minutes(), 45, "45 minutes");
test.equal(moment.duration("06:45").seconds(), 0, "0 seconds");
test.equal(moment.duration("06:45").milliseconds(), 0, "0 milliseconds");
test.done();
},
"instantiation from 24-hour time >24 hours" : function (test) {
test.expect(6);
test.equal(moment.duration("26:45").years(), 0, "0 years");
test.equal(moment.duration("26:45").days(), 1, "0 days");
test.equal(moment.duration("26:45").hours(), 2, "2 hours");
test.equal(moment.duration("26:45").minutes(), 45, "45 minutes");
test.equal(moment.duration("26:45").seconds(), 0, "0 seconds");
test.equal(moment.duration("26:45").milliseconds(), 0, "0 milliseconds");
test.done();
},
"instatiation from serialized C# TimeSpan zero" : function (test) {
test.expect(6);
test.equal(moment.duration("00:00:00").years(), 0, "0 years");
test.equal(moment.duration("00:00:00").days(), 0, "0 days");
test.equal(moment.duration("00:00:00").hours(), 0, "0 hours");
test.equal(moment.duration("00:00:00").minutes(), 0, "0 minutes");
test.equal(moment.duration("00:00:00").seconds(), 0, "0 seconds");
test.equal(moment.duration("00:00:00").milliseconds(), 0, "0 milliseconds");
test.done();
},
"instatiation from serialized C# TimeSpan with days" : function (test) {
test.expect(6);
test.equal(moment.duration("1.02:03:04.9999999").years(), 0, "0 years");
test.equal(moment.duration("1.02:03:04.9999999").days(), 1, "1 day");
test.equal(moment.duration("1.02:03:04.9999999").hours(), 2, "2 hours");
test.equal(moment.duration("1.02:03:04.9999999").minutes(), 3, "3 minutes");
test.equal(moment.duration("1.02:03:04.9999999").seconds(), 4, "4 seconds");
test.equal(moment.duration("1.02:03:04.9999999").milliseconds(), 999, "999 milliseconds");
test.done();
},
"instatiation from serialized C# TimeSpan without days" : function (test) {
test.expect(10);
test.equal(moment.duration("01:02:03.9999999").years(), 0, "0 years");
test.equal(moment.duration("01:02:03.9999999").days(), 0, "0 days");
test.equal(moment.duration("01:02:03.9999999").hours(), 1, "1 hour");
test.equal(moment.duration("01:02:03.9999999").minutes(), 2, "2 minutes");
test.equal(moment.duration("01:02:03.9999999").seconds(), 3, "3 seconds");
test.equal(moment.duration("01:02:03.9999999").milliseconds(), 999, "999 milliseconds");
test.equal(moment.duration("23:59:59.9999999").days(), 0, "0 days");
test.equal(moment.duration("23:59:59.9999999").hours(), 23, "23 hours");
test.equal(moment.duration("500:59:59.9999999").days(), 20, "500 hours overflows to 20 days");
test.equal(moment.duration("500:59:59.9999999").hours(), 20, "500 hours overflows to 20 hours");
test.done();
},
"instatiation from serialized C# TimeSpan without days or milliseconds" : function (test) {
test.expect(6);
test.equal(moment.duration("01:02:03").years(), 0, "0 years");
test.equal(moment.duration("01:02:03").days(), 0, "0 days");
test.equal(moment.duration("01:02:03").hours(), 1, "1 hour");
test.equal(moment.duration("01:02:03").minutes(), 2, "2 minutes");
test.equal(moment.duration("01:02:03").seconds(), 3, "3 seconds");
test.equal(moment.duration("01:02:03").milliseconds(), 0, "0 milliseconds");
test.done();
},
"instatiation from serialized C# TimeSpan without milliseconds" : function (test) {
test.expect(6);
test.equal(moment.duration("1.02:03:04").years(), 0, "0 years");
test.equal(moment.duration("1.02:03:04").days(), 1, "1 day");
test.equal(moment.duration("1.02:03:04").hours(), 2, "2 hours");
test.equal(moment.duration("1.02:03:04").minutes(), 3, "3 minutes");
test.equal(moment.duration("1.02:03:04").seconds(), 4, "4 seconds");
test.equal(moment.duration("1.02:03:04").milliseconds(), 0, "0 milliseconds");
test.done();
},
"instatiation from serialized C# TimeSpan maxValue" : function (test) {
test.expect(6);
test.equal(moment.duration("10675199.02:48:05.4775807").years(), 29653, "29653 years");
test.equal(moment.duration("10675199.02:48:05.4775807").days(), 29, "29 day");
test.equal(moment.duration("10675199.02:48:05.4775807").hours(), 2, "2 hours");
test.equal(moment.duration("10675199.02:48:05.4775807").minutes(), 48, "48 minutes");
test.equal(moment.duration("10675199.02:48:05.4775807").seconds(), 5, "5 seconds");
test.equal(moment.duration("10675199.02:48:05.4775807").milliseconds(), 477, "477 milliseconds");
test.done();
},
"instatiation from serialized C# TimeSpan minValue" : function (test) {
test.expect(6);
test.equal(moment.duration("-10675199.02:48:05.4775808").years(), -29653, "29653 years");
test.equal(moment.duration("-10675199.02:48:05.4775808").days(), -29, "29 day");
test.equal(moment.duration("-10675199.02:48:05.4775808").hours(), -2, "2 hours");
test.equal(moment.duration("-10675199.02:48:05.4775808").minutes(), -48, "48 minutes");
test.equal(moment.duration("-10675199.02:48:05.4775808").seconds(), -5, "5 seconds");
test.equal(moment.duration("-10675199.02:48:05.4775808").milliseconds(), -477, "477 milliseconds");
test.done();
},
"humanize" : function (test) {
test.expect(32);
moment.lang('en');
test.equal(moment.duration({seconds: 44}).humanize(), "a few seconds", "44 seconds = a few seconds");
test.equal(moment.duration({seconds: 45}).humanize(), "a minute", "45 seconds = a minute");
test.equal(moment.duration({seconds: 89}).humanize(), "a minute", "89 seconds = a minute");
test.equal(moment.duration({seconds: 90}).humanize(), "2 minutes", "90 seconds = 2 minutes");
test.equal(moment.duration({minutes: 44}).humanize(), "44 minutes", "44 minutes = 44 minutes");
test.equal(moment.duration({minutes: 45}).humanize(), "an hour", "45 minutes = an hour");
test.equal(moment.duration({minutes: 89}).humanize(), "an hour", "89 minutes = an hour");
test.equal(moment.duration({minutes: 90}).humanize(), "2 hours", "90 minutes = 2 hours");
test.equal(moment.duration({hours: 5}).humanize(), "5 hours", "5 hours = 5 hours");
test.equal(moment.duration({hours: 21}).humanize(), "21 hours", "21 hours = 21 hours");
test.equal(moment.duration({hours: 22}).humanize(), "a day", "22 hours = a day");
test.equal(moment.duration({hours: 35}).humanize(), "a day", "35 hours = a day");
test.equal(moment.duration({hours: 36}).humanize(), "2 days", "36 hours = 2 days");
test.equal(moment.duration({days: 1}).humanize(), "a day", "1 day = a day");
test.equal(moment.duration({days: 5}).humanize(), "5 days", "5 days = 5 days");
test.equal(moment.duration({weeks: 1}).humanize(), "7 days", "1 week = 7 days");
test.equal(moment.duration({days: 25}).humanize(), "25 days", "25 days = 25 days");
test.equal(moment.duration({days: 26}).humanize(), "a month", "26 days = a month");
test.equal(moment.duration({days: 30}).humanize(), "a month", "30 days = a month");
test.equal(moment.duration({days: 45}).humanize(), "a month", "45 days = a month");
test.equal(moment.duration({days: 46}).humanize(), "2 months", "46 days = 2 months");
test.equal(moment.duration({days: 74}).humanize(), "2 months", "75 days = 2 months");
test.equal(moment.duration({days: 76}).humanize(), "3 months", "76 days = 3 months");
test.equal(moment.duration({months: 1}).humanize(), "a month", "1 month = a month");
test.equal(moment.duration({months: 5}).humanize(), "5 months", "5 months = 5 months");
test.equal(moment.duration({days: 344}).humanize(), "11 months", "344 days = 11 months");
test.equal(moment.duration({days: 345}).humanize(), "a year", "345 days = a year");
test.equal(moment.duration({days: 547}).humanize(), "a year", "547 days = a year");
test.equal(moment.duration({days: 548}).humanize(), "2 years", "548 days = 2 years");
test.equal(moment.duration({years: 1}).humanize(), "a year", "1 year = a year");
test.equal(moment.duration({years: 5}).humanize(), "5 years", "5 years = 5 years");
test.equal(moment.duration(7200000).humanize(), "2 hours", "7200000 = 2 minutes");
test.done();
},
"humanize duration with suffix" : function (test) {
test.expect(2);
moment.lang('en');
test.equal(moment.duration({seconds: 44}).humanize(true), "in a few seconds", "44 seconds = a few seconds");
test.equal(moment.duration({seconds: -44}).humanize(true), "a few seconds ago", "44 seconds = a few seconds");
test.done();
},
"bubble value up" : function (test) {
test.expect(5);
test.equal(moment.duration({milliseconds: 61001}).milliseconds(), 1, "61001 milliseconds has 1 millisecond left over");
test.equal(moment.duration({milliseconds: 61001}).seconds(), 1, "61001 milliseconds has 1 second left over");
test.equal(moment.duration({milliseconds: 61001}).minutes(), 1, "61001 milliseconds has 1 minute left over");
test.equal(moment.duration({minutes: 350}).minutes(), 50, "350 minutes has 50 minutes left over");
test.equal(moment.duration({minutes: 350}).hours(), 5, "350 minutes has 5 hours left over");
test.done();
},
"clipping" : function (test) {
test.expect(18);
test.equal(moment.duration({months: 11}).months(), 11, "11 months is 11 months");
test.equal(moment.duration({months: 11}).years(), 0, "11 months makes no year");
test.equal(moment.duration({months: 12}).months(), 0, "12 months is 0 months left over");
test.equal(moment.duration({months: 12}).years(), 1, "12 months makes 1 year");
test.equal(moment.duration({months: 13}).months(), 1, "13 months is 1 month left over");
test.equal(moment.duration({months: 13}).years(), 1, "13 months makes 1 year");
test.equal(moment.duration({days: 29}).days(), 29, "29 days is 29 days");
test.equal(moment.duration({days: 29}).months(), 0, "29 days makes no month");
test.equal(moment.duration({days: 30}).days(), 0, "30 days is 0 days left over");
test.equal(moment.duration({days: 30}).months(), 1, "30 days is a month");
test.equal(moment.duration({days: 31}).days(), 1, "31 days is 1 day left over");
test.equal(moment.duration({days: 31}).months(), 1, "31 days is a month");
test.equal(moment.duration({hours: 23}).hours(), 23, "23 hours is 23 hours");
test.equal(moment.duration({hours: 23}).days(), 0, "23 hours makes no day");
test.equal(moment.duration({hours: 24}).hours(), 0, "24 hours is 0 hours left over");
test.equal(moment.duration({hours: 24}).days(), 1, "24 hours makes 1 day");
test.equal(moment.duration({hours: 25}).hours(), 1, "25 hours is 1 hour left over");
test.equal(moment.duration({hours: 25}).days(), 1, "25 hours makes 1 day");
test.done();
},
"effective equivalency" : function (test) {
test.expect(7);
test.deepEqual(moment.duration({seconds: 1})._data, moment.duration({milliseconds: 1000})._data, "1 second is the same as 1000 milliseconds");
test.deepEqual(moment.duration({seconds: 60})._data, moment.duration({minutes: 1})._data, "1 minute is the same as 60 seconds");
test.deepEqual(moment.duration({minutes: 60})._data, moment.duration({hours: 1})._data, "1 hour is the same as 60 minutes");
test.deepEqual(moment.duration({hours: 24})._data, moment.duration({days: 1})._data, "1 day is the same as 24 hours");
test.deepEqual(moment.duration({days: 7})._data, moment.duration({weeks: 1})._data, "1 week is the same as 7 days");
test.deepEqual(moment.duration({days: 30})._data, moment.duration({months: 1})._data, "1 month is the same as 30 days");
test.deepEqual(moment.duration({months: 12})._data, moment.duration({years: 1})._data, "1 years is the same as 12 months");
test.done();
},
"asGetters" : function (test) {
var d = moment.duration({
years: 2,
months: 3,
weeks: 2,
days: 1,
hours: 8,
minutes: 9,
seconds: 20,
milliseconds: 12
});
test.expect(8);
test.equal(d.asYears().toFixed(2), "2.29", "years");
test.equal(d.asMonths().toFixed(2), "27.51", "months");
test.equal(d.asWeeks().toFixed(2), "119.33", "weeks");
test.equal(d.asDays().toFixed(2), "835.34", "days");
test.equal(d.asHours().toFixed(2), "20048.16", "hours");
test.equal(d.asMinutes().toFixed(2), "1202889.33", "minutes");
test.equal(d.asSeconds().toFixed(2), "72173360.01", "seconds");
test.equal(d.asMilliseconds(), 72173360012, "milliseconds");
test.done();
},
"generic as getter" : function (test) {
var d = moment.duration({
years: 2,
months: 3,
weeks: 2,
days: 1,
hours: 8,
minutes: 9,
seconds: 20,
milliseconds: 12
});
test.expect(24);
test.equal(d.as("years").toFixed(2), "2.29", "years");
test.equal(d.as("year").toFixed(2), "2.29", "years = year");
test.equal(d.as("y").toFixed(2), "2.29", "years = y");
test.equal(d.as("months").toFixed(2), "27.51", "months");
test.equal(d.as("month").toFixed(2), "27.51", "months = month");
test.equal(d.as("M").toFixed(2), "27.51", "months = M");
test.equal(d.as("weeks").toFixed(2), "119.33", "weeks");
test.equal(d.as("week").toFixed(2), "119.33", "weeks = week");
test.equal(d.as("w").toFixed(2), "119.33", "weeks = w");
test.equal(d.as("days").toFixed(2), "835.34", "days");
test.equal(d.as("day").toFixed(2), "835.34", "days = day");
test.equal(d.as("d").toFixed(2), "835.34", "days = d");
test.equal(d.as("hours").toFixed(2), "20048.16", "hours");
test.equal(d.as("hour").toFixed(2), "20048.16", "hours = hour");
test.equal(d.as("h").toFixed(2), "20048.16", "hours = h");
test.equal(d.as("minutes").toFixed(2), "1202889.33", "minutes");
test.equal(d.as("minute").toFixed(2), "1202889.33", "minutes = minute");
test.equal(d.as("m").toFixed(2), "1202889.33", "minutes = m");
test.equal(d.as("seconds").toFixed(2), "72173360.01", "seconds");
test.equal(d.as("second").toFixed(2), "72173360.01", "seconds = second");
test.equal(d.as("s").toFixed(2), "72173360.01", "seconds = s");
test.equal(d.as("milliseconds"), 72173360012, "milliseconds");
test.equal(d.as("millisecond"), 72173360012, "milliseconds = millisecond");
test.equal(d.as("ms"), 72173360012, "milliseconds = ms");
test.done();
},
"isDuration" : function (test) {
test.expect(3);
test.ok(moment.isDuration(moment.duration(12345678)), "correctly says true");
test.ok(!moment.isDuration(moment()), "moment object is not a duration");
test.ok(!moment.isDuration({milliseconds: 1}), "plain object is not a duration");
test.done();
},
"add" : function (test) {
test.expect(4);
var d = moment.duration({months: 4, weeks: 3, days: 2});
// for some reason, d._data._months does not get updated; use d._months instead.
test.equal(d.add(1, 'month')._months, 5, 'Add months');
test.equal(d.add(5, 'days')._days, 28, 'Add days');
test.equal(d.add(10000)._milliseconds, 10000, 'Add milliseconds');
test.equal(d.add({h: 23, m: 59})._milliseconds, 23 * 60 * 60 * 1000 + 59 * 60 * 1000 + 10000, 'Add hour:minute');
test.done();
},
"add and bubble" : function (test) {
test.expect(4);
test.equal(moment.duration(1, 'second').add(1000, 'milliseconds').seconds(), 2, 'Adding milliseconds should bubble up to seconds');
test.equal(moment.duration(1, 'minute').add(60, 'second').minutes(), 2, 'Adding seconds should bubble up to minutes');
test.equal(moment.duration(1, 'hour').add(60, 'minutes').hours(), 2, 'Adding minutes should bubble up to hours');
test.equal(moment.duration(1, 'day').add(24, 'hours').days(), 2, 'Adding hours should bubble up to days');
test.done();
},
"subtract and bubble" : function (test) {
test.expect(4);
test.equal(moment.duration(2, 'second').subtract(1000, 'milliseconds').seconds(), 1, 'Subtracting milliseconds should bubble up to seconds');
test.equal(moment.duration(2, 'minute').subtract(60, 'second').minutes(), 1, 'Subtracting seconds should bubble up to minutes');
test.equal(moment.duration(2, 'hour').subtract(60, 'minutes').hours(), 1, 'Subtracting minutes should bubble up to hours');
test.equal(moment.duration(2, 'day').subtract(24, 'hours').days(), 1, 'Subtracting hours should bubble up to days');
test.done();
},
"subtract" : function (test) {
test.expect(4);
var d = moment.duration({months: 2, weeks: 2, days: 0, hours: 5});
// for some reason, d._data._months does not get updated; use d._months instead.
test.equal(d.subtract(1, 'months')._months, 1, 'Subtract months');
test.equal(d.subtract(14, 'days')._days, 0, 'Subtract days');
test.equal(d.subtract(10000)._milliseconds, 5 * 60 * 60 * 1000 - 10000, 'Subtract milliseconds');
test.equal(d.subtract({h: 1, m: 59})._milliseconds, 3 * 60 * 60 * 1000 + 1 * 60 * 1000 - 10000, 'Subtract hour:minute');
test.done();
}
};

View File

@@ -0,0 +1,330 @@
var moment = require("../../moment");
exports.format = {
"format YY" : function (test) {
test.expect(1);
var b = moment(new Date(2009, 1, 14, 15, 25, 50, 125));
test.equal(b.format('YY'), '09', 'YY ---> 09');
test.done();
},
"format escape brackets" : function (test) {
test.expect(9);
moment.lang('en');
var b = moment(new Date(2009, 1, 14, 15, 25, 50, 125));
test.equal(b.format('[day]'), 'day', 'Single bracket');
test.equal(b.format('[day] YY [YY]'), 'day 09 YY', 'Double bracket');
test.equal(b.format('[YY'), '[09', 'Un-ended bracket');
test.equal(b.format('[[YY]]'), '[YY]', 'Double nested brackets');
test.equal(b.format('[[]'), '[', 'Escape open bracket');
test.equal(b.format('[Last]'), 'Last', 'localized tokens');
test.equal(b.format('[L] L'), 'L 02/14/2009', 'localized tokens with escaped localized tokens');
test.equal(b.format('[L LL LLL LLLL aLa]'), 'L LL LLL LLLL aLa', 'localized tokens with escaped localized tokens');
test.equal(b.format('[LLL] LLL'), 'LLL February 14 2009 3:25 PM', 'localized tokens with escaped localized tokens (recursion)');
test.done();
},
"format milliseconds" : function (test) {
test.expect(6);
var b = moment(new Date(2009, 1, 14, 15, 25, 50, 123));
test.equal(b.format('S'), '1', 'Deciseconds');
test.equal(b.format('SS'), '12', 'Centiseconds');
test.equal(b.format('SSS'), '123', 'Milliseconds');
b.milliseconds(789);
test.equal(b.format('S'), '7', 'Deciseconds');
test.equal(b.format('SS'), '78', 'Centiseconds');
test.equal(b.format('SSS'), '789', 'Milliseconds');
test.done();
},
"format timezone" : function (test) {
test.expect(2);
var b = moment(new Date(2010, 1, 14, 15, 25, 50, 125)),
explanation = 'moment().format("z") = ' + b.format('z') + ' It should be something like "PST"';
if (moment().zone() === -60) {
explanation += "For UTC+1 this is a known issue, see https://github.com/timrwood/moment/issues/162";
}
test.ok(b.format('Z').match(/^[\+\-]\d\d:\d\d$/), b.format('Z') + ' should be something like "+07:30"');
test.ok(b.format('ZZ').match(/^[\+\-]\d{4}$/), b.format('ZZ') + ' should be something like "+0700"');
test.done();
},
"format multiple with zone" : function (test) {
test.expect(1);
var b = moment('2012-10-08 -1200', ['YYYY-MM-DD HH:mm ZZ', 'YYYY-MM-DD ZZ', 'YYYY-MM-DD']);
test.equals(b.format('YYYY-MM'), '2012-10', 'Parsing multiple formats should not crash with different sized formats');
test.done();
},
"isDST" : function (test) {
test.expect(2);
var janOffset = new Date(2011, 0, 1).getTimezoneOffset(),
julOffset = new Date(2011, 6, 1).getTimezoneOffset(),
janIsDst = janOffset < julOffset,
julIsDst = julOffset < janOffset,
jan1 = moment([2011]),
jul1 = moment([2011, 6]);
if (janIsDst && julIsDst) {
test.ok(0, 'January and July cannot both be in DST');
test.ok(0, 'January and July cannot both be in DST');
} else if (janIsDst) {
test.ok(jan1.isDST(), 'January 1 is DST');
test.ok(!jul1.isDST(), 'July 1 is not DST');
} else if (julIsDst) {
test.ok(!jan1.isDST(), 'January 1 is not DST');
test.ok(jul1.isDST(), 'July 1 is DST');
} else {
test.ok(!jan1.isDST(), 'January 1 is not DST');
test.ok(!jul1.isDST(), 'July 1 is not DST');
}
test.done();
},
"unix timestamp" : function (test) {
test.expect(5);
var m = moment('1234567890.123', 'X');
test.equals(m.format('X'), '1234567890', 'unix timestamp without milliseconds');
test.equals(m.format('X.S'), '1234567890.1', 'unix timestamp with deciseconds');
test.equals(m.format('X.SS'), '1234567890.12', 'unix timestamp with centiseconds');
test.equals(m.format('X.SSS'), '1234567890.123', 'unix timestamp with milliseconds');
m = moment(1234567890.123, 'X');
test.equals(m.format('X'), '1234567890', 'unix timestamp as integer');
test.done();
},
"zone" : function (test) {
test.expect(3);
if (moment().zone() > 0) {
test.ok(moment().format('ZZ').indexOf('-') > -1, 'When the zone() offset is greater than 0, the ISO offset should be less than zero');
}
if (moment().zone() < 0) {
test.ok(moment().format('ZZ').indexOf('+') > -1, 'When the zone() offset is less than 0, the ISO offset should be greater than zero');
}
if (moment().zone() === 0) {
test.ok(moment().format('ZZ').indexOf('+') > -1, 'When the zone() offset is equal to 0, the ISO offset should be positive zero');
}
if (moment().zone() === 0) {
test.equal(moment().zone(), 0, 'moment.fn.zone should be a multiple of 15 (was ' + moment().zone() + ')');
} else {
test.equal(moment().zone() % 15, 0, 'moment.fn.zone should be a multiple of 15 (was ' + moment().zone() + ')');
}
test.equal(moment().zone(), new Date().getTimezoneOffset(), 'zone should equal getTimezoneOffset');
test.done();
},
"default format" : function (test) {
test.expect(1);
var isoRegex = /\d{4}.\d\d.\d\dT\d\d.\d\d.\d\d[\+\-]\d\d:\d\d/;
test.ok(isoRegex.exec(moment().format()), "default format (" + moment().format() + ") should match ISO");
test.done();
},
"escaping quotes" : function (test) {
test.expect(4);
moment.lang('en');
var date = moment([2012, 0]);
test.equal(date.format('MMM \'YY'), "Jan '12", "Should be able to format with single parenthesis");
test.equal(date.format('MMM "YY'), 'Jan "12', "Should be able to format with double parenthesis");
test.equal(date.format("MMM 'YY"), "Jan '12", "Should be able to format with single parenthesis");
test.equal(date.format("MMM \"YY"), 'Jan "12', "Should be able to format with double parenthesis");
test.done();
},
"toJSON" : function (test) {
var supportsJson = typeof JSON !== "undefined" && JSON.stringify && JSON.stringify.call,
date = moment("2012-10-09T21:30:40.678+0100");
test.expect(supportsJson ? 2 : 1);
test.equal(date.toJSON(), "2012-10-09T20:30:40.678Z", "should output ISO8601 on moment.fn.toJSON");
if (supportsJson) {
test.equal(JSON.stringify({
date : date
}), '{"date":"2012-10-09T20:30:40.678Z"}', "should output ISO8601 on JSON.stringify");
}
test.done();
},
"toISOString" : function (test) {
var date = moment.utc("2012-10-09T20:30:40.678");
test.equal(date.toISOString(), "2012-10-09T20:30:40.678Z", "should output ISO8601 on moment.fn.toISOString");
test.done();
},
"weeks format" : function (test) {
// http://en.wikipedia.org/wiki/ISO_week_date
var cases = {
"2005-01-02": "2004-53",
"2005-12-31": "2005-52",
"2007-01-01": "2007-01",
"2007-12-30": "2007-52",
"2007-12-31": "2008-01",
"2008-01-01": "2008-01",
"2008-12-28": "2008-52",
"2008-12-29": "2009-01",
"2008-12-30": "2009-01",
"2008-12-31": "2009-01",
"2009-01-01": "2009-01",
"2009-12-31": "2009-53",
"2010-01-01": "2009-53",
"2010-01-02": "2009-53",
"2010-01-03": "2009-53"
}, i, iso, the;
for (i in cases) {
iso = cases[i].split('-').pop();
the = moment(i).format('WW');
test.equal(iso, the, i + ": should be " + iso + ", but " + the);
}
test.done();
},
"iso week year formats" : function (test) {
// http://en.wikipedia.org/wiki/ISO_week
var cases = {
"2005-01-02": "2004-53",
"2005-12-31": "2005-52",
"2007-01-01": "2007-01",
"2007-12-30": "2007-52",
"2007-12-31": "2008-01",
"2008-01-01": "2008-01",
"2008-12-28": "2008-52",
"2008-12-29": "2009-01",
"2008-12-30": "2009-01",
"2008-12-31": "2009-01",
"2009-01-01": "2009-01",
"2009-12-31": "2009-53",
"2010-01-01": "2009-53",
"2010-01-02": "2009-53",
"2010-01-03": "2009-53"
}, i, isoWeekYear, formatted5, formatted4, formatted2;
for (i in cases) {
isoWeekYear = cases[i].split('-')[0];
formatted5 = moment(i).format('GGGGG');
test.equal('0' + isoWeekYear, formatted5, i + ": should be " + isoWeekYear + ", but " + formatted4);
formatted4 = moment(i).format('GGGG');
test.equal(isoWeekYear, formatted4, i + ": should be " + isoWeekYear + ", but " + formatted4);
formatted2 = moment(i).format('GG');
test.equal(isoWeekYear.slice(2, 4), formatted2, i + ": should be " + isoWeekYear + ", but " + formatted2);
}
test.done();
},
"week year formats" : function (test) {
// http://en.wikipedia.org/wiki/ISO_week
var cases = {
"2005-01-02": "2004-53",
"2005-12-31": "2005-52",
"2007-01-01": "2007-01",
"2007-12-30": "2007-52",
"2007-12-31": "2008-01",
"2008-01-01": "2008-01",
"2008-12-28": "2008-52",
"2008-12-29": "2009-01",
"2008-12-30": "2009-01",
"2008-12-31": "2009-01",
"2009-01-01": "2009-01",
"2009-12-31": "2009-53",
"2010-01-01": "2009-53",
"2010-01-02": "2009-53",
"2010-01-03": "2009-53"
}, i, formatted5, formatted4, formatted2, isoWeekYear;
moment.lang('en-gb'); // 1, 4
for (i in cases) {
isoWeekYear = cases[i].split('-')[0];
formatted5 = moment(i).format('ggggg');
test.equal('0' + isoWeekYear, formatted5, i + ": should be " + isoWeekYear + ", but " + formatted4);
formatted4 = moment(i).format('gggg');
test.equal(isoWeekYear, formatted4, i + ": should be " + isoWeekYear + ", but " + formatted4);
formatted2 = moment(i).format('gg');
test.equal(isoWeekYear.slice(2, 4), formatted2, i + ": should be " + isoWeekYear + ", but " + formatted2);
}
test.done();
},
"iso weekday formats" : function (test) {
test.expect(7);
test.equal(moment([1985, 1, 4]).format('E'), '1', "Feb 4 1985 is Monday -- 1st day");
test.equal(moment([2029, 8, 18]).format('E'), '2', "Sep 18 2029 is Tuesday -- 2nd day");
test.equal(moment([2013, 3, 24]).format('E'), '3', "Apr 24 2013 is Wednesday -- 3rd day");
test.equal(moment([2015, 2, 5]).format('E'), '4', "Mar 5 2015 is Thursday -- 4th day");
test.equal(moment([1970, 0, 2]).format('E'), '5', "Jan 2 1970 is Friday -- 5th day");
test.equal(moment([2001, 4, 12]).format('E'), '6', "May 12 2001 is Saturday -- 6th day");
test.equal(moment([2000, 0, 2]).format('E'), '7', "Jan 2 2000 is Sunday -- 7th day");
test.done();
},
"weekday formats" : function (test) {
test.expect(7);
moment.lang('dow: 3,doy: 5', {week: {dow: 3, doy: 5}});
test.equal(moment([1985, 1, 6]).format('e'), '0', "Feb 6 1985 is Wednesday -- 0th day");
test.equal(moment([2029, 8, 20]).format('e'), '1', "Sep 20 2029 is Thursday -- 1st day");
test.equal(moment([2013, 3, 26]).format('e'), '2', "Apr 26 2013 is Friday -- 2nd day");
test.equal(moment([2015, 2, 7]).format('e'), '3', "Mar 7 2015 is Saturday -- 3nd day");
test.equal(moment([1970, 0, 4]).format('e'), '4', "Jan 4 1970 is Sunday -- 4th day");
test.equal(moment([2001, 4, 14]).format('e'), '5', "May 14 2001 is Monday -- 5th day");
test.equal(moment([2000, 0, 4]).format('e'), '6', "Jan 4 2000 is Tuesday -- 6th day");
test.done();
},
"toString is just human readable format" : function (test) {
test.expect(1);
var b = moment(new Date(2009, 1, 5, 15, 25, 50, 125));
test.equal(b.toString(), b.format('ddd MMM DD YYYY HH:mm:ss [GMT]ZZ'));
test.done();
},
"toJSON skips postformat" : function (test) {
test.expect(1);
moment.lang('postformat', {postformat: function (s) { s.replace(/./g, 'X'); }});
test.equal(moment.utc([2000, 0, 1]).toJSON(), "2000-01-01T00:00:00.000Z", "toJSON doesn't postformat");
moment.lang('postformat', null);
test.done();
},
"calendar day timezone" : function (test) {
test.expect(10);
var zones = [60, -60, 90, -90, 360, -360, 720, -720],
b = moment().utc().startOf('day').subtract({ m : 1 }),
c = moment().local().startOf('day').subtract({ m : 1 });
zones.forEach(function (z) {
var a = moment().zone(z).startOf('day').subtract({ m: 1 });
test.equal(moment(a).zone(z).calendar(), "Yesterday at 11:59 PM", "Yesterday at 11:59 PM, not Today, or the wrong time");
});
test.equal(moment(b).utc().calendar(), "Yesterday at 11:59 PM", "Yesterday at 11:59 PM, not Today, or the wrong time");
test.equal(moment(c).local().calendar(), "Yesterday at 11:59 PM", "Yesterday at 11:59 PM, not Today, or the wrong time");
test.done();
}
};

View File

@@ -0,0 +1,210 @@
var moment = require("../../moment");
exports.getters_setters = {
"getters" : function (test) {
test.expect(8);
var a = moment([2011, 9, 12, 6, 7, 8, 9]);
test.equal(a.year(), 2011, 'year');
test.equal(a.month(), 9, 'month');
test.equal(a.date(), 12, 'date');
test.equal(a.day(), 3, 'day');
test.equal(a.hours(), 6, 'hour');
test.equal(a.minutes(), 7, 'minute');
test.equal(a.seconds(), 8, 'second');
test.equal(a.milliseconds(), 9, 'milliseconds');
test.done();
},
"getters programmatic" : function (test) {
test.expect(8);
var a = moment([2011, 9, 12, 6, 7, 8, 9]);
test.equal(a.get('year'), 2011, 'year');
test.equal(a.get('month'), 9, 'month');
test.equal(a.get('date'), 12, 'date');
test.equal(a.get('day'), 3, 'day');
test.equal(a.get('hour'), 6, 'hour');
test.equal(a.get('minute'), 7, 'minute');
test.equal(a.get('second'), 8, 'second');
test.equal(a.get('milliseconds'), 9, 'milliseconds');
test.done();
},
"setters plural" : function (test) {
test.expect(8);
var a = moment();
a.years(2011);
a.months(9);
a.dates(12);
a.hours(6);
a.minutes(7);
a.seconds(8);
a.milliseconds(9);
test.equal(a.years(), 2011, 'years');
test.equal(a.months(), 9, 'months');
test.equal(a.dates(), 12, 'dates');
test.equal(a.days(), 3, 'days');
test.equal(a.hours(), 6, 'hours');
test.equal(a.minutes(), 7, 'minutes');
test.equal(a.seconds(), 8, 'seconds');
test.equal(a.milliseconds(), 9, 'milliseconds');
test.done();
},
"setters singular" : function (test) {
test.expect(8);
var a = moment();
a.year(2011);
a.month(9);
a.date(12);
a.hour(6);
a.minute(7);
a.second(8);
a.millisecond(9);
test.equal(a.year(), 2011, 'year');
test.equal(a.month(), 9, 'month');
test.equal(a.date(), 12, 'date');
test.equal(a.day(), 3, 'day');
test.equal(a.hour(), 6, 'hour');
test.equal(a.minute(), 7, 'minute');
test.equal(a.second(), 8, 'second');
test.equal(a.millisecond(), 9, 'milliseconds');
test.done();
},
"setters" : function (test) {
test.expect(9);
var a = moment();
a.year(2011);
a.month(9);
a.date(12);
a.hours(6);
a.minutes(7);
a.seconds(8);
a.milliseconds(9);
test.equal(a.year(), 2011, 'year');
test.equal(a.month(), 9, 'month');
test.equal(a.date(), 12, 'date');
test.equal(a.day(), 3, 'day');
test.equal(a.hours(), 6, 'hour');
test.equal(a.minutes(), 7, 'minute');
test.equal(a.seconds(), 8, 'second');
test.equal(a.milliseconds(), 9, 'milliseconds');
// Test month() behavior. See https://github.com/timrwood/moment/pull/822
a = moment('20130531', 'YYYYMMDD');
a.month(3);
test.equal(a.month(), 3, 'month edge case');
test.done();
},
"setter programmatic" : function (test) {
test.expect(9);
var a = moment();
a.set('year', 2011);
a.set('month', 9);
a.set('date', 12);
a.set('hours', 6);
a.set('minutes', 7);
a.set('seconds', 8);
a.set('milliseconds', 9);
test.equal(a.year(), 2011, 'year');
test.equal(a.month(), 9, 'month');
test.equal(a.date(), 12, 'date');
test.equal(a.day(), 3, 'day');
test.equal(a.hours(), 6, 'hour');
test.equal(a.minutes(), 7, 'minute');
test.equal(a.seconds(), 8, 'second');
test.equal(a.milliseconds(), 9, 'milliseconds');
// Test month() behavior. See https://github.com/timrwood/moment/pull/822
a = moment('20130531', 'YYYYMMDD');
a.month(3);
test.equal(a.month(), 3, 'month edge case');
test.done();
},
"setters strings" : function (test) {
test.expect(7);
var a = moment([2012]).lang('en');
test.equal(a.clone().day(0).day('Wednesday').day(), 3, 'day full name');
test.equal(a.clone().day(0).day('Wed').day(), 3, 'day short name');
test.equal(a.clone().day(0).day('We').day(), 3, 'day minimal name');
test.equal(a.clone().day(0).day('invalid').day(), 0, 'invalid day name');
test.equal(a.clone().month(0).month('April').month(), 3, 'month full name');
test.equal(a.clone().month(0).month('Apr').month(), 3, 'month short name');
test.equal(a.clone().month(0).month('invalid').month(), 0, 'invalid month name');
test.done();
},
"setters - falsey values" : function (test) {
test.expect(1);
var a = moment();
// ensure minutes wasn't coincidentally 0 already
a.minutes(1);
a.minutes(0);
test.equal(a.minutes(), 0, 'falsey value');
test.done();
},
"chaining setters" : function (test) {
test.expect(7);
var a = moment();
a.year(2011)
.month(9)
.date(12)
.hours(6)
.minutes(7)
.seconds(8);
test.equal(a.year(), 2011, 'year');
test.equal(a.month(), 9, 'month');
test.equal(a.date(), 12, 'date');
test.equal(a.day(), 3, 'day');
test.equal(a.hours(), 6, 'hour');
test.equal(a.minutes(), 7, 'minute');
test.equal(a.seconds(), 8, 'second');
test.done();
},
"day setter" : function (test) {
test.expect(18);
var a = moment([2011, 0, 15]);
test.equal(moment(a).day(0).date(), 9, 'set from saturday to sunday');
test.equal(moment(a).day(6).date(), 15, 'set from saturday to saturday');
test.equal(moment(a).day(3).date(), 12, 'set from saturday to wednesday');
a = moment([2011, 0, 9]);
test.equal(moment(a).day(0).date(), 9, 'set from sunday to sunday');
test.equal(moment(a).day(6).date(), 15, 'set from sunday to saturday');
test.equal(moment(a).day(3).date(), 12, 'set from sunday to wednesday');
a = moment([2011, 0, 12]);
test.equal(moment(a).day(0).date(), 9, 'set from wednesday to sunday');
test.equal(moment(a).day(6).date(), 15, 'set from wednesday to saturday');
test.equal(moment(a).day(3).date(), 12, 'set from wednesday to wednesday');
test.equal(moment(a).day(-7).date(), 2, 'set from wednesday to last sunday');
test.equal(moment(a).day(-1).date(), 8, 'set from wednesday to last saturday');
test.equal(moment(a).day(-4).date(), 5, 'set from wednesday to last wednesday');
test.equal(moment(a).day(7).date(), 16, 'set from wednesday to next sunday');
test.equal(moment(a).day(13).date(), 22, 'set from wednesday to next saturday');
test.equal(moment(a).day(10).date(), 19, 'set from wednesday to next wednesday');
test.equal(moment(a).day(14).date(), 23, 'set from wednesday to second next sunday');
test.equal(moment(a).day(20).date(), 29, 'set from wednesday to second next saturday');
test.equal(moment(a).day(17).date(), 26, 'set from wednesday to second next wednesday');
test.done();
}
};

View File

@@ -0,0 +1,188 @@
var moment = require("../../moment");
exports.is_after = {
"is after without units" : function (test) {
test.expect(17);
var m = moment(new Date(2011, 3, 2, 3, 4, 5, 10)), mCopy = moment(m);
test.equal(m.isAfter(moment(new Date(2012, 3, 2, 3, 5, 5, 10))), false, "year is later");
test.equal(m.isAfter(moment(new Date(2010, 3, 2, 3, 3, 5, 10))), true, "year is earlier");
test.equal(m.isAfter(moment(new Date(2011, 4, 2, 3, 4, 5, 10))), false, "month is later");
test.equal(m.isAfter(moment(new Date(2011, 2, 2, 3, 4, 5, 10))), true, "month is earlier");
test.equal(m.isAfter(moment(new Date(2011, 3, 3, 3, 4, 5, 10))), false, "day is later");
test.equal(m.isAfter(moment(new Date(2011, 3, 1, 3, 4, 5, 10))), true, "day is earlier");
test.equal(m.isAfter(moment(new Date(2011, 3, 2, 4, 4, 5, 10))), false, "hour is later");
test.equal(m.isAfter(moment(new Date(2011, 3, 2, 2, 4, 5, 10))), true, "hour is earlier");
test.equal(m.isAfter(moment(new Date(2011, 3, 2, 3, 5, 5, 10))), false, "minute is later");
test.equal(m.isAfter(moment(new Date(2011, 3, 2, 3, 3, 5, 10))), true, "minute is earlier");
test.equal(m.isAfter(moment(new Date(2011, 3, 2, 3, 4, 6, 10))), false, "second is later");
test.equal(m.isAfter(moment(new Date(2011, 3, 2, 3, 4, 4, 11))), true, "second is earlier");
test.equal(m.isAfter(moment(new Date(2011, 3, 2, 3, 4, 5, 10))), false, "millisecond match");
test.equal(m.isAfter(moment(new Date(2011, 3, 2, 3, 4, 5, 11))), false, "millisecond is later");
test.equal(m.isAfter(moment(new Date(2011, 3, 2, 3, 4, 5, 9))), true, "millisecond is earlier");
test.equal(m.isAfter(m), false, "moments are not after themselves");
test.equal(+m, +mCopy, "isAfter second should not change moment");
test.done();
},
"is after year" : function (test) {
test.expect(11);
var m = moment(new Date(2011, 1, 2, 3, 4, 5, 6)), mCopy = moment(m);
test.equal(m.isAfter(moment(new Date(2011, 5, 6, 7, 8, 9, 10)), 'year'), false, "year match");
test.equal(m.isAfter(moment(new Date(2010, 5, 6, 7, 8, 9, 10)), 'years'), true, "plural should work");
test.equal(m.isAfter(moment(new Date(2013, 5, 6, 7, 8, 9, 10)), 'year'), false, "year is later");
test.equal(m.isAfter(moment(new Date(2010, 5, 6, 7, 8, 9, 10)), 'year'), true, "year is earlier");
test.equal(m.isAfter(moment(new Date(2011, 0, 1, 0, 0, 0, 0)), 'year'), false, "exact start of year");
test.equal(m.isAfter(moment(new Date(2011, 11, 31, 23, 59, 59, 999)), 'year'), false, "exact end of year");
test.equal(m.isAfter(moment(new Date(2012, 0, 1, 0, 0, 0, 0)), 'year'), false, "start of next year");
test.equal(m.isAfter(moment(new Date(2010, 11, 31, 23, 59, 59, 999)), 'year'), true, "end of previous year");
test.equal(m.isAfter(moment(new Date(1980, 11, 31, 23, 59, 59, 999)), 'year'), true, "end of year far before");
test.equal(m.isAfter(m, 'year'), false, "same moments are not after the same year");
test.equal(+m, +mCopy, "isAfter year should not change moment");
test.done();
},
"is after month" : function (test) {
test.expect(13);
var m = moment(new Date(2011, 2, 3, 4, 5, 6, 7)), mCopy = moment(m);
test.equal(m.isAfter(moment(new Date(2011, 2, 6, 7, 8, 9, 10)), 'month'), false, "month match");
test.equal(m.isAfter(moment(new Date(2010, 2, 6, 7, 8, 9, 10)), 'months'), true, "plural should work");
test.equal(m.isAfter(moment(new Date(2012, 2, 6, 7, 8, 9, 10)), 'month'), false, "year is later");
test.equal(m.isAfter(moment(new Date(2010, 2, 6, 7, 8, 9, 10)), 'month'), true, "year is earlier");
test.equal(m.isAfter(moment(new Date(2011, 5, 6, 7, 8, 9, 10)), 'month'), false, "month is later");
test.equal(m.isAfter(moment(new Date(2011, 1, 6, 7, 8, 9, 10)), 'month'), true, "month is earlier");
test.equal(m.isAfter(moment(new Date(2011, 2, 1, 0, 0, 0, 0)), 'month'), false, "exact start of month");
test.equal(m.isAfter(moment(new Date(2011, 2, 31, 23, 59, 59, 999)), 'month'), false, "exact end of month");
test.equal(m.isAfter(moment(new Date(2011, 3, 1, 0, 0, 0, 0)), 'month'), false, "start of next month");
test.equal(m.isAfter(moment(new Date(2011, 1, 27, 23, 59, 59, 999)), 'month'), true, "end of previous month");
test.equal(m.isAfter(moment(new Date(2010, 12, 31, 23, 59, 59, 999)), 'month'), true, "later month but earlier year");
test.equal(m.isAfter(m, 'month'), false, "same moments are not after the same month");
test.equal(+m, +mCopy, "isAfter month should not change moment");
test.done();
},
"is after day" : function (test) {
test.expect(15);
var m = moment(new Date(2011, 3, 2, 3, 4, 5, 6)), mCopy = moment(m);
test.equal(m.isAfter(moment(new Date(2011, 3, 2, 7, 8, 9, 10)), 'day'), false, "day match");
test.equal(m.isAfter(moment(new Date(2010, 3, 2, 7, 8, 9, 10)), 'days'), true, "plural should work");
test.equal(m.isAfter(moment(new Date(2012, 3, 2, 7, 8, 9, 10)), 'day'), false, "year is later");
test.equal(m.isAfter(moment(new Date(2010, 3, 2, 7, 8, 9, 10)), 'day'), true, "year is earlier");
test.equal(m.isAfter(moment(new Date(2011, 4, 2, 7, 8, 9, 10)), 'day'), false, "month is later");
test.equal(m.isAfter(moment(new Date(2011, 2, 2, 7, 8, 9, 10)), 'day'), true, "month is earlier");
test.equal(m.isAfter(moment(new Date(2011, 3, 3, 7, 8, 9, 10)), 'day'), false, "day is later");
test.equal(m.isAfter(moment(new Date(2011, 3, 1, 7, 8, 9, 10)), 'day'), true, "day is earlier");
test.equal(m.isAfter(moment(new Date(2011, 3, 2, 0, 0, 0, 0)), 'day'), false, "exact start of day");
test.equal(m.isAfter(moment(new Date(2011, 3, 2, 23, 59, 59, 999)), 'day'), false, "exact end of day");
test.equal(m.isAfter(moment(new Date(2011, 3, 3, 0, 0, 0, 0)), 'day'), false, "start of next day");
test.equal(m.isAfter(moment(new Date(2011, 3, 1, 23, 59, 59, 999)), 'day'), true, "end of previous day");
test.equal(m.isAfter(moment(new Date(2010, 3, 10, 0, 0, 0, 0)), 'day'), true, "later day but earlier year");
test.equal(m.isAfter(m, 'day'), false, "same moments are not after the same day");
test.equal(+m, +mCopy, "isAfter day should not change moment");
test.done();
},
"is after hour" : function (test) {
test.expect(16);
var m = moment(new Date(2011, 3, 2, 3, 4, 5, 6)), mCopy = moment(m);
test.equal(m.isAfter(moment(new Date(2011, 3, 2, 3, 8, 9, 10)), 'hour'), false, "hour match");
test.equal(m.isAfter(moment(new Date(2010, 3, 2, 3, 8, 9, 10)), 'hours'), true, "plural should work");
test.equal(m.isAfter(moment(new Date(2012, 3, 2, 3, 8, 9, 10)), 'hour'), false, "year is later");
test.equal(m.isAfter(moment(new Date(2010, 3, 2, 3, 8, 9, 10)), 'hour'), true, "year is earlier");
test.equal(m.isAfter(moment(new Date(2011, 4, 2, 3, 8, 9, 10)), 'hour'), false, "month is later");
test.equal(m.isAfter(moment(new Date(2011, 1, 2, 3, 8, 9, 10)), 'hour'), true, "month is earlier");
test.equal(m.isAfter(moment(new Date(2011, 3, 3, 3, 8, 9, 10)), 'hour'), false, "day is later");
test.equal(m.isAfter(moment(new Date(2011, 3, 1, 3, 8, 9, 10)), 'hour'), true, "day is earlier");
test.equal(m.isAfter(moment(new Date(2011, 3, 2, 4, 8, 9, 10)), 'hour'), false, "hour is later");
test.equal(m.isAfter(moment(new Date(2011, 3, 2, 3, 8, 9, 10)), 'hour'), false, "hour is earlier");
test.equal(m.isAfter(moment(new Date(2011, 3, 2, 3, 0, 0, 0)), 'hour'), false, "exact start of hour");
test.equal(m.isAfter(moment(new Date(2011, 3, 2, 3, 59, 59, 999)), 'hour'), false, "exact end of hour");
test.equal(m.isAfter(moment(new Date(2011, 3, 2, 4, 0, 0, 0)), 'hour'), false, "start of next hour");
test.equal(m.isAfter(moment(new Date(2011, 3, 2, 2, 59, 59, 999)), 'hour'), true, "end of previous hour");
test.equal(m.isAfter(m, 'hour'), false, "same moments are not after the same hour");
test.equal(+m, +mCopy, "isAfter hour should not change moment");
test.done();
},
"is after minute" : function (test) {
test.expect(18);
var m = moment(new Date(2011, 3, 2, 3, 4, 5, 6)), mCopy = moment(m);
test.equal(m.isAfter(moment(new Date(2011, 3, 2, 3, 4, 9, 10)), 'minute'), false, "minute match");
test.equal(m.isAfter(moment(new Date(2010, 3, 2, 3, 4, 9, 10)), 'minutes'), true, "plural should work");
test.equal(m.isAfter(moment(new Date(2012, 3, 2, 3, 4, 9, 10)), 'minute'), false, "year is later");
test.equal(m.isAfter(moment(new Date(2010, 3, 2, 3, 4, 9, 10)), 'minute'), true, "year is earlier");
test.equal(m.isAfter(moment(new Date(2011, 4, 2, 3, 4, 9, 10)), 'minute'), false, "month is later");
test.equal(m.isAfter(moment(new Date(2011, 2, 2, 3, 4, 9, 10)), 'minute'), true, "month is earlier");
test.equal(m.isAfter(moment(new Date(2011, 3, 3, 3, 4, 9, 10)), 'minute'), false, "day is later");
test.equal(m.isAfter(moment(new Date(2011, 3, 1, 3, 4, 9, 10)), 'minute'), true, "day is earlier");
test.equal(m.isAfter(moment(new Date(2011, 3, 2, 4, 4, 9, 10)), 'minute'), false, "hour is later");
test.equal(m.isAfter(moment(new Date(2011, 3, 2, 2, 4, 9, 10)), 'minute'), true, "hour is earler");
test.equal(m.isAfter(moment(new Date(2011, 3, 2, 3, 5, 9, 10)), 'minute'), false, "minute is later");
test.equal(m.isAfter(moment(new Date(2011, 3, 2, 3, 3, 9, 10)), 'minute'), true, "minute is earlier");
test.equal(m.isAfter(moment(new Date(2011, 3, 2, 3, 4, 0, 0)), 'minute'), false, "exact start of minute");
test.equal(m.isAfter(moment(new Date(2011, 3, 2, 3, 4, 59, 999)), 'minute'), false, "exact end of minute");
test.equal(m.isAfter(moment(new Date(2011, 3, 2, 3, 5, 0, 0)), 'minute'), false, "start of next minute");
test.equal(m.isAfter(moment(new Date(2011, 3, 2, 3, 3, 59, 999)), 'minute'), true, "end of previous minute");
test.equal(m.isAfter(m, 'minute'), false, "same moments are not after the same minute");
test.equal(+m, +mCopy, "isAfter minute should not change moment");
test.done();
},
"is after second" : function (test) {
test.expect(20);
var m = moment(new Date(2011, 3, 2, 3, 4, 5, 10)), mCopy = moment(m);
test.equal(m.isAfter(moment(new Date(2011, 3, 2, 3, 4, 5, 10)), 'second'), false, "second match");
test.equal(m.isAfter(moment(new Date(2010, 3, 2, 3, 4, 5, 10)), 'seconds'), true, "plural should work");
test.equal(m.isAfter(moment(new Date(2012, 3, 2, 3, 4, 5, 10)), 'second'), false, "year is later");
test.equal(m.isAfter(moment(new Date(2010, 3, 2, 3, 4, 5, 10)), 'second'), true, "year is earlier");
test.equal(m.isAfter(moment(new Date(2011, 4, 2, 3, 4, 5, 10)), 'second'), false, "month is later");
test.equal(m.isAfter(moment(new Date(2011, 2, 2, 3, 4, 5, 10)), 'second'), true, "month is earlier");
test.equal(m.isAfter(moment(new Date(2011, 3, 3, 3, 4, 5, 10)), 'second'), false, "day is later");
test.equal(m.isAfter(moment(new Date(2011, 3, 1, 1, 4, 5, 10)), 'second'), true, "day is earlier");
test.equal(m.isAfter(moment(new Date(2011, 3, 2, 4, 4, 5, 10)), 'second'), false, "hour is later");
test.equal(m.isAfter(moment(new Date(2011, 3, 1, 4, 1, 5, 10)), 'second'), true, "hour is earlier");
test.equal(m.isAfter(moment(new Date(2011, 3, 2, 3, 5, 5, 10)), 'second'), false, "minute is later");
test.equal(m.isAfter(moment(new Date(2011, 3, 2, 3, 3, 5, 10)), 'second'), true, "minute is earlier");
test.equal(m.isAfter(moment(new Date(2011, 3, 2, 3, 4, 6, 10)), 'second'), false, "second is later");
test.equal(m.isAfter(moment(new Date(2011, 3, 2, 3, 4, 4, 5)), 'second'), true, "second is earlier");
test.equal(m.isAfter(moment(new Date(2011, 3, 2, 3, 4, 5, 0)), 'second'), false, "exact start of second");
test.equal(m.isAfter(moment(new Date(2011, 3, 2, 3, 4, 5, 999)), 'second'), false, "exact end of second");
test.equal(m.isAfter(moment(new Date(2011, 3, 2, 3, 4, 6, 0)), 'second'), false, "start of next second");
test.equal(m.isAfter(moment(new Date(2011, 3, 2, 3, 4, 4, 999)), 'second'), true, "end of previous second");
test.equal(m.isAfter(m, 'second'), false, "same moments are not after the same second");
test.equal(+m, +mCopy, "isAfter second should not change moment");
test.done();
},
"is after millisecond" : function (test) {
test.expect(18);
var m = moment(new Date(2011, 3, 2, 3, 4, 5, 10)), mCopy = moment(m);
test.equal(m.isAfter(moment(new Date(2011, 3, 2, 3, 4, 5, 10)), 'millisecond'), false, "millisecond match");
test.equal(m.isAfter(moment(new Date(2010, 3, 2, 3, 4, 5, 10)), 'milliseconds'), true, "plural should work");
test.equal(m.isAfter(moment(new Date(2012, 3, 2, 3, 4, 5, 10)), 'millisecond'), false, "year is later");
test.equal(m.isAfter(moment(new Date(2010, 3, 2, 3, 4, 5, 10)), 'millisecond'), true, "year is earlier");
test.equal(m.isAfter(moment(new Date(2011, 4, 2, 3, 4, 5, 10)), 'millisecond'), false, "month is later");
test.equal(m.isAfter(moment(new Date(2011, 2, 2, 3, 4, 5, 10)), 'millisecond'), true, "month is earlier");
test.equal(m.isAfter(moment(new Date(2011, 3, 3, 3, 4, 5, 10)), 'millisecond'), false, "day is later");
test.equal(m.isAfter(moment(new Date(2011, 3, 1, 1, 4, 5, 10)), 'millisecond'), true, "day is earlier");
test.equal(m.isAfter(moment(new Date(2011, 3, 2, 4, 4, 5, 10)), 'millisecond'), false, "hour is later");
test.equal(m.isAfter(moment(new Date(2011, 3, 1, 4, 1, 5, 10)), 'millisecond'), true, "hour is earlier");
test.equal(m.isAfter(moment(new Date(2011, 3, 2, 3, 5, 5, 10)), 'millisecond'), false, "minute is later");
test.equal(m.isAfter(moment(new Date(2011, 3, 2, 3, 3, 5, 10)), 'millisecond'), true, "minute is earlier");
test.equal(m.isAfter(moment(new Date(2011, 3, 2, 3, 4, 6, 10)), 'millisecond'), false, "second is later");
test.equal(m.isAfter(moment(new Date(2011, 3, 2, 3, 4, 4, 5)), 'millisecond'), true, "second is earlier");
test.equal(m.isAfter(moment(new Date(2011, 3, 2, 3, 4, 6, 11)), 'millisecond'), false, "millisecond is later");
test.equal(m.isAfter(moment(new Date(2011, 3, 2, 3, 4, 4, 9)), 'millisecond'), true, "millisecond is earlier");
test.equal(m.isAfter(m, 'millisecond'), false, "same moments are not after the same millisecond");
test.equal(+m, +mCopy, "isAfter millisecond should not change moment");
test.done();
}
};

View File

@@ -0,0 +1,187 @@
var moment = require("../../moment");
exports.is_before = {
"is after without units" : function (test) {
test.expect(17);
var m = moment(new Date(2011, 3, 2, 3, 4, 5, 10)), mCopy = moment(m);
test.equal(m.isBefore(moment(new Date(2012, 3, 2, 3, 5, 5, 10))), true, "year is later");
test.equal(m.isBefore(moment(new Date(2010, 3, 2, 3, 3, 5, 10))), false, "year is earlier");
test.equal(m.isBefore(moment(new Date(2011, 4, 2, 3, 4, 5, 10))), true, "month is later");
test.equal(m.isBefore(moment(new Date(2011, 2, 2, 3, 4, 5, 10))), false, "month is earlier");
test.equal(m.isBefore(moment(new Date(2011, 3, 3, 3, 4, 5, 10))), true, "day is later");
test.equal(m.isBefore(moment(new Date(2011, 3, 1, 3, 4, 5, 10))), false, "day is earlier");
test.equal(m.isBefore(moment(new Date(2011, 3, 2, 4, 4, 5, 10))), true, "hour is later");
test.equal(m.isBefore(moment(new Date(2011, 3, 2, 2, 4, 5, 10))), false, "hour is earlier");
test.equal(m.isBefore(moment(new Date(2011, 3, 2, 3, 5, 5, 10))), true, "minute is later");
test.equal(m.isBefore(moment(new Date(2011, 3, 2, 3, 3, 5, 10))), false, "minute is earlier");
test.equal(m.isBefore(moment(new Date(2011, 3, 2, 3, 4, 6, 10))), true, "second is later");
test.equal(m.isBefore(moment(new Date(2011, 3, 2, 3, 4, 4, 11))), false, "second is earlier");
test.equal(m.isBefore(moment(new Date(2011, 3, 2, 3, 4, 5, 10))), false, "millisecond match");
test.equal(m.isBefore(moment(new Date(2011, 3, 2, 3, 4, 5, 11))), true, "millisecond is later");
test.equal(m.isBefore(moment(new Date(2011, 3, 2, 3, 4, 5, 9))), false, "millisecond is earlier");
test.equal(m.isBefore(m), false, "moments are not before themselves");
test.equal(+m, +mCopy, "isBefore second should not change moment");
test.done();
},
"is before year" : function (test) {
test.expect(11);
var m = moment(new Date(2011, 1, 2, 3, 4, 5, 6)), mCopy = moment(m);
test.equal(m.isBefore(moment(new Date(2011, 5, 6, 7, 8, 9, 10)), 'year'), false, "year match");
test.equal(m.isBefore(moment(new Date(2012, 5, 6, 7, 8, 9, 10)), 'years'), true, "plural should work");
test.equal(m.isBefore(moment(new Date(2013, 5, 6, 7, 8, 9, 10)), 'year'), true, "year is later");
test.equal(m.isBefore(moment(new Date(2010, 5, 6, 7, 8, 9, 10)), 'year'), false, "year is earlier");
test.equal(m.isBefore(moment(new Date(2011, 0, 1, 0, 0, 0, 0)), 'year'), false, "exact start of year");
test.equal(m.isBefore(moment(new Date(2011, 11, 31, 23, 59, 59, 999)), 'year'), false, "exact end of year");
test.equal(m.isBefore(moment(new Date(2012, 0, 1, 0, 0, 0, 0)), 'year'), true, "start of next year");
test.equal(m.isBefore(moment(new Date(2010, 11, 31, 23, 59, 59, 999)), 'year'), false, "end of previous year");
test.equal(m.isBefore(moment(new Date(1980, 11, 31, 23, 59, 59, 999)), 'year'), false, "end of year far before");
test.equal(m.isBefore(m, 'year'), false, "same moments are not before the same year");
test.equal(+m, +mCopy, "isBefore year should not change moment");
test.done();
},
"is before month" : function (test) {
test.expect(13);
var m = moment(new Date(2011, 2, 3, 4, 5, 6, 7)), mCopy = moment(m);
test.equal(m.isBefore(moment(new Date(2011, 2, 6, 7, 8, 9, 10)), 'month'), false, "month match");
test.equal(m.isBefore(moment(new Date(2012, 2, 6, 7, 8, 9, 10)), 'months'), true, "plural should work");
test.equal(m.isBefore(moment(new Date(2012, 2, 6, 7, 8, 9, 10)), 'month'), true, "year is later");
test.equal(m.isBefore(moment(new Date(2010, 2, 6, 7, 8, 9, 10)), 'month'), false, "year is earlier");
test.equal(m.isBefore(moment(new Date(2011, 5, 6, 7, 8, 9, 10)), 'month'), true, "month is later");
test.equal(m.isBefore(moment(new Date(2011, 1, 6, 7, 8, 9, 10)), 'month'), false, "month is earlier");
test.equal(m.isBefore(moment(new Date(2011, 2, 1, 0, 0, 0, 0)), 'month'), false, "exact start of month");
test.equal(m.isBefore(moment(new Date(2011, 2, 31, 23, 59, 59, 999)), 'month'), false, "exact end of month");
test.equal(m.isBefore(moment(new Date(2011, 3, 1, 0, 0, 0, 0)), 'month'), true, "start of next month");
test.equal(m.isBefore(moment(new Date(2011, 1, 27, 23, 59, 59, 999)), 'month'), false, "end of previous month");
test.equal(m.isBefore(moment(new Date(2010, 12, 31, 23, 59, 59, 999)), 'month'), false, "later month but earlier year");
test.equal(m.isBefore(m, 'month'), false, "same moments are not before the same month");
test.equal(+m, +mCopy, "isBefore month should not change moment");
test.done();
},
"is before day" : function (test) {
test.expect(15);
var m = moment(new Date(2011, 3, 2, 3, 4, 5, 6)), mCopy = moment(m);
test.equal(m.isBefore(moment(new Date(2011, 3, 2, 7, 8, 9, 10)), 'day'), false, "day match");
test.equal(m.isBefore(moment(new Date(2012, 3, 2, 7, 8, 9, 10)), 'days'), true, "plural should work");
test.equal(m.isBefore(moment(new Date(2012, 3, 2, 7, 8, 9, 10)), 'day'), true, "year is later");
test.equal(m.isBefore(moment(new Date(2010, 3, 2, 7, 8, 9, 10)), 'day'), false, "year is earlier");
test.equal(m.isBefore(moment(new Date(2011, 4, 2, 7, 8, 9, 10)), 'day'), true, "month is later");
test.equal(m.isBefore(moment(new Date(2011, 2, 2, 7, 8, 9, 10)), 'day'), false, "month is earlier");
test.equal(m.isBefore(moment(new Date(2011, 3, 3, 7, 8, 9, 10)), 'day'), true, "day is later");
test.equal(m.isBefore(moment(new Date(2011, 3, 1, 7, 8, 9, 10)), 'day'), false, "day is earlier");
test.equal(m.isBefore(moment(new Date(2011, 3, 2, 0, 0, 0, 0)), 'day'), false, "exact start of day");
test.equal(m.isBefore(moment(new Date(2011, 3, 2, 23, 59, 59, 999)), 'day'), false, "exact end of day");
test.equal(m.isBefore(moment(new Date(2011, 3, 3, 0, 0, 0, 0)), 'day'), true, "start of next day");
test.equal(m.isBefore(moment(new Date(2011, 3, 1, 23, 59, 59, 999)), 'day'), false, "end of previous day");
test.equal(m.isBefore(moment(new Date(2010, 3, 10, 0, 0, 0, 0)), 'day'), false, "later day but earlier year");
test.equal(m.isBefore(m, 'day'), false, "same moments are not before the same day");
test.equal(+m, +mCopy, "isBefore day should not change moment");
test.done();
},
"is before hour" : function (test) {
test.expect(16);
var m = moment(new Date(2011, 3, 2, 3, 4, 5, 6)), mCopy = moment(m);
test.equal(m.isBefore(moment(new Date(2011, 3, 2, 3, 8, 9, 10)), 'hour'), false, "hour match");
test.equal(m.isBefore(moment(new Date(2012, 3, 2, 3, 8, 9, 10)), 'hours'), true, "plural should work");
test.equal(m.isBefore(moment(new Date(2012, 3, 2, 3, 8, 9, 10)), 'hour'), true, "year is later");
test.equal(m.isBefore(moment(new Date(2010, 3, 2, 3, 8, 9, 10)), 'hour'), false, "year is earlier");
test.equal(m.isBefore(moment(new Date(2011, 4, 2, 3, 8, 9, 10)), 'hour'), true, "month is later");
test.equal(m.isBefore(moment(new Date(2011, 1, 2, 3, 8, 9, 10)), 'hour'), false, "month is earlier");
test.equal(m.isBefore(moment(new Date(2011, 3, 3, 3, 8, 9, 10)), 'hour'), true, "day is later");
test.equal(m.isBefore(moment(new Date(2011, 3, 1, 3, 8, 9, 10)), 'hour'), false, "day is earlier");
test.equal(m.isBefore(moment(new Date(2011, 3, 2, 4, 8, 9, 10)), 'hour'), true, "hour is later");
test.equal(m.isBefore(moment(new Date(2011, 3, 2, 3, 8, 9, 10)), 'hour'), false, "hour is earlier");
test.equal(m.isBefore(moment(new Date(2011, 3, 2, 3, 0, 0, 0)), 'hour'), false, "exact start of hour");
test.equal(m.isBefore(moment(new Date(2011, 3, 2, 3, 59, 59, 999)), 'hour'), false, "exact end of hour");
test.equal(m.isBefore(moment(new Date(2011, 3, 2, 4, 0, 0, 0)), 'hour'), true, "start of next hour");
test.equal(m.isBefore(moment(new Date(2011, 3, 2, 2, 59, 59, 999)), 'hour'), false, "end of previous hour");
test.equal(m.isBefore(m, 'hour'), false, "same moments are not before the same hour");
test.equal(+m, +mCopy, "isBefore hour should not change moment");
test.done();
},
"is before minute" : function (test) {
test.expect(18);
var m = moment(new Date(2011, 3, 2, 3, 4, 5, 6)), mCopy = moment(m);
test.equal(m.isBefore(moment(new Date(2011, 3, 2, 3, 4, 9, 10)), 'minute'), false, "minute match");
test.equal(m.isBefore(moment(new Date(2012, 3, 2, 3, 4, 9, 10)), 'minutes'), true, "plural should work");
test.equal(m.isBefore(moment(new Date(2012, 3, 2, 3, 4, 9, 10)), 'minute'), true, "year is later");
test.equal(m.isBefore(moment(new Date(2010, 3, 2, 3, 4, 9, 10)), 'minute'), false, "year is earlier");
test.equal(m.isBefore(moment(new Date(2011, 4, 2, 3, 4, 9, 10)), 'minute'), true, "month is later");
test.equal(m.isBefore(moment(new Date(2011, 2, 2, 3, 4, 9, 10)), 'minute'), false, "month is earlier");
test.equal(m.isBefore(moment(new Date(2011, 3, 3, 3, 4, 9, 10)), 'minute'), true, "day is later");
test.equal(m.isBefore(moment(new Date(2011, 3, 1, 3, 4, 9, 10)), 'minute'), false, "day is earlier");
test.equal(m.isBefore(moment(new Date(2011, 3, 2, 4, 4, 9, 10)), 'minute'), true, "hour is later");
test.equal(m.isBefore(moment(new Date(2011, 3, 2, 2, 4, 9, 10)), 'minute'), false, "hour is earler");
test.equal(m.isBefore(moment(new Date(2011, 3, 2, 3, 5, 9, 10)), 'minute'), true, "minute is later");
test.equal(m.isBefore(moment(new Date(2011, 3, 2, 3, 3, 9, 10)), 'minute'), false, "minute is earlier");
test.equal(m.isBefore(moment(new Date(2011, 3, 2, 3, 4, 0, 0)), 'minute'), false, "exact start of minute");
test.equal(m.isBefore(moment(new Date(2011, 3, 2, 3, 4, 59, 999)), 'minute'), false, "exact end of minute");
test.equal(m.isBefore(moment(new Date(2011, 3, 2, 3, 5, 0, 0)), 'minute'), true, "start of next minute");
test.equal(m.isBefore(moment(new Date(2011, 3, 2, 3, 3, 59, 999)), 'minute'), false, "end of previous minute");
test.equal(m.isBefore(m, 'minute'), false, "same moments are not before the same minute");
test.equal(+m, +mCopy, "isBefore minute should not change moment");
test.done();
},
"is before second" : function (test) {
test.expect(20);
var m = moment(new Date(2011, 3, 2, 3, 4, 5, 10)), mCopy = moment(m);
test.equal(m.isBefore(moment(new Date(2011, 3, 2, 3, 4, 5, 10)), 'second'), false, "second match");
test.equal(m.isBefore(moment(new Date(2012, 3, 2, 3, 4, 5, 10)), 'seconds'), true, "plural should work");
test.equal(m.isBefore(moment(new Date(2012, 3, 2, 3, 4, 5, 10)), 'second'), true, "year is later");
test.equal(m.isBefore(moment(new Date(2010, 3, 2, 3, 4, 5, 10)), 'second'), false, "year is earlier");
test.equal(m.isBefore(moment(new Date(2011, 4, 2, 3, 4, 5, 10)), 'second'), true, "month is later");
test.equal(m.isBefore(moment(new Date(2011, 2, 2, 3, 4, 5, 10)), 'second'), false, "month is earlier");
test.equal(m.isBefore(moment(new Date(2011, 3, 3, 3, 4, 5, 10)), 'second'), true, "day is later");
test.equal(m.isBefore(moment(new Date(2011, 3, 1, 1, 4, 5, 10)), 'second'), false, "day is earlier");
test.equal(m.isBefore(moment(new Date(2011, 3, 2, 4, 4, 5, 10)), 'second'), true, "hour is later");
test.equal(m.isBefore(moment(new Date(2011, 3, 1, 4, 1, 5, 10)), 'second'), false, "hour is earlier");
test.equal(m.isBefore(moment(new Date(2011, 3, 2, 3, 5, 5, 10)), 'second'), true, "minute is later");
test.equal(m.isBefore(moment(new Date(2011, 3, 2, 3, 3, 5, 10)), 'second'), false, "minute is earlier");
test.equal(m.isBefore(moment(new Date(2011, 3, 2, 3, 4, 6, 10)), 'second'), true, "second is later");
test.equal(m.isBefore(moment(new Date(2011, 3, 2, 3, 4, 4, 5)), 'second'), false, "second is earlier");
test.equal(m.isBefore(moment(new Date(2011, 3, 2, 3, 4, 5, 0)), 'second'), false, "exact start of second");
test.equal(m.isBefore(moment(new Date(2011, 3, 2, 3, 4, 5, 999)), 'second'), false, "exact end of second");
test.equal(m.isBefore(moment(new Date(2011, 3, 2, 3, 4, 6, 0)), 'second'), true, "start of next second");
test.equal(m.isBefore(moment(new Date(2011, 3, 2, 3, 4, 4, 999)), 'second'), false, "end of previous second");
test.equal(m.isBefore(m, 'second'), false, "same moments are not before the same second");
test.equal(+m, +mCopy, "isBefore second should not change moment");
test.done();
},
"is before millisecond" : function (test) {
test.expect(18);
var m = moment(new Date(2011, 3, 2, 3, 4, 5, 10)), mCopy = moment(m);
test.equal(m.isBefore(moment(new Date(2011, 3, 2, 3, 4, 5, 10)), 'millisecond'), false, "millisecond match");
test.equal(m.isBefore(moment(new Date(2010, 3, 2, 3, 4, 5, 10)), 'milliseconds'), false, "plural should work");
test.equal(m.isBefore(moment(new Date(2012, 3, 2, 3, 4, 5, 10)), 'millisecond'), true, "year is later");
test.equal(m.isBefore(moment(new Date(2010, 3, 2, 3, 4, 5, 10)), 'millisecond'), false, "year is earlier");
test.equal(m.isBefore(moment(new Date(2011, 4, 2, 3, 4, 5, 10)), 'millisecond'), true, "month is later");
test.equal(m.isBefore(moment(new Date(2011, 2, 2, 3, 4, 5, 10)), 'millisecond'), false, "month is earlier");
test.equal(m.isBefore(moment(new Date(2011, 3, 3, 3, 4, 5, 10)), 'millisecond'), true, "day is later");
test.equal(m.isBefore(moment(new Date(2011, 3, 1, 1, 4, 5, 10)), 'millisecond'), false, "day is earlier");
test.equal(m.isBefore(moment(new Date(2011, 3, 2, 4, 4, 5, 10)), 'millisecond'), true, "hour is later");
test.equal(m.isBefore(moment(new Date(2011, 3, 1, 4, 1, 5, 10)), 'millisecond'), false, "hour is earlier");
test.equal(m.isBefore(moment(new Date(2011, 3, 2, 3, 5, 5, 10)), 'millisecond'), true, "minute is later");
test.equal(m.isBefore(moment(new Date(2011, 3, 2, 3, 3, 5, 10)), 'millisecond'), false, "minute is earlier");
test.equal(m.isBefore(moment(new Date(2011, 3, 2, 3, 4, 6, 10)), 'millisecond'), true, "second is later");
test.equal(m.isBefore(moment(new Date(2011, 3, 2, 3, 4, 4, 5)), 'millisecond'), false, "second is earlier");
test.equal(m.isBefore(moment(new Date(2011, 3, 2, 3, 4, 6, 11)), 'millisecond'), true, "millisecond is later");
test.equal(m.isBefore(moment(new Date(2011, 3, 2, 3, 4, 4, 9)), 'millisecond'), false, "millisecond is earlier");
test.equal(m.isBefore(m, 'millisecond'), false, "same moments are not before the same millisecond");
test.equal(+m, +mCopy, "isBefore millisecond should not change moment");
test.done();
}
};

View File

@@ -0,0 +1,27 @@
var moment = require('../../moment');
exports.is_moment = {
"is moment object": function (test) {
test.expect(11);
var MyObj = function () {};
MyObj.prototype.toDate = function () {
return new Date();
};
test.ok(moment.isMoment(moment()), 'simple moment object');
test.ok(moment.isMoment(moment('invalid date')), 'invalid moment object');
test.ok(!moment.isMoment(new MyObj()), 'myObj is not moment object');
test.ok(!moment.isMoment(moment), 'moment function is not moment object');
test.ok(!moment.isMoment(new Date()), 'date object is not moment object');
test.ok(!moment.isMoment(Object), 'Object is not moment object');
test.ok(!moment.isMoment('foo'), 'string is not moment object');
test.ok(!moment.isMoment(1), 'number is not moment object');
test.ok(!moment.isMoment(NaN), 'NaN is not moment object');
test.ok(!moment.isMoment(null), 'null is not moment object');
test.ok(!moment.isMoment(undefined), 'undefined is not moment object');
test.done();
}
};

View File

@@ -0,0 +1,163 @@
var moment = require("../../moment");
exports.is_same = {
"is same without units" : function (test) {
test.expect(17);
var m = moment(new Date(2011, 3, 2, 3, 4, 5, 10)), mCopy = moment(m);
test.equal(m.isSame(moment(new Date(2012, 3, 2, 3, 5, 5, 10))), false, "year is later");
test.equal(m.isSame(moment(new Date(2010, 3, 2, 3, 3, 5, 10))), false, "year is earlier");
test.equal(m.isSame(moment(new Date(2011, 4, 2, 3, 4, 5, 10))), false, "month is later");
test.equal(m.isSame(moment(new Date(2011, 2, 2, 3, 4, 5, 10))), false, "month is earlier");
test.equal(m.isSame(moment(new Date(2011, 3, 3, 3, 4, 5, 10))), false, "day is later");
test.equal(m.isSame(moment(new Date(2011, 3, 1, 3, 4, 5, 10))), false, "day is earlier");
test.equal(m.isSame(moment(new Date(2011, 3, 2, 4, 4, 5, 10))), false, "hour is later");
test.equal(m.isSame(moment(new Date(2011, 3, 2, 2, 4, 5, 10))), false, "hour is earlier");
test.equal(m.isSame(moment(new Date(2011, 3, 2, 3, 5, 5, 10))), false, "minute is later");
test.equal(m.isSame(moment(new Date(2011, 3, 2, 3, 3, 5, 10))), false, "minute is earlier");
test.equal(m.isSame(moment(new Date(2011, 3, 2, 3, 4, 6, 10))), false, "second is later");
test.equal(m.isSame(moment(new Date(2011, 3, 2, 3, 4, 4, 11))), false, "second is earlier");
test.equal(m.isSame(moment(new Date(2011, 3, 2, 3, 4, 5, 10))), true, "millisecond match");
test.equal(m.isSame(moment(new Date(2011, 3, 2, 3, 4, 5, 11))), false, "millisecond is later");
test.equal(m.isSame(moment(new Date(2011, 3, 2, 3, 4, 5, 9))), false, "millisecond is earlier");
test.equal(m.isSame(m), true, "moments are the same as themselves");
test.equal(+m, +mCopy, "isSame second should not change moment");
test.done();
},
"is same year" : function (test) {
test.expect(9);
var m = moment(new Date(2011, 1, 2, 3, 4, 5, 6)), mCopy = moment(m);
test.equal(m.isSame(moment(new Date(2011, 5, 6, 7, 8, 9, 10)), 'year'), true, "year match");
test.equal(m.isSame(moment(new Date(2011, 5, 6, 7, 8, 9, 10)), 'years'), true, "plural should work");
test.equal(m.isSame(moment(new Date(2012, 5, 6, 7, 8, 9, 10)), 'year'), false, "year mismatch");
test.equal(m.isSame(moment(new Date(2011, 0, 1, 0, 0, 0, 0)), 'year'), true, "exact start of year");
test.equal(m.isSame(moment(new Date(2011, 11, 31, 23, 59, 59, 999)), 'year'), true, "exact end of year");
test.equal(m.isSame(moment(new Date(2012, 0, 1, 0, 0, 0, 0)), 'year'), false, "start of next year");
test.equal(m.isSame(moment(new Date(2010, 11, 31, 23, 59, 59, 999)), 'year'), false, "end of previous year");
test.equal(m.isSame(m, 'year'), true, "same moments are in the same year");
test.equal(+m, +mCopy, "isSame year should not change moment");
test.done();
},
"is same month" : function (test) {
test.expect(10);
var m = moment(new Date(2011, 2, 3, 4, 5, 6, 7)), mCopy = moment(m);
test.equal(m.isSame(moment(new Date(2011, 2, 6, 7, 8, 9, 10)), 'month'), true, "month match");
test.equal(m.isSame(moment(new Date(2011, 2, 6, 7, 8, 9, 10)), 'months'), true, "plural should work");
test.equal(m.isSame(moment(new Date(2012, 2, 6, 7, 8, 9, 10)), 'month'), false, "year mismatch");
test.equal(m.isSame(moment(new Date(2011, 5, 6, 7, 8, 9, 10)), 'month'), false, "month mismatch");
test.equal(m.isSame(moment(new Date(2011, 2, 1, 0, 0, 0, 0)), 'month'), true, "exact start of month");
test.equal(m.isSame(moment(new Date(2011, 2, 31, 23, 59, 59, 999)), 'month'), true, "exact end of month");
test.equal(m.isSame(moment(new Date(2011, 3, 1, 0, 0, 0, 0)), 'month'), false, "start of next month");
test.equal(m.isSame(moment(new Date(2011, 1, 27, 23, 59, 59, 999)), 'month'), false, "end of previous month");
test.equal(m.isSame(m, 'month'), true, "same moments are in the same month");
test.equal(+m, +mCopy, "isSame month should not change moment");
test.done();
},
"is same day" : function (test) {
test.expect(11);
var m = moment(new Date(2011, 1, 2, 3, 4, 5, 6)), mCopy = moment(m);
test.equal(m.isSame(moment(new Date(2011, 1, 2, 7, 8, 9, 10)), 'day'), true, "day match");
test.equal(m.isSame(moment(new Date(2011, 1, 2, 7, 8, 9, 10)), 'days'), true, "plural should work");
test.equal(m.isSame(moment(new Date(2012, 1, 2, 7, 8, 9, 10)), 'day'), false, "year mismatch");
test.equal(m.isSame(moment(new Date(2011, 2, 2, 7, 8, 9, 10)), 'day'), false, "month mismatch");
test.equal(m.isSame(moment(new Date(2011, 1, 3, 7, 8, 9, 10)), 'day'), false, "day mismatch");
test.equal(m.isSame(moment(new Date(2011, 1, 2, 0, 0, 0, 0)), 'day'), true, "exact start of day");
test.equal(m.isSame(moment(new Date(2011, 1, 2, 23, 59, 59, 999)), 'day'), true, "exact end of day");
test.equal(m.isSame(moment(new Date(2011, 1, 3, 0, 0, 0, 0)), 'day'), false, "start of next day");
test.equal(m.isSame(moment(new Date(2011, 1, 1, 23, 59, 59, 999)), 'day'), false, "end of previous day");
test.equal(m.isSame(m, 'day'), true, "same moments are in the same day");
test.equal(+m, +mCopy, "isSame day should not change moment");
test.done();
},
"is same hour" : function (test) {
test.expect(12);
var m = moment(new Date(2011, 1, 2, 3, 4, 5, 6)), mCopy = moment(m);
test.equal(m.isSame(moment(new Date(2011, 1, 2, 3, 8, 9, 10)), 'hour'), true, "hour match");
test.equal(m.isSame(moment(new Date(2011, 1, 2, 3, 8, 9, 10)), 'hours'), true, "plural should work");
test.equal(m.isSame(moment(new Date(2012, 1, 2, 3, 8, 9, 10)), 'hour'), false, "year mismatch");
test.equal(m.isSame(moment(new Date(2011, 2, 2, 3, 8, 9, 10)), 'hour'), false, "month mismatch");
test.equal(m.isSame(moment(new Date(2011, 1, 3, 3, 8, 9, 10)), 'hour'), false, "day mismatch");
test.equal(m.isSame(moment(new Date(2011, 1, 2, 4, 8, 9, 10)), 'hour'), false, "hour mismatch");
test.equal(m.isSame(moment(new Date(2011, 1, 2, 3, 0, 0, 0)), 'hour'), true, "exact start of hour");
test.equal(m.isSame(moment(new Date(2011, 1, 2, 3, 59, 59, 999)), 'hour'), true, "exact end of hour");
test.equal(m.isSame(moment(new Date(2011, 1, 2, 4, 0, 0, 0)), 'hour'), false, "start of next hour");
test.equal(m.isSame(moment(new Date(2011, 1, 2, 2, 59, 59, 999)), 'hour'), false, "end of previous hour");
test.equal(m.isSame(m, 'hour'), true, "same moments are in the same hour");
test.equal(+m, +mCopy, "isSame hour should not change moment");
test.done();
},
"is same minute" : function (test) {
test.expect(13);
var m = moment(new Date(2011, 1, 2, 3, 4, 5, 6)), mCopy = moment(m);
test.equal(m.isSame(moment(new Date(2011, 1, 2, 3, 4, 9, 10)), 'minute'), true, "minute match");
test.equal(m.isSame(moment(new Date(2011, 1, 2, 3, 4, 9, 10)), 'minutes'), true, "plural should work");
test.equal(m.isSame(moment(new Date(2012, 1, 2, 3, 4, 9, 10)), 'minute'), false, "year mismatch");
test.equal(m.isSame(moment(new Date(2011, 2, 2, 3, 4, 9, 10)), 'minute'), false, "month mismatch");
test.equal(m.isSame(moment(new Date(2011, 1, 3, 3, 4, 9, 10)), 'minute'), false, "day mismatch");
test.equal(m.isSame(moment(new Date(2011, 1, 2, 4, 4, 9, 10)), 'minute'), false, "hour mismatch");
test.equal(m.isSame(moment(new Date(2011, 1, 2, 3, 5, 9, 10)), 'minute'), false, "minute mismatch");
test.equal(m.isSame(moment(new Date(2011, 1, 2, 3, 4, 0, 0)), 'minute'), true, "exact start of minute");
test.equal(m.isSame(moment(new Date(2011, 1, 2, 3, 4, 59, 999)), 'minute'), true, "exact end of minute");
test.equal(m.isSame(moment(new Date(2011, 1, 2, 3, 5, 0, 0)), 'minute'), false, "start of next minute");
test.equal(m.isSame(moment(new Date(2011, 1, 2, 3, 3, 59, 999)), 'minute'), false, "end of previous minute");
test.equal(m.isSame(m, 'minute'), true, "same moments are in the same minute");
test.equal(+m, +mCopy, "isSame minute should not change moment");
test.done();
},
"is same second" : function (test) {
test.expect(14);
var m = moment(new Date(2011, 1, 2, 3, 4, 5, 6)), mCopy = moment(m);
test.equal(m.isSame(moment(new Date(2011, 1, 2, 3, 4, 5, 10)), 'second'), true, "second match");
test.equal(m.isSame(moment(new Date(2011, 1, 2, 3, 4, 5, 10)), 'seconds'), true, "plural should work");
test.equal(m.isSame(moment(new Date(2012, 1, 2, 3, 4, 5, 10)), 'second'), false, "year mismatch");
test.equal(m.isSame(moment(new Date(2011, 2, 2, 3, 4, 5, 10)), 'second'), false, "month mismatch");
test.equal(m.isSame(moment(new Date(2011, 1, 3, 3, 4, 5, 10)), 'second'), false, "day mismatch");
test.equal(m.isSame(moment(new Date(2011, 1, 2, 4, 4, 5, 10)), 'second'), false, "hour mismatch");
test.equal(m.isSame(moment(new Date(2011, 1, 2, 3, 5, 5, 10)), 'second'), false, "minute mismatch");
test.equal(m.isSame(moment(new Date(2011, 1, 2, 3, 4, 6, 10)), 'second'), false, "second mismatch");
test.equal(m.isSame(moment(new Date(2011, 1, 2, 3, 4, 5, 0)), 'second'), true, "exact start of second");
test.equal(m.isSame(moment(new Date(2011, 1, 2, 3, 4, 5, 999)), 'second'), true, "exact end of second");
test.equal(m.isSame(moment(new Date(2011, 1, 2, 3, 4, 6, 0)), 'second'), false, "start of next second");
test.equal(m.isSame(moment(new Date(2011, 1, 2, 3, 4, 4, 999)), 'second'), false, "end of previous second");
test.equal(m.isSame(m, 'second'), true, "same moments are in the same second");
test.equal(+m, +mCopy, "isSame second should not change moment");
test.done();
},
"is same millisecond" : function (test) {
test.expect(18);
var m = moment(new Date(2011, 3, 2, 3, 4, 5, 10)), mCopy = moment(m);
test.equal(m.isSame(moment(new Date(2011, 3, 2, 3, 4, 5, 10)), 'millisecond'), true, "millisecond match");
test.equal(m.isSame(moment(new Date(2011, 3, 2, 3, 4, 5, 10)), 'milliseconds'), true, "plural should work");
test.equal(m.isSame(moment(new Date(2012, 3, 2, 3, 4, 5, 10)), 'millisecond'), false, "year is later");
test.equal(m.isSame(moment(new Date(2010, 3, 2, 3, 4, 5, 10)), 'millisecond'), false, "year is earlier");
test.equal(m.isSame(moment(new Date(2011, 4, 2, 3, 4, 5, 10)), 'millisecond'), false, "month is later");
test.equal(m.isSame(moment(new Date(2011, 2, 2, 3, 4, 5, 10)), 'millisecond'), false, "month is earlier");
test.equal(m.isSame(moment(new Date(2011, 3, 3, 3, 4, 5, 10)), 'millisecond'), false, "day is later");
test.equal(m.isSame(moment(new Date(2011, 3, 1, 1, 4, 5, 10)), 'millisecond'), false, "day is earlier");
test.equal(m.isSame(moment(new Date(2011, 3, 2, 4, 4, 5, 10)), 'millisecond'), false, "hour is later");
test.equal(m.isSame(moment(new Date(2011, 3, 1, 4, 1, 5, 10)), 'millisecond'), false, "hour is earlier");
test.equal(m.isSame(moment(new Date(2011, 3, 2, 3, 5, 5, 10)), 'millisecond'), false, "minute is later");
test.equal(m.isSame(moment(new Date(2011, 3, 2, 3, 3, 5, 10)), 'millisecond'), false, "minute is earlier");
test.equal(m.isSame(moment(new Date(2011, 3, 2, 3, 4, 6, 10)), 'millisecond'), false, "second is later");
test.equal(m.isSame(moment(new Date(2011, 3, 2, 3, 4, 4, 5)), 'millisecond'), false, "second is earlier");
test.equal(m.isSame(moment(new Date(2011, 3, 2, 3, 4, 6, 11)), 'millisecond'), false, "millisecond is later");
test.equal(m.isSame(moment(new Date(2011, 3, 2, 3, 4, 4, 9)), 'millisecond'), false, "millisecond is earlier");
test.equal(m.isSame(m, 'millisecond'), true, "same moments are in the same millisecond");
test.equal(+m, +mCopy, "isSame millisecond should not change moment");
test.done();
}
};

View File

@@ -0,0 +1,225 @@
var moment = require("../../moment");
exports.is_valid = {
"array bad month" : function (test) {
test.expect(2);
test.equal(moment([2010, -1]).isValid(), false, 'month -1');
test.equal(moment([2100, 12]).isValid(), false, 'month 12');
test.done();
},
"array good month" : function (test) {
test.expect(24);
for (var i = 0; i < 12; i++) {
test.equal(moment([2010, i]).isValid(), true, 'month ' + i);
test.equal(moment.utc([2010, i]).isValid(), true, 'month ' + i);
}
test.done();
},
"array bad date" : function (test) {
test.expect(4);
test.equal(moment([2010, 0, 0]).isValid(), false, 'date 0');
test.equal(moment([2100, 0, 32]).isValid(), false, 'date 32');
test.equal(moment.utc([2010, 0, 0]).isValid(), false, 'utc date 0');
test.equal(moment.utc([2100, 0, 32]).isValid(), false, 'utc date 32');
test.done();
},
"array bad date leap year" : function (test) {
test.expect(8);
test.equal(moment([2010, 1, 29]).isValid(), false, '2010 feb 29');
test.equal(moment([2100, 1, 29]).isValid(), false, '2100 feb 29');
test.equal(moment([2008, 1, 30]).isValid(), false, '2008 feb 30');
test.equal(moment([2000, 1, 30]).isValid(), false, '2000 feb 30');
test.equal(moment.utc([2010, 1, 29]).isValid(), false, 'utc 2010 feb 29');
test.equal(moment.utc([2100, 1, 29]).isValid(), false, 'utc 2100 feb 29');
test.equal(moment.utc([2008, 1, 30]).isValid(), false, 'utc 2008 feb 30');
test.equal(moment.utc([2000, 1, 30]).isValid(), false, 'utc 2000 feb 30');
test.done();
},
"string + formats bad date" : function (test) {
test.expect(9);
test.equal(moment('2020-00-00', ['YYYY-MM-DD', 'DD-MM-YYYY']).isValid(), false, 'invalid on all in array');
test.equal(moment('2020-00-00', ['DD-MM-YYYY', 'YYYY-MM-DD']).isValid(), false, 'invalid on all in array');
test.equal(moment('2020-01-01', ['YYYY-MM-DD', 'DD-MM-YYYY']).isValid(), true, 'valid on first');
test.equal(moment('2020-01-01', ['DD-MM-YYYY', 'YYYY-MM-DD']).isValid(), true, 'valid on last');
test.equal(moment('2020-01-01', ['YYYY-MM-DD', 'YYYY-DD-MM']).isValid(), true, 'valid on both');
test.equal(moment('2020-13-01', ['YYYY-MM-DD', 'YYYY-DD-MM']).isValid(), true, 'valid on last');
test.equal(moment('12-13-2012', ['DD-MM-YYYY', 'YYYY-MM-DD']).isValid(), false, 'month rollover');
test.equal(moment('12-13-2012', ['DD-MM-YYYY', 'DD-MM-YYYY']).isValid(), false, 'month rollover');
test.equal(moment('38-12-2012', ['DD-MM-YYYY']).isValid(), false, 'day rollover');
test.done();
},
"string nonsensical" : function (test) {
test.expect(1);
test.equal(moment('fail').isValid(), false, 'string "fail"');
test.done();
},
"string nonsensical with format" : function (test) {
test.expect(2);
test.equal(moment('fail', "MM-DD-YYYY").isValid(), false, 'string "fail" with format "MM-DD-YYYY"');
test.equal(moment("xx-xx-2001", 'DD-MM-YYY').isValid(), false, 'string "xx-xx-2001" with format "MM-DD-YYYY"');
test.done();
},
"string with bad month name" : function (test) {
test.expect(2);
moment.lang('en');
test.equal(moment('01-Nam-2012', 'DD-MMM-YYYY').isValid(), false, '"Nam" is an invalid month');
test.equal(moment('01-Aug-2012', 'DD-MMM-YYYY').isValid(), true, '"Aug" is a valid month');
test.done();
},
"string with spaceless format" : function (test) {
test.expect(1);
test.equal(moment('10Sep2001', 'DDMMMYYYY').isValid(), true, "Parsing 10Sep2001 should result in a valid date");
test.done();
},
"invalid string iso 8601" : function (test) {
var tests = [
'2010-00-00',
'2010-01-00',
'2010-01-40',
'2010-01-01T24',
'2010-01-01T23:60',
'2010-01-01T23:59:60'
], i;
test.expect(tests.length * 2);
for (i = 0; i < tests.length; i++) {
test.equal(moment(tests[i]).isValid(), false, tests[i] + ' should be invalid');
test.equal(moment.utc(tests[i]).isValid(), false, tests[i] + ' should be invalid');
}
test.done();
},
"invalid string iso 8601 + timezone" : function (test) {
var tests = [
'2010-00-00+00:00',
'2010-01-00+00:00',
'2010-01-40+00:00',
'2010-01-40T24+00:00',
'2010-01-40T23:60+00:00',
'2010-01-40T23:59:60+00:00',
'2010-01-40T23:59:59.9999+00:00'
], i;
test.expect(tests.length * 2);
for (i = 0; i < tests.length; i++) {
test.equal(moment(tests[i]).isValid(), false, tests[i] + ' should be invalid');
test.equal(moment.utc(tests[i]).isValid(), false, tests[i] + ' should be invalid');
}
test.done();
},
"valid string iso 8601 + timezone" : function (test) {
var tests = [
'2010-01-01',
'2010-01-30',
'2010-01-30T23+00:00',
'2010-01-30T23:59+00:00',
'2010-01-30T23:59:59+00:00',
'2010-01-30T23:59:59.999+00:00',
'2010-01-30T23:59:59.999-07:00',
'2010-01-30T00:00:00.000+07:00'
], i;
test.expect(tests.length * 2);
for (i = 0; i < tests.length; i++) {
test.equal(moment(tests[i]).isValid(), true, tests[i] + ' should be valid');
test.equal(moment.utc(tests[i]).isValid(), true, tests[i] + ' should be valid');
}
test.done();
},
"invalidAt" : function (test) {
test.expect(7);
test.equal(moment([2000, 12]).invalidAt(), 1, 'month 12 is invalid: 0-11');
test.equal(moment([2000, 1, 30]).invalidAt(), 2, '30 is not a valid february day');
test.equal(moment([2000, 1, 29, 24]).invalidAt(), 3, '24 is invalid hour');
test.equal(moment([2000, 1, 29, 23, 60]).invalidAt(), 4, '60 is invalid minute');
test.equal(moment([2000, 1, 29, 23, 59, 60]).invalidAt(), 5, '60 is invalid second');
test.equal(moment([2000, 1, 29, 23, 59, 59, 1000]).invalidAt(), 6, '1000 is invalid millisecond');
test.equal(moment([2000, 1, 29, 23, 59, 59, 999]).invalidAt(), -1, '-1 if everything is fine');
test.done();
},
"valid Unix timestamp" : function (test) {
test.expect(21);
test.equal(moment(1371065286, "X").isValid(), true, 'number integer');
test.equal(moment(1379066897.0, "X").isValid(), true, 'number whole 1dp');
test.equal(moment(1379066897.7, "X").isValid(), true, 'number 1dp');
test.equal(moment(1379066897.00, "X").isValid(), true, 'number whole 2dp');
test.equal(moment(1379066897.07, "X").isValid(), true, 'number 2dp');
test.equal(moment(1379066897.17, "X").isValid(), true, 'number 2dp');
test.equal(moment(1379066897.000, "X").isValid(), true, 'number whole 3dp');
test.equal(moment(1379066897.007, "X").isValid(), true, 'number 3dp');
test.equal(moment(1379066897.017, "X").isValid(), true, 'number 3dp');
test.equal(moment(1379066897.157, "X").isValid(), true, 'number 3dp');
test.equal(moment("1371065286", "X").isValid(), true, 'string integer');
test.equal(moment("1379066897.", "X").isValid(), true, 'string trailing .');
test.equal(moment("1379066897.0", "X").isValid(), true, 'string whole 1dp');
test.equal(moment("1379066897.7", "X").isValid(), true, 'string 1dp');
test.equal(moment("1379066897.00", "X").isValid(), true, 'string whole 2dp');
test.equal(moment("1379066897.07", "X").isValid(), true, 'string 2dp');
test.equal(moment("1379066897.17", "X").isValid(), true, 'string 2dp');
test.equal(moment("1379066897.000", "X").isValid(), true, 'string whole 3dp');
test.equal(moment("1379066897.007", "X").isValid(), true, 'string 3dp');
test.equal(moment("1379066897.017", "X").isValid(), true, 'string 3dp');
test.equal(moment("1379066897.157", "X").isValid(), true, 'string 3dp');
test.done();
},
"invalid Unix timestamp" : function (test) {
test.expect(8);
test.equal(moment(undefined, "X").isValid(), false, 'undefined');
test.equal(moment("undefined", "X").isValid(), false, 'string undefined');
try {
test.equal(moment(null, "X").isValid(), false, 'null');
} catch (e) {
test.ok(true, 'null');
}
test.equal(moment("null", "X").isValid(), false, 'string null');
test.equal(moment([], "X").isValid(), false, 'array');
test.equal(moment("{}", "X").isValid(), false, 'object');
try {
test.equal(moment("", "X").isValid(), false, 'string empty');
} catch (e) {
test.ok(true, 'string empty');
}
test.equal(moment(" ", "X").isValid(), false, 'string space');
test.done();
}
};

View File

@@ -0,0 +1,242 @@
var moment = require("../../moment");
exports.lang = {
"library getter" : function (test) {
test.expect(7);
moment.lang('en');
test.equal(moment.lang(), 'en', 'Lang should return en by default');
moment.lang('fr');
test.equal(moment.lang(), 'fr', 'Lang should return the changed language');
moment.lang('en-gb');
test.equal(moment.lang(), 'en-gb', 'Lang should return the changed language');
moment.lang('en');
test.equal(moment.lang(), 'en', 'Lang should reset');
moment.lang('does-not-exist');
test.equal(moment.lang(), 'en', 'Lang should reset');
moment.lang('EN');
test.equal(moment.lang(), 'en', 'Normalize language key case');
moment.lang('EN_gb');
test.equal(moment.lang(), 'en-gb', 'Normalize language key underscore');
test.done();
},
"library ensure inheritance" : function (test) {
test.expect(2);
moment.lang('made-up', {
// I put them out of order
months : "February_March_April_May_June_July_August_September_October_November_December_January".split("_")
// the rest of the properties should be inherited.
});
test.equal(moment([2012, 5, 6]).format('MMMM'), 'July', 'Override some of the configs');
test.equal(moment([2012, 5, 6]).format('MMM'), 'Jun', 'But not all of them');
test.done();
},
"library ensure inheritance LT L LL LLL LLLL" : function (test) {
test.expect(5);
var lang = 'test-inherit-lt';
moment.lang(lang, {
longDateFormat : {
LT : "-[LT]-",
L : "-[L]-",
LL : "-[LL]-",
LLL : "-[LLL]-",
LLLL : "-[LLLL]-"
},
calendar : {
sameDay : '[sameDay] LT',
nextDay : '[nextDay] L',
nextWeek : '[nextWeek] LL',
lastDay : '[lastDay] LLL',
lastWeek : '[lastWeek] LLLL',
sameElse : 'L'
}
});
moment.lang('es');
test.equal(moment().lang(lang).calendar(), "sameDay -LT-", "Should use instance lang in LT formatting");
test.equal(moment().add('days', 1).lang(lang).calendar(), "nextDay -L-", "Should use instance lang in L formatting");
test.equal(moment().add('days', -1).lang(lang).calendar(), "lastDay -LLL-", "Should use instance lang in LL formatting");
test.equal(moment().add('days', 4).lang(lang).calendar(), "nextWeek -LL-", "Should use instance lang in LLL formatting");
test.equal(moment().add('days', -4).lang(lang).calendar(), "lastWeek -LLLL-", "Should use instance lang in LLLL formatting");
test.done();
},
"library langData" : function (test) {
test.expect(3);
moment.lang('en');
var jan = moment([2000, 0]);
test.equal(moment.langData().months(jan), 'January', 'no arguments returns global');
test.equal(moment.langData('zh-cn').months(jan), '一月', 'a string returns the language based on key');
test.equal(moment.langData(moment().lang('es')).months(jan), 'enero', "if you pass in a moment it uses the moment's language");
test.done();
},
"instance lang method" : function (test) {
test.expect(3);
moment.lang('en');
test.equal(moment([2012, 5, 6]).format('MMMM'), 'June', 'Normally default to global');
test.equal(moment([2012, 5, 6]).lang('es').format('MMMM'), 'junio', 'Use the instance specific language');
test.equal(moment([2012, 5, 6]).format('MMMM'), 'June', 'Using an instance specific language does not affect other moments');
test.done();
},
"instance lang persists with manipulation" : function (test) {
test.expect(3);
moment.lang('en');
test.equal(moment([2012, 5, 6]).lang('es').add({days: 1}).format('MMMM'), 'junio', 'With addition');
test.equal(moment([2012, 5, 6]).lang('es').day(0).format('MMMM'), 'junio', 'With day getter');
test.equal(moment([2012, 5, 6]).lang('es').endOf('day').format('MMMM'), 'junio', 'With endOf');
test.done();
},
"instance lang persists with cloning" : function (test) {
test.expect(2);
moment.lang('en');
var a = moment([2012, 5, 6]).lang('es'),
b = a.clone(),
c = moment(a);
test.equal(b.format('MMMM'), 'junio', 'using moment.fn.clone()');
test.equal(b.format('MMMM'), 'junio', 'using moment()');
test.done();
},
"duration lang method" : function (test) {
test.expect(3);
moment.lang('en');
test.equal(moment.duration({seconds: 44}).humanize(), 'a few seconds', 'Normally default to global');
test.equal(moment.duration({seconds: 44}).lang('es').humanize(), 'unos segundos', 'Use the instance specific language');
test.equal(moment.duration({seconds: 44}).humanize(), 'a few seconds', 'Using an instance specific language does not affect other durations');
test.done();
},
"duration lang persists with cloning" : function (test) {
test.expect(1);
moment.lang('en');
var a = moment.duration({seconds: 44}).lang('es'),
b = moment.duration(a);
test.equal(b.humanize(), 'unos segundos', 'using moment.duration()');
test.done();
},
"instance lang used with from" : function (test) {
test.expect(2);
moment.lang('en');
var a = moment([2012, 5, 6]).lang('es'),
b = moment([2012, 5, 7]);
test.equal(a.from(b), 'hace un día', 'preserve language of first moment');
test.equal(b.from(a), 'in a day', 'do not preserve language of second moment');
test.done();
},
"month name callback function" : function (test) {
test.expect(3);
function fakeReplace(m, format) {
if (/test/.test(format)) {
return "test";
}
if (m.date() === 1) {
return "date";
}
return 'default';
}
moment.lang('made-up-2', {
months : fakeReplace,
monthsShort : fakeReplace,
weekdays : fakeReplace,
weekdaysShort : fakeReplace,
weekdaysMin : fakeReplace
});
test.equal(moment().format('[test] dd ddd dddd MMM MMMM'), 'test test test test test test', 'format month name function should be able to access the format string');
test.equal(moment([2011, 0, 1]).format('dd ddd dddd MMM MMMM'), 'date date date date date', 'format month name function should be able to access the moment object');
test.equal(moment([2011, 0, 2]).format('dd ddd dddd MMM MMMM'), 'default default default default default', 'format month name function should be able to access the moment object');
test.done();
},
"changing parts of a language config" : function (test) {
test.expect(2);
moment.lang('partial-lang', {
months : 'a b c d e f g h i j k l'.split(' ')
});
test.equal(moment([2011, 0, 1]).format('MMMM'), 'a', 'should be able to set language values when creating the language');
moment.lang('partial-lang', {
monthsShort : 'A B C D E F G H I J K L'.split(' ')
});
test.equal(moment([2011, 0, 1]).format('MMMM MMM'), 'a A', 'should be able to set language values after creating the language');
test.done();
},
"start/endOf week feature for first-day-is-monday langs" : function (test) {
test.expect(2);
moment.lang('monday-lang', {
week : {
dow : 1 // Monday is the first day of the week
}
});
moment.lang('monday-lang');
test.equal(moment([2013, 0, 1]).startOf('week').day(), 1, 'for lang monday-lang first day of the week should be monday');
test.equal(moment([2013, 0, 1]).endOf('week').day(), 0, 'for lang monday-lang last day of the week should be sunday');
test.done();
},
"meridiem parsing" : function (test) {
test.expect(2);
moment.lang('meridiem-parsing', {
meridiemParse : /[bd]/i,
isPM : function (input) {
return input === 'b';
}
});
moment.lang('meridiem-parsing');
test.equal(moment('2012-01-01 3b', 'YYYY-MM-DD ha').hour(), 15, 'Custom parsing of meridiem should work');
test.equal(moment('2012-01-01 3d', 'YYYY-MM-DD ha').hour(), 3, 'Custom parsing of meridiem should work');
test.done();
}
};

View File

@@ -0,0 +1,13 @@
var moment = require("../../moment");
exports.leapyear = {
"leap year" : function (test) {
test.expect(4);
test.equal(moment([2010, 0, 1]).isLeapYear(), false, '2010');
test.equal(moment([2100, 0, 1]).isLeapYear(), false, '2100');
test.equal(moment([2008, 0, 1]).isLeapYear(), true, '2008');
test.equal(moment([2000, 0, 1]).isLeapYear(), true, '2000');
test.done();
}
};

View File

@@ -0,0 +1,88 @@
var moment = require("../../moment");
exports.listers = {
setUp : function (cb) {
moment.lang('en');
cb();
},
tearDown : function (cb) {
moment.lang('en');
cb();
},
"default" : function (test) {
test.expect(5);
test.deepEqual(moment.months(), ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]);
test.deepEqual(moment.monthsShort(), ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]);
test.deepEqual(moment.weekdays(), ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]);
test.deepEqual(moment.weekdaysShort(), ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]);
test.deepEqual(moment.weekdaysMin(), ["Su", "Mo", "Tu", "We", "Th", "Fr", "Sa"]);
test.done();
},
"index" : function (test) {
test.expect(5);
test.equal(moment.months(2), "March");
test.equal(moment.monthsShort(2), "Mar");
test.equal(moment.weekdays(2), "Tuesday");
test.equal(moment.weekdaysShort(2), "Tue");
test.equal(moment.weekdaysMin(2), "Tu");
test.done();
},
"localized" : function (test) {
var months = "one_two_three_four_five_six_seven_eight_nine_ten_eleven_twelve".split('_'),
monthsShort = "on_tw_th_fo_fi_si_se_ei_ni_te_el_tw".split("_"),
weekdays = "one_two_three_four_five_six_seven".split("_"),
weekdaysShort = "on_tw_th_fo_fi_si_se".split("_"),
weekdaysMin = "1_2_3_4_5_6_7".split("_");
moment.lang('numerologists', {
months : months,
monthsShort : monthsShort,
weekdays : weekdays,
weekdaysShort: weekdaysShort,
weekdaysMin: weekdaysMin
});
test.expect(10);
test.deepEqual(moment.months(), months);
test.deepEqual(moment.monthsShort(), monthsShort);
test.deepEqual(moment.weekdays(), weekdays);
test.deepEqual(moment.weekdaysShort(), weekdaysShort);
test.deepEqual(moment.weekdaysMin(), weekdaysMin);
test.equal(moment.months(2), "three");
test.equal(moment.monthsShort(2), "th");
test.equal(moment.weekdays(2), "three");
test.equal(moment.weekdaysShort(2), "th");
test.equal(moment.weekdaysMin(2), "3");
test.done();
},
"with functions" : function (test) {
var monthsShort = "one_two_three_four_five_six_seven_eight_nine_ten_eleven_twelve".split('_'),
monthsShortWeird = "onesy_twosy_threesy_foursy_fivesy_sixsy_sevensy_eightsy_ninesy_tensy_elevensy_twelvesy".split('_');
moment.lang("difficult", {
monthsShort: function (m, format) {
var arr = format.match(/-MMM-/) ? monthsShortWeird : monthsShort;
return arr[m.month()];
}
});
test.expect(6);
test.deepEqual(moment.monthsShort(), monthsShort);
test.deepEqual(moment.monthsShort('MMM'), monthsShort);
test.deepEqual(moment.monthsShort('-MMM-'), monthsShortWeird);
test.deepEqual(moment.monthsShort('MMM', 2), 'three');
test.deepEqual(moment.monthsShort('-MMM-', 2), 'threesy');
test.deepEqual(moment.monthsShort(2), 'three');
test.done();
}
};

View File

@@ -0,0 +1,56 @@
var moment = require("../../moment");
var equalMoment = function (test, a, b, msg) {
test.equal(a.valueOf(), b.valueOf(), msg);
};
exports.min_max = {
setUp : function (cb) {
moment.lang('en');
cb();
},
tearDown : function (cb) {
moment.lang('en');
cb();
},
"min" : function (test) {
test.expect(6);
var now = moment(),
future = now.clone().add(1, 'month'),
past = now.clone().subtract(1, 'month');
equalMoment(test, past.min(now), now, "A past date with the minimum of now should be now");
equalMoment(test, past.min(future), future, "A past date with the minimum of the future should be the future date");
equalMoment(test, future.min(now), future, "A future date with the minimum of now should be the future");
equalMoment(test, future.min(past), future, "A future date with the minimum of the past should be the future");
equalMoment(test, now.min(past), now, "Now with the minimum of the past should be now");
equalMoment(test, now.min(future), future, "Now with the minimum of the future should be the future");
test.done();
},
"max" : function (test) {
test.expect(6);
var now = moment(),
future = now.clone().add(1, 'month'),
past = now.clone().subtract(1, 'month');
equalMoment(test, past.max(now), past, "A past date with the maximum of now should be the past");
equalMoment(test, past.max(future), past, "A past date with the maximum of the future should be the past");
equalMoment(test, future.max(now), now, "A future date with the maximum of now should be now");
equalMoment(test, future.max(past), past, "A future date with the maximum of the past should be the past");
equalMoment(test, now.max(past), past, "Now with the maximum of the past should be the past");
equalMoment(test, now.max(future), now, "Now with the maximum of the future should be now");
test.done();
}
};

View File

@@ -0,0 +1,55 @@
var moment = require("../../moment");
exports.mutable = {
"manipulation methods" : function (test) {
var mutableMethods = {
'year': function (m) { return m.year(2011); },
'month': function (m) { return m.month(1); },
'date': function (m) { return m.date(9); },
'hours': function (m) { return m.hours(7); },
'minutes': function (m) { return m.minutes(33); },
'seconds': function (m) { return m.seconds(44); },
'milliseconds': function (m) { return m.milliseconds(55); },
'day': function (m) { return m.day(2); },
'startOf': function (m) { return m.startOf('week'); },
'endOf': function (m) { return m.endOf('week'); },
'add': function (m) { return m.add('days', 1); },
'subtract': function (m) { return m.subtract('years', 2); },
'local': function (m) { return m.local(); },
'utc': function (m) { return m.utc(); }
}, method, d, d2;
test.expect(14);
for (method in mutableMethods) {
if (mutableMethods.hasOwnProperty(method)) {
d = moment();
d2 = mutableMethods[method](d);
test.equal(d, d2, method + "() should be mutable");
}
}
test.done();
},
"non mutable methods" : function (test) {
var nonMutableMethods = {
'clone': function (m) { return m.clone(); }
}, method, d, d2;
test.expect(1);
for (method in nonMutableMethods) {
if (nonMutableMethods.hasOwnProperty(method)) {
d = new Date();
d2 = nonMutableMethods[method](moment(d)).toDate();
test.notEqual(d, d2, method + "() should not be mutable");
}
}
test.done();
}
};

View File

@@ -0,0 +1,33 @@
/*global require, exports */
var moment = require("../../moment");
exports.normalizeUnits = {
"normalize units" : function (test) {
test.expect(45);
var fullKeys = ["year", "month", "isoweek", "week", "day", "hour", "minute", "second", "millisecond"],
aliases = ["y", "M", "W", "w", "d", "h", "m", "s", "ms"],
length = fullKeys.length,
fullKey,
fullKeyCaps,
fullKeyPlural,
fullKeyCapsPlural,
alias,
index;
for (index = 0; index < length; index += 1) {
fullKey = fullKeys[index];
fullKeyCaps = fullKey.toUpperCase();
fullKeyPlural = fullKey + "s";
fullKeyCapsPlural = fullKeyCaps + "s";
alias = aliases[index];
test.equal(moment.normalizeUnits(fullKey), fullKey, "Testing full key " + fullKey);
test.equal(moment.normalizeUnits(fullKeyCaps), fullKey, "Testing full key capitalised " + fullKey);
test.equal(moment.normalizeUnits(fullKeyPlural), fullKey, "Testing full key plural " + fullKey);
test.equal(moment.normalizeUnits(fullKeyCapsPlural), fullKey, "Testing full key capitalised and plural " + fullKey);
test.equal(moment.normalizeUnits(alias), fullKey, "Testing alias " + fullKey);
}
test.done();
}
};

View File

@@ -0,0 +1,91 @@
var moment = require("../../moment");
var symbolMap = {
'1': '!',
'2': '@',
'3': '#',
'4': '$',
'5': '%',
'6': '^',
'7': '&',
'8': '*',
'9': '(',
'0': ')'
};
var numberMap = {
'!': '1',
'@': '2',
'#': '3',
'$': '4',
'%': '5',
'^': '6',
'&': '7',
'*': '8',
'(': '9',
')': '0'
};
var symbolLang = {
preparse: function (string) {
return string.replace(/[!@#$%\^&*()]/g, function (match) {
return numberMap[match];
});
},
postformat: function (string) {
return string.replace(/\d/g, function (match) {
return symbolMap[match];
});
}
};
exports.preparse_postformat = {
setUp: function (cb) {
moment.lang('symbol', symbolLang);
cb();
},
tearDown: function (cb) {
moment.lang('en-gb');
cb();
},
"transform": function (test) {
test.expect(3);
test.equal(moment.utc('@)!@-)*-@&', 'YYYY-MM-DD').unix(), 1346025600, "preparse string + format");
test.equal(moment.utc('@)!@-)*-@&').unix(), 1346025600, "preparse ISO8601 string");
test.equal(moment.unix(1346025600).utc().format('YYYY-MM-DD'), '@)!@-)*-@&', "postformat");
test.done();
},
"transform from": function (test) {
test.expect(3);
var start = moment([2007, 1, 28]);
test.equal(start.from(moment([2007, 1, 28]).add({s: 90}), true), "@ minutes", "postformat should work on moment.fn.from");
test.equal(moment().add('d', 6).fromNow(true), "^ days", "postformat should work on moment.fn.fromNow");
test.equal(moment.duration(10, "h").humanize(), "!) hours", "postformat should work on moment.duration.fn.humanize");
test.done();
},
"calendar day" : function (test) {
test.expect(6);
var a = moment().hours(2).minutes(0).seconds(0);
test.equal(moment(a).calendar(), "Today at @:)) AM", "today at the same time");
test.equal(moment(a).add({ m: 25 }).calendar(), "Today at @:@% AM", "Now plus 25 min");
test.equal(moment(a).add({ h: 1 }).calendar(), "Today at #:)) AM", "Now plus 1 hour");
test.equal(moment(a).add({ d: 1 }).calendar(), "Tomorrow at @:)) AM", "tomorrow at the same time");
test.equal(moment(a).subtract({ h: 1 }).calendar(), "Today at !:)) AM", "Now minus 1 hour");
test.equal(moment(a).subtract({ d: 1 }).calendar(), "Yesterday at @:)) AM", "yesterday at the same time");
test.done();
}
};

View File

@@ -0,0 +1,305 @@
var moment = require("../../moment");
exports.end_start_of = {
setUp : function (cb) {
moment.lang('en');
cb();
},
tearDown : function (cb) {
moment.lang('en');
cb();
},
"start of year" : function (test) {
test.expect(9);
var m = moment(new Date(2011, 1, 2, 3, 4, 5, 6)).startOf('year'),
ms = moment(new Date(2011, 1, 2, 3, 4, 5, 6)).startOf('years'),
ma = moment(new Date(2011, 1, 2, 3, 4, 5, 6)).startOf('y');
test.equal(+m, +ms, "Plural or singular should work");
test.equal(+m, +ma, "Full or abbreviated should work");
test.equal(m.year(), 2011, "keep the year");
test.equal(m.month(), 0, "strip out the month");
test.equal(m.date(), 1, "strip out the day");
test.equal(m.hours(), 0, "strip out the hours");
test.equal(m.minutes(), 0, "strip out the minutes");
test.equal(m.seconds(), 0, "strip out the seconds");
test.equal(m.milliseconds(), 0, "strip out the milliseconds");
test.done();
},
"end of year" : function (test) {
test.expect(9);
var m = moment(new Date(2011, 1, 2, 3, 4, 5, 6)).endOf('year'),
ms = moment(new Date(2011, 1, 2, 3, 4, 5, 6)).endOf('years'),
ma = moment(new Date(2011, 1, 2, 3, 4, 5, 6)).endOf('y');
test.equal(+m, +ms, "Plural or singular should work");
test.equal(+m, +ma, "Full or abbreviated should work");
test.equal(m.year(), 2011, "keep the year");
test.equal(m.month(), 11, "set the month");
test.equal(m.date(), 31, "set the day");
test.equal(m.hours(), 23, "set the hours");
test.equal(m.minutes(), 59, "set the minutes");
test.equal(m.seconds(), 59, "set the seconds");
test.equal(m.milliseconds(), 999, "set the seconds");
test.done();
},
"start of month" : function (test) {
test.expect(9);
var m = moment(new Date(2011, 1, 2, 3, 4, 5, 6)).startOf('month'),
ms = moment(new Date(2011, 1, 2, 3, 4, 5, 6)).startOf('months'),
ma = moment(new Date(2011, 1, 2, 3, 4, 5, 6)).startOf('M');
test.equal(+m, +ms, "Plural or singular should work");
test.equal(+m, +ma, "Full or abbreviated should work");
test.equal(m.year(), 2011, "keep the year");
test.equal(m.month(), 1, "keep the month");
test.equal(m.date(), 1, "strip out the day");
test.equal(m.hours(), 0, "strip out the hours");
test.equal(m.minutes(), 0, "strip out the minutes");
test.equal(m.seconds(), 0, "strip out the seconds");
test.equal(m.milliseconds(), 0, "strip out the milliseconds");
test.done();
},
"end of month" : function (test) {
test.expect(9);
var m = moment(new Date(2011, 1, 2, 3, 4, 5, 6)).endOf('month'),
ms = moment(new Date(2011, 1, 2, 3, 4, 5, 6)).endOf('months'),
ma = moment(new Date(2011, 1, 2, 3, 4, 5, 6)).endOf('M');
test.equal(+m, +ms, "Plural or singular should work");
test.equal(+m, +ma, "Full or abbreviated should work");
test.equal(m.year(), 2011, "keep the year");
test.equal(m.month(), 1, "keep the month");
test.equal(m.date(), 28, "set the day");
test.equal(m.hours(), 23, "set the hours");
test.equal(m.minutes(), 59, "set the minutes");
test.equal(m.seconds(), 59, "set the seconds");
test.equal(m.milliseconds(), 999, "set the seconds");
test.done();
},
"start of week" : function (test) {
test.expect(10);
var m = moment(new Date(2011, 1, 2, 3, 4, 5, 6)).startOf('week'),
ms = moment(new Date(2011, 1, 2, 3, 4, 5, 6)).startOf('weeks'),
ma = moment(new Date(2011, 1, 2, 3, 4, 5, 6)).startOf('w');
test.equal(+m, +ms, "Plural or singular should work");
test.equal(+m, +ma, "Full or abbreviated should work");
test.equal(m.year(), 2011, "keep the year");
test.equal(m.month(), 0, "rolls back to January");
test.equal(m.day(), 0, "set day of week");
test.equal(m.date(), 30, "set correct date");
test.equal(m.hours(), 0, "strip out the hours");
test.equal(m.minutes(), 0, "strip out the minutes");
test.equal(m.seconds(), 0, "strip out the seconds");
test.equal(m.milliseconds(), 0, "strip out the milliseconds");
test.done();
},
"end of week" : function (test) {
test.expect(10);
var m = moment(new Date(2011, 1, 2, 3, 4, 5, 6)).endOf('week'),
ms = moment(new Date(2011, 1, 2, 3, 4, 5, 6)).endOf('weeks'),
ma = moment(new Date(2011, 1, 2, 3, 4, 5, 6)).endOf('weeks');
test.equal(+m, +ms, "Plural or singular should work");
test.equal(+m, +ma, "Full or abbreviated should work");
test.equal(m.year(), 2011, "keep the year");
test.equal(m.month(), 1, "keep the month");
test.equal(m.day(), 6, "set the day of the week");
test.equal(m.date(), 5, "set the day");
test.equal(m.hours(), 23, "set the hours");
test.equal(m.minutes(), 59, "set the minutes");
test.equal(m.seconds(), 59, "set the seconds");
test.equal(m.milliseconds(), 999, "set the seconds");
test.done();
},
"start of iso-week" : function (test) {
test.expect(10);
var m = moment(new Date(2011, 1, 2, 3, 4, 5, 6)).startOf('isoWeek'),
ms = moment(new Date(2011, 1, 2, 3, 4, 5, 6)).startOf('isoWeeks'),
ma = moment(new Date(2011, 1, 2, 3, 4, 5, 6)).startOf('W');
test.equal(+m, +ms, "Plural or singular should work");
test.equal(+m, +ma, "Full or abbreviated should work");
test.equal(m.year(), 2011, "keep the year");
test.equal(m.month(), 0, "rollback to January");
test.equal(m.isoWeekday(), 1, "set day of iso-week");
test.equal(m.date(), 31, "set correct date");
test.equal(m.hours(), 0, "strip out the hours");
test.equal(m.minutes(), 0, "strip out the minutes");
test.equal(m.seconds(), 0, "strip out the seconds");
test.equal(m.milliseconds(), 0, "strip out the milliseconds");
test.done();
},
"end of iso-week" : function (test) {
test.expect(10);
var m = moment(new Date(2011, 1, 2, 3, 4, 5, 6)).endOf('isoWeek'),
ms = moment(new Date(2011, 1, 2, 3, 4, 5, 6)).endOf('isoWeeks'),
ma = moment(new Date(2011, 1, 2, 3, 4, 5, 6)).endOf('W');
test.equal(+m, +ms, "Plural or singular should work");
test.equal(+m, +ma, "Full or abbreviated should work");
test.equal(m.year(), 2011, "keep the year");
test.equal(m.month(), 1, "keep the month");
test.equal(m.isoWeekday(), 7, "set the day of the week");
test.equal(m.date(), 6, "set the day");
test.equal(m.hours(), 23, "set the hours");
test.equal(m.minutes(), 59, "set the minutes");
test.equal(m.seconds(), 59, "set the seconds");
test.equal(m.milliseconds(), 999, "set the seconds");
test.done();
},
"start of day" : function (test) {
test.expect(9);
var m = moment(new Date(2011, 1, 2, 3, 4, 5, 6)).startOf('day'),
ms = moment(new Date(2011, 1, 2, 3, 4, 5, 6)).startOf('days'),
ma = moment(new Date(2011, 1, 2, 3, 4, 5, 6)).startOf('d');
test.equal(+m, +ms, "Plural or singular should work");
test.equal(+m, +ma, "Full or abbreviated should work");
test.equal(m.year(), 2011, "keep the year");
test.equal(m.month(), 1, "keep the month");
test.equal(m.date(), 2, "keep the day");
test.equal(m.hours(), 0, "strip out the hours");
test.equal(m.minutes(), 0, "strip out the minutes");
test.equal(m.seconds(), 0, "strip out the seconds");
test.equal(m.milliseconds(), 0, "strip out the milliseconds");
test.done();
},
"end of day" : function (test) {
test.expect(9);
var m = moment(new Date(2011, 1, 2, 3, 4, 5, 6)).endOf('day'),
ms = moment(new Date(2011, 1, 2, 3, 4, 5, 6)).endOf('days'),
ma = moment(new Date(2011, 1, 2, 3, 4, 5, 6)).endOf('d');
test.equal(+m, +ms, "Plural or singular should work");
test.equal(+m, +ma, "Full or abbreviated should work");
test.equal(m.year(), 2011, "keep the year");
test.equal(m.month(), 1, "keep the month");
test.equal(m.date(), 2, "keep the day");
test.equal(m.hours(), 23, "set the hours");
test.equal(m.minutes(), 59, "set the minutes");
test.equal(m.seconds(), 59, "set the seconds");
test.equal(m.milliseconds(), 999, "set the seconds");
test.done();
},
"start of hour" : function (test) {
test.expect(9);
var m = moment(new Date(2011, 1, 2, 3, 4, 5, 6)).startOf('hour'),
ms = moment(new Date(2011, 1, 2, 3, 4, 5, 6)).startOf('hours'),
ma = moment(new Date(2011, 1, 2, 3, 4, 5, 6)).startOf('h');
test.equal(+m, +ms, "Plural or singular should work");
test.equal(+m, +ma, "Full or abbreviated should work");
test.equal(m.year(), 2011, "keep the year");
test.equal(m.month(), 1, "keep the month");
test.equal(m.date(), 2, "keep the day");
test.equal(m.hours(), 3, "keep the hours");
test.equal(m.minutes(), 0, "strip out the minutes");
test.equal(m.seconds(), 0, "strip out the seconds");
test.equal(m.milliseconds(), 0, "strip out the milliseconds");
test.done();
},
"end of hour" : function (test) {
test.expect(9);
var m = moment(new Date(2011, 1, 2, 3, 4, 5, 6)).endOf('hour'),
ms = moment(new Date(2011, 1, 2, 3, 4, 5, 6)).endOf('hours'),
ma = moment(new Date(2011, 1, 2, 3, 4, 5, 6)).endOf('h');
test.equal(+m, +ms, "Plural or singular should work");
test.equal(+m, +ma, "Full or abbreviated should work");
test.equal(m.year(), 2011, "keep the year");
test.equal(m.month(), 1, "keep the month");
test.equal(m.date(), 2, "keep the day");
test.equal(m.hours(), 3, "keep the hours");
test.equal(m.minutes(), 59, "set the minutes");
test.equal(m.seconds(), 59, "set the seconds");
test.equal(m.milliseconds(), 999, "set the seconds");
test.done();
},
"start of minute" : function (test) {
test.expect(9);
var m = moment(new Date(2011, 1, 2, 3, 4, 5, 6)).startOf('minute'),
ms = moment(new Date(2011, 1, 2, 3, 4, 5, 6)).startOf('minutes'),
ma = moment(new Date(2011, 1, 2, 3, 4, 5, 6)).startOf('m');
test.equal(+m, +ms, "Plural or singular should work");
test.equal(+m, +ma, "Full or abbreviated should work");
test.equal(m.year(), 2011, "keep the year");
test.equal(m.month(), 1, "keep the month");
test.equal(m.date(), 2, "keep the day");
test.equal(m.hours(), 3, "keep the hours");
test.equal(m.minutes(), 4, "keep the minutes");
test.equal(m.seconds(), 0, "strip out the seconds");
test.equal(m.milliseconds(), 0, "strip out the milliseconds");
test.done();
},
"end of minute" : function (test) {
test.expect(9);
var m = moment(new Date(2011, 1, 2, 3, 4, 5, 6)).endOf('minute'),
ms = moment(new Date(2011, 1, 2, 3, 4, 5, 6)).endOf('minutes'),
ma = moment(new Date(2011, 1, 2, 3, 4, 5, 6)).endOf('m');
test.equal(+m, +ms, "Plural or singular should work");
test.equal(+m, +ma, "Full or abbreviated should work");
test.equal(m.year(), 2011, "keep the year");
test.equal(m.month(), 1, "keep the month");
test.equal(m.date(), 2, "keep the day");
test.equal(m.hours(), 3, "keep the hours");
test.equal(m.minutes(), 4, "keep the minutes");
test.equal(m.seconds(), 59, "set the seconds");
test.equal(m.milliseconds(), 999, "set the seconds");
test.done();
},
"start of second" : function (test) {
test.expect(9);
var m = moment(new Date(2011, 1, 2, 3, 4, 5, 6)).startOf('second'),
ms = moment(new Date(2011, 1, 2, 3, 4, 5, 6)).startOf('seconds'),
ma = moment(new Date(2011, 1, 2, 3, 4, 5, 6)).startOf('s');
test.equal(+m, +ms, "Plural or singular should work");
test.equal(+m, +ma, "Full or abbreviated should work");
test.equal(m.year(), 2011, "keep the year");
test.equal(m.month(), 1, "keep the month");
test.equal(m.date(), 2, "keep the day");
test.equal(m.hours(), 3, "keep the hours");
test.equal(m.minutes(), 4, "keep the minutes");
test.equal(m.seconds(), 5, "keep the the seconds");
test.equal(m.milliseconds(), 0, "strip out the milliseconds");
test.done();
},
"end of second" : function (test) {
test.expect(9);
var m = moment(new Date(2011, 1, 2, 3, 4, 5, 6)).endOf('second'),
ms = moment(new Date(2011, 1, 2, 3, 4, 5, 6)).endOf('seconds'),
ma = moment(new Date(2011, 1, 2, 3, 4, 5, 6)).endOf('s');
test.equal(+m, +ms, "Plural or singular should work");
test.equal(+m, +ma, "Full or abbreviated should work");
test.equal(m.year(), 2011, "keep the year");
test.equal(m.month(), 1, "keep the month");
test.equal(m.date(), 2, "keep the day");
test.equal(m.hours(), 3, "keep the hours");
test.equal(m.minutes(), 4, "keep the minutes");
test.equal(m.seconds(), 5, "keep the seconds");
test.equal(m.milliseconds(), 999, "set the seconds");
test.done();
}
};

View File

@@ -0,0 +1,18 @@
var moment = require("../../moment");
exports.string_prototype = {
"string prototype overrides call" : function (test) {
test.expect(1);
moment.lang('en');
var prior = String.prototype.call, b;
String.prototype.call = function () { return null; };
b = moment(new Date(2011, 7, 28, 15, 25, 50, 125));
test.equal(b.format('MMMM Do YYYY, h:mm a'), 'August 28th 2011, 3:25 pm');
String.prototype.call = prior;
test.done();
}
};

View File

@@ -0,0 +1,103 @@
var moment = require("../../moment");
exports.utc = {
setUp : function (cb) {
moment.lang('en');
cb();
},
tearDown : function (cb) {
moment.lang('en');
cb();
},
"utc and local" : function (test) {
test.expect(7);
var m = moment(Date.UTC(2011, 1, 2, 3, 4, 5, 6)), zone, expected;
m.utc();
// utc
test.equal(m.date(), 2, "the day should be correct for utc");
test.equal(m.day(), 3, "the date should be correct for utc");
test.equal(m.hours(), 3, "the hours should be correct for utc");
// local
m.local();
if (m.zone() > 180) {
test.equal(m.date(), 1, "the date should be correct for local");
test.equal(m.day(), 2, "the day should be correct for local");
} else {
test.equal(m.date(), 2, "the date should be correct for local");
test.equal(m.day(), 3, "the day should be correct for local");
}
zone = Math.ceil(m.zone() / 60);
expected = (24 + 3 - zone) % 24;
test.equal(m.hours(), expected, "the hours (" + m.hours() + ") should be correct for local");
test.equal(moment().utc().zone(), 0, "timezone in utc should always be zero");
test.done();
},
"creating with utc" : function (test) {
test.expect(7);
var diff = moment.utc().valueOf() - moment().valueOf(), m;
diff = Math.abs(diff);
// we check the diff rather than equality because sometimes they are off by a millisecond
test.ok(diff < 5, "Calling moment.utc() should default to the current time");
m = moment.utc([2011, 1, 2, 3, 4, 5, 6]);
test.equal(m.date(), 2, "the day should be correct for utc array");
test.equal(m.hours(), 3, "the hours should be correct for utc array");
m = moment.utc("2011-02-02 3:04:05", "YYYY-MM-DD HH:mm:ss");
test.equal(m.date(), 2, "the day should be correct for utc parsing format");
test.equal(m.hours(), 3, "the hours should be correct for utc parsing format");
m = moment.utc("2011-02-02T03:04:05+00:00");
test.equal(m.date(), 2, "the day should be correct for utc parsing iso");
test.equal(m.hours(), 3, "the hours should be correct for utc parsing iso");
test.done();
},
"creating with utc without timezone" : function (test) {
test.expect(4);
var m = moment.utc("2012-01-02T08:20:00");
test.equal(m.date(), 2, "the day should be correct for utc parse without timezone");
test.equal(m.hours(), 8, "the hours should be correct for utc parse without timezone");
m = moment.utc("2012-01-02T08:20:00+09:00");
test.equal(m.date(), 1, "the day should be correct for utc parse with timezone");
test.equal(m.hours(), 23, "the hours should be correct for utc parse with timezone");
test.done();
},
"cloning with utc" : function (test) {
test.expect(4);
var m = moment.utc("2012-01-02T08:20:00");
test.equal(moment.utc(m)._isUTC, true, "the local zone should be converted to UTC");
test.equal(moment.utc(m.clone().utc())._isUTC, true, "the local zone should stay in UTC");
m.zone(120);
test.equal(moment.utc(m)._isUTC, true, "the explicit zone should stay in UTC");
test.equal(moment.utc(m).zone(), 0, "the explicit zone should have an offset of 0");
test.done();
},
"weekday with utc" : function (test) {
test.expect(1);
test.equal(
moment('2013-09-15T00:00:00Z').utc().weekday(), // first minute of the day
moment('2013-09-15T23:59:00Z').utc().weekday(), // last minute of the day
"a UTC-moment's .weekday() should not be affected by the local timezone"
);
test.done();
}
};

View File

@@ -0,0 +1,72 @@
var moment = require("../../moment");
exports.week_year = {
"iso week year": function (test) {
test.expect(19);
// Some examples taken from http://en.wikipedia.org/wiki/ISO_week
test.equal(moment([2005, 0, 1]).isoWeekYear(), 2004);
test.equal(moment([2005, 0, 2]).isoWeekYear(), 2004);
test.equal(moment([2005, 0, 3]).isoWeekYear(), 2005);
test.equal(moment([2005, 11, 31]).isoWeekYear(), 2005);
test.equal(moment([2006, 0, 1]).isoWeekYear(), 2005);
test.equal(moment([2006, 0, 2]).isoWeekYear(), 2006);
test.equal(moment([2007, 0, 1]).isoWeekYear(), 2007);
test.equal(moment([2007, 11, 30]).isoWeekYear(), 2007);
test.equal(moment([2007, 11, 31]).isoWeekYear(), 2008);
test.equal(moment([2008, 0, 1]).isoWeekYear(), 2008);
test.equal(moment([2008, 11, 28]).isoWeekYear(), 2008);
test.equal(moment([2008, 11, 29]).isoWeekYear(), 2009);
test.equal(moment([2008, 11, 30]).isoWeekYear(), 2009);
test.equal(moment([2008, 11, 31]).isoWeekYear(), 2009);
test.equal(moment([2009, 0, 1]).isoWeekYear(), 2009);
test.equal(moment([2010, 0, 1]).isoWeekYear(), 2009);
test.equal(moment([2010, 0, 2]).isoWeekYear(), 2009);
test.equal(moment([2010, 0, 3]).isoWeekYear(), 2009);
test.equal(moment([2010, 0, 4]).isoWeekYear(), 2010);
test.done();
},
"week year": function (test) {
test.expect(31);
// Some examples taken from http://en.wikipedia.org/wiki/ISO_week
moment.lang('dow: 1,doy: 4', {week: {dow: 1, doy: 4}}); // like iso
test.equal(moment([2005, 0, 1]).weekYear(), 2004);
test.equal(moment([2005, 0, 2]).weekYear(), 2004);
test.equal(moment([2005, 0, 3]).weekYear(), 2005);
test.equal(moment([2005, 11, 31]).weekYear(), 2005);
test.equal(moment([2006, 0, 1]).weekYear(), 2005);
test.equal(moment([2006, 0, 2]).weekYear(), 2006);
test.equal(moment([2007, 0, 1]).weekYear(), 2007);
test.equal(moment([2007, 11, 30]).weekYear(), 2007);
test.equal(moment([2007, 11, 31]).weekYear(), 2008);
test.equal(moment([2008, 0, 1]).weekYear(), 2008);
test.equal(moment([2008, 11, 28]).weekYear(), 2008);
test.equal(moment([2008, 11, 29]).weekYear(), 2009);
test.equal(moment([2008, 11, 30]).weekYear(), 2009);
test.equal(moment([2008, 11, 31]).weekYear(), 2009);
test.equal(moment([2009, 0, 1]).weekYear(), 2009);
test.equal(moment([2010, 0, 1]).weekYear(), 2009);
test.equal(moment([2010, 0, 2]).weekYear(), 2009);
test.equal(moment([2010, 0, 3]).weekYear(), 2009);
test.equal(moment([2010, 0, 4]).weekYear(), 2010);
moment.lang('dow: 1,doy: 7', {week: {dow: 1, doy: 7}});
test.equal(moment([2004, 11, 26]).weekYear(), 2004);
test.equal(moment([2004, 11, 27]).weekYear(), 2005);
test.equal(moment([2005, 11, 25]).weekYear(), 2005);
test.equal(moment([2005, 11, 26]).weekYear(), 2006);
test.equal(moment([2006, 11, 31]).weekYear(), 2006);
test.equal(moment([2007, 0, 1]).weekYear(), 2007);
test.equal(moment([2007, 11, 30]).weekYear(), 2007);
test.equal(moment([2007, 11, 31]).weekYear(), 2008);
test.equal(moment([2008, 11, 28]).weekYear(), 2008);
test.equal(moment([2008, 11, 29]).weekYear(), 2009);
test.equal(moment([2009, 11, 27]).weekYear(), 2009);
test.equal(moment([2009, 11, 28]).weekYear(), 2010);
test.done();
}
};

View File

@@ -0,0 +1,159 @@
var moment = require("../../moment");
exports.week_year = {
"iso weekday": function (test) {
var i;
test.expect(7 * 7);
for (i = 0; i < 7; ++i) {
moment.lang('dow:' + i + ',doy: 6', {week: {dow: i, doy: 6}});
test.equal(moment([1985, 1, 4]).isoWeekday(), 1, "Feb 4 1985 is Monday -- 1st day");
test.equal(moment([2029, 8, 18]).isoWeekday(), 2, "Sep 18 2029 is Tuesday -- 2nd day");
test.equal(moment([2013, 3, 24]).isoWeekday(), 3, "Apr 24 2013 is Wednesday -- 3rd day");
test.equal(moment([2015, 2, 5]).isoWeekday(), 4, "Mar 5 2015 is Thursday -- 4th day");
test.equal(moment([1970, 0, 2]).isoWeekday(), 5, "Jan 2 1970 is Friday -- 5th day");
test.equal(moment([2001, 4, 12]).isoWeekday(), 6, "May 12 2001 is Saturday -- 6th day");
test.equal(moment([2000, 0, 2]).isoWeekday(), 7, "Jan 2 2000 is Sunday -- 7th day");
}
test.done();
},
"iso weekday setter" : function (test) {
test.expect(27);
var a = moment([2011, 0, 10]);
test.equal(moment(a).isoWeekday(1).date(), 10, 'set from mon to mon');
test.equal(moment(a).isoWeekday(4).date(), 13, 'set from mon to thu');
test.equal(moment(a).isoWeekday(7).date(), 16, 'set from mon to sun');
test.equal(moment(a).isoWeekday(-6).date(), 3, 'set from mon to last mon');
test.equal(moment(a).isoWeekday(-3).date(), 6, 'set from mon to last thu');
test.equal(moment(a).isoWeekday(0).date(), 9, 'set from mon to last sun');
test.equal(moment(a).isoWeekday(8).date(), 17, 'set from mon to next mon');
test.equal(moment(a).isoWeekday(11).date(), 20, 'set from mon to next thu');
test.equal(moment(a).isoWeekday(14).date(), 23, 'set from mon to next sun');
a = moment([2011, 0, 13]);
test.equal(moment(a).isoWeekday(1).date(), 10, 'set from thu to mon');
test.equal(moment(a).isoWeekday(4).date(), 13, 'set from thu to thu');
test.equal(moment(a).isoWeekday(7).date(), 16, 'set from thu to sun');
test.equal(moment(a).isoWeekday(-6).date(), 3, 'set from thu to last mon');
test.equal(moment(a).isoWeekday(-3).date(), 6, 'set from thu to last thu');
test.equal(moment(a).isoWeekday(0).date(), 9, 'set from thu to last sun');
test.equal(moment(a).isoWeekday(8).date(), 17, 'set from thu to next mon');
test.equal(moment(a).isoWeekday(11).date(), 20, 'set from thu to next thu');
test.equal(moment(a).isoWeekday(14).date(), 23, 'set from thu to next sun');
a = moment([2011, 0, 16]);
test.equal(moment(a).isoWeekday(1).date(), 10, 'set from sun to mon');
test.equal(moment(a).isoWeekday(4).date(), 13, 'set from sun to thu');
test.equal(moment(a).isoWeekday(7).date(), 16, 'set from sun to sun');
test.equal(moment(a).isoWeekday(-6).date(), 3, 'set from sun to last mon');
test.equal(moment(a).isoWeekday(-3).date(), 6, 'set from sun to last thu');
test.equal(moment(a).isoWeekday(0).date(), 9, 'set from sun to last sun');
test.equal(moment(a).isoWeekday(8).date(), 17, 'set from sun to next mon');
test.equal(moment(a).isoWeekday(11).date(), 20, 'set from sun to next thu');
test.equal(moment(a).isoWeekday(14).date(), 23, 'set from sun to next sun');
test.done();
},
"weekday first day of week Sunday (dow 0)": function (test) {
test.expect(7);
moment.lang('dow: 0,doy: 6', {week: {dow: 0, doy: 6}});
test.equal(moment([1985, 1, 3]).weekday(), 0, "Feb 3 1985 is Sunday -- 0th day");
test.equal(moment([2029, 8, 17]).weekday(), 1, "Sep 17 2029 is Monday -- 1st day");
test.equal(moment([2013, 3, 23]).weekday(), 2, "Apr 23 2013 is Tuesday -- 2nd day");
test.equal(moment([2015, 2, 4]).weekday(), 3, "Mar 4 2015 is Wednesday -- 3nd day");
test.equal(moment([1970, 0, 1]).weekday(), 4, "Jan 1 1970 is Thursday -- 4th day");
test.equal(moment([2001, 4, 11]).weekday(), 5, "May 11 2001 is Friday -- 5th day");
test.equal(moment([2000, 0, 1]).weekday(), 6, "Jan 1 2000 is Saturday -- 6th day");
test.done();
},
"weekday first day of week Monday (dow 1)": function (test) {
test.expect(7);
moment.lang('dow: 1,doy: 6', {week: {dow: 1, doy: 6}});
test.equal(moment([1985, 1, 4]).weekday(), 0, "Feb 4 1985 is Monday -- 0th day");
test.equal(moment([2029, 8, 18]).weekday(), 1, "Sep 18 2029 is Tuesday -- 1st day");
test.equal(moment([2013, 3, 24]).weekday(), 2, "Apr 24 2013 is Wednesday -- 2nd day");
test.equal(moment([2015, 2, 5]).weekday(), 3, "Mar 5 2015 is Thursday -- 3nd day");
test.equal(moment([1970, 0, 2]).weekday(), 4, "Jan 2 1970 is Friday -- 4th day");
test.equal(moment([2001, 4, 12]).weekday(), 5, "May 12 2001 is Saturday -- 5th day");
test.equal(moment([2000, 0, 2]).weekday(), 6, "Jan 2 2000 is Sunday -- 6th day");
test.done();
},
"weekday first day of week Tuesday (dow 2)": function (test) {
test.expect(7);
moment.lang('dow: 2,doy: 6', {week: {dow: 2, doy: 6}});
test.equal(moment([1985, 1, 5]).weekday(), 0, "Feb 5 1985 is Tuesday -- 0th day");
test.equal(moment([2029, 8, 19]).weekday(), 1, "Sep 19 2029 is Wednesday -- 1st day");
test.equal(moment([2013, 3, 25]).weekday(), 2, "Apr 25 2013 is Thursday -- 2nd day");
test.equal(moment([2015, 2, 6]).weekday(), 3, "Mar 6 2015 is Friday -- 3nd day");
test.equal(moment([1970, 0, 3]).weekday(), 4, "Jan 3 1970 is Staturday -- 4th day");
test.equal(moment([2001, 4, 13]).weekday(), 5, "May 13 2001 is Sunday -- 5th day");
test.equal(moment([2000, 0, 3]).weekday(), 6, "Jan 3 2000 is Monday -- 6th day");
test.done();
},
"weekday first day of week Wednesday (dow 3)": function (test) {
test.expect(7);
moment.lang('dow: 3,doy: 6', {week: {dow: 3, doy: 6}});
test.equal(moment([1985, 1, 6]).weekday(), 0, "Feb 6 1985 is Wednesday -- 0th day");
test.equal(moment([2029, 8, 20]).weekday(), 1, "Sep 20 2029 is Thursday -- 1st day");
test.equal(moment([2013, 3, 26]).weekday(), 2, "Apr 26 2013 is Friday -- 2nd day");
test.equal(moment([2015, 2, 7]).weekday(), 3, "Mar 7 2015 is Saturday -- 3nd day");
test.equal(moment([1970, 0, 4]).weekday(), 4, "Jan 4 1970 is Sunday -- 4th day");
test.equal(moment([2001, 4, 14]).weekday(), 5, "May 14 2001 is Monday -- 5th day");
test.equal(moment([2000, 0, 4]).weekday(), 6, "Jan 4 2000 is Tuesday -- 6th day");
moment.lang('dow:3,doy:6', null);
test.done();
},
"weekday first day of week Thursday (dow 4)": function (test) {
test.expect(7);
moment.lang('dow: 4,doy: 6', {week: {dow: 4, doy: 6}});
test.equal(moment([1985, 1, 7]).weekday(), 0, "Feb 7 1985 is Thursday -- 0th day");
test.equal(moment([2029, 8, 21]).weekday(), 1, "Sep 21 2029 is Friday -- 1st day");
test.equal(moment([2013, 3, 27]).weekday(), 2, "Apr 27 2013 is Saturday -- 2nd day");
test.equal(moment([2015, 2, 8]).weekday(), 3, "Mar 8 2015 is Sunday -- 3nd day");
test.equal(moment([1970, 0, 5]).weekday(), 4, "Jan 5 1970 is Monday -- 4th day");
test.equal(moment([2001, 4, 15]).weekday(), 5, "May 15 2001 is Tuesday -- 5th day");
test.equal(moment([2000, 0, 5]).weekday(), 6, "Jan 5 2000 is Wednesday -- 6th day");
test.done();
},
"weekday first day of week Friday (dow 5)": function (test) {
test.expect(7);
moment.lang('dow: 5,doy: 6', {week: {dow: 5, doy: 6}});
test.equal(moment([1985, 1, 8]).weekday(), 0, "Feb 8 1985 is Friday -- 0th day");
test.equal(moment([2029, 8, 22]).weekday(), 1, "Sep 22 2029 is Staturday -- 1st day");
test.equal(moment([2013, 3, 28]).weekday(), 2, "Apr 28 2013 is Sunday -- 2nd day");
test.equal(moment([2015, 2, 9]).weekday(), 3, "Mar 9 2015 is Monday -- 3nd day");
test.equal(moment([1970, 0, 6]).weekday(), 4, "Jan 6 1970 is Tuesday -- 4th day");
test.equal(moment([2001, 4, 16]).weekday(), 5, "May 16 2001 is Wednesday -- 5th day");
test.equal(moment([2000, 0, 6]).weekday(), 6, "Jan 6 2000 is Thursday -- 6th day");
test.done();
},
"weekday first day of week Saturday (dow 6)": function (test) {
test.expect(7);
moment.lang('dow: 6,doy: 6', {week: {dow: 6, doy: 6}});
test.equal(moment([1985, 1, 9]).weekday(), 0, "Feb 9 1985 is Staturday -- 0th day");
test.equal(moment([2029, 8, 23]).weekday(), 1, "Sep 23 2029 is Sunday -- 1st day");
test.equal(moment([2013, 3, 29]).weekday(), 2, "Apr 29 2013 is Monday -- 2nd day");
test.equal(moment([2015, 2, 10]).weekday(), 3, "Mar 10 2015 is Tuesday -- 3nd day");
test.equal(moment([1970, 0, 7]).weekday(), 4, "Jan 7 1970 is Wednesday -- 4th day");
test.equal(moment([2001, 4, 17]).weekday(), 5, "May 17 2001 is Thursday -- 5th day");
test.equal(moment([2000, 0, 7]).weekday(), 6, "Jan 7 2000 is Friday -- 6th day");
test.done();
}
};

View File

@@ -0,0 +1,205 @@
var moment = require("../../moment");
exports.weeks = {
setUp : function (cb) {
moment.lang('en');
cb();
},
tearDown : function (cb) {
moment.lang('en');
cb();
},
"day of year" : function (test) {
test.expect(8);
test.equal(moment([2000, 0, 1]).dayOfYear(), 1, "Jan 1 2000 should be day 1 of the year");
test.equal(moment([2000, 1, 28]).dayOfYear(), 59, "Feb 28 2000 should be day 59 of the year");
test.equal(moment([2000, 1, 29]).dayOfYear(), 60, "Feb 28 2000 should be day 60 of the year");
test.equal(moment([2000, 11, 31]).dayOfYear(), 366, "Dec 31 2000 should be day 366 of the year");
test.equal(moment([2001, 0, 1]).dayOfYear(), 1, "Jan 1 2001 should be day 1 of the year");
test.equal(moment([2001, 1, 28]).dayOfYear(), 59, "Feb 28 2001 should be day 59 of the year");
test.equal(moment([2001, 2, 1]).dayOfYear(), 60, "Mar 1 2001 should be day 60 of the year");
test.equal(moment([2001, 11, 31]).dayOfYear(), 365, "Dec 31 2001 should be day 365 of the year");
test.done();
},
"day of year setters" : function (test) {
test.expect(8);
test.equal(moment([2000, 0, 1]).dayOfYear(200).dayOfYear(), 200, "Setting Jan 1 2000 day of the year to 200 should work");
test.equal(moment([2000, 1, 28]).dayOfYear(200).dayOfYear(), 200, "Setting Feb 28 2000 day of the year to 200 should work");
test.equal(moment([2000, 1, 29]).dayOfYear(200).dayOfYear(), 200, "Setting Feb 28 2000 day of the year to 200 should work");
test.equal(moment([2000, 11, 31]).dayOfYear(200).dayOfYear(), 200, "Setting Dec 31 2000 day of the year to 200 should work");
test.equal(moment().dayOfYear(1).dayOfYear(), 1, "Setting day of the year to 1 should work");
test.equal(moment().dayOfYear(59).dayOfYear(), 59, "Setting day of the year to 59 should work");
test.equal(moment().dayOfYear(60).dayOfYear(), 60, "Setting day of the year to 60 should work");
test.equal(moment().dayOfYear(365).dayOfYear(), 365, "Setting day of the year to 365 should work");
test.done();
},
"iso weeks year starting sunday" : function (test) {
test.expect(5);
test.equal(moment([2012, 0, 1]).isoWeek(), 52, "Jan 1 2012 should be iso week 52");
test.equal(moment([2012, 0, 2]).isoWeek(), 1, "Jan 2 2012 should be iso week 1");
test.equal(moment([2012, 0, 8]).isoWeek(), 1, "Jan 8 2012 should be iso week 1");
test.equal(moment([2012, 0, 9]).isoWeek(), 2, "Jan 9 2012 should be iso week 2");
test.equal(moment([2012, 0, 15]).isoWeek(), 2, "Jan 15 2012 should be iso week 2");
test.done();
},
"iso weeks year starting monday" : function (test) {
test.expect(5);
test.equal(moment([2007, 0, 1]).isoWeek(), 1, "Jan 1 2007 should be iso week 1");
test.equal(moment([2007, 0, 7]).isoWeek(), 1, "Jan 7 2007 should be iso week 1");
test.equal(moment([2007, 0, 8]).isoWeek(), 2, "Jan 8 2007 should be iso week 2");
test.equal(moment([2007, 0, 14]).isoWeek(), 2, "Jan 14 2007 should be iso week 2");
test.equal(moment([2007, 0, 15]).isoWeek(), 3, "Jan 15 2007 should be iso week 3");
test.done();
},
"iso weeks year starting tuesday" : function (test) {
test.expect(6);
test.equal(moment([2007, 11, 31]).isoWeek(), 1, "Dec 31 2007 should be iso week 1");
test.equal(moment([2008, 0, 1]).isoWeek(), 1, "Jan 1 2008 should be iso week 1");
test.equal(moment([2008, 0, 6]).isoWeek(), 1, "Jan 6 2008 should be iso week 1");
test.equal(moment([2008, 0, 7]).isoWeek(), 2, "Jan 7 2008 should be iso week 2");
test.equal(moment([2008, 0, 13]).isoWeek(), 2, "Jan 13 2008 should be iso week 2");
test.equal(moment([2008, 0, 14]).isoWeek(), 3, "Jan 14 2008 should be iso week 3");
test.done();
},
"iso weeks year starting wednesday" : function (test) {
test.expect(6);
test.equal(moment([2002, 11, 30]).isoWeek(), 1, "Dec 30 2002 should be iso week 1");
test.equal(moment([2003, 0, 1]).isoWeek(), 1, "Jan 1 2003 should be iso week 1");
test.equal(moment([2003, 0, 5]).isoWeek(), 1, "Jan 5 2003 should be iso week 1");
test.equal(moment([2003, 0, 6]).isoWeek(), 2, "Jan 6 2003 should be iso week 2");
test.equal(moment([2003, 0, 12]).isoWeek(), 2, "Jan 12 2003 should be iso week 2");
test.equal(moment([2003, 0, 13]).isoWeek(), 3, "Jan 13 2003 should be iso week 3");
test.done();
},
"iso weeks year starting thursday" : function (test) {
test.expect(6);
test.equal(moment([2008, 11, 29]).isoWeek(), 1, "Dec 29 2008 should be iso week 1");
test.equal(moment([2009, 0, 1]).isoWeek(), 1, "Jan 1 2009 should be iso week 1");
test.equal(moment([2009, 0, 4]).isoWeek(), 1, "Jan 4 2009 should be iso week 1");
test.equal(moment([2009, 0, 5]).isoWeek(), 2, "Jan 5 2009 should be iso week 2");
test.equal(moment([2009, 0, 11]).isoWeek(), 2, "Jan 11 2009 should be iso week 2");
test.equal(moment([2009, 0, 13]).isoWeek(), 3, "Jan 12 2009 should be iso week 3");
test.done();
},
"iso weeks year starting friday" : function (test) {
test.expect(6);
test.equal(moment([2009, 11, 28]).isoWeek(), 53, "Dec 28 2009 should be iso week 53");
test.equal(moment([2010, 0, 1]).isoWeek(), 53, "Jan 1 2010 should be iso week 53");
test.equal(moment([2010, 0, 3]).isoWeek(), 53, "Jan 3 2010 should be iso week 53");
test.equal(moment([2010, 0, 4]).isoWeek(), 1, "Jan 4 2010 should be iso week 1");
test.equal(moment([2010, 0, 10]).isoWeek(), 1, "Jan 10 2010 should be iso week 1");
test.equal(moment([2010, 0, 11]).isoWeek(), 2, "Jan 11 2010 should be iso week 2");
test.done();
},
"iso weeks year starting saturday" : function (test) {
test.expect(6);
test.equal(moment([2010, 11, 27]).isoWeek(), 52, "Dec 27 2010 should be iso week 52");
test.equal(moment([2011, 0, 1]).isoWeek(), 52, "Jan 1 2011 should be iso week 52");
test.equal(moment([2011, 0, 2]).isoWeek(), 52, "Jan 2 2011 should be iso week 52");
test.equal(moment([2011, 0, 3]).isoWeek(), 1, "Jan 3 2011 should be iso week 1");
test.equal(moment([2011, 0, 9]).isoWeek(), 1, "Jan 9 2011 should be iso week 1");
test.equal(moment([2011, 0, 10]).isoWeek(), 2, "Jan 10 2011 should be iso week 2");
test.done();
},
"iso weeks year starting sunday formatted" : function (test) {
test.expect(5);
test.equal(moment([2012, 0, 1]).format('W WW Wo'), '52 52 52nd', "Jan 1 2012 should be iso week 52");
test.equal(moment([2012, 0, 2]).format('W WW Wo'), '1 01 1st', "Jan 2 2012 should be iso week 1");
test.equal(moment([2012, 0, 8]).format('W WW Wo'), '1 01 1st', "Jan 8 2012 should be iso week 1");
test.equal(moment([2012, 0, 9]).format('W WW Wo'), '2 02 2nd', "Jan 9 2012 should be iso week 2");
test.equal(moment([2012, 0, 15]).format('W WW Wo'), '2 02 2nd', "Jan 15 2012 should be iso week 2");
test.done();
},
"weeks plural year starting sunday" : function (test) {
test.expect(5);
test.equal(moment([2012, 0, 1]).weeks(), 1, "Jan 1 2012 should be week 1");
test.equal(moment([2012, 0, 7]).weeks(), 1, "Jan 7 2012 should be week 1");
test.equal(moment([2012, 0, 8]).weeks(), 2, "Jan 8 2012 should be week 2");
test.equal(moment([2012, 0, 14]).weeks(), 2, "Jan 14 2012 should be week 2");
test.equal(moment([2012, 0, 15]).weeks(), 3, "Jan 15 2012 should be week 3");
test.done();
},
"iso weeks plural year starting sunday" : function (test) {
test.expect(5);
test.equal(moment([2012, 0, 1]).isoWeeks(), 52, "Jan 1 2012 should be iso week 52");
test.equal(moment([2012, 0, 2]).isoWeeks(), 1, "Jan 2 2012 should be iso week 1");
test.equal(moment([2012, 0, 8]).isoWeeks(), 1, "Jan 8 2012 should be iso week 1");
test.equal(moment([2012, 0, 9]).isoWeeks(), 2, "Jan 9 2012 should be iso week 2");
test.equal(moment([2012, 0, 15]).isoWeeks(), 2, "Jan 15 2012 should be iso week 2");
test.done();
},
"weeks setter" : function (test) {
test.expect(5);
test.equal(moment([2012, 0, 1]).week(30).week(), 30, "Setting Jan 1 2012 to week 30 should work");
test.equal(moment([2012, 0, 7]).week(30).week(), 30, "Setting Jan 7 2012 to week 30 should work");
test.equal(moment([2012, 0, 8]).week(30).week(), 30, "Setting Jan 8 2012 to week 30 should work");
test.equal(moment([2012, 0, 14]).week(30).week(), 30, "Setting Jan 14 2012 to week 30 should work");
test.equal(moment([2012, 0, 15]).week(30).week(), 30, "Setting Jan 15 2012 to week 30 should work");
test.done();
},
"iso weeks setter" : function (test) {
test.expect(5);
test.equal(moment([2012, 0, 1]).isoWeeks(25).isoWeeks(), 25, "Setting Jan 1 2012 to week 25 should work");
test.equal(moment([2012, 0, 2]).isoWeeks(24).isoWeeks(), 24, "Setting Jan 2 2012 to week 24 should work");
test.equal(moment([2012, 0, 8]).isoWeeks(23).isoWeeks(), 23, "Setting Jan 8 2012 to week 23 should work");
test.equal(moment([2012, 0, 9]).isoWeeks(22).isoWeeks(), 22, "Setting Jan 9 2012 to week 22 should work");
test.equal(moment([2012, 0, 15]).isoWeeks(21).isoWeeks(), 21, "Setting Jan 15 2012 to week 21 should work");
test.done();
},
"iso weeks setter day of year" : function (test) {
test.expect(6);
test.equal(moment([2012, 0, 1]).isoWeek(1).dayOfYear(), 9, "Setting Jan 1 2012 to week 1 should be day of year 8");
test.equal(moment([2012, 0, 1]).isoWeek(1).year(), 2011, "Setting Jan 1 2012 to week 1 should be year 2011");
test.equal(moment([2012, 0, 2]).isoWeek(1).dayOfYear(), 2, "Setting Jan 2 2012 to week 1 should be day of year 2");
test.equal(moment([2012, 0, 8]).isoWeek(1).dayOfYear(), 8, "Setting Jan 8 2012 to week 1 should be day of year 8");
test.equal(moment([2012, 0, 9]).isoWeek(1).dayOfYear(), 2, "Setting Jan 9 2012 to week 1 should be day of year 2");
test.equal(moment([2012, 0, 15]).isoWeek(1).dayOfYear(), 8, "Setting Jan 15 2012 to week 1 should be day of year 8");
test.done();
}
};

View File

@@ -0,0 +1,481 @@
var moment = require("../../moment");
exports.zones = {
setUp : function (cb) {
moment.lang('en');
cb();
},
tearDown : function (cb) {
moment.lang('en');
cb();
},
"set zone" : function (test) {
var zone = moment();
zone.zone(0);
test.equal(zone.zone(), 0, "should be able to set the zone to 0");
zone.zone(60);
test.equal(zone.zone(), 60, "should be able to set the zone to 60");
zone.zone(-60);
test.equal(zone.zone(), -60, "should be able to set the zone to -60");
test.done();
},
"set zone shorthand" : function (test) {
var zone = moment();
zone.zone(1);
test.equal(zone.zone(), 60, "setting the zone to 1 should imply hours and convert to 60");
zone.zone(-1);
test.equal(zone.zone(), -60, "setting the zone to -1 should imply hours and convert to -60");
zone.zone(15);
test.equal(zone.zone(), 900, "setting the zone to 15 should imply hours and convert to 900");
zone.zone(-15);
test.equal(zone.zone(), -900, "setting the zone to -15 should imply hours and convert to -900");
zone.zone(16);
test.equal(zone.zone(), 16, "setting the zone to 16 should imply minutes");
zone.zone(-16);
test.equal(zone.zone(), -16, "setting the zone to -16 should imply minutes");
test.done();
},
"set zone with string" : function (test) {
var zone = moment();
zone.zone("+00:00");
test.equal(zone.zone(), 0, "set the zone with a timezone string");
zone.zone("2013-03-07T07:00:00-08:00");
test.equal(zone.zone(), 480, "set the zone with a string that does not begin with the timezone");
zone.zone("2013-03-07T07:00:00+0100");
test.equal(zone.zone(), -60, "set the zone with a string that uses the +0000 syntax");
test.done();
},
"change hours when changing the zone" : function (test) {
var zone = moment.utc([2000, 0, 1, 6]);
zone.zone(0);
test.equal(zone.hour(), 6, "UTC 6AM should be 6AM at +0000");
zone.zone(60);
test.equal(zone.hour(), 5, "UTC 6AM should be 5AM at -0100");
zone.zone(-60);
test.equal(zone.hour(), 7, "UTC 6AM should be 7AM at +0100");
test.done();
},
"change minutes when changing the zone" : function (test) {
var zone = moment.utc([2000, 0, 1, 6, 31]);
zone.zone(0);
test.equal(zone.format("HH:mm"), "06:31", "UTC 6:31AM should be 6:31AM at +0000");
zone.zone(30);
test.equal(zone.format("HH:mm"), "06:01", "UTC 6:31AM should be 6:01AM at -0030");
zone.zone(-30);
test.equal(zone.format("HH:mm"), "07:01", "UTC 6:31AM should be 7:01AM at +0030");
zone.zone(1380);
test.equal(zone.format("HH:mm"), "07:31", "UTC 6:31AM should be 7:31AM at +1380");
test.done();
},
"distance from the unix epoch" : function (test) {
var zoneA = moment(),
zoneB = moment(zoneA),
zoneC = moment(zoneA),
zoneD = moment(zoneA),
zoneE = moment(zoneA);
zoneB.utc();
test.equal(+zoneA, +zoneB, "moment should equal moment.utc");
zoneC.zone(-60);
test.equal(+zoneA, +zoneC, "moment should equal moment.zone(-60)");
zoneD.zone(480);
test.equal(+zoneA, +zoneD, "moment should equal moment.zone(480)");
zoneE.zone(1000);
test.equal(+zoneA, +zoneE, "moment should equal moment.zone(1000)");
test.done();
},
"update offset after changing any values" : function (test) {
var oldOffset = moment.updateOffset,
m = moment.utc([2000, 6, 1]);
moment.updateOffset = function (mom) {
if (mom.__doChange) {
if (+mom > 962409600000) {
mom.zone(120);
} else {
mom.zone(60);
}
}
};
test.equal(m.format("ZZ"), "+0000", "should be at +0000");
test.equal(m.format("HH:mm"), "00:00", "should start 12AM at +0000 timezone");
m.__doChange = true;
m.add('h', 1);
test.equal(m.format("ZZ"), "-0200", "should be at -0200");
test.equal(m.format("HH:mm"), "23:00", "1AM at +0000 should be 11PM at -0200 timezone");
m.subtract('h', 1);
test.equal(m.format("ZZ"), "-0100", "should be at -0100");
test.equal(m.format("HH:mm"), "23:00", "12AM at +0000 should be 11PM at -0100 timezone");
moment.updateOffset = oldOffset;
test.done();
},
"getters and setters" : function (test) {
var a = moment([2011, 5, 20]);
test.equal(a.clone().zone(120).year(2012).year(), 2012, "should get and set year correctly");
test.equal(a.clone().zone(120).month(1).month(), 1, "should get and set month correctly");
test.equal(a.clone().zone(120).date(2).date(), 2, "should get and set date correctly");
test.equal(a.clone().zone(120).day(1).day(), 1, "should get and set day correctly");
test.equal(a.clone().zone(120).hour(1).hour(), 1, "should get and set hour correctly");
test.equal(a.clone().zone(120).minute(1).minute(), 1, "should get and set minute correctly");
test.done();
},
"getters" : function (test) {
var a = moment.utc([2012, 0, 1, 0, 0, 0]);
test.equal(a.clone().zone(120).year(), 2011, "should get year correctly");
test.equal(a.clone().zone(120).month(), 11, "should get month correctly");
test.equal(a.clone().zone(120).date(), 31, "should get date correctly");
test.equal(a.clone().zone(120).hour(), 22, "should get hour correctly");
test.equal(a.clone().zone(120).minute(), 0, "should get minute correctly");
test.equal(a.clone().zone(-120).year(), 2012, "should get year correctly");
test.equal(a.clone().zone(-120).month(), 0, "should get month correctly");
test.equal(a.clone().zone(-120).date(), 1, "should get date correctly");
test.equal(a.clone().zone(-120).hour(), 2, "should get hour correctly");
test.equal(a.clone().zone(-120).minute(), 0, "should get minute correctly");
test.equal(a.clone().zone(-90).year(), 2012, "should get year correctly");
test.equal(a.clone().zone(-90).month(), 0, "should get month correctly");
test.equal(a.clone().zone(-90).date(), 1, "should get date correctly");
test.equal(a.clone().zone(-90).hour(), 1, "should get hour correctly");
test.equal(a.clone().zone(-90).minute(), 30, "should get minute correctly");
test.done();
},
"from" : function (test) {
var zoneA = moment(),
zoneB = moment(zoneA).zone(720),
zoneC = moment(zoneA).zone(360),
zoneD = moment(zoneA).zone(-690),
other = moment(zoneA).add('m', 35);
test.equal(zoneA.from(other), zoneB.from(other), "moment#from should be the same in all zones");
test.equal(zoneA.from(other), zoneC.from(other), "moment#from should be the same in all zones");
test.equal(zoneA.from(other), zoneD.from(other), "moment#from should be the same in all zones");
test.done();
},
"diff" : function (test) {
var zoneA = moment(),
zoneB = moment(zoneA).zone(720),
zoneC = moment(zoneA).zone(360),
zoneD = moment(zoneA).zone(-690),
other = moment(zoneA).add('m', 35);
test.equal(zoneA.diff(other), zoneB.diff(other), "moment#diff should be the same in all zones");
test.equal(zoneA.diff(other), zoneC.diff(other), "moment#diff should be the same in all zones");
test.equal(zoneA.diff(other), zoneD.diff(other), "moment#diff should be the same in all zones");
test.equal(zoneA.diff(other, 'minute', true), zoneB.diff(other, 'minute', true), "moment#diff should be the same in all zones");
test.equal(zoneA.diff(other, 'minute', true), zoneC.diff(other, 'minute', true), "moment#diff should be the same in all zones");
test.equal(zoneA.diff(other, 'minute', true), zoneD.diff(other, 'minute', true), "moment#diff should be the same in all zones");
test.equal(zoneA.diff(other, 'hour', true), zoneB.diff(other, 'hour', true), "moment#diff should be the same in all zones");
test.equal(zoneA.diff(other, 'hour', true), zoneC.diff(other, 'hour', true), "moment#diff should be the same in all zones");
test.equal(zoneA.diff(other, 'hour', true), zoneD.diff(other, 'hour', true), "moment#diff should be the same in all zones");
test.done();
},
"unix offset and timestamp" : function (test) {
var zoneA = moment(),
zoneB = moment(zoneA).zone(720),
zoneC = moment(zoneA).zone(360),
zoneD = moment(zoneA).zone(-690);
test.equal(zoneA.unix(), zoneB.unix(), "moment#unix should be the same in all zones");
test.equal(zoneA.unix(), zoneC.unix(), "moment#unix should be the same in all zones");
test.equal(zoneA.unix(), zoneD.unix(), "moment#unix should be the same in all zones");
test.equal(+zoneA, +zoneB, "moment#valueOf should be the same in all zones");
test.equal(+zoneA, +zoneC, "moment#valueOf should be the same in all zones");
test.equal(+zoneA, +zoneD, "moment#valueOf should be the same in all zones");
test.done();
},
"cloning" : function (test) {
test.equal(moment().zone(120).clone().zone(), 120, "explicit cloning should retain the zone");
test.equal(moment().zone(-120).clone().zone(), -120, "explicit cloning should retain the zone");
test.equal(moment(moment().zone(120)).zone(), 120, "implicit cloning should retain the zone");
test.equal(moment(moment().zone(-120)).zone(), -120, "implicit cloning should retain the zone");
test.done();
},
"start of / end of" : function (test) {
var a = moment.utc([2010, 1, 2, 0, 0, 0]).zone(450);
test.equal(a.clone().startOf('day').hour(), 0, "start of day should work on moments with a zone");
test.equal(a.clone().startOf('day').minute(), 0, "start of day should work on moments with a zone");
test.equal(a.clone().startOf('hour').minute(), 0, "start of hour should work on moments with a zone");
test.equal(a.clone().endOf('day').hour(), 23, "end of day should work on moments with a zone");
test.equal(a.clone().endOf('day').minute(), 59, "end of day should work on moments with a zone");
test.equal(a.clone().endOf('hour').minute(), 59, "end of hour should work on moments with a zone");
test.done();
},
"reset zone with moment#utc" : function (test) {
var a = moment.utc([2012]).zone(480);
test.equal(a.clone().hour(), 16, "different zone should have different hour");
test.equal(a.clone().utc().hour(), 0, "calling moment#utc should reset the offset");
test.done();
},
"reset zone with moment#local" : function (test) {
var a = moment([2012]).zone(480);
test.equal(a.clone().local().hour(), 0, "calling moment#local should reset the offset");
test.done();
},
"toDate" : function (test) {
var zoneA = new Date(),
zoneB = moment(zoneA).zone(720).toDate(),
zoneC = moment(zoneA).zone(360).toDate(),
zoneD = moment(zoneA).zone(-690).toDate();
test.equal(+zoneA, +zoneB, "moment#toDate should output a date with the right unix timestamp");
test.equal(+zoneA, +zoneC, "moment#toDate should output a date with the right unix timestamp");
test.equal(+zoneA, +zoneD, "moment#toDate should output a date with the right unix timestamp");
test.done();
},
"same / before / after" : function (test) {
var zoneA = moment().utc(),
zoneB = moment(zoneA).zone(120),
zoneC = moment(zoneA).zone(-120);
test.ok(zoneA.isSame(zoneB), "two moments with different offsets should be the same");
test.ok(zoneA.isSame(zoneC), "two moments with different offsets should be the same");
test.ok(zoneA.isSame(zoneB, 'hour'), "two moments with different offsets should be the same hour");
test.ok(zoneA.isSame(zoneC, 'hour'), "two moments with different offsets should be the same hour");
zoneA.add('hour', 1);
test.ok(zoneA.isAfter(zoneB), "isAfter should work with two moments with different offsets");
test.ok(zoneA.isAfter(zoneC), "isAfter should work with two moments with different offsets");
test.ok(zoneA.isAfter(zoneB, 'hour'), "isAfter:hour should work with two moments with different offsets");
test.ok(zoneA.isAfter(zoneC, 'hour'), "isAfter:hour should work with two moments with different offsets");
zoneA.subtract('hour', 2);
test.ok(zoneA.isBefore(zoneB), "isBefore should work with two moments with different offsets");
test.ok(zoneA.isBefore(zoneC), "isBefore should work with two moments with different offsets");
test.ok(zoneA.isBefore(zoneB, 'hour'), "isBefore:hour should work with two moments with different offsets");
test.ok(zoneA.isBefore(zoneC, 'hour'), "isBefore:hour should work with two moments with different offsets");
test.done();
},
"add / subtract over dst" : function (test) {
var oldOffset = moment.updateOffset,
m = moment.utc([2000, 2, 31, 3]);
moment.updateOffset = function (mom) {
if (mom.clone().utc().month() > 2) {
mom.zone(-60);
} else {
mom.zone(0);
}
};
test.equal(m.hour(), 3, "should start at 00:00");
m.add('hour', 24);
test.equal(m.hour(), 4, "adding 24 hours should disregard dst");
m.subtract('hour', 24);
test.equal(m.hour(), 3, "subtracting 24 hours should disregard dst");
m.add('day', 1);
test.equal(m.hour(), 3, "adding 1 day should have the same hour");
m.subtract('day', 1);
test.equal(m.hour(), 3, "subtracting 1 day should have the same hour");
m.add('month', 1);
test.equal(m.hour(), 3, "adding 1 month should have the same hour");
m.subtract('month', 1);
test.equal(m.hour(), 3, "subtracting 1 month should have the same hour");
moment.updateOffset = oldOffset;
test.done();
},
"isDST" : function (test) {
var oldOffset = moment.updateOffset;
moment.updateOffset = function (mom) {
if (mom.month() > 2 && mom.month() < 9) {
mom.zone(-60);
} else {
mom.zone(0);
}
};
test.ok(!moment().month(0).isDST(), "Jan should not be summer dst");
test.ok(moment().month(6).isDST(), "Jul should be summer dst");
test.ok(!moment().month(11).isDST(), "Dec should not be summer dst");
moment.updateOffset = function (mom) {
if (mom.month() > 2 && mom.month() < 9) {
mom.zone(0);
} else {
mom.zone(-60);
}
};
test.ok(moment().month(0).isDST(), "Jan should be winter dst");
test.ok(!moment().month(6).isDST(), "Jul should not be winter dst");
test.ok(moment().month(11).isDST(), "Dec should be winter dst");
moment.updateOffset = oldOffset;
test.done();
},
"zone names" : function (test) {
test.expect(8);
test.equal(moment().zoneAbbr(), "", "Local zone abbr should be empty");
test.equal(moment().format('z'), "", "Local zone formatted abbr should be empty");
test.equal(moment().zoneName(), "", "Local zone name should be empty");
test.equal(moment().format('zz'), "", "Local zone formatted name should be empty");
test.equal(moment.utc().zoneAbbr(), "UTC", "UTC zone abbr should be UTC");
test.equal(moment.utc().format('z'), "UTC", "UTC zone formatted abbr should be UTC");
test.equal(moment.utc().zoneName(), "Coordinated Universal Time", "UTC zone abbr should be Coordinated Universal Time");
test.equal(moment.utc().format('zz'), "Coordinated Universal Time", "UTC zone formatted abbr should be Coordinated Universal Time");
test.done();
},
"hours alignment with UTC" : function (test) {
test.expect(4);
test.equals(moment().zone(120).hasAlignedHourOffset(), true);
test.equals(moment().zone(-180).hasAlignedHourOffset(), true);
test.equals(moment().zone(90).hasAlignedHourOffset(), false);
test.equals(moment().zone(-90).hasAlignedHourOffset(), false);
test.done();
},
"hours alignment with other zone" : function (test) {
test.expect(16);
var m = moment().zone(120);
test.equals(m.hasAlignedHourOffset(moment().zone(180)), true);
test.equals(m.hasAlignedHourOffset(moment().zone(-180)), true);
test.equals(m.hasAlignedHourOffset(moment().zone(90)), false);
test.equals(m.hasAlignedHourOffset(moment().zone(-90)), false);
m = moment().zone(90);
test.equals(m.hasAlignedHourOffset(moment().zone(180)), false);
test.equals(m.hasAlignedHourOffset(moment().zone(-180)), false);
test.equals(m.hasAlignedHourOffset(moment().zone(30)), true);
test.equals(m.hasAlignedHourOffset(moment().zone(-30)), true);
m = moment().zone(-60);
test.equals(m.hasAlignedHourOffset(moment().zone(180)), true);
test.equals(m.hasAlignedHourOffset(moment().zone(-180)), true);
test.equals(m.hasAlignedHourOffset(moment().zone(90)), false);
test.equals(m.hasAlignedHourOffset(moment().zone(-90)), false);
m = moment().zone(25);
test.equals(m.hasAlignedHourOffset(moment().zone(-35)), true);
test.equals(m.hasAlignedHourOffset(moment().zone(85)), true);
test.equals(m.hasAlignedHourOffset(moment().zone(35)), false);
test.equals(m.hasAlignedHourOffset(moment().zone(-85)), false);
test.done();
},
"parse zone" : function (test) {
test.expect(2);
var m = moment("2013-01-01T00:00:00-13:00").parseZone();
test.equal(m.zone(), 13 * 60);
test.equal(m.hours(), 0);
test.done();
},
"parse zone static" : function (test) {
test.expect(2);
var m = moment.parseZone("2013-01-01T00:00:00-13:00");
test.equal(m.zone(), 13 * 60);
test.equal(m.hours(), 0);
test.done();
}
};