2015-04-29 21:26:05 +00:00
|
|
|
describe("Zotero.Date", function() {
|
2017-01-09 09:21:45 +00:00
|
|
|
describe("#getMonths()", function () {
|
|
|
|
var origLocale;
|
|
|
|
var englishShort = [
|
|
|
|
"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
|
|
|
|
];
|
|
|
|
var englishLong = [
|
|
|
|
"January", "February", "March", "April", "May", "June", "July", "August", "September",
|
|
|
|
"October", "November", "December"
|
|
|
|
];
|
|
|
|
var frenchShort = [
|
2017-01-16 14:54:02 +00:00
|
|
|
"jan", "fév", "mar", "avr", "mai", "juin", "juil", "aoû", "sep", "oct", "nov", "déc"
|
2017-01-09 09:21:45 +00:00
|
|
|
];
|
|
|
|
var frenchLong = [
|
|
|
|
"janvier", "février", "mars", "avril", "mai", "juin", "juillet", "août", "septembre",
|
|
|
|
"octobre", "novembre", "décembre"
|
|
|
|
];
|
|
|
|
|
|
|
|
before(function () {
|
|
|
|
origLocale = Zotero.locale;
|
|
|
|
});
|
|
|
|
|
|
|
|
after(function () {
|
|
|
|
Zotero.locale = origLocale;
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("English", function () {
|
|
|
|
beforeEach(function* () {
|
|
|
|
if (Zotero.locale != 'en-US') {
|
|
|
|
Zotero.locale = 'en-US';
|
2018-07-05 16:12:22 +00:00
|
|
|
Zotero.Date.init();
|
2017-01-09 09:21:45 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should get English short months", function () {
|
|
|
|
let months = Zotero.Date.getMonths().short;
|
|
|
|
assert.lengthOf(months, 12);
|
|
|
|
assert.sameMembers(months, englishShort);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should get English long months", function () {
|
|
|
|
let months = Zotero.Date.getMonths().long;
|
|
|
|
assert.lengthOf(months, 12);
|
|
|
|
assert.sameMembers(months, englishLong);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("shouldn't repeat months in 'withEnglish' mode", function () {
|
|
|
|
let months = Zotero.Date.getMonths(true).short;
|
|
|
|
assert.lengthOf(months, 12);
|
|
|
|
assert.sameMembers(months, englishShort);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should resolve to English from unknown locale", function* () {
|
|
|
|
Zotero.locale = 'zz';
|
2018-07-05 16:12:22 +00:00
|
|
|
Zotero.Date.init();
|
2017-01-09 09:21:45 +00:00
|
|
|
let months = Zotero.Date.getMonths().short;
|
|
|
|
assert.lengthOf(months, 12);
|
|
|
|
assert.sameMembers(months, englishShort);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("shouldn't repeat English with unknown locale", function* () {
|
|
|
|
Zotero.locale = 'zz';
|
2018-07-05 16:12:22 +00:00
|
|
|
Zotero.Date.init();
|
2017-01-09 09:21:45 +00:00
|
|
|
let months = Zotero.Date.getMonths(true).short;
|
|
|
|
assert.lengthOf(months, 12);
|
|
|
|
assert.sameMembers(months, englishShort);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("French", function () {
|
|
|
|
beforeEach(function* () {
|
|
|
|
if (Zotero.locale != 'fr-FR') {
|
|
|
|
Zotero.locale = 'fr-FR';
|
2018-07-05 16:12:22 +00:00
|
|
|
Zotero.Date.init();
|
2017-01-09 09:21:45 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should get French short months", function () {
|
|
|
|
let months = Zotero.Date.getMonths().short;
|
|
|
|
assert.lengthOf(months, 12);
|
|
|
|
assert.sameMembers(months, frenchShort);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should get French long months", function () {
|
|
|
|
let months = Zotero.Date.getMonths().long;
|
|
|
|
assert.lengthOf(months, 12);
|
|
|
|
assert.sameMembers(months, frenchLong);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should get French short months with English", function () {
|
|
|
|
let months = Zotero.Date.getMonths(true).short;
|
|
|
|
assert.lengthOf(months, 24);
|
|
|
|
assert.sameMembers(months, frenchShort.concat(englishShort));
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should get French long months with English", function () {
|
|
|
|
let months = Zotero.Date.getMonths(true).long;
|
|
|
|
assert.lengthOf(months, 24);
|
|
|
|
assert.sameMembers(months, frenchLong.concat(englishLong));
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should resolve from two-letter locale", function* () {
|
|
|
|
Zotero.locale = 'fr';
|
2018-07-05 16:12:22 +00:00
|
|
|
Zotero.Date.init();
|
2017-01-09 09:21:45 +00:00
|
|
|
let months = Zotero.Date.getMonths().short;
|
|
|
|
assert.lengthOf(months, 12);
|
|
|
|
assert.sameMembers(months, frenchShort);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should resolve from unknown four-letter locale with common prefix", function* () {
|
|
|
|
Zotero.locale = 'fr-ZZ';
|
2018-07-05 16:12:22 +00:00
|
|
|
Zotero.Date.init();
|
2017-01-09 09:21:45 +00:00
|
|
|
let months = Zotero.Date.getMonths().short;
|
|
|
|
assert.lengthOf(months, 12);
|
|
|
|
assert.sameMembers(months, frenchShort);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-03-11 08:16:24 +00:00
|
|
|
describe("#sqlToDate()", function () {
|
|
|
|
it("should convert an SQL local date into a JS Date object", function* () {
|
2016-03-14 00:00:29 +00:00
|
|
|
var d1 = new Date();
|
|
|
|
var sqlDate = d1.getFullYear()
|
|
|
|
+ '-'
|
|
|
|
+ Zotero.Utilities.lpad(d1.getMonth() + 1, '0', 2)
|
|
|
|
+ '-'
|
|
|
|
+ Zotero.Utilities.lpad(d1.getDate(), '0', 2)
|
|
|
|
+ ' '
|
|
|
|
+ Zotero.Utilities.lpad(d1.getHours(), '0', 2)
|
|
|
|
+ ':'
|
|
|
|
+ Zotero.Utilities.lpad(d1.getMinutes(), '0', 2)
|
|
|
|
+ ':'
|
|
|
|
+ Zotero.Utilities.lpad(d1.getSeconds(), '0', 2);
|
|
|
|
var offset = d1.getTimezoneOffset() * 60 * 1000;
|
|
|
|
var d2 = Zotero.Date.sqlToDate(sqlDate);
|
|
|
|
assert.equal(
|
|
|
|
Zotero.Date.sqlToDate(sqlDate).getTime(),
|
|
|
|
Math.floor(new Date().getTime() / 1000) * 1000
|
|
|
|
);
|
2016-03-11 08:16:24 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
it("should convert an SQL UTC date into a JS Date object", function* () {
|
|
|
|
var date = "2016-02-27 22:00:00";
|
|
|
|
date = Zotero.Date.sqlToDate(date, true);
|
|
|
|
assert.equal(date.getTime(), 1456610400000);
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2015-04-29 21:26:05 +00:00
|
|
|
describe("#isISODate()", function () {
|
|
|
|
it("should determine whether a date is an ISO 8601 date", function () {
|
|
|
|
assert.ok(Zotero.Date.isISODate("2015"));
|
|
|
|
assert.ok(Zotero.Date.isISODate("2015-04"));
|
|
|
|
assert.ok(Zotero.Date.isISODate("2015-04-29"));
|
|
|
|
assert.ok(Zotero.Date.isISODate("2015-04-29T17:28Z"));
|
|
|
|
assert.isFalse(Zotero.Date.isISODate("2015-04-29 17:28"));
|
|
|
|
})
|
|
|
|
})
|
2016-06-13 10:03:20 +00:00
|
|
|
|
2017-01-17 07:23:15 +00:00
|
|
|
describe("#strToDate()", function () {
|
2019-11-29 08:50:43 +00:00
|
|
|
it("should return object without date parts for null", function () {
|
|
|
|
var o = Zotero.Date.strToDate(null);
|
|
|
|
assert.notProperty(o, 'year');
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should return object without date parts for undefined", function () {
|
|
|
|
var o = Zotero.Date.strToDate();
|
|
|
|
assert.notProperty(o, 'year');
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should return object without date parts for false", function () {
|
|
|
|
var o = Zotero.Date.strToDate(false);
|
|
|
|
assert.notProperty(o, 'year');
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should return object without date parts for empty string", function () {
|
|
|
|
var o = Zotero.Date.strToDate('');
|
|
|
|
assert.notProperty(o, 'year');
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should return object without date parts for blank string", function () {
|
|
|
|
var o = Zotero.Date.strToDate(' ');
|
|
|
|
assert.notProperty(o, 'year');
|
|
|
|
});
|
|
|
|
|
2019-12-19 07:13:57 +00:00
|
|
|
it("should parse two- and three-digit dates with leading zeros", function () {
|
|
|
|
var o = Zotero.Date.strToDate('0068');
|
|
|
|
assert.equal(o.year, 68);
|
|
|
|
|
|
|
|
o = Zotero.Date.strToDate('068');
|
|
|
|
assert.equal(o.year, 68);
|
|
|
|
|
|
|
|
o = Zotero.Date.strToDate('0168');
|
|
|
|
assert.equal(o.year, 168);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should parse two-digit year greater than current year as previous century", function () {
|
|
|
|
var o = Zotero.Date.strToDate('1/1/68');
|
|
|
|
assert.equal(o.year, 1968);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should parse two-digit year less than or equal to current year as current century", function () {
|
|
|
|
var o = Zotero.Date.strToDate('1/1/19');
|
|
|
|
assert.equal(o.year, 2019);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should parse string with just month number", function () {
|
|
|
|
var o = Zotero.Date.strToDate('1');
|
|
|
|
assert.equal(o.month, 0);
|
|
|
|
assert.isUndefined(o.year);
|
2020-05-28 08:32:14 +00:00
|
|
|
assert.equal(o.order, 'm');
|
2019-12-19 07:13:57 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should parse string with just day number", function () {
|
|
|
|
var o = Zotero.Date.strToDate('25');
|
|
|
|
assert.equal(o.day, 25);
|
|
|
|
assert.isUndefined(o.month);
|
|
|
|
assert.isUndefined(o.year);
|
2020-05-28 08:32:14 +00:00
|
|
|
assert.equal(o.order, 'd');
|
2019-12-19 07:13:57 +00:00
|
|
|
});
|
|
|
|
|
2017-01-17 07:23:15 +00:00
|
|
|
it("should work in translator sandbox", function* () {
|
|
|
|
var item = createUnsavedDataObject('item');
|
|
|
|
item.libraryID = Zotero.Libraries.userLibraryID;
|
|
|
|
item.setField('date', '2017-01-17');
|
|
|
|
|
|
|
|
var called = false;
|
|
|
|
var translation = new Zotero.Translate.Export();
|
|
|
|
translation.setItems([item]);
|
|
|
|
translation.setTranslator("9cb70025-a888-4a29-a210-93ec52da40d4"); // BibTeX
|
|
|
|
translation.setHandler("done", function (obj, worked) {
|
|
|
|
called = true;
|
|
|
|
assert.isTrue(worked);
|
|
|
|
assert.include(obj.string, "{2017}");
|
|
|
|
});
|
|
|
|
yield translation.translate();
|
|
|
|
assert.ok(called);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-06-13 10:03:20 +00:00
|
|
|
describe("#isHTTPDate()", function() {
|
|
|
|
it("should determine whether a date is an RFC 2822 compliant date", function() {
|
|
|
|
assert.ok(Zotero.Date.isHTTPDate("Mon, 13 Jun 2016 02:09:08 +4000"));
|
|
|
|
assert.ok(Zotero.Date.isHTTPDate("13 Jun 2016 02:09:08 +4000"));
|
|
|
|
assert.ok(Zotero.Date.isHTTPDate("13 Jun 2016 02:09 +4000"));
|
|
|
|
assert.ok(Zotero.Date.isHTTPDate("13 Jun 2016 02:09 EDT"));
|
|
|
|
})
|
|
|
|
})
|
2015-04-29 21:26:05 +00:00
|
|
|
})
|