/adei/trunk

To get this branch, use:
bzr branch http://darksoft.org/webbzr/adei/trunk
15 by Suren A. Chilingaryan
Provide information in the statusbar, fix of global menu positioning
1
/*
2
	Date Format 1.1
3
	(c) 2007 Steven Levithan <stevenlevithan.com>
4
	MIT license
5
	With code by Scott Trenda (Z and o flags, and enhanced brevity)
6
*/
7
8
/*** dateFormat
9
	Accepts a date, a mask, or a date and a mask.
10
	Returns a formatted version of the given date.
11
	The date defaults to the current date/time.
12
	The mask defaults ``"ddd mmm d yyyy HH:MM:ss"``.
13
*/
14
var dateFormater = function () {
15
	var	token        = /d{1,4}|m{1,4}|yy(?:yy)?|([HhMsTt])\1?|[LloZ]|"[^"]*"|'[^']*'/g,
16
		timezone     = /\b(?:[PMCEA][SDP]T|(?:Pacific|Mountain|Central|Eastern|Atlantic) (?:Standard|Daylight|Prevailing) Time|(?:GMT|UTC)(?:[-+]\d{4})?)\b/g,
17
		timezoneClip = /[^-+\dA-Z]/g,
18
		pad = function (value, length) {
19
			value = String(value);
20
			length = parseInt(length) || 2;
21
			while (value.length < length)
22
				value = "0" + value;
23
			return value;
24
		};
25
26
	// Regexes and supporting functions are cached through closure
27
	return function (date, mask, utc) {
28
		// Treat the first argument as a mask if it doesn't contain any numbers
29
		if (
30
			arguments.length == 1 &&
31
			(typeof date == "string" || date instanceof String) &&
32
			!/\d/.test(date)
33
		) {
34
			mask = date;
35
			date = undefined;
36
		}
37
38
		date = date ? new Date(date) : new Date();
39
		if (isNaN(date))
40
			throw "invalid date";
41
42
		var dF = dateFormater;
43
		mask   = String(dF.masks[mask] || mask || dF.masks["default"]);
44
45
		if (utc) {
46
		    var	d = date.getUTCDate(),
47
			D = date.getUTCDay(),
48
			m = date.getUTCMonth(),
49
			y = date.getUTCFullYear(),
50
			H = date.getUTCHours(),
51
			M = date.getUTCMinutes(),
52
			s = date.getUTCSeconds(),
53
			L = date.getUTCMilliseconds(),
54
			o = 0;
55
		} else {
56
		    var	d = date.getDate(),
57
			D = date.getDay(),
58
			m = date.getMonth(),
59
			y = date.getFullYear(),
60
			H = date.getHours(),
61
			M = date.getMinutes(),
62
			s = date.getSeconds(),
63
			L = date.getMilliseconds(),
64
			o = date.getTimezoneOffset();
65
		}
66
		
67
		var	flags = {
68
				d:    d,
69
				dd:   pad(d),
70
				ddd:  dF.i18n.dayNames[D],
71
				dddd: dF.i18n.dayNames[D + 7],
72
				m:    m + 1,
73
				mm:   pad(m + 1),
74
				mmm:  dF.i18n.monthNames[m],
75
				mmmm: dF.i18n.monthNames[m + 12],
76
				yy:   String(y).slice(2),
77
				yyyy: y,
78
				h:    H % 12 || 12,
79
				hh:   pad(H % 12 || 12),
80
				H:    H,
81
				HH:   pad(H),
82
				M:    M,
83
				MM:   pad(M),
84
				s:    s,
85
				ss:   pad(s),
86
				l:    pad(L, 3),
87
				L:    pad(L > 99 ? Math.round(L / 10) : L),
88
				t:    H < 12 ? "a"  : "p",
89
				tt:   H < 12 ? "am" : "pm",
90
				T:    H < 12 ? "A"  : "P",
91
				TT:   H < 12 ? "AM" : "PM",
92
				Z:    utc?"GMT":(String(date).match(timezone) || [""]).pop().replace(timezoneClip, ""),
93
				o:    (o > 0 ? "-" : "+") + pad(Math.floor(Math.abs(o) / 60) * 100 + Math.abs(o) % 60, 4)
94
			};
95
96
		return mask.replace(token, function ($0) {
97
			return ($0 in flags) ? flags[$0] : $0.slice(1, $0.length - 1);
98
		});
99
	};
100
}();
101
102
// Some common format strings
103
dateFormater.masks = {
104
	"default":       "ddd mmm d yyyy HH:MM:ss",
105
	shortDate:       "m/d/yy",
106
	mediumDate:      "mmm d, yyyy",
107
	longDate:        "mmmm d, yyyy",
108
	fullDate:        "dddd, mmmm d, yyyy",
109
	shortTime:       "h:MM TT",
110
	mediumTime:      "h:MM:ss TT",
111
	longTime:        "h:MM:ss TT Z",
112
	isoDate:         "yyyy-mm-dd",
113
	isoTime:         "HH:MM:ss",
114
	isoDateTime:     "yyyy-mm-dd'T'HH:MM:ss",
115
	isoFullDateTime: "yyyy-mm-dd'T'HH:MM:ss.lo"
116
};
117
118
// Internationalization strings
119
dateFormater.i18n = {
120
	dayNames: [
121
		"Sun", "Mon", "Tue", "Wed", "Thr", "Fri", "Sat",
122
		"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"
123
	],
124
	monthNames: [
125
		"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
126
		"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"
127
	]
128
};
129
130
// For convenience...
131
Date.prototype.format = function (mask, utc) {
132
	return dateFormater(this, mask, utc);
133
}
134