Add support for working with negative dates

See discussion https://groups.google.com/g/tiddlywiki/c/aHlyaHr93Io/m/vGcDa6lxAgAJ
This commit is contained in:
jeremy@jermolene.com
2020-12-01 10:39:27 +00:00
parent dde4182830
commit 6a91dbfe2f
5 changed files with 41 additions and 80 deletions

View File

@@ -25,6 +25,16 @@ describe("Utility tests", function() {
expect(psa(" [[Tidd\u00a0ler8]] two ")).toEqual(["Tidd\u00a0ler8","two"]);
});
it("should handle parsing a date", function() {
var pd = function(v) {
return $tw.utils.parseDate(v).toString();
};
expect(pd("20150428204930183")).toEqual("Tue Apr 28 2015 21:49:30 GMT+0100 (British Summer Time)");
expect(pd("-20150428204930183")).toEqual("Sun Apr 28 -2015 20:48:15 GMT-0001 (British Summer Time)");
expect(pd("00730428204930183")).toEqual("Fri Apr 28 0073 20:48:15 GMT-0001 (British Summer Time)");
expect(pd("-00730428204930183")).toEqual("Thu Apr 28 -0073 20:48:15 GMT-0001 (British Summer Time)");
});
it("should handle base64 encoding emojis", function() {
var booksEmoji = "📚";
expect(booksEmoji).toBe(booksEmoji);
@@ -62,6 +72,8 @@ describe("Utility tests", function() {
var fds = $tw.utils.formatDateString,
// nov is month: 10!
d = new Date(2014,10,9,17,41,28,542);
expect(fds(d,"{era:bce||ce}")).toBe("ce");
expect(fds(d,"YYYY")).toBe("2014");
expect(fds(d,"DDD DD MMM YYYY")).toBe("Sunday 9 November 2014");
expect(fds(d,"ddd hh mm ssss")).toBe("Sun 17 41 2828");
expect(fds(d,"MM0DD")).toBe("1109");
@@ -92,6 +104,19 @@ describe("Utility tests", function() {
d = new Date(2014,11,29,23,59,59);
expect(fds(d,"WW")).toBe("1");
expect(fds(d,"wYYYY")).toBe("2015");
// Negative years
d = new Date(-2014,10,9,17,41,28,542);
expect(fds(d,"YYYY")).toBe("-2014");
expect(fds(d,"aYYYY")).toBe("2014");
expect(fds(d,"{era:bce||ce}")).toBe("bce");
// Zero years
d = new Date(0,10,9,17,41,28,542);
d.setUTCFullYear(0); // See https://stackoverflow.com/a/5870822
expect(fds(d,"YYYY")).toBe("0000");
expect(fds(d,"aYYYY")).toBe("0000");
expect(fds(d,"{era:bce|z|ce}")).toBe("z");
});
it("should parse text references", function() {