Debian Codegraph @

dojox/calendar/time.js

raw symbols SCIP
1 define(["dojo/_base/lang", "dojo/date", "dojo/cldr/supplemental","dojo/date/stamp"], function(lang, date, cldr, stamp) {
2
3 // summary: Advanced date manipulation utilities.
4
5 var time = {};
6
7
8 time.newDate = function(obj, dateClassObj){
9 // summary:
10 // Creates a new Date object.
11 // obj: Object
12 // This object can have several values:
13 // - the time in milliseconds since gregorian epoch.
14 // - a Date instance
15 // - a String instance that can be decoded by the dojo/date/stamp class.
16 // dateClassObj: Object?
17 // The Date class used, by default the native Date.
18
19 // returns: Date
20 dateClassObj = dateClassObj || Date;
21 var d;
22
23 if(typeof(obj) == "number"){
24 return new dateClassObj(obj);
25 }else if(obj.getTime){
26 return new dateClassObj(obj.getTime());
27 }else if(obj.toGregorian){
28 d = obj.toGregorian();
29 if(dateClassObj !== Date){
30 d = new dateClassObj(d.getTime());
31 }
32 return d;
33 }else if(typeof obj == "string"){
34 d = stamp.fromISOString(obj);
35 if(d === null){
36 throw new Error("Cannot parse date string ("+obj+"), specify a \"decodeDate\" function that translates this string into a Date object"); // cannot build date
37 }else if(dateClassObj !== Date){ // from Date to dateClassObj
38 d = new dateClassObj(d.getTime());
39 }
40 return d;
41 }
42
43 };
44
45 time.floorToDay = function(d, reuse, dateClassObj){
46 // summary:
47 // Floors the specified date to the start of day.
48 // date: Date
49 // The date to floor.
50 // reuse: Boolean
51 // Whether use the specified instance or create a new one. Default is false.
52 // dateClassObj: Object?
53 // The Date class used, by default the native Date.
54 // returns: Date
55 dateClassObj = dateClassObj || Date;
56
57 if(!reuse){
58 d = time.newDate(d, dateClassObj);
59 }
60
61 d.setHours(0, 0, 0, 0);
62
63 return d;
64 };
65
66 time.floorToMonth = function(d, reuse, dateClassObj){
67 // summary:
68 // Floors the specified date to the start of the date's month.
69 // date: Date
70 // The date to floor.
71 // reuse: Boolean
72 // Whether use the specified instance or create a new one. Default is false.
73 // dateClassObj: Object?
74 // The Date class used, by default the native Date.
75 // returns: Date
76 dateClassObj = dateClassObj || Date;
77
78 if(!reuse){
79 d = time.newDate(d, dateClassObj);
80 }
81
82 d.setDate(1);
83 d.setHours(0, 0, 0, 0);
84
85 return d;
86 };
87
88
89 time.floorToWeek = function(d, dateClassObj, dateModule, firstDayOfWeek, locale){
90 // summary:
91 // Floors the specified date to the beginning of week.
92 // d: Date
93 // Date to floor.
94 // dateClassObj: Object?
95 // The Date class used, by default the native Date.
96 // dateModule: Object?
97 // Object that contains the "add" method. By default dojo.date is used.
98 // firstDayOfWeek: Integer?
99 // Optional day of week that overrides the one provided by the CLDR.
100 // locale: String?
101 // Optional locale used to determine first day of week.
102 dateClassObj = dateClassObj || Date;
103 dateModule = dateModule || date;
104
105 var fd = firstDayOfWeek == undefined || firstDayOfWeek < 0 ? cldr.getFirstDayOfWeek(locale) : firstDayOfWeek;
106 var day = d.getDay();
107 if(day == fd){
108 return d;
109 }
110 return time.floorToDay(
111 dateModule.add(d, "day", day > fd ? -day+fd : -day+fd-7),
112 true, dateClassObj);
113 };
114
115 time.floor = function(date, unit, steps, reuse, dateClassObj){
116 // summary:
117 // floors the date to the unit.
118 // date: Date
119 // The date/time to floor.
120 // unit: String
121 // The unit. Valid values are "minute", "hour", "day".
122 // steps: Integer
123 // Valid for "minute" or "hour" units.
124 // reuse: Boolean
125 // Whether use the specified instance or create a new one. Default is false.
126 // dateClassObj: Object?
127 // The Date class used, by default the native Date.
128 // returns: Date
129
130 var d = time.floorToDay(date, reuse, dateClassObj);
131
132 switch(unit){
133 case "week":
134 return time.floorToWeek(d, firstDayOfWeek, dateModule, locale);
135 case "minute":
136 d.setHours(date.getHours());
137 d.setMinutes(Math.floor(date.getMinutes() /steps) * steps);
138 break;
139 case "hour":
140 d.setHours(Math.floor(date.getHours() /steps) * steps);
141 break;
142 }
143 return d;
144 };
145
146 time.isStartOfDay = function(d, dateClassObj, dateModule){
147 // summary:
148 // Tests if the specified date represents the starts of day.
149 // d: Date
150 // The date to test.
151 // dateClassObj: Object?
152 // The Date class used, by default the native Date.
153 // dateModule: Object?
154 // Object that contains the "add" method. By default dojo.date is used.
155 // returns: Boolean
156 dateModule = dateModule || date;
157 return dateModule.compare(this.floorToDay(d, false, dateClassObj), d) == 0;
158 };
159
160 time.isToday = function(d, dateClassObj){
161 // summary:
162 // Returns whether the specified date is in the current day.
163 // d: Date
164 // The date to test.
165 // dateClassObj: Object?
166 // The Date class used, by default the native Date.
167 // returns: Boolean
168 dateClassObj = dateClassObj || Date;
169 var today = new dateClassObj();
170 return d.getFullYear() == today.getFullYear() &&
171 d.getMonth() == today.getMonth() &&
172 d.getDate() == today.getDate();
173 };
174
175 time.isOverlapping = function(renderData, start1, end1, start2, end2, includeLimits){
176 // summary:
177 // Computes if the first time range defined by the start1 and end1 parameters
178 // is overlapping the second time range defined by the start2 and end2 parameters.
179 // renderData: Object
180 // The render data.
181 // start1: Date
182 // The start time of the first time range.
183 // end1: Date
184 // The end time of the first time range.
185 // start2: Date
186 // The start time of the second time range.
187 // end2: Date
188 // The end time of the second time range.
189 // includeLimits: Boolean
190 // Whether include the end time or not.
191 // returns: Boolean
192 if(start1 == null || start2 == null || end1 == null || end2 == null){
193 return false;
194 }
195
196 var cal = renderData.dateModule;
197
198 if(includeLimits){
199 if(cal.compare(start1, end2) == 1 || cal.compare(start2, end1) == 1){
200 return false;
201 }
202 }else if(cal.compare(start1, end2) != -1 || cal.compare(start2, end1) != -1){
203 return false;
204 }
205 return true;
206 };
207
208 return time;
209 });