-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathquarter.js
75 lines (62 loc) · 1.24 KB
/
quarter.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
var Quarter = function(year, quarter){
this.year = year;
this.quarter = quarter;
this.str = function(){
return this.year+"_"+this.quarter;
}
this.increase = function(){
this.quarter += 1;
if(this.quarter > 3){
this.quarter = 0
this.year++
}
return this;
}
this.decrease = function(add){
this.quarter -= 1;
if(this.quarter < 0){
this.quarter = 3
this.year--
}
return this;
}
this.add = function(num){
this.quarter += num % 4;
this.year += Math.floor(num / 4);
if(this.quarter > 3){
this.quarter = this.quarter - 4
this.year++;
}
return this;
}
this.sub = function(num){
this.quarter -= num % 4;
this.year -= Math.floor(num / 4);
if(this.quarter < 0){
this.quarter = this.quarter + 4
this.year--;
}
return this;
}
/**
* Get previous quarters, including current, one based.
* @param numOfQuarters : quarter to start counting back from
* @return Array : [Quarter, ...]
*/
this.getLastQuarters = function (numOfQuarters){
var res = [];
var q = this.quarter;
for (var i = 0; i < numOfQuarters; i++) {
res.push(new Quarter(this.year, this.quarter).sub(i));
if (q == 1){
year--;
q = 4;
}
else{
q--;
}
};
return res;
}
}
module.exports = Quarter;