commit
message
[WIP:WEEKNUM] shifting using a better shift-system
author
Ben Vogt <[email protected]>
date
2017-04-15 01:27:50
stats
2 file(s) changed,
179 insertions(+),
16 deletions(-)
files
src/RawFormulas/Date.ts
tests/DateFormulasTest.ts
1diff --git a/src/RawFormulas/Date.ts b/src/RawFormulas/Date.ts
2index 8ec43db..c01d51a 100644
3--- a/src/RawFormulas/Date.ts
4+++ b/src/RawFormulas/Date.ts
5@@ -282,48 +282,58 @@ var WEEKNUM = function (...values) {
6 }
7 var dm = date.toMoment();
8 var week = dm.week();
9- var dayOfWeek = dm.day() + 1; // between 1 and 7, inclusively
10+ var dayOfWeek = dm.day(); // between 1 and 7, inclusively
11 if (shiftType === 1) {
12 // If this weekYear is not the same as the year, then we're technically in "week 53"
13 // See https://momentjs.com/docs/#/get-set/week-year/ for more info.
14 if (dm.weekYear() !== dm.year()) {
15- week = 53;
16+ week = dm.weeksInYear() + 1;
17 }
18 return week;
19 } else if (shiftType === 2 || shiftType === 11) {
20 if (dm.weekYear() !== dm.year()) {
21- week = 53;
22+ week = dm.weeksInYear() + 1;
23 }
24- if (dayOfWeek === 1) { // sunday shift back
25+ if (dayOfWeek === 0) { // sunday shift back
26 return week - 1;
27 }
28 return week;
29 } else if (shiftType === 12) {
30 if (dm.weekYear() !== dm.year()) {
31- week = 53;
32+ week = dm.weeksInYear() + 1;
33 }
34- if (dayOfWeek <= 2) { // sunday, monday shift back
35+ if (dayOfWeek <= 1) { // sunday, monday shift back
36 return week - 1;
37 }
38 return week;
39 } else if (shiftType === 13) {
40 if (dm.weekYear() !== dm.year()) {
41- week = 53;
42+ week = dm.weeksInYear() + 1;
43 }
44- if (dayOfWeek <= 3) { // sunday, monday, tuesday shift back
45+ if (dayOfWeek <= 2) { // sunday, monday, tuesday shift back
46 return week - 1;
47 }
48 return week;
49 } else if (shiftType === 14) {
50- if (dm.weekYear() !== dm.year()) {
51- week = 53;
52- }
53- if (dayOfWeek <= 4) { // sunday, monday, tuesday, wednesday shift back
54- return week - 1;
55+ var SHIFTER = [3, 4, 5, 6, 0, 1, 2];
56+ var startOfYear = moment.utc(dm).startOf("year");
57+ var weeksCount = 1;
58+ var d = moment.utc(dm).startOf("year").add(6 - SHIFTER[startOfYear.day()], "days");
59+ while (d.isBefore(dm)) {
60+ d.add(7, "days");
61+ weeksCount++;
62 }
63- return week;
64+ return weeksCount;
65 } else if (shiftType === 15) {
66-
67+ var SHIFTER = [2, 3, 4, 5, 6, 0, 1];
68+ var startOfYear = moment.utc(dm).startOf("year");
69+ var weeksCount = 1;
70+ var d = moment.utc(dm).startOf("year").add(6 - SHIFTER[startOfYear.day()], "days");
71+ while (d.isBefore(dm)) {
72+ d.add(7, "days");
73+ weeksCount++;
74+ }
75+ return weeksCount;
76 } else if (shiftType === 16) {
77
78 } else if (shiftType === 17) {
79@@ -333,6 +343,13 @@ var WEEKNUM = function (...values) {
80 } else {
81 throw new NumError("Function WEEKNUM parameter 2 value " + shiftType + " is out of range.");
82 }
83+
84+
85+
86+ // var startOfYear = moment.utc(dm).startOf("year");
87+ // var dayOfWeekOfNewYearsDay = startOfYear.weekday();
88+ // var weekNum = 1;// let's start with the week being the first one, and walking it forward
89+ // var shiftedNewYearsDay = dayShift[dayOfWeekOfNewYearsDay];
90 };
91
92
93diff --git a/tests/DateFormulasTest.ts b/tests/DateFormulasTest.ts
94index 78f7e03..d6659ab 100644
95--- a/tests/DateFormulasTest.ts
96+++ b/tests/DateFormulasTest.ts
97@@ -204,6 +204,152 @@ assertEquals(WEEKNUM(23757, 14), 3);
98 assertEquals(WEEKNUM(23758, 14), 3);
99 assertEquals(WEEKNUM(23759, 14), 3);
100 assertEquals(WEEKNUM(23760, 14), 3);
101+//type=14 again
102+assertEquals(WEEKNUM(355, 14), 52);
103+assertEquals(WEEKNUM(356, 14), 52);
104+assertEquals(WEEKNUM(357, 14), 52);
105+assertEquals(WEEKNUM(358, 14), 52);
106+assertEquals(WEEKNUM(359, 14), 52);
107+assertEquals(WEEKNUM(360, 14), 52);
108+assertEquals(WEEKNUM(361, 14), 52);
109+assertEquals(WEEKNUM(362, 14), 53);
110+assertEquals(WEEKNUM(363, 14), 53);
111+assertEquals(WEEKNUM(364, 14), 53);
112+assertEquals(WEEKNUM(365, 14), 53);
113+assertEquals(WEEKNUM(366, 14), 53);
114+assertEquals(WEEKNUM(367, 14), 1);
115+assertEquals(WEEKNUM(368, 14), 1);
116+assertEquals(WEEKNUM(369, 14), 2);
117+assertEquals(WEEKNUM(370, 14), 2);
118+assertEquals(WEEKNUM(371, 14), 2);
119+assertEquals(WEEKNUM(372, 14), 2);
120+assertEquals(WEEKNUM(373, 14), 2);
121+assertEquals(WEEKNUM(374, 14), 2);
122+assertEquals(WEEKNUM(375, 14), 2);
123+assertEquals(WEEKNUM(376, 14), 3);
124+assertEquals(WEEKNUM(377, 14), 3);
125+assertEquals(WEEKNUM(378, 14), 3);
126+assertEquals(WEEKNUM(379, 14), 3);
127+assertEquals(WEEKNUM(380, 14), 3);
128+assertEquals(WEEKNUM(381, 14), 3);
129+//type=14 again
130+assertEquals(WEEKNUM(730, 14), 53);
131+assertEquals(WEEKNUM(731, 14), 53);
132+assertEquals(WEEKNUM(732, 14), 1);
133+assertEquals(WEEKNUM(733, 14), 2);
134+assertEquals(WEEKNUM(734, 14), 2);
135+assertEquals(WEEKNUM(735, 14), 2);
136+assertEquals(WEEKNUM(736, 14), 2);
137+assertEquals(WEEKNUM(737, 14), 2);
138+assertEquals(WEEKNUM(738, 14), 2);
139+assertEquals(WEEKNUM(739, 14), 2);
140+assertEquals(WEEKNUM(740, 14), 3);
141+assertEquals(WEEKNUM(741, 14), 3);
142+assertEquals(WEEKNUM(742, 14), 3);
143+assertEquals(WEEKNUM(743, 14), 3);
144+assertEquals(WEEKNUM(744, 14), 3);
145+assertEquals(WEEKNUM(745, 14), 3);
146+assertEquals(WEEKNUM(746, 14), 3);
147+
148+//type=15
149+assertEquals(WEEKNUM(23734, 15), 52);
150+assertEquals(WEEKNUM(23735, 15), 52);
151+assertEquals(WEEKNUM(23736, 15), 53);
152+assertEquals(WEEKNUM(23737, 15), 53);
153+assertEquals(WEEKNUM(23738, 15), 53);
154+assertEquals(WEEKNUM(23739, 15), 53);
155+assertEquals(WEEKNUM(23740, 15), 53);
156+assertEquals(WEEKNUM(23741, 15), 53);
157+assertEquals(WEEKNUM(23742, 15), 53);
158+assertEquals(WEEKNUM(23743, 15), 1);
159+assertEquals(WEEKNUM(23744, 15), 1);
160+assertEquals(WEEKNUM(23745, 15), 1);
161+assertEquals(WEEKNUM(23746, 15), 1);
162+assertEquals(WEEKNUM(23747, 15), 1);
163+assertEquals(WEEKNUM(23748, 15), 1);
164+assertEquals(WEEKNUM(23749, 15), 1);
165+assertEquals(WEEKNUM(23750, 15), 2);
166+assertEquals(WEEKNUM(23751, 15), 2);
167+assertEquals(WEEKNUM(23752, 15), 2);
168+assertEquals(WEEKNUM(23753, 15), 2);
169+assertEquals(WEEKNUM(23754, 15), 2);
170+assertEquals(WEEKNUM(23755, 15), 2);
171+assertEquals(WEEKNUM(23756, 15), 2);
172+assertEquals(WEEKNUM(23757, 15), 3);
173+assertEquals(WEEKNUM(23758, 15), 3);
174+assertEquals(WEEKNUM(23759, 15), 3);
175+assertEquals(WEEKNUM(23760, 15), 3);
176+//type=15 again
177+assertEquals(WEEKNUM(355, 15), 51);
178+assertEquals(WEEKNUM(356, 15), 52);
179+assertEquals(WEEKNUM(357, 15), 52);
180+assertEquals(WEEKNUM(358, 15), 52);
181+assertEquals(WEEKNUM(359, 15), 52);
182+assertEquals(WEEKNUM(360, 15), 52);
183+assertEquals(WEEKNUM(361, 15), 52);
184+assertEquals(WEEKNUM(362, 15), 52);
185+assertEquals(WEEKNUM(363, 15), 53);
186+assertEquals(WEEKNUM(364, 15), 53);
187+assertEquals(WEEKNUM(365, 15), 53);
188+assertEquals(WEEKNUM(366, 15), 53);
189+assertEquals(WEEKNUM(367, 15), 1);
190+assertEquals(WEEKNUM(368, 15), 1);
191+assertEquals(WEEKNUM(369, 15), 1);
192+assertEquals(WEEKNUM(370, 15), 2);
193+assertEquals(WEEKNUM(371, 15), 2);
194+assertEquals(WEEKNUM(372, 15), 2);
195+assertEquals(WEEKNUM(373, 15), 2);
196+assertEquals(WEEKNUM(374, 15), 2);
197+assertEquals(WEEKNUM(375, 15), 2);
198+assertEquals(WEEKNUM(376, 15), 2);
199+assertEquals(WEEKNUM(377, 15), 3);
200+assertEquals(WEEKNUM(378, 15), 3);
201+assertEquals(WEEKNUM(379, 15), 3);
202+assertEquals(WEEKNUM(380, 15), 3);
203+assertEquals(WEEKNUM(381, 15), 3);
204+//type=15 again
205+assertEquals(WEEKNUM(730, 15), 53);
206+assertEquals(WEEKNUM(731, 15), 53);
207+assertEquals(WEEKNUM(732, 15), 1);
208+assertEquals(WEEKNUM(733, 15), 1);
209+assertEquals(WEEKNUM(734, 15), 2);
210+assertEquals(WEEKNUM(735, 15), 2);
211+assertEquals(WEEKNUM(736, 15), 2);
212+assertEquals(WEEKNUM(737, 15), 2);
213+assertEquals(WEEKNUM(738, 15), 2);
214+assertEquals(WEEKNUM(739, 15), 2);
215+assertEquals(WEEKNUM(740, 15), 2);
216+assertEquals(WEEKNUM(741, 15), 3);
217+assertEquals(WEEKNUM(742, 15), 3);
218+assertEquals(WEEKNUM(743, 15), 3);
219+assertEquals(WEEKNUM(744, 15), 3);
220+assertEquals(WEEKNUM(745, 15), 3);
221+assertEquals(WEEKNUM(746, 15), 3);
222+
223+
224+
225+
226+
227+
228+
229+
230+
231+
232+
233+
234+
235+
236+
237+
238+
239+
240+
241+
242+
243+
244+
245+
246+
247
248
249 // Test WEEKDAY