-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathHDate.js
More file actions
219 lines (197 loc) · 5.36 KB
/
HDate.js
File metadata and controls
219 lines (197 loc) · 5.36 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
//
// Copyright (c) 2015, Shawn Jacobson
// Licensed under the Academic Free License version 3.0
//
// Ported from @see {@link https://bitbucket.org/brianfrank/haystack-java|Haystack Java Toolkit}
//
// History:
// 21 Mar 2015 Shawn Jacobson Creation
//
var HVal = require('./HVal');
/**
* HDate models a date (day in year) tag value.
* @see {@link http://project-haystack.org/doc/TagModel#tagKinds|Project Haystack}
*
* @constructor
* @private
* @extends {HVal}
* @param {int} year - Four digit year such as 2011
* @param {int} month - Month as 1-12 (Jan is 1, Dec is 12)
* @param {int} day - Day of month as 1-31
*/
function HDate(year, month, day) {
this.year = year;
this.month = month;
this.day = day;
}
HDate.prototype = Object.create(HVal.prototype);
module.exports = HDate;
var HDateTime = require('./HDateTime'),
HTime = require('./HTime');
/**
* Encode as "YYYY-MM-DD"
* @return {string}
*/
HDate.prototype.toZinc = function() {
var s = this.year + "-";
if (this.month < 10) s += "0";
s += this.month + "-";
if (this.day < 10) s += "0";
s += this.day;
return s;
};
/**
* Encode as "d:YYYY-MM-DD"
* @returns string
*/
HDate.prototype.toJSON = function() {
return "d:" + this.toZinc();
};
/**
* Equals is based on year, month, day
* @param {HDate} that - object to be compared to
* @return {boolean}
*/
HDate.prototype.equals = function(that) {
return that instanceof HDate && this.year === that.year && this.month === that.month && this.day === that.day;
};
/**
* Return sort order as negative, 0, or positive
* @param {HDate} that - object to be compared to
* @return {boolean}
*/
HDate.prototype.compareTo = function(that) {
if (this.year < that.year)
return -1;
else if (this.year > that.year)
return 1;
if (this.month < that.month)
return -1;
else if (this.month > that.month)
return 1;
if (this.day < that.day)
return -1;
else if (this.day > that.day)
return 1;
return 0;
};
/**
* Return date in future given number of days
* @param {int} numDays
* @return {HDate}
*/
HDate.prototype.plusDays = function(numDays) {
if (numDays === 0) return this;
if (numDays < 0) return this.minusDays(-numDays);
var year = this.year;
var month = this.month;
var day = this.day;
for (var i=0; i<numDays; i++) {
day++;
if (day > HDate.daysInMonth(year, month)) {
day = 1;
month++;
if (month > 12) {
month = 1;
year++;
}
}
}
return HDate.make(year, month, day);
};
/**
* Return date in past given number of days
* @param {int} numDays
* @return {HDate}
*/
HDate.prototype.minusDays = function(numDays) {
if (numDays === 0) return this;
if (numDays < 0) return this.plusDays(-numDays);
var year = this.year;
var month = this.month;
var day = this.day;
for (var i=0; i<numDays; i++) {
day--;
if (day <= 0) {
month--;
if (month < 1) {
month = 12;
year--;
}
day = HDate.daysInMonth(year, month);
}
}
return HDate.make(year, month, day);
};
/**
* Return day of week: Sunday is 1, Saturday is 7
* @return {int}
*/
HDate.prototype.weekday = function() {
return new Date(this.year, this.month - 1, this.day).getDay() + 1;
};
/**
* Construct from basic fields, javascript Date instance, or String in format "YYYY-MM-DD"
* @static
* @param {JSDate|string|int} arg - {JSDate} build HDate based of Javascript Date. month & day should be undefined<br/>
* {string} build HDate from format "YYYY-MM-DD". month & day should be undefined<br/>
* {int} Four digit year - month & day must be defined
* @param {int} month - Month as 1-12 (Jan is 1, Dec is 12)
* @param {int} day - Day of month as 1-31
* @return {HDate}
*/
HDate.make = function(arg, month, day) {
if (arg instanceof Date) {
return new HDate(arg.getFullYear(), arg.getMonth() + 1, arg.getDate());
} else if (HVal.typeis(arg, 'string', String)) {
var s = arg.split('-');
if (s.length<3)
throw new Error("Invalid string format, should be YYYY-MM-DD");
return new HDate(parseInt(s[0]), parseInt(s[1]), parseInt(s[2]));
} else {
if (arg < 1900) throw new Error("Invalid year");
if (month < 1 || month > 12) throw new Error("Invalid month");
if (day < 1 || day > 31) throw new Error("Invalid day");
return new HDate(arg, month, day);
}
};
/**
* Return if given year a leap year
* @static
* @param {int} year - Four digit year
* @return {boolean}
*/
HDate.isLeapYear = function(year) {
if ((year & 3) !== 0) return false;
return (year % 100 !== 0) || (year % 400 === 0);
};
var daysInMon = [-1, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
var daysInMonLeap = [-1, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
/**
* Return number of days in given year (xxxx) and month (1-12)
* @static
* @param {int} year - Four digit year
* @param {int} mon - Month as 1-12 (Jan is 1, Dec is 12)
* @return {int}
*/
HDate.daysInMonth = function(year, mon) {
return HDate.isLeapYear(year) ? daysInMonLeap[mon] : daysInMon[mon];
};
/**
* Get HDate for current time in default timezone
* @static
* @return {HDate}
*/
HDate.today = function() {
return HDateTime.now().date;
};
/**
* Convert a date into HDateTime for midnight in given timezone.
* @static
* @param {HDate} date
* @param {HTimeZone} tz
* @return {HDateTime}
*/
HDate.midnight = function(date, tz) {
return HDateTime.make(date, HTime.MIDNIGHT, tz);
};