From 766bc7aceea116b73de1d8df3da7dfb808c94f0a Mon Sep 17 00:00:00 2001 From: Mario Pietsch Date: Sun, 27 Nov 2016 18:33:19 +0100 Subject: [PATCH] Fix for #2634 problem with week calculations (#2635) --- core/modules/utils/utils.js | 3 ++- editions/test/tiddlers/tests/test-utils.js | 27 ++++++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/core/modules/utils/utils.js b/core/modules/utils/utils.js index 94c08f329..101f202cf 100644 --- a/core/modules/utils/utils.js +++ b/core/modules/utils/utils.js @@ -358,7 +358,8 @@ exports.getWeek = function(date) { d = 7; // JavaScript Sun=0, ISO Sun=7 } dt.setTime(dt.getTime() + (4 - d) * 86400000);// shift day to Thurs of same week to calculate weekNo - var n = Math.floor((dt.getTime()-new Date(dt.getFullYear(),0,1) + 3600000) / 86400000); + var x = new Date(dt.getFullYear(),0,1); + var n = Math.floor((dt.getTime() - x.getTime()) / 86400000); return Math.floor(n / 7) + 1; }; diff --git a/editions/test/tiddlers/tests/test-utils.js b/editions/test/tiddlers/tests/test-utils.js index 2aa3ea665..fd924e1bf 100644 --- a/editions/test/tiddlers/tests/test-utils.js +++ b/editions/test/tiddlers/tests/test-utils.js @@ -27,11 +27,38 @@ describe("Utility tests", function() { it("should handle formatting a date string", function() { var fds = $tw.utils.formatDateString, + // nov is month: 10! d = new Date(2014,10,9,17,41,28,542); 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"); expect(fds(d,"MM0\\D\\D")).toBe("110DD"); + + // test some edge cases found at: https://en.wikipedia.org/wiki/ISO_week_date + // 2016-11-13 is Week 45 and it's a Sunday (month nr: 10) + d = new Date(2016,10,12,23,59,59); + expect(fds(d,"WW")).toBe("45"); + d = new Date(2016,10,13,23,59,59,999); + expect(fds(d,"WW")).toBe("45"); + d = new Date(2016,10,13,23,59,60); // see 60 seconds. so it's week 46 + expect(fds(d,"WW")).toBe("46"); + + // 2006 Dez. 31 is end of week 52 (month nr: 11) + d = new Date(2006,11,31,23,59,59); + expect(fds(d,"WW")).toBe("52"); + d = new Date(2006,11,31,23,59,60); + expect(fds(d,"WW")).toBe("1"); + + // 2010 Jan 03 is in week 53 (month nr: 0) + d = new Date(2010,0,3,23,59,59); + expect(fds(d,"WW")).toBe("53"); + d = new Date(2010,0,3,23,59,60); + expect(fds(d,"WW")).toBe("1"); + + // 2014 12 29 is in week 1 of 2015 (month nr. 11) + d = new Date(2014,11,29,23,59,59); + expect(fds(d,"WW")).toBe("1"); + expect(fds(d,"wYYYY")).toBe("2015"); }); it("should parse text references", function() {