Subversion Repositories Applications.papyrus

Rev

Go to most recent revision | Details | 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
 
11
dojo.provide("dojo.date.format");
12
dojo.require("dojo.date.common");
13
dojo.require("dojo.date.supplemental");
14
dojo.require("dojo.lang.array");
15
dojo.require("dojo.lang.common");
16
dojo.require("dojo.lang.func");
17
dojo.require("dojo.string.common");
18
dojo.require("dojo.i18n.common");
19
dojo.requireLocalization("dojo.i18n.calendar", "gregorian", null, "de,en,es,fi,fr,hu,ja,it,ko,nl,pt,sv,zh,pt-br,zh-cn,zh-hk,zh-tw,ROOT");
20
dojo.requireLocalization("dojo.i18n.calendar", "gregorianExtras", null, "ja,zh,ROOT");
21
(function () {
22
	dojo.date.format = function (dateObject, options) {
23
		if (typeof options == "string") {
24
			dojo.deprecated("dojo.date.format", "To format dates with POSIX-style strings, please use dojo.date.strftime instead", "0.5");
25
			return dojo.date.strftime(dateObject, options);
26
		}
27
		function formatPattern(dateObject, pattern) {
28
			return pattern.replace(/([a-z])\1*/ig, function (match) {
29
				var s;
30
				var c = match.charAt(0);
31
				var l = match.length;
32
				var pad;
33
				var widthList = ["abbr", "wide", "narrow"];
34
				switch (c) {
35
				  case "G":
36
					if (l > 3) {
37
						dojo.unimplemented("Era format not implemented");
38
					}
39
					s = info.eras[dateObject.getFullYear() < 0 ? 1 : 0];
40
					break;
41
				  case "y":
42
					s = dateObject.getFullYear();
43
					switch (l) {
44
					  case 1:
45
						break;
46
					  case 2:
47
						s = String(s).substr(-2);
48
						break;
49
					  default:
50
						pad = true;
51
					}
52
					break;
53
				  case "Q":
54
				  case "q":
55
					s = Math.ceil((dateObject.getMonth() + 1) / 3);
56
					switch (l) {
57
					  case 1:
58
					  case 2:
59
						pad = true;
60
						break;
61
					  case 3:
62
					  case 4:
63
						dojo.unimplemented("Quarter format not implemented");
64
					}
65
					break;
66
				  case "M":
67
				  case "L":
68
					var m = dateObject.getMonth();
69
					var width;
70
					switch (l) {
71
					  case 1:
72
					  case 2:
73
						s = m + 1;
74
						pad = true;
75
						break;
76
					  case 3:
77
					  case 4:
78
					  case 5:
79
						width = widthList[l - 3];
80
						break;
81
					}
82
					if (width) {
83
						var type = (c == "L") ? "standalone" : "format";
84
						var prop = ["months", type, width].join("-");
85
						s = info[prop][m];
86
					}
87
					break;
88
				  case "w":
89
					var firstDay = 0;
90
					s = dojo.date.getWeekOfYear(dateObject, firstDay);
91
					pad = true;
92
					break;
93
				  case "d":
94
					s = dateObject.getDate();
95
					pad = true;
96
					break;
97
				  case "D":
98
					s = dojo.date.getDayOfYear(dateObject);
99
					pad = true;
100
					break;
101
				  case "E":
102
				  case "e":
103
				  case "c":
104
					var d = dateObject.getDay();
105
					var width;
106
					switch (l) {
107
					  case 1:
108
					  case 2:
109
						if (c == "e") {
110
							var first = dojo.date.getFirstDayOfWeek(options.locale);
111
							d = (d - first + 7) % 7;
112
						}
113
						if (c != "c") {
114
							s = d + 1;
115
							pad = true;
116
							break;
117
						}
118
					  case 3:
119
					  case 4:
120
					  case 5:
121
						width = widthList[l - 3];
122
						break;
123
					}
124
					if (width) {
125
						var type = (c == "c") ? "standalone" : "format";
126
						var prop = ["days", type, width].join("-");
127
						s = info[prop][d];
128
					}
129
					break;
130
				  case "a":
131
					var timePeriod = (dateObject.getHours() < 12) ? "am" : "pm";
132
					s = info[timePeriod];
133
					break;
134
				  case "h":
135
				  case "H":
136
				  case "K":
137
				  case "k":
138
					var h = dateObject.getHours();
139
					switch (c) {
140
					  case "h":
141
						s = (h % 12) || 12;
142
						break;
143
					  case "H":
144
						s = h;
145
						break;
146
					  case "K":
147
						s = (h % 12);
148
						break;
149
					  case "k":
150
						s = h || 24;
151
						break;
152
					}
153
					pad = true;
154
					break;
155
				  case "m":
156
					s = dateObject.getMinutes();
157
					pad = true;
158
					break;
159
				  case "s":
160
					s = dateObject.getSeconds();
161
					pad = true;
162
					break;
163
				  case "S":
164
					s = Math.round(dateObject.getMilliseconds() * Math.pow(10, l - 3));
165
					break;
166
				  case "v":
167
				  case "z":
168
					s = dojo.date.getTimezoneName(dateObject);
169
					if (s) {
170
						break;
171
					}
172
					l = 4;
173
				  case "Z":
174
					var offset = dateObject.getTimezoneOffset();
175
					var tz = [(offset <= 0 ? "+" : "-"), dojo.string.pad(Math.floor(Math.abs(offset) / 60), 2), dojo.string.pad(Math.abs(offset) % 60, 2)];
176
					if (l == 4) {
177
						tz.splice(0, 0, "GMT");
178
						tz.splice(3, 0, ":");
179
					}
180
					s = tz.join("");
181
					break;
182
				  case "Y":
183
				  case "u":
184
				  case "W":
185
				  case "F":
186
				  case "g":
187
				  case "A":
188
					dojo.debug(match + " modifier not yet implemented");
189
					s = "?";
190
					break;
191
				  default:
192
					dojo.raise("dojo.date.format: invalid pattern char: " + pattern);
193
				}
194
				if (pad) {
195
					s = dojo.string.pad(s, l);
196
				}
197
				return s;
198
			});
199
		}
200
		options = options || {};
201
		var locale = dojo.hostenv.normalizeLocale(options.locale);
202
		var formatLength = options.formatLength || "full";
203
		var info = dojo.date._getGregorianBundle(locale);
204
		var str = [];
205
		var sauce = dojo.lang.curry(this, formatPattern, dateObject);
206
		if (options.selector != "timeOnly") {
207
			var datePattern = options.datePattern || info["dateFormat-" + formatLength];
208
			if (datePattern) {
209
				str.push(_processPattern(datePattern, sauce));
210
			}
211
		}
212
		if (options.selector != "dateOnly") {
213
			var timePattern = options.timePattern || info["timeFormat-" + formatLength];
214
			if (timePattern) {
215
				str.push(_processPattern(timePattern, sauce));
216
			}
217
		}
218
		var result = str.join(" ");
219
		return result;
220
	};
221
	dojo.date.parse = function (value, options) {
222
		options = options || {};
223
		var locale = dojo.hostenv.normalizeLocale(options.locale);
224
		var info = dojo.date._getGregorianBundle(locale);
225
		var formatLength = options.formatLength || "full";
226
		if (!options.selector) {
227
			options.selector = "dateOnly";
228
		}
229
		var datePattern = options.datePattern || info["dateFormat-" + formatLength];
230
		var timePattern = options.timePattern || info["timeFormat-" + formatLength];
231
		var pattern;
232
		if (options.selector == "dateOnly") {
233
			pattern = datePattern;
234
		} else {
235
			if (options.selector == "timeOnly") {
236
				pattern = timePattern;
237
			} else {
238
				if (options.selector == "dateTime") {
239
					pattern = datePattern + " " + timePattern;
240
				} else {
241
					var msg = "dojo.date.parse: Unknown selector param passed: '" + options.selector + "'.";
242
					msg += " Defaulting to date pattern.";
243
					dojo.debug(msg);
244
					pattern = datePattern;
245
				}
246
			}
247
		}
248
		var groups = [];
249
		var dateREString = _processPattern(pattern, dojo.lang.curry(this, _buildDateTimeRE, groups, info, options));
250
		var dateRE = new RegExp("^" + dateREString + "$");
251
		var match = dateRE.exec(value);
252
		if (!match) {
253
			return null;
254
		}
255
		var widthList = ["abbr", "wide", "narrow"];
256
		var result = new Date(1972, 0);
257
		var expected = {};
258
		for (var i = 1; i < match.length; i++) {
259
			var grp = groups[i - 1];
260
			var l = grp.length;
261
			var v = match[i];
262
			switch (grp.charAt(0)) {
263
			  case "y":
264
				if (l != 2) {
265
					result.setFullYear(v);
266
					expected.year = v;
267
				} else {
268
					if (v < 100) {
269
						v = Number(v);
270
						var year = "" + new Date().getFullYear();
271
						var century = year.substring(0, 2) * 100;
272
						var yearPart = Number(year.substring(2, 4));
273
						var cutoff = Math.min(yearPart + 20, 99);
274
						var num = (v < cutoff) ? century + v : century - 100 + v;
275
						result.setFullYear(num);
276
						expected.year = num;
277
					} else {
278
						if (options.strict) {
279
							return null;
280
						}
281
						result.setFullYear(v);
282
						expected.year = v;
283
					}
284
				}
285
				break;
286
			  case "M":
287
				if (l > 2) {
288
					if (!options.strict) {
289
						v = v.replace(/\./g, "");
290
						v = v.toLowerCase();
291
					}
292
					var months = info["months-format-" + widthList[l - 3]].concat();
293
					for (var j = 0; j < months.length; j++) {
294
						if (!options.strict) {
295
							months[j] = months[j].toLowerCase();
296
						}
297
						if (v == months[j]) {
298
							result.setMonth(j);
299
							expected.month = j;
300
							break;
301
						}
302
					}
303
					if (j == months.length) {
304
						dojo.debug("dojo.date.parse: Could not parse month name: '" + v + "'.");
305
						return null;
306
					}
307
				} else {
308
					result.setMonth(v - 1);
309
					expected.month = v - 1;
310
				}
311
				break;
312
			  case "E":
313
			  case "e":
314
				if (!options.strict) {
315
					v = v.toLowerCase();
316
				}
317
				var days = info["days-format-" + widthList[l - 3]].concat();
318
				for (var j = 0; j < days.length; j++) {
319
					if (!options.strict) {
320
						days[j] = days[j].toLowerCase();
321
					}
322
					if (v == days[j]) {
323
						break;
324
					}
325
				}
326
				if (j == days.length) {
327
					dojo.debug("dojo.date.parse: Could not parse weekday name: '" + v + "'.");
328
					return null;
329
				}
330
				break;
331
			  case "d":
332
				result.setDate(v);
333
				expected.date = v;
334
				break;
335
			  case "a":
336
				var am = options.am || info.am;
337
				var pm = options.pm || info.pm;
338
				if (!options.strict) {
339
					v = v.replace(/\./g, "").toLowerCase();
340
					am = am.replace(/\./g, "").toLowerCase();
341
					pm = pm.replace(/\./g, "").toLowerCase();
342
				}
343
				if (options.strict && v != am && v != pm) {
344
					dojo.debug("dojo.date.parse: Could not parse am/pm part.");
345
					return null;
346
				}
347
				var hours = result.getHours();
348
				if (v == pm && hours < 12) {
349
					result.setHours(hours + 12);
350
				} else {
351
					if (v == am && hours == 12) {
352
						result.setHours(0);
353
					}
354
				}
355
				break;
356
			  case "K":
357
				if (v == 24) {
358
					v = 0;
359
				}
360
			  case "h":
361
			  case "H":
362
			  case "k":
363
				if (v > 23) {
364
					dojo.debug("dojo.date.parse: Illegal hours value");
365
					return null;
366
				}
367
				result.setHours(v);
368
				break;
369
			  case "m":
370
				result.setMinutes(v);
371
				break;
372
			  case "s":
373
				result.setSeconds(v);
374
				break;
375
			  case "S":
376
				result.setMilliseconds(v);
377
				break;
378
			  default:
379
				dojo.unimplemented("dojo.date.parse: unsupported pattern char=" + grp.charAt(0));
380
			}
381
		}
382
		if (expected.year && result.getFullYear() != expected.year) {
383
			dojo.debug("Parsed year: '" + result.getFullYear() + "' did not match input year: '" + expected.year + "'.");
384
			return null;
385
		}
386
		if (expected.month && result.getMonth() != expected.month) {
387
			dojo.debug("Parsed month: '" + result.getMonth() + "' did not match input month: '" + expected.month + "'.");
388
			return null;
389
		}
390
		if (expected.date && result.getDate() != expected.date) {
391
			dojo.debug("Parsed day of month: '" + result.getDate() + "' did not match input day of month: '" + expected.date + "'.");
392
			return null;
393
		}
394
		return result;
395
	};
396
	function _processPattern(pattern, applyPattern, applyLiteral, applyAll) {
397
		var identity = function (x) {
398
			return x;
399
		};
400
		applyPattern = applyPattern || identity;
401
		applyLiteral = applyLiteral || identity;
402
		applyAll = applyAll || identity;
403
		var chunks = pattern.match(/(''|[^'])+/g);
404
		var literal = false;
405
		for (var i = 0; i < chunks.length; i++) {
406
			if (!chunks[i]) {
407
				chunks[i] = "";
408
			} else {
409
				chunks[i] = (literal ? applyLiteral : applyPattern)(chunks[i]);
410
				literal = !literal;
411
			}
412
		}
413
		return applyAll(chunks.join(""));
414
	}
415
	function _buildDateTimeRE(groups, info, options, pattern) {
416
		return pattern.replace(/([a-z])\1*/ig, function (match) {
417
			var s;
418
			var c = match.charAt(0);
419
			var l = match.length;
420
			switch (c) {
421
			  case "y":
422
				s = "\\d" + ((l == 2) ? "{2,4}" : "+");
423
				break;
424
			  case "M":
425
				s = (l > 2) ? "\\S+" : "\\d{1,2}";
426
				break;
427
			  case "d":
428
				s = "\\d{1,2}";
429
				break;
430
			  case "E":
431
				s = "\\S+";
432
				break;
433
			  case "h":
434
			  case "H":
435
			  case "K":
436
			  case "k":
437
				s = "\\d{1,2}";
438
				break;
439
			  case "m":
440
			  case "s":
441
				s = "[0-5]\\d";
442
				break;
443
			  case "S":
444
				s = "\\d{1,3}";
445
				break;
446
			  case "a":
447
				var am = options.am || info.am || "AM";
448
				var pm = options.pm || info.pm || "PM";
449
				if (options.strict) {
450
					s = am + "|" + pm;
451
				} else {
452
					s = am;
453
					s += (am != am.toLowerCase()) ? "|" + am.toLowerCase() : "";
454
					s += "|";
455
					s += (pm != pm.toLowerCase()) ? pm + "|" + pm.toLowerCase() : pm;
456
				}
457
				break;
458
			  default:
459
				dojo.unimplemented("parse of date format, pattern=" + pattern);
460
			}
461
			if (groups) {
462
				groups.push(match);
463
			}
464
			return "\\s*(" + s + ")\\s*";
465
		});
466
	}
467
})();
468
dojo.date.strftime = function (dateObject, format, locale) {
469
	var padChar = null;
470
	function _(s, n) {
471
		return dojo.string.pad(s, n || 2, padChar || "0");
472
	}
473
	var info = dojo.date._getGregorianBundle(locale);
474
	function $(property) {
475
		switch (property) {
476
		  case "a":
477
			return dojo.date.getDayShortName(dateObject, locale);
478
		  case "A":
479
			return dojo.date.getDayName(dateObject, locale);
480
		  case "b":
481
		  case "h":
482
			return dojo.date.getMonthShortName(dateObject, locale);
483
		  case "B":
484
			return dojo.date.getMonthName(dateObject, locale);
485
		  case "c":
486
			return dojo.date.format(dateObject, {locale:locale});
487
		  case "C":
488
			return _(Math.floor(dateObject.getFullYear() / 100));
489
		  case "d":
490
			return _(dateObject.getDate());
491
		  case "D":
492
			return $("m") + "/" + $("d") + "/" + $("y");
493
		  case "e":
494
			if (padChar == null) {
495
				padChar = " ";
496
			}
497
			return _(dateObject.getDate());
498
		  case "f":
499
			if (padChar == null) {
500
				padChar = " ";
501
			}
502
			return _(dateObject.getMonth() + 1);
503
		  case "g":
504
			break;
505
		  case "G":
506
			dojo.unimplemented("unimplemented modifier 'G'");
507
			break;
508
		  case "F":
509
			return $("Y") + "-" + $("m") + "-" + $("d");
510
		  case "H":
511
			return _(dateObject.getHours());
512
		  case "I":
513
			return _(dateObject.getHours() % 12 || 12);
514
		  case "j":
515
			return _(dojo.date.getDayOfYear(dateObject), 3);
516
		  case "k":
517
			if (padChar == null) {
518
				padChar = " ";
519
			}
520
			return _(dateObject.getHours());
521
		  case "l":
522
			if (padChar == null) {
523
				padChar = " ";
524
			}
525
			return _(dateObject.getHours() % 12 || 12);
526
		  case "m":
527
			return _(dateObject.getMonth() + 1);
528
		  case "M":
529
			return _(dateObject.getMinutes());
530
		  case "n":
531
			return "\n";
532
		  case "p":
533
			return info[dateObject.getHours() < 12 ? "am" : "pm"];
534
		  case "r":
535
			return $("I") + ":" + $("M") + ":" + $("S") + " " + $("p");
536
		  case "R":
537
			return $("H") + ":" + $("M");
538
		  case "S":
539
			return _(dateObject.getSeconds());
540
		  case "t":
541
			return "\t";
542
		  case "T":
543
			return $("H") + ":" + $("M") + ":" + $("S");
544
		  case "u":
545
			return String(dateObject.getDay() || 7);
546
		  case "U":
547
			return _(dojo.date.getWeekOfYear(dateObject));
548
		  case "V":
549
			return _(dojo.date.getIsoWeekOfYear(dateObject));
550
		  case "W":
551
			return _(dojo.date.getWeekOfYear(dateObject, 1));
552
		  case "w":
553
			return String(dateObject.getDay());
554
		  case "x":
555
			return dojo.date.format(dateObject, {selector:"dateOnly", locale:locale});
556
		  case "X":
557
			return dojo.date.format(dateObject, {selector:"timeOnly", locale:locale});
558
		  case "y":
559
			return _(dateObject.getFullYear() % 100);
560
		  case "Y":
561
			return String(dateObject.getFullYear());
562
		  case "z":
563
			var timezoneOffset = dateObject.getTimezoneOffset();
564
			return (timezoneOffset > 0 ? "-" : "+") + _(Math.floor(Math.abs(timezoneOffset) / 60)) + ":" + _(Math.abs(timezoneOffset) % 60);
565
		  case "Z":
566
			return dojo.date.getTimezoneName(dateObject);
567
		  case "%":
568
			return "%";
569
		}
570
	}
571
	var string = "";
572
	var i = 0;
573
	var index = 0;
574
	var switchCase = null;
575
	while ((index = format.indexOf("%", i)) != -1) {
576
		string += format.substring(i, index++);
577
		switch (format.charAt(index++)) {
578
		  case "_":
579
			padChar = " ";
580
			break;
581
		  case "-":
582
			padChar = "";
583
			break;
584
		  case "0":
585
			padChar = "0";
586
			break;
587
		  case "^":
588
			switchCase = "upper";
589
			break;
590
		  case "*":
591
			switchCase = "lower";
592
			break;
593
		  case "#":
594
			switchCase = "swap";
595
			break;
596
		  default:
597
			padChar = null;
598
			index--;
599
			break;
600
		}
601
		var property = $(format.charAt(index++));
602
		switch (switchCase) {
603
		  case "upper":
604
			property = property.toUpperCase();
605
			break;
606
		  case "lower":
607
			property = property.toLowerCase();
608
			break;
609
		  case "swap":
610
			var compareString = property.toLowerCase();
611
			var swapString = "";
612
			var j = 0;
613
			var ch = "";
614
			while (j < property.length) {
615
				ch = property.charAt(j);
616
				swapString += (ch == compareString.charAt(j)) ? ch.toUpperCase() : ch.toLowerCase();
617
				j++;
618
			}
619
			property = swapString;
620
			break;
621
		  default:
622
			break;
623
		}
624
		switchCase = null;
625
		string += property;
626
		i = index;
627
	}
628
	string += format.substring(i);
629
	return string;
630
};
631
(function () {
632
	var _customFormats = [];
633
	dojo.date.addCustomFormats = function (packageName, bundleName) {
634
		_customFormats.push({pkg:packageName, name:bundleName});
635
	};
636
	dojo.date._getGregorianBundle = function (locale) {
637
		var gregorian = {};
638
		dojo.lang.forEach(_customFormats, function (desc) {
639
			var bundle = dojo.i18n.getLocalization(desc.pkg, desc.name, locale);
640
			gregorian = dojo.lang.mixin(gregorian, bundle);
641
		}, this);
642
		return gregorian;
643
	};
644
})();
645
dojo.date.addCustomFormats("dojo.i18n.calendar", "gregorian");
646
dojo.date.addCustomFormats("dojo.i18n.calendar", "gregorianExtras");
647
dojo.date.getNames = function (item, type, use, locale) {
648
	var label;
649
	var lookup = dojo.date._getGregorianBundle(locale);
650
	var props = [item, use, type];
651
	if (use == "standAlone") {
652
		label = lookup[props.join("-")];
653
	}
654
	props[1] = "format";
655
	return (label || lookup[props.join("-")]).concat();
656
};
657
dojo.date.getDayName = function (dateObject, locale) {
658
	return dojo.date.getNames("days", "wide", "format", locale)[dateObject.getDay()];
659
};
660
dojo.date.getDayShortName = function (dateObject, locale) {
661
	return dojo.date.getNames("days", "abbr", "format", locale)[dateObject.getDay()];
662
};
663
dojo.date.getMonthName = function (dateObject, locale) {
664
	return dojo.date.getNames("months", "wide", "format", locale)[dateObject.getMonth()];
665
};
666
dojo.date.getMonthShortName = function (dateObject, locale) {
667
	return dojo.date.getNames("months", "abbr", "format", locale)[dateObject.getMonth()];
668
};
669
dojo.date.toRelativeString = function (dateObject) {
670
	var now = new Date();
671
	var diff = (now - dateObject) / 1000;
672
	var end = " ago";
673
	var future = false;
674
	if (diff < 0) {
675
		future = true;
676
		end = " from now";
677
		diff = -diff;
678
	}
679
	if (diff < 60) {
680
		diff = Math.round(diff);
681
		return diff + " second" + (diff == 1 ? "" : "s") + end;
682
	}
683
	if (diff < 60 * 60) {
684
		diff = Math.round(diff / 60);
685
		return diff + " minute" + (diff == 1 ? "" : "s") + end;
686
	}
687
	if (diff < 60 * 60 * 24) {
688
		diff = Math.round(diff / 3600);
689
		return diff + " hour" + (diff == 1 ? "" : "s") + end;
690
	}
691
	if (diff < 60 * 60 * 24 * 7) {
692
		diff = Math.round(diff / (3600 * 24));
693
		if (diff == 1) {
694
			return future ? "Tomorrow" : "Yesterday";
695
		} else {
696
			return diff + " days" + end;
697
		}
698
	}
699
	return dojo.date.format(dateObject);
700
};
701
dojo.date.toSql = function (dateObject, noTime) {
702
	return dojo.date.strftime(dateObject, "%F" + !noTime ? " %T" : "");
703
};
704
dojo.date.fromSql = function (sqlDate) {
705
	var parts = sqlDate.split(/[\- :]/g);
706
	while (parts.length < 6) {
707
		parts.push(0);
708
	}
709
	return new Date(parts[0], (parseInt(parts[1], 10) - 1), parts[2], parts[3], parts[4], parts[5]);
710
};
711