forked from damiengarbarino/dojo-calendar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
time.js
209 lines (182 loc) · 5.87 KB
/
time.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
209
define(["dojo/_base/lang", "dojo/date", "dojo/cldr/supplemental","dojo/date/stamp"], function(lang, date, cldr, stamp) {
// summary: Advanced date manipulation utilities.
var time = {};
time.newDate = function(obj, dateClassObj){
// summary:
// Creates a new Date object.
// obj: Object
// This object can have several values:
// - the time in milliseconds since gregorian epoch.
// - a Date instance
// - a String instance that can be decoded by the dojo/date/stamp class.
// dateClassObj: Object?
// The Date class used, by default the native Date.
// returns: Date
dateClassObj = dateClassObj || Date;
var d;
if(typeof(obj) == "number"){
return new dateClassObj(obj);
}else if(obj.getTime){
return new dateClassObj(obj.getTime());
}else if(obj.toGregorian){
d = obj.toGregorian();
if(dateClassObj !== Date){
d = new dateClassObj(d.getTime());
}
return d;
}else if(typeof obj == "string"){
d = stamp.fromISOString(obj);
if(d === null){
throw new Error("Cannot parse date string ("+obj+"), specify a \"decodeDate\" function that translates this string into a Date object"); // cannot build date
}else if(dateClassObj !== Date){ // from Date to dateClassObj
d = new dateClassObj(d.getTime());
}
return d;
}
};
time.floorToDay = function(d, reuse, dateClassObj){
// summary:
// Floors the specified date to the start of day.
// date: Date
// The date to floor.
// reuse: Boolean
// Whether use the specified instance or create a new one. Default is false.
// dateClassObj: Object?
// The Date class used, by default the native Date.
// returns: Date
dateClassObj = dateClassObj || Date;
if(!reuse){
d = time.newDate(d, dateClassObj);
}
d.setHours(0, 0, 0, 0);
return d;
};
time.floorToMonth = function(d, reuse, dateClassObj){
// summary:
// Floors the specified date to the start of the date's month.
// date: Date
// The date to floor.
// reuse: Boolean
// Whether use the specified instance or create a new one. Default is false.
// dateClassObj: Object?
// The Date class used, by default the native Date.
// returns: Date
dateClassObj = dateClassObj || Date;
if(!reuse){
d = time.newDate(d, dateClassObj);
}
d.setDate(1);
d.setHours(0, 0, 0, 0);
return d;
};
time.floorToWeek = function(d, dateClassObj, dateModule, firstDayOfWeek, locale){
// summary:
// Floors the specified date to the beginning of week.
// d: Date
// Date to floor.
// dateClassObj: Object?
// The Date class used, by default the native Date.
// dateModule: Object?
// Object that contains the "add" method. By default dojo.date is used.
// firstDayOfWeek: Integer?
// Optional day of week that overrides the one provided by the CLDR.
// locale: String?
// Optional locale used to determine first day of week.
dateClassObj = dateClassObj || Date;
dateModule = dateModule || date;
var fd = firstDayOfWeek == undefined || firstDayOfWeek < 0 ? cldr.getFirstDayOfWeek(locale) : firstDayOfWeek;
var day = d.getDay();
if(day == fd){
return d;
}
return time.floorToDay(
dateModule.add(d, "day", day > fd ? -day+fd : -day+fd-7),
true, dateClassObj);
};
time.floor = function(date, unit, steps, reuse, dateClassObj){
// summary:
// floors the date to the unit.
// date: Date
// The date/time to floor.
// unit: String
// The unit. Valid values are "minute", "hour", "day".
// steps: Integer
// Valid for "minute" or "hour" units.
// reuse: Boolean
// Whether use the specified instance or create a new one. Default is false.
// dateClassObj: Object?
// The Date class used, by default the native Date.
// returns: Date
var d = time.floorToDay(date, reuse, dateClassObj);
switch(unit){
case "week":
return time.floorToWeek(d, firstDayOfWeek, dateModule, locale);
case "minute":
d.setHours(date.getHours());
d.setMinutes(Math.floor(date.getMinutes() /steps) * steps);
break;
case "hour":
d.setHours(Math.floor(date.getHours() /steps) * steps);
break;
}
return d;
};
time.isStartOfDay = function(d, dateClassObj, dateModule){
// summary:
// Tests if the specified date represents the starts of day.
// d: Date
// The date to test.
// dateClassObj: Object?
// The Date class used, by default the native Date.
// dateModule: Object?
// Object that contains the "add" method. By default dojo.date is used.
// returns: Boolean
dateModule = dateModule || date;
return dateModule.compare(this.floorToDay(d, false, dateClassObj), d) == 0;
};
time.isToday = function(d, dateClassObj){
// summary:
// Returns whether the specified date is in the current day.
// d: Date
// The date to test.
// dateClassObj: Object?
// The Date class used, by default the native Date.
// returns: Boolean
dateClassObj = dateClassObj || Date;
var today = new dateClassObj();
return d.getFullYear() == today.getFullYear() &&
d.getMonth() == today.getMonth() &&
d.getDate() == today.getDate();
};
time.isOverlapping = function(renderData, start1, end1, start2, end2, includeLimits){
// summary:
// 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.
// renderData: Object
// The render data.
// start1: Date
// The start time of the first time range.
// end1: Date
// The end time of the first time range.
// start2: Date
// The start time of the second time range.
// end2: Date
// The end time of the second time range.
// includeLimits: Boolean
// Whether include the end time or not.
// returns: Boolean
if(start1 == null || start2 == null || end1 == null || end2 == null){
return false;
}
var cal = renderData.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;
};
return time;
});