forked from ibm-js/deliteful
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TimeBase.js
208 lines (188 loc) · 5.41 KB
/
TimeBase.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
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
define([
"dcl/dcl",
"dojo/date",
"dojo/date/locale",
"dojo/cldr/supplemental",
"dojo/date/stamp"
], function (
dcl,
ddate,
locale,
cldr,
stamp
) {
/**
* Mixin with time methods.
*/
return dcl(null, {
_calendar: "gregorian",
/**
* Object with same API as native Date class.
*/
dateClassObj: Date,
/**
* Object with same API as dojo/date.
*/
dateModule: ddate,
/**
* Object with same API as dojo/date/locale.
*/
dateLocaleModule: locale,
/**
* First day of the week for current locale.
* 0 = Sunday, 1 = Monday, 2 = Tuesday, 3 = Wednesday, 4 = Thursday, 5 = Friday, 6 = Saturday.
*/
firstDayOfWeek: cldr.getFirstDayOfWeek(),
/**
* Creates a new Date object.
* @param obj
* This object can have several values:
*
* - the time in milliseconds since gregorian epoch.
* - a Date instance
* @returns {Date}
*/
newDate: function (obj) {
if (obj.toGregorian) {
obj = obj.toGregorian();
}
if (obj.getTime) {
// obj is a Date. Standard way to copy a Date is new Date(oldDate.getTime()), but that has problems
// with timezone-js around DST when dealing with a timezone different than the machine's timezone.
return new this.dateClassObj(obj.getFullYear(), obj.getMonth(), obj.getDate(),
obj.getHours(), obj.getMinutes(), obj.getSeconds(), obj.getMilliseconds());
} else if (typeof obj === "number") {
// obj is a timestamp.
return new this.dateClassObj(obj);
} else if (typeof obj === "string") {
// obj is an ISO string like "2017-10-20".
var d = stamp.fromISOString(obj);
if (d === null) {
throw new Error("Cannot parse date string (" + obj + ")"); // cannot build date
}
if (this.dateClassObj === Date) {
return d;
} else {
return this.newDate(d); // from Date to this.dateClassObj
}
}
},
/**
* Determines whether the specified date is a week-end.
* @param {Date} date
* @returns {boolean}
*/
isWeekEnd: function (date) {
return locale.isWeekend(date);
},
/**
* Returns the week number string from dojo.date.locale.format() method
* @param {Date} date
* @returns {string}
*/
getWeekNumberLabel: function (date) {
if (date.toGregorian) {
date = date.toGregorian();
}
return locale.format(date, {
selector: "date",
datePattern: "w"
});
},
addAndFloor: function (date, unit, steps) {
// date must be floored!!
// unit >= day
var d = this.dateModule.add(date, unit, steps);
if (d.getHours() === 23) {
d = this.dateModule.add(d, "hour", 2); // go to 1am
} else {
d = this.floorToDay(d);
}
return d;
},
/**
* Floors the specified date to the start of day.
* @param {Date} date
* @returns {Date}
*/
floorToDay: function (date) {
return new this.dateClassObj(date.getFullYear(), date.getMonth(), date.getDate());
},
/**
* Floors the specified date to the beginning of week.
* @param {Date} date
* @returns {Date}
*/
floorToWeek: function (date) {
var fd = this.firstDayOfWeek;
var day = date.getDay();
var dayAdjust = day >= fd ? -day + fd : -day + fd - 7;
return new this.dateClassObj(date.getFullYear(), date.getMonth(), date.getDate() + dayAdjust);
},
/**
* Floors the specified date to the start of the date's month.
* @param {Date} date
* @returns {Date}
*/
floorToMonth: function (date) {
return new this.dateClassObj(date.getFullYear(), date.getMonth(), 1);
},
/**
* Returns whether the specified date is in the current day.
* @param {Date} date
* @returns {boolean}
*/
isToday: function (date) {
var today = new this.dateClassObj();
return date.getFullYear() === today.getFullYear() &&
date.getMonth() === today.getMonth() &&
date.getDate() === today.getDate();
},
/**
* Tests if the specified date represents the start of the day.
* @param {Date} date
* @returns {boolean}
*/
isStartOfDay: function (date) {
return this.dateModule.compare(this.floorToDay(date), date) === 0;
},
/**
* Computes if the first time range defined by the start1 and end1 parameters
* is overlapping the second time range defined by the start2 and end2 parameters.
* @param {Date} start1 - The start time of the first time range.
* @param {Date} end1 - The end time of the first time range.
* @param {Date} start2 - The start time of the second time range.
* @param {Date} end2 - The end time of the second time range.
* @param {boolean} includeLimits - Whether include the end time or not.
* @returns {boolean}
*/
isOverlapping: function (start1, end1, start2, end2, includeLimits) {
if (start1 === null || start2 === null || end1 === null || end2 === null) {
return false;
}
var cal = this.dateModule;
if (includeLimits) {
if (cal.compare(start1, end2) === 1 || cal.compare(start2, end1) === 1) {
return false;
}
} else if (cal.compare(start1, end2) !== -1 || cal.compare(start2, end1) !== -1) {
return false;
}
return true;
},
/**
* Tests if the specified dates are in the same day.
* @param {Date} date1 - The first date.
* @param {Date} date2 - The second date.
* @returns {boolean}
*/
isSameDay: function (date1, date2) {
if (date1 === null || date2 === null) {
return false;
}
return date1.getFullYear() === date2.getFullYear() &&
date1.getMonth() === date2.getMonth() &&
date1.getDate() === date2.getDate();
}
});
});