Subversion Repositories Applications.papyrus

Rev

Rev 1318 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1318 alexandre_ 1
/*
2
	Copyright (c) 2004-2006, The Dojo Foundation
3
	All Rights Reserved.
4
 
5
	Licensed under the Academic Free License version 2.1 or above OR the
6
	modified BSD license. For more information on Dojo licensing, see:
7
 
8
		http://dojotoolkit.org/community/licensing.shtml
9
*/
10
 
1422 alexandre_ 11
 
12
 
1318 alexandre_ 13
dojo.provide("dojo.date.common");
14
dojo.date.setDayOfYear = function (dateObject, dayOfYear) {
15
	dateObject.setMonth(0);
16
	dateObject.setDate(dayOfYear);
17
	return dateObject;
18
};
19
dojo.date.getDayOfYear = function (dateObject) {
20
	var fullYear = dateObject.getFullYear();
21
	var lastDayOfPrevYear = new Date(fullYear - 1, 11, 31);
22
	return Math.floor((dateObject.getTime() - lastDayOfPrevYear.getTime()) / 86400000);
23
};
24
dojo.date.setWeekOfYear = function (dateObject, week, firstDay) {
25
	if (arguments.length == 1) {
26
		firstDay = 0;
27
	}
28
	dojo.unimplemented("dojo.date.setWeekOfYear");
29
};
30
dojo.date.getWeekOfYear = function (dateObject, firstDay) {
31
	if (arguments.length == 1) {
32
		firstDay = 0;
33
	}
34
	var firstDayOfYear = new Date(dateObject.getFullYear(), 0, 1);
35
	var day = firstDayOfYear.getDay();
36
	firstDayOfYear.setDate(firstDayOfYear.getDate() - day + firstDay - (day > firstDay ? 7 : 0));
37
	return Math.floor((dateObject.getTime() - firstDayOfYear.getTime()) / 604800000);
38
};
39
dojo.date.setIsoWeekOfYear = function (dateObject, week, firstDay) {
40
	if (arguments.length == 1) {
41
		firstDay = 1;
42
	}
43
	dojo.unimplemented("dojo.date.setIsoWeekOfYear");
44
};
45
dojo.date.getIsoWeekOfYear = function (dateObject, firstDay) {
46
	if (arguments.length == 1) {
47
		firstDay = 1;
48
	}
49
	dojo.unimplemented("dojo.date.getIsoWeekOfYear");
50
};
51
dojo.date.shortTimezones = ["IDLW", "BET", "HST", "MART", "AKST", "PST", "MST", "CST", "EST", "AST", "NFT", "BST", "FST", "AT", "GMT", "CET", "EET", "MSK", "IRT", "GST", "AFT", "AGTT", "IST", "NPT", "ALMT", "MMT", "JT", "AWST", "JST", "ACST", "AEST", "LHST", "VUT", "NFT", "NZT", "CHAST", "PHOT", "LINT"];
52
dojo.date.timezoneOffsets = [-720, -660, -600, -570, -540, -480, -420, -360, -300, -240, -210, -180, -120, -60, 0, 60, 120, 180, 210, 240, 270, 300, 330, 345, 360, 390, 420, 480, 540, 570, 600, 630, 660, 690, 720, 765, 780, 840];
53
dojo.date.getDaysInMonth = function (dateObject) {
54
	var month = dateObject.getMonth();
55
	var days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
56
	if (month == 1 && dojo.date.isLeapYear(dateObject)) {
57
		return 29;
58
	} else {
59
		return days[month];
60
	}
61
};
62
dojo.date.isLeapYear = function (dateObject) {
63
	var year = dateObject.getFullYear();
64
	return (year % 400 == 0) ? true : (year % 100 == 0) ? false : (year % 4 == 0) ? true : false;
65
};
66
dojo.date.getTimezoneName = function (dateObject) {
67
	var str = dateObject.toString();
68
	var tz = "";
69
	var match;
70
	var pos = str.indexOf("(");
71
	if (pos > -1) {
72
		pos++;
73
		tz = str.substring(pos, str.indexOf(")"));
74
	} else {
75
		var pat = /([A-Z\/]+) \d{4}$/;
76
		if ((match = str.match(pat))) {
77
			tz = match[1];
78
		} else {
79
			str = dateObject.toLocaleString();
80
			pat = / ([A-Z\/]+)$/;
81
			if ((match = str.match(pat))) {
82
				tz = match[1];
83
			}
84
		}
85
	}
86
	return tz == "AM" || tz == "PM" ? "" : tz;
87
};
88
dojo.date.getOrdinal = function (dateObject) {
89
	var date = dateObject.getDate();
90
	if (date % 100 != 11 && date % 10 == 1) {
91
		return "st";
92
	} else {
93
		if (date % 100 != 12 && date % 10 == 2) {
94
			return "nd";
95
		} else {
96
			if (date % 100 != 13 && date % 10 == 3) {
97
				return "rd";
98
			} else {
99
				return "th";
100
			}
101
		}
102
	}
103
};
104
dojo.date.compareTypes = {DATE:1, TIME:2};
105
dojo.date.compare = function (dateA, dateB, options) {
106
	var dA = dateA;
107
	var dB = dateB || new Date();
108
	var now = new Date();
109
	with (dojo.date.compareTypes) {
110
		var opt = options || (DATE | TIME);
111
		var d1 = new Date((opt & DATE) ? dA.getFullYear() : now.getFullYear(), (opt & DATE) ? dA.getMonth() : now.getMonth(), (opt & DATE) ? dA.getDate() : now.getDate(), (opt & TIME) ? dA.getHours() : 0, (opt & TIME) ? dA.getMinutes() : 0, (opt & TIME) ? dA.getSeconds() : 0);
112
		var d2 = new Date((opt & DATE) ? dB.getFullYear() : now.getFullYear(), (opt & DATE) ? dB.getMonth() : now.getMonth(), (opt & DATE) ? dB.getDate() : now.getDate(), (opt & TIME) ? dB.getHours() : 0, (opt & TIME) ? dB.getMinutes() : 0, (opt & TIME) ? dB.getSeconds() : 0);
113
	}
114
	if (d1.valueOf() > d2.valueOf()) {
115
		return 1;
116
	}
117
	if (d1.valueOf() < d2.valueOf()) {
118
		return -1;
119
	}
120
	return 0;
121
};
122
dojo.date.dateParts = {YEAR:0, MONTH:1, DAY:2, HOUR:3, MINUTE:4, SECOND:5, MILLISECOND:6, QUARTER:7, WEEK:8, WEEKDAY:9};
123
dojo.date.add = function (dt, interv, incr) {
124
	if (typeof dt == "number") {
125
		dt = new Date(dt);
126
	}
127
	function fixOvershoot() {
128
		if (sum.getDate() < dt.getDate()) {
129
			sum.setDate(0);
130
		}
131
	}
132
	var sum = new Date(dt);
133
	with (dojo.date.dateParts) {
134
		switch (interv) {
135
		  case YEAR:
136
			sum.setFullYear(dt.getFullYear() + incr);
137
			fixOvershoot();
138
			break;
139
		  case QUARTER:
140
			incr *= 3;
141
		  case MONTH:
142
			sum.setMonth(dt.getMonth() + incr);
143
			fixOvershoot();
144
			break;
145
		  case WEEK:
146
			incr *= 7;
147
		  case DAY:
148
			sum.setDate(dt.getDate() + incr);
149
			break;
150
		  case WEEKDAY:
151
			var dat = dt.getDate();
152
			var weeks = 0;
153
			var days = 0;
154
			var strt = 0;
155
			var trgt = 0;
156
			var adj = 0;
157
			var mod = incr % 5;
158
			if (mod == 0) {
159
				days = (incr > 0) ? 5 : -5;
160
				weeks = (incr > 0) ? ((incr - 5) / 5) : ((incr + 5) / 5);
161
			} else {
162
				days = mod;
163
				weeks = parseInt(incr / 5);
164
			}
165
			strt = dt.getDay();
166
			if (strt == 6 && incr > 0) {
167
				adj = 1;
168
			} else {
169
				if (strt == 0 && incr < 0) {
170
					adj = -1;
171
				}
172
			}
173
			trgt = (strt + days);
174
			if (trgt == 0 || trgt == 6) {
175
				adj = (incr > 0) ? 2 : -2;
176
			}
177
			sum.setDate(dat + (7 * weeks) + days + adj);
178
			break;
179
		  case HOUR:
180
			sum.setHours(sum.getHours() + incr);
181
			break;
182
		  case MINUTE:
183
			sum.setMinutes(sum.getMinutes() + incr);
184
			break;
185
		  case SECOND:
186
			sum.setSeconds(sum.getSeconds() + incr);
187
			break;
188
		  case MILLISECOND:
189
			sum.setMilliseconds(sum.getMilliseconds() + incr);
190
			break;
191
		  default:
192
			break;
193
		}
194
	}
195
	return sum;
196
};
197
dojo.date.diff = function (dtA, dtB, interv) {
198
	if (typeof dtA == "number") {
199
		dtA = new Date(dtA);
200
	}
201
	if (typeof dtB == "number") {
202
		dtB = new Date(dtB);
203
	}
204
	var yeaDiff = dtB.getFullYear() - dtA.getFullYear();
205
	var monDiff = (dtB.getMonth() - dtA.getMonth()) + (yeaDiff * 12);
206
	var msDiff = dtB.getTime() - dtA.getTime();
207
	var secDiff = msDiff / 1000;
208
	var minDiff = secDiff / 60;
209
	var houDiff = minDiff / 60;
210
	var dayDiff = houDiff / 24;
211
	var weeDiff = dayDiff / 7;
212
	var delta = 0;
213
	with (dojo.date.dateParts) {
214
		switch (interv) {
215
		  case YEAR:
216
			delta = yeaDiff;
217
			break;
218
		  case QUARTER:
219
			var mA = dtA.getMonth();
220
			var mB = dtB.getMonth();
221
			var qA = Math.floor(mA / 3) + 1;
222
			var qB = Math.floor(mB / 3) + 1;
223
			qB += (yeaDiff * 4);
224
			delta = qB - qA;
225
			break;
226
		  case MONTH:
227
			delta = monDiff;
228
			break;
229
		  case WEEK:
230
			delta = parseInt(weeDiff);
231
			break;
232
		  case DAY:
233
			delta = dayDiff;
234
			break;
235
		  case WEEKDAY:
236
			var days = Math.round(dayDiff);
237
			var weeks = parseInt(days / 7);
238
			var mod = days % 7;
239
			if (mod == 0) {
240
				days = weeks * 5;
241
			} else {
242
				var adj = 0;
243
				var aDay = dtA.getDay();
244
				var bDay = dtB.getDay();
245
				weeks = parseInt(days / 7);
246
				mod = days % 7;
247
				var dtMark = new Date(dtA);
248
				dtMark.setDate(dtMark.getDate() + (weeks * 7));
249
				var dayMark = dtMark.getDay();
250
				if (dayDiff > 0) {
251
					switch (true) {
252
					  case aDay == 6:
253
						adj = -1;
254
						break;
255
					  case aDay == 0:
256
						adj = 0;
257
						break;
258
					  case bDay == 6:
259
						adj = -1;
260
						break;
261
					  case bDay == 0:
262
						adj = -2;
263
						break;
264
					  case (dayMark + mod) > 5:
265
						adj = -2;
266
						break;
267
					  default:
268
						break;
269
					}
270
				} else {
271
					if (dayDiff < 0) {
272
						switch (true) {
273
						  case aDay == 6:
274
							adj = 0;
275
							break;
276
						  case aDay == 0:
277
							adj = 1;
278
							break;
279
						  case bDay == 6:
280
							adj = 2;
281
							break;
282
						  case bDay == 0:
283
							adj = 1;
284
							break;
285
						  case (dayMark + mod) < 0:
286
							adj = 2;
287
							break;
288
						  default:
289
							break;
290
						}
291
					}
292
				}
293
				days += adj;
294
				days -= (weeks * 2);
295
			}
296
			delta = days;
297
			break;
298
		  case HOUR:
299
			delta = houDiff;
300
			break;
301
		  case MINUTE:
302
			delta = minDiff;
303
			break;
304
		  case SECOND:
305
			delta = secDiff;
306
			break;
307
		  case MILLISECOND:
308
			delta = msDiff;
309
			break;
310
		  default:
311
			break;
312
		}
313
	}
314
	return Math.round(delta);
315
};
316